diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/FallingBlockLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/FallingBlockLimit.java index 58f8f26a..239b7621 100644 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/FallingBlockLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/FallingBlockLimit.java @@ -7,18 +7,23 @@ import me.xginko.aef.utils.models.ChunkUID; import me.xginko.aef.utils.models.ExpiringSet; import org.bukkit.Chunk; +import org.bukkit.Location; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockPhysicsEvent; import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntitySpawnEvent; import java.time.Duration; +import java.util.Objects; public class FallingBlockLimit extends AEFModule implements Listener { + private static final EntityType FALLING_BLOCK_TYPE = Objects.requireNonNull(XEntityType.FALLING_BLOCK.get()); + private final long chunkCheckDelay; private final int maxFallingGravityBlockPerChunk; private final boolean logIsEnabled; @@ -50,65 +55,46 @@ public void disable() { HandlerList.unregisterAll(this); if (checkedChunks != null) { checkedChunks.clear(); - checkedChunks.cleanUp(); checkedChunks = null; } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onBlockPhysics(BlockPhysicsEvent event) { - Chunk chunk = event.getBlock().getChunk(); - if (ChunkUtil.isRetrievalUnsafe(chunk)) return; - - final ChunkUID chunkUID = ChunkUID.of(chunk); - if (checkedChunks.contains(chunkUID)) return; - - int count = 0; - boolean removed_falling = false; - - for (Entity entity : chunk.getEntities()) { - if (entity.getType() == XEntityType.FALLING_BLOCK.get()) { - count++; - if (count > maxFallingGravityBlockPerChunk) { - entity.remove(); - removed_falling = true; - } - } - } - - checkedChunks.add(chunkUID); - - if (logIsEnabled && removed_falling) info("Removed falling block(s) at " + - LocationUtil.toString(event.getSourceBlock().getLocation()) + " because reached limit of " + - maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"); + private void onEntitySpawn(EntitySpawnEvent event) { + if (event.getEntityType() != FALLING_BLOCK_TYPE) return; + tryEnforceLimit(event.getEntity().getChunk(), event.getEntity().getLocation(), 1); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onChangeBlock(EntityChangeBlockEvent event) { - if (event.getEntityType() != XEntityType.FALLING_BLOCK.get()) return; - Chunk chunk = event.getBlock().getChunk(); - if (ChunkUtil.isRetrievalUnsafe(chunk)) return; + if (event.getEntityType() != FALLING_BLOCK_TYPE) return; + tryEnforceLimit(event.getBlock().getChunk(), event.getBlock().getLocation(), 0); + } + + private void tryEnforceLimit(Chunk chunk, Location logLocation, int initialCount) { + if (ChunkUtil.isRetrievalUnsafe(chunk) || !ChunkUtil.isEntitiesLoaded(chunk)) return; + + ChunkUID chunkKey = ChunkUID.of(chunk); + if (checkedChunks.contains(chunkKey)) return; - final ChunkUID chunkUID = ChunkUID.of(chunk); - if (checkedChunks.contains(chunkUID)) return; + checkedChunks.add(chunkKey); - int count = 0; - boolean removed_falling = false; + int count = initialCount; + boolean removedFalling = false; for (Entity entity : chunk.getEntities()) { - if (entity.getType() == XEntityType.FALLING_BLOCK.get()) { - count++; - if (count > maxFallingGravityBlockPerChunk) { - entity.remove(); - removed_falling = true; - } + if (entity.getType() != FALLING_BLOCK_TYPE) continue; + + count++; + if (count > maxFallingGravityBlockPerChunk) { + entity.remove(); + removedFalling = true; } } - checkedChunks.add(chunkUID); - - if (logIsEnabled && removed_falling) info("Removed falling block(s) at " + - LocationUtil.toString(event.getBlock().getLocation()) + " because reached limit of " + - maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"); + if (logIsEnabled && removedFalling) { + info("Removed falling block(s) at " + LocationUtil.toString(logLocation) + " because reached limit of " + + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"); + } } -} \ No newline at end of file +} diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/FallingBlockLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/FallingBlockLimit.java index 3118d771..4abcd667 100644 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/FallingBlockLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/FallingBlockLimit.java @@ -7,18 +7,23 @@ import me.xginko.aef.utils.models.ChunkUID; import me.xginko.aef.utils.models.ExpiringSet; import org.bukkit.Chunk; +import org.bukkit.Location; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockPhysicsEvent; import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntitySpawnEvent; import java.time.Duration; +import java.util.Objects; public class FallingBlockLimit extends AEFModule implements Listener { + private static final EntityType FALLING_BLOCK_TYPE = Objects.requireNonNull(XEntityType.FALLING_BLOCK.get()); + private final long chunkCheckDelay; private final int maxFallingGravityBlockPerChunk; private final boolean logIsEnabled; @@ -50,65 +55,46 @@ public void disable() { HandlerList.unregisterAll(this); if (checkedChunks != null) { checkedChunks.clear(); - checkedChunks.cleanUp(); checkedChunks = null; } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onBlockPhysics(BlockPhysicsEvent event) { - Chunk chunk = event.getBlock().getChunk(); - if (ChunkUtil.isRetrievalUnsafe(chunk)) return; - - final ChunkUID chunkUID = ChunkUID.of(chunk); - if (checkedChunks.contains(chunkUID)) return; - - int count = 0; - boolean removed_falling = false; - - for (Entity entity : chunk.getEntities()) { - if (entity.getType() == XEntityType.FALLING_BLOCK.get()) { - count++; - if (count > maxFallingGravityBlockPerChunk) { - entity.remove(); - removed_falling = true; - } - } - } - - checkedChunks.add(chunkUID); - - if (logIsEnabled && removed_falling) info("Removed falling block(s) at " + - LocationUtil.toString(event.getSourceBlock().getLocation()) + " because reached limit of " + - maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"); + private void onEntitySpawn(EntitySpawnEvent event) { + if (event.getEntityType() != FALLING_BLOCK_TYPE) return; + tryEnforceLimit(event.getEntity().getChunk(), event.getEntity().getLocation(), 1); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onChangeBlock(EntityChangeBlockEvent event) { - if (event.getEntityType() != XEntityType.FALLING_BLOCK.get()) return; - Chunk chunk = event.getBlock().getChunk(); - if (ChunkUtil.isRetrievalUnsafe(chunk)) return; + if (event.getEntityType() != FALLING_BLOCK_TYPE) return; + tryEnforceLimit(event.getBlock().getChunk(), event.getBlock().getLocation(), 0); + } + + private void tryEnforceLimit(Chunk chunk, Location logLocation, int initialCount) { + if (ChunkUtil.isRetrievalUnsafe(chunk) || !ChunkUtil.isEntitiesLoaded(chunk)) return; + + ChunkUID chunkKey = ChunkUID.of(chunk); + if (checkedChunks.contains(chunkKey)) return; - final ChunkUID chunkUID = ChunkUID.of(chunk); - if (checkedChunks.contains(chunkUID)) return; + checkedChunks.add(chunkKey); - int count = 0; - boolean removed_falling = false; + int count = initialCount; + boolean removedFalling = false; for (Entity entity : chunk.getEntities()) { - if (entity.getType() == XEntityType.FALLING_BLOCK.get()) { - count++; - if (count > maxFallingGravityBlockPerChunk) { - entity.remove(); - removed_falling = true; - } + if (entity.getType() != FALLING_BLOCK_TYPE) continue; + + count++; + if (count > maxFallingGravityBlockPerChunk) { + entity.remove(); + removedFalling = true; } } - checkedChunks.add(chunkUID); - - if (logIsEnabled && removed_falling) info("Removed falling block(s) at " + - LocationUtil.toString(event.getBlock().getLocation()) + " because reached limit of " + - maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"); + if (logIsEnabled && removedFalling) { + info("Removed falling block(s) at " + LocationUtil.toString(logLocation) + " because reached limit of " + + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"); + } } -} \ No newline at end of file +} diff --git a/shared/src/main/java/me/xginko/aef/utils/models/ChunkUID.java b/shared/src/main/java/me/xginko/aef/utils/models/ChunkUID.java index e30a9490..4a77899a 100644 --- a/shared/src/main/java/me/xginko/aef/utils/models/ChunkUID.java +++ b/shared/src/main/java/me/xginko/aef/utils/models/ChunkUID.java @@ -5,7 +5,6 @@ import org.bukkit.World; import org.jetbrains.annotations.Nullable; -import java.util.Objects; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -48,7 +47,7 @@ public String toString() { @Override public int hashCode() { - return Objects.hash(this.worldUID, this.x, this.z); + return 31 * (31 * this.worldUID.hashCode() + this.x) + this.z; } @Override