From 8071f9a946bb49cc971c9c79c5c1492e35087fd2 Mon Sep 17 00:00:00 2001 From: xGinko Date: Tue, 2 Jan 2024 16:23:36 +0100 Subject: [PATCH] curate chunk-limits --- .../bedrock/FillNetherCeilingOnChunkload.java | 9 +- .../bedrock/FillNetherFloorOnChunkload.java | 3 +- .../FillOverworldFloorOnChunkload.java | 12 +- .../CWChatPacketListener.java | 7 +- .../commandwhitelist/CommandWhitelist.java | 9 +- .../modules/chunklimits/BlockLimit.java | 3 +- .../chunklimits/CustomEntityLimit.java | 62 +++++------ .../modules/chunklimits/DroppedItemLimit.java | 84 +++++++------- .../modules/chunklimits/ExpBottleLimit.java | 23 ++-- .../chunklimits/FallingBlockLimit.java | 11 +- .../modules/chunklimits/MinecartLimit.java | 69 ++++++++---- .../chunklimits/NonLivingEntityLimit.java | 103 +++++++++++++----- .../modules/chunklimits/VehicleLimit.java | 96 +++++++++++++--- .../modules/chunklimits/VillagerLimit.java | 21 ++-- .../modules/chunklimits/BlockLimit.java | 3 +- .../chunklimits/CustomEntityLimit.java | 50 ++++----- .../modules/chunklimits/DroppedItemLimit.java | 66 +++++------ .../modules/chunklimits/ExpBottleLimit.java | 19 ++-- .../chunklimits/FallingBlockLimit.java | 11 +- .../modules/chunklimits/MinecartLimit.java | 67 ++++++++---- .../chunklimits/NonLivingEntityLimit.java | 93 +++++++++++----- .../modules/chunklimits/VehicleLimit.java | 84 +++++++++++--- .../modules/chunklimits/VillagerLimit.java | 22 ++-- 23 files changed, 594 insertions(+), 333 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java index 45f5725a2..1841da190 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java @@ -29,10 +29,13 @@ public FillNetherCeilingOnChunkload() { this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); this.ceilingY = config.nether_ceiling_max_y; - this.alsoCheckNewChunks = config.getBoolean("bedrock.fill-in-bedrock.nether-ceiling.fill-on-chunkload.also-check-new-chunks", false, "Recommended to leave off. Only useful if world generation is broken for some reason."); - this.exemptedWorlds.addAll(config.getList("bedrock.fill-in-bedrock.nether-ceiling.exempted-worlds", List.of("exampleworld", "exampleworld2"), "Case sensitive!")); + this.alsoCheckNewChunks = config.getBoolean("bedrock.fill-in-bedrock.nether-ceiling.fill-on-chunkload.also-check-new-chunks", false, + "Recommended to leave off. Only useful if world generation is broken for some reason."); + this.exemptedWorlds.addAll(config.getList("bedrock.fill-in-bedrock.nether-ceiling.exempted-worlds", List.of("exampleworld", "exampleworld2"), + "Case sensitive!")); this.checkShouldPauseOnLowTPS = config.getBoolean("bedrock.fill-in-bedrock.nether-ceiling.fill-on-chunkload.pause-on-low-tps", true); - this.pauseTPS = config.getDouble("bedrock.fill-in-bedrock.nether-ceiling.fill-on-chunkload.pause-tps", 16.0, "The TPS at which bedrock filling will pause to avoid lag."); + this.pauseTPS = config.getDouble("bedrock.fill-in-bedrock.nether-ceiling.fill-on-chunkload.pause-tps", 16.0, + "The TPS at which bedrock filling will pause to avoid lag."); } @Override diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java index d50ddba8e..508607768 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java @@ -28,7 +28,8 @@ public FillNetherFloorOnChunkload() { this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); this.alsoCheckNewChunks = config.getBoolean("bedrock.fill-in-bedrock.nether-floor.fill-on-chunkload.also-check-new-chunks", false); - this.exemptedWorlds.addAll(config.getList("bedrock.fill-in-bedrock.nether-floor.exempted-worlds", List.of("exampleworld", "exampleworld2"), "Case sensitive!")); + this.exemptedWorlds.addAll(config.getList("bedrock.fill-in-bedrock.nether-floor.exempted-worlds", List.of("exampleworld", "exampleworld2"), + "Case sensitive!")); this.checkShouldPauseOnLowTPS = config.getBoolean("bedrock.fill-in-bedrock.nether-floor.fill-on-chunkload.pause-on-low-tps", true); this.pauseTPS = config.getDouble("bedrock.fill-in-bedrock.nether-floor.fill-on-chunkload.pause-tps", 16.0); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java index ab23d58ee..bc1c740ca 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java @@ -27,10 +27,14 @@ public FillOverworldFloorOnChunkload() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); - this.alsoCheckNewChunks = config.getBoolean("bedrock.fill-in-bedrock.overworld-floor.fill-on-chunkload.also-check-new-chunks", false, "Recommended to leave off. Only useful if world generation is broken for some reason."); - this.exemptedWorlds.addAll(config.getList("bedrock.fill-in-bedrock.overworld-floor.exempted-worlds", List.of("exampleworld", "exampleworld2"), "Case sensitive!")); - this.checkShouldPauseOnLowTPS = config.getBoolean("bedrock.fill-in-bedrock.overworld-floor.fill-on-chunkload.pause-on-low-tps", true, "Pauses the task during low tps to avoid lag."); - this.pauseTPS = config.getDouble("bedrock.fill-in-bedrock.overworld-floor.fill-on-chunkload.pause-tps", 16.0, "The TPS at which bedrock filling will pause."); + this.alsoCheckNewChunks = config.getBoolean("bedrock.fill-in-bedrock.overworld-floor.fill-on-chunkload.also-check-new-chunks", false, + "Recommended to leave off. Only useful if world generation is broken for some reason."); + this.exemptedWorlds.addAll(config.getList("bedrock.fill-in-bedrock.overworld-floor.exempted-worlds", List.of("exampleworld", "exampleworld2"), + "Case sensitive!")); + this.checkShouldPauseOnLowTPS = config.getBoolean("bedrock.fill-in-bedrock.overworld-floor.fill-on-chunkload.pause-on-low-tps", true, + "Pauses the task during low tps to avoid lag."); + this.pauseTPS = config.getDouble("bedrock.fill-in-bedrock.overworld-floor.fill-on-chunkload.pause-tps", 16.0, + "The TPS at which bedrock filling will pause."); } @Override diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chat/commandwhitelist/CWChatPacketListener.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chat/commandwhitelist/CWChatPacketListener.java index 303da50d3..2dfed62af 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chat/commandwhitelist/CWChatPacketListener.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chat/commandwhitelist/CWChatPacketListener.java @@ -23,12 +23,7 @@ public class CWChatPacketListener extends PacketAdapter { private final HashSet bannedSubCommands; private final boolean shouldLog; - protected CWChatPacketListener( - AnarchyExploitFixes plugin, - HashSet allowedCommands, - HashSet bannedSubCommands, - boolean shouldLog - ) { + protected CWChatPacketListener(AnarchyExploitFixes plugin, HashSet allowedCommands, HashSet bannedSubCommands, boolean shouldLog) { super(plugin, ListenerPriority.HIGHEST, PacketType.Play.Client.CHAT); this.allowedCommands = allowedCommands; this.bannedSubCommands = bannedSubCommands; diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chat/commandwhitelist/CommandWhitelist.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chat/commandwhitelist/CommandWhitelist.java index d6a670dae..26baa32c4 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chat/commandwhitelist/CommandWhitelist.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chat/commandwhitelist/CommandWhitelist.java @@ -39,13 +39,16 @@ public CommandWhitelist() { "This will make it pretty much impossible to find your plugins as only the commands you specify will be able to work.\n" + "Allow bypass using permission: anarchyexploitfixes.commandwhitelistbypass" ); - this.shouldLog = config.getBoolean("chat.command-whitelist.log", false, "Will show logs when a command was denied."); - this.shouldUseProtocolLib = config.getBoolean("chat.command-whitelist.use-protocollib", false, "Enable only if you have problems with the regular commandwhitelist. Otherwise not recommended to use."); + this.shouldLog = config.getBoolean("chat.command-whitelist.log", false, + "Will show logs when a command was denied."); + this.shouldUseProtocolLib = config.getBoolean("chat.command-whitelist.use-protocollib", false, + "Enable only if you have problems with the regular commandwhitelist. Otherwise not recommended to use."); config.getList("chat.command-whitelist.whitelisted-commands", List.of( "help", "vote", "kill", "discord", "togglechat", "toggleconnectionmsgs", "toggletells", "togglewhispering", "toggleprivatemsgs", "ignore", "ignorelist", "ignorehard", "toggledeathmsg", "dmt", "worldstats", "stats", "tps", "msg", "whisper", "w", "m", "t", "pm", "tell", "r", "reply", "last" - ), "Add all commands you want your players to be able to access (without the '/'). Not case sensitive.").forEach(configuredAllowedCmd -> this.allowedCommands.add(configuredAllowedCmd.toLowerCase())); + ), "Add all commands you want your players to be able to access (without the '/'). Not case sensitive." + ).forEach(configuredAllowedCmd -> this.allowedCommands.add(configuredAllowedCmd.toLowerCase())); bannedSubCommands.addAll(config.getList("chat.command-whitelist.blacklisted-subcommands", List.of( "help about", "vote List", "vote Best", "vote Total", "worldstats reload", "stats reload" ))); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/BlockLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/BlockLimit.java index 673eb6222..502461fc9 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/BlockLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/BlockLimit.java @@ -123,7 +123,8 @@ public BlockLimit() { defaults.put("YELLOW_BANNER", 12); defaults.put("YELLOW_WALL_BANNER", 12); - ConfigSection section = AnarchyExploitFixes.getConfiguration().getConfigSection("chunk-limits.block-limit.max-blocks-per-chunk", defaults, "Attempt to prevent ChunkBan / Client FPS Lag"); + ConfigSection section = AnarchyExploitFixes.getConfiguration().getConfigSection("chunk-limits.block-limit.max-blocks-per-chunk", defaults, + "Attempt to prevent ChunkBan / Client FPS Lag"); for (String configuredMaterial : section.getKeys(false)) { try { Material blockMaterial = Material.valueOf(configuredMaterial); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/CustomEntityLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/CustomEntityLimit.java index 06898b428..8ba42ea8f 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/CustomEntityLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/CustomEntityLimit.java @@ -23,10 +23,10 @@ public class CustomEntityLimit implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; - private ScheduledTask scheduledTask; private final HashMap entityLimits = new HashMap<>(); - private final boolean logIsEnabled, enableChunkAgeSkip, forceLoadEntities; + private ScheduledTask scheduledTask; private final long checkPeriod, minChunkAge; + private final boolean logIsEnabled, enableChunkAgeSkip, forceLoadEntities; public CustomEntityLimit() { shouldEnable(); @@ -157,58 +157,56 @@ public void disable() { if (scheduledTask != null) scheduledTask.cancel(); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onSpawn(EntitySpawnEvent event) { final EntityType spawnedType = event.getEntityType(); if (!entityLimits.containsKey(spawnedType)) return; - final int maxAllowed = entityLimits.get(spawnedType); + final int maxAllowedPerChunk = entityLimits.get(spawnedType); + int entityCount = 0; + for (Entity entity : event.getEntity().getChunk().getEntities()) { - int count = 0; - if (entity.getType().equals(spawnedType)) { - count++; - if (count > maxAllowed) { - entity.getScheduler().run(plugin, kill -> entity.remove(), null); - if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType() - + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowed - ); - } - } + if (!entity.getType().equals(spawnedType)) continue; + + entityCount++; + if (entityCount <= maxAllowedPerChunk) continue; + + entity.getScheduler().run(plugin, kill -> entity.remove(), null); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowedPerChunk); } } private void run() { for (World world : plugin.getServer().getWorlds()) { for (Chunk chunk : world.getLoadedChunks()) { - plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), task -> { + plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), chunkCheck -> { if (!forceLoadEntities && !chunk.isEntitiesLoaded()) return; if (enableChunkAgeSkip && chunk.getInhabitedTime() < minChunkAge) return; Entity[] chunkEntities = chunk.getEntities(); for (Map.Entry limit : entityLimits.entrySet()) { - - final int maxAllowed = limit.getValue(); - int count = 0; + final int maxAllowedPerChunk = limit.getValue(); + int entityCount = 0; for (Entity entity : chunkEntities) { - if (entity.getType().equals(limit.getKey())) { - count++; - if (count > maxAllowed) { - entity.getScheduler().run(plugin, kill -> { - entity.remove(); - if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType() - + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowed - ); - }, null); - } - } + if (!entity.getType().equals(limit.getKey())) continue; + + entityCount++; + if (entityCount <= maxAllowedPerChunk) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowedPerChunk); + }, null); } } }); } } } -} +} \ No newline at end of file diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/DroppedItemLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/DroppedItemLimit.java index 0ce1c4721..ee5e2c065 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/DroppedItemLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/DroppedItemLimit.java @@ -21,30 +21,31 @@ import java.util.List; import java.util.logging.Level; -import static me.moomoo.anarchyexploitfixes.utils.LogUtils.moduleLog; - public class DroppedItemLimit implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; + private final HashSet whitelistedItems = new HashSet<>(); private ScheduledTask scheduledTask; - private final boolean logIsEnabled, usingWhitelist; - private final int maxDroppedItemsPerChunk; private final long checkPeriod; - private final HashSet whitelistedItems = new HashSet<>(); + private final int maxDroppedItemsPerChunk; + private final boolean logIsEnabled, usingWhitelist; public DroppedItemLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.entity-limits.dropped-item-limit.enable", "Limit the amount of dropped items in a chunk to combat lag.\nBe aware this does not prioritize higher value items."); + config.addComment("chunk-limits.entity-limits.dropped-item-limit.enable", """ + Limit the amount of dropped items in a chunk to combat lag.\s + Be aware this does not prioritize items by value or anything. Just whatever gets over the counter."""); this.logIsEnabled = config.getBoolean("chunk-limits.entity-limits.dropped-item-limit.log-removals", true); this.maxDroppedItemsPerChunk = config.getInt("chunk-limits.entity-limits.dropped-item-limit.max-dropped-items-per-chunk", 200); - this.checkPeriod = config.getInt("chunk-limits.entity-limits.dropped-item-limit.check-period-in-ticks", 800, "20 ticks = 1 second"); + this.checkPeriod = config.getInt("chunk-limits.entity-limits.dropped-item-limit.check-period-in-ticks", 800, + "20 ticks = 1 second"); this.usingWhitelist = config.getBoolean("chunk-limits.entity-limits.dropped-item-limit.whitelist-specific-item-types", false); config.getList("chunk-limits.entity-limits.dropped-item-limit.whitelisted-types", List.of( "SHULKER_BOX", "BLACK_SHULKER_BOX", "BLUE_SHULKER_BOX", "BROWN_SHULKER_BOX", "CYAN_SHULKER_BOX", "GRAY_SHULKER_BOX", - "GREEN_SHULKER_BOX", "LIGHT_BLUE_SHULKER_BOX", "LIGHT_GRAY_SHULKER_BOX", "LIME_SHULKER_BOX", "MAGENTA_SHULKER_BOX", "ORANGE_SHULKER_BOX", - "PINK_SHULKER_BOX", "PURPLE_SHULKER_BOX", "RED_SHULKER_BOX", "WHITE_SHULKER_BOX", "YELLOW_SHULKER_BOX" + "GREEN_SHULKER_BOX", "LIGHT_BLUE_SHULKER_BOX", "LIGHT_GRAY_SHULKER_BOX", "LIME_SHULKER_BOX", "MAGENTA_SHULKER_BOX", + "ORANGE_SHULKER_BOX", "PINK_SHULKER_BOX", "PURPLE_SHULKER_BOX", "RED_SHULKER_BOX", "WHITE_SHULKER_BOX", "YELLOW_SHULKER_BOX" ), "You need to use correct Material enums for your minecraft version here.").forEach(configuredWhitelistedType -> { try { Material whitelistedType = Material.valueOf(configuredWhitelistedType); @@ -82,54 +83,47 @@ public void disable() { if (scheduledTask != null) scheduledTask.cancel(); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onItemDrop(ItemSpawnEvent event) { - int count = 0; + int droppedItemCount = 0; + for (Entity entity : event.getEntity().getChunk().getEntities()) { - if (entity.getType().equals(EntityType.DROPPED_ITEM)) { - count++; - if (count > maxDroppedItemsPerChunk) { - if (usingWhitelist && whitelistedItems.contains(event.getEntity().getItemStack().getType())) continue; - entity.getScheduler().run(plugin, kill -> entity.remove(), null); - } - } + if (!entity.getType().equals(EntityType.DROPPED_ITEM)) continue; + + droppedItemCount++; + if (droppedItemCount <= maxDroppedItemsPerChunk) continue; + if (usingWhitelist && whitelistedItems.contains(((Item) entity).getItemStack().getType())) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed dropped item at" + + " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + " in world " + entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk); + }, null); } } private void run() { for (World world : plugin.getServer().getWorlds()) { for (Chunk chunk : world.getLoadedChunks()) { - plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), task -> { + plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), chunkCheck -> { if (!chunk.getLoadLevel().equals(Chunk.LoadLevel.ENTITY_TICKING)) return; - int droppedItems = 0; + + int droppedItemCount = 0; for (Entity entity : chunk.getEntities()) { if (!entity.getType().equals(EntityType.DROPPED_ITEM)) continue; - if (usingWhitelist) { - if (!whitelistedItems.contains(((Item) entity).getItemStack().getType())) { - droppedItems++; - if (droppedItems > maxDroppedItemsPerChunk) { - entity.getScheduler().run(plugin, kill -> { - entity.remove(); - if (logIsEnabled) moduleLog(Level.INFO, name(), "Removed dropped item at" - + " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + " in world "+ entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk - ); - }, null); - } - } - } else { - droppedItems++; - if (droppedItems > maxDroppedItemsPerChunk) { - entity.getScheduler().run(plugin, kill -> { - entity.remove(); - if (logIsEnabled) moduleLog(Level.INFO, name(), "Removed dropped item at" - + " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + " in world "+ entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk - ); - }, null); - } - } + + droppedItemCount++; + if (droppedItemCount <= maxDroppedItemsPerChunk) continue; + if (usingWhitelist && whitelistedItems.contains(((Item) entity).getItemStack().getType())) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed dropped item at" + + " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + " in world " + entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk); + }, null); } }); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/ExpBottleLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/ExpBottleLimit.java index accb773a8..f59aa8b64 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/ExpBottleLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/ExpBottleLimit.java @@ -20,8 +20,10 @@ public ExpBottleLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.exp-bottle-limit.enable", "Prevent having a shit ton of exp bottles in one chunk then loading it to kill the server."); - this.maxExpBottlePerChunk = config.getInt("chunk-limits.exp-bottle-limit.max-exp-bottle-per-chunk", 25, "Max in a chunk, doesn't limit the actual xp orbs."); + config.addComment("chunk-limits.exp-bottle-limit.enable", + "Prevent having a shitton of exp bottles in one chunk then loading it to kill the server."); + this.maxExpBottlePerChunk = config.getInt("chunk-limits.exp-bottle-limit.max-exp-bottle-per-chunk", 25, + "Max in a chunk, doesn't limit the actual xp orbs."); } @Override @@ -49,15 +51,18 @@ public void disable() { HandlerList.unregisterAll(this); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onExpBottle(ExpBottleEvent event) { - int count=0; + int expBottleCount=0; + for (Entity entity : event.getEntity().getChunk().getEntities()) { - if (entity.getType().equals(EntityType.THROWN_EXP_BOTTLE)) { - count++; - if (count > maxExpBottlePerChunk) - entity.getScheduler().run(plugin, kill -> entity.remove(), null); + if (!entity.getType().equals(EntityType.THROWN_EXP_BOTTLE)) continue; + + expBottleCount++; + if (expBottleCount > maxExpBottlePerChunk) { + event.setCancelled(true); + entity.getScheduler().run(plugin, kill -> entity.remove(), null); } } } -} +} \ No newline at end of file diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/FallingBlockLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/FallingBlockLimit.java index ea90342ac..4048cb44d 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/FallingBlockLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/FallingBlockLimit.java @@ -3,6 +3,7 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.LogUtils; import me.moomoo.anarchyexploitfixes.utils.models.ExpiringSet; import org.bukkit.Chunk; import org.bukkit.entity.Entity; @@ -17,8 +18,6 @@ import java.time.Duration; import java.util.logging.Level; -import static me.moomoo.anarchyexploitfixes.utils.LogUtils.moduleLog; - public class FallingBlockLimit implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; @@ -66,7 +65,7 @@ public void disable() { HandlerList.unregisterAll(this); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onBlockPhysics(BlockPhysicsEvent event) { Chunk chunk = event.getBlock().getChunk(); if (checkedChunks.contains(chunk)) return; @@ -84,7 +83,7 @@ private void onBlockPhysics(BlockPhysicsEvent event) { } } - if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(), + if (logIsEnabled && removed_falling) LogUtils.moduleLog(Level.INFO, name(), "Removed falling blocks at " + event.getSourceBlock().getLocation() + ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk" ); @@ -92,7 +91,7 @@ private void onBlockPhysics(BlockPhysicsEvent event) { checkedChunks.add(chunk); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onChangeBlock(EntityChangeBlockEvent event) { if (!event.getEntityType().equals(EntityType.FALLING_BLOCK)) return; Chunk chunk = event.getBlock().getChunk(); @@ -111,7 +110,7 @@ private void onChangeBlock(EntityChangeBlockEvent event) { } } - if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(), + if (logIsEnabled && removed_falling) LogUtils.moduleLog(Level.INFO, name(), "Removed falling blocks at " + event.getBlock().getLocation() + ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk" ); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/MinecartLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/MinecartLimit.java index f83ef891b..2b26ba456 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/MinecartLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/MinecartLimit.java @@ -4,29 +4,45 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.LogUtils; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Vehicle; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.vehicle.VehicleCreateEvent; +import java.util.Arrays; +import java.util.HashSet; +import java.util.logging.Level; +import java.util.stream.Collectors; + public class MinecartLimit implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; + private final HashSet MINECART_TYPES; private ScheduledTask scheduledTask; - private final int maxMinecartsPerChunk; private final long checkPeriod; + private final int maxMinecartsPerChunk; + private final boolean logIsEnabled; public MinecartLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); + this.MINECART_TYPES = Arrays.stream(EntityType.values()) + .filter(type -> type.name().toUpperCase().contains("MINECART")) + .collect(Collectors.toCollection(HashSet::new)); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.minecart-limit.enable", "Limit the amount of minecarts to prevent lag."); + config.addComment("chunk-limits.minecart-limit.enable", + "Limit the amount of minecarts to prevent lag."); + this.logIsEnabled = config.getBoolean("chunk-limits.minecart-limit.log-removals", false); this.maxMinecartsPerChunk = config.getInt("chunk-limits.minecart-limit.max-minecarts-per-chunk", 25); - this.checkPeriod = config.getInt("chunk-limits.minecart-limit.check-period-in-ticks", 400, "200 ticks = 10 seconds."); + this.checkPeriod = config.getInt("chunk-limits.minecart-limit.check-period-in-ticks", 400, + "200 ticks = 10 seconds."); } @Override @@ -56,18 +72,24 @@ public void disable() { HandlerList.unregisterAll(this); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onCreate(VehicleCreateEvent event) { - if (!event.getVehicle().getType().name().contains("MINECART")) return; - - int count = 0; - for (Entity entity : event.getVehicle().getChunk().getEntities()) { - if (entity.getType().name().contains("MINECART")) { - count++; - if (count > maxMinecartsPerChunk) { - entity.getScheduler().run(plugin, kill -> entity.remove(), null); - } - } + Vehicle vehicle = event.getVehicle(); + if (!MINECART_TYPES.contains(vehicle.getType())) return; + + int minecartCount = 0; + for (Entity entity : vehicle.getChunk().getEntities()) { + if (!MINECART_TYPES.contains(entity.getType())) continue; + + minecartCount++; + if (minecartCount <= maxMinecartsPerChunk) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed minecart " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + + " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxMinecartsPerChunk); + }, null); } } @@ -78,14 +100,19 @@ private void run() { Chunk.LoadLevel level = chunk.getLoadLevel(); if (!level.equals(Chunk.LoadLevel.ENTITY_TICKING) && !level.equals(Chunk.LoadLevel.TICKING)) return; - int count = 0; + int minecartCount = 0; for (Entity entity : chunk.getEntities()) { - if (entity.getType().name().contains("MINECART")) { - count++; - if (count > maxMinecartsPerChunk) { - entity.getScheduler().run(plugin, kill -> entity.remove(), null); - } - } + if (!MINECART_TYPES.contains(entity.getType())) continue; + + minecartCount++; + if (minecartCount <= maxMinecartsPerChunk) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed minecart " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + + " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxMinecartsPerChunk); + }, null); } }); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/NonLivingEntityLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/NonLivingEntityLimit.java index e5d05e0eb..133c4d726 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/NonLivingEntityLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/NonLivingEntityLimit.java @@ -4,6 +4,7 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.LogUtils; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Entity; @@ -15,26 +16,28 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntitySpawnEvent; +import java.util.HashSet; import java.util.logging.Level; -import static me.moomoo.anarchyexploitfixes.utils.LogUtils.moduleLog; - public class NonLivingEntityLimit implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; + private final HashSet NON_LIVING_TYPES = new HashSet<>(); private ScheduledTask scheduledTask; - private final boolean logIsEnabled; - private final int maxNonLivingEntities; private final long checkPeriod; + private final int maxNonLivingEntities; + private final boolean logIsEnabled; public NonLivingEntityLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.entity-limits.non-living-limit.enable", "Limit the amount of non living entities in a chunk to prevent lag (does not affect items)."); + config.addComment("chunk-limits.entity-limits.non-living-limit.enable", + "Limit the amount of non living entities in a chunk to prevent lag (does not affect items)."); this.logIsEnabled = config.getBoolean("chunk-limits.entity-limits.non-living-limit.log-removals", true); this.maxNonLivingEntities = config.getInt("chunk-limits.entity-limits.non-living-limit.max-non-living-per-chunk", 100); - this.checkPeriod = config.getInt("chunk-limits.entity-limits.non-living-limit.check-period-in-ticks", 20, "20 ticks = 1 second"); + this.checkPeriod = config.getInt("chunk-limits.entity-limits.non-living-limit.check-period-in-ticks", 20, + "20 ticks = 1 second"); } @Override @@ -50,7 +53,7 @@ public String category() { @Override public void enable() { plugin.getServer().getPluginManager().registerEvents(this, plugin); - this.scheduledTask = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, task -> run(), checkPeriod, checkPeriod); + scheduledTask = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, task -> run(), checkPeriod, checkPeriod); } @Override @@ -64,21 +67,47 @@ public void disable() { HandlerList.unregisterAll(this); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onSpawn(EntitySpawnEvent event) { if (event.getEntityType().equals(EntityType.DROPPED_ITEM) || event.getEntity() instanceof LivingEntity) return; - int count = 0; + int nonLivingCount = 0; + for (Entity entity : event.getEntity().getChunk().getEntities()) { + final EntityType type = entity.getType(); + + if (NON_LIVING_TYPES.contains(type)) { + nonLivingCount++; + if (nonLivingCount <= maxNonLivingEntities) continue; + + event.setCancelled(true); + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed non-living entity " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxNonLivingEntities); + }, null); + + continue; + } + if ( - !entity.getType().equals(EntityType.DROPPED_ITEM) - && !entity.getType().equals(EntityType.ENDER_PEARL) + !type.equals(EntityType.DROPPED_ITEM) + && !type.equals(EntityType.ENDER_PEARL) && !(entity instanceof LivingEntity) ) { - count++; - if (count > maxNonLivingEntities) { - entity.getScheduler().run(plugin, kill -> entity.remove(), null); - } + NON_LIVING_TYPES.add(type); // Add type of non-living to HashSet for faster operation in the future + + nonLivingCount++; + if (nonLivingCount <= maxNonLivingEntities) continue; + + event.setCancelled(true); + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed non-living entity " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxNonLivingEntities); + }, null); } } } @@ -90,23 +119,41 @@ private void run() { Chunk.LoadLevel level = chunk.getLoadLevel(); if (!level.equals(Chunk.LoadLevel.ENTITY_TICKING) && !level.equals(Chunk.LoadLevel.TICKING)) return; - int nonLiving = 0; + int nonLivingCount = 0; + for (Entity entity : chunk.getEntities()) { + final EntityType type = entity.getType(); + + if (NON_LIVING_TYPES.contains(type)) { + nonLivingCount++; + if (nonLivingCount <= maxNonLivingEntities) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed non-living entity " + + entity.getType() + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + + " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxNonLivingEntities); + }, null); + + continue; + } + if ( - !entity.getType().equals(EntityType.DROPPED_ITEM) - && !entity.getType().equals(EntityType.ENDER_PEARL) + !type.equals(EntityType.DROPPED_ITEM) + && !type.equals(EntityType.ENDER_PEARL) && !(entity instanceof LivingEntity) ) { - nonLiving++; - if (nonLiving > maxNonLivingEntities) { - entity.getScheduler().run(plugin, kill -> { - entity.remove(); - if (logIsEnabled) moduleLog(Level.INFO, name(), "Removed non-living entity " + entity.getType() - + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + ", because reached limit of " + maxNonLivingEntities - ); - }, null); - } + NON_LIVING_TYPES.add(type); + + nonLivingCount++; + if (nonLivingCount <= maxNonLivingEntities) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed non-living entity " + + entity.getType() + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + + " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxNonLivingEntities); + }, null); } } }); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VehicleLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VehicleLimit.java index 3127a91b5..612476e03 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VehicleLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VehicleLimit.java @@ -4,9 +4,11 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.LogUtils; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Vehicle; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -14,20 +16,36 @@ import org.bukkit.event.Listener; import org.bukkit.event.vehicle.VehicleCreateEvent; +import java.util.Arrays; +import java.util.HashSet; +import java.util.logging.Level; +import java.util.stream.Collectors; + public class VehicleLimit implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; + private final HashSet VEHICLE_TYPES; private ScheduledTask scheduledTask; - private final int maxVehiclesPerChunk; private final long checkPeriod; + private final int maxVehiclesPerChunk; + private final boolean logIsEnabled; public VehicleLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); + this.VEHICLE_TYPES = new HashSet<>(); // Used to cache entity types of vehicles for faster operations + // Prefill with known types; + this.VEHICLE_TYPES.addAll(Arrays.stream(EntityType.values()).filter(entityType -> { + final String name = entityType.name().toUpperCase(); + return name.contains("MINECART") && name.contains("BOAT"); + }).collect(Collectors.toSet())); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.vehicle-limit.enable", "Limit the amount of vehicles to prevent some lag machines involving boats and a dispenser,"); + config.addComment("chunk-limits.vehicle-limit.enable", + "Limit the amount of vehicles to prevent some lag machines involving boats and a dispenser."); + this.logIsEnabled = config.getBoolean("chunk-limits.vehicle-limit.log-removals", false); this.maxVehiclesPerChunk = config.getInt("chunk-limits.vehicle-limit.max-vehicles-per-chunk", 25); - this.checkPeriod = config.getInt("chunk-limits.vehicle-limit.check-period-in-ticks", 400, "200 ticks = 10 seconds."); + this.checkPeriod = config.getInt("chunk-limits.vehicle-limit.check-period-in-ticks", 400, + "200 ticks = 10 seconds."); } @Override @@ -43,7 +61,7 @@ public String category() { @Override public void enable() { plugin.getServer().getPluginManager().registerEvents(this, plugin); - this.scheduledTask = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, task -> run(), checkPeriod, checkPeriod); + scheduledTask = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, task -> run(), checkPeriod, checkPeriod); } @Override @@ -57,15 +75,38 @@ public void disable() { HandlerList.unregisterAll(this); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onCreate(VehicleCreateEvent event) { - int count = 0; + int vehicleCount = 0; + for (Entity entity : event.getVehicle().getChunk().getEntities()) { + final EntityType type = entity.getType(); + + if (VEHICLE_TYPES.contains(type)) { + vehicleCount++; + if (vehicleCount <= maxVehiclesPerChunk) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed vehicle " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxVehiclesPerChunk); + }, null); + + continue; + } + if (entity instanceof Vehicle) { - count++; - if (count > maxVehiclesPerChunk) { - entity.getScheduler().run(plugin, kill -> entity.remove(), null); - } + VEHICLE_TYPES.add(type); // Add type of vehicle to HashSet for faster operation in the future + vehicleCount++; + if (vehicleCount <= maxVehiclesPerChunk) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed vehicle " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxVehiclesPerChunk); + }, null); } } } @@ -77,13 +118,38 @@ private void run() { Chunk.LoadLevel level = chunk.getLoadLevel(); if (!level.equals(Chunk.LoadLevel.ENTITY_TICKING) && !level.equals(Chunk.LoadLevel.TICKING)) return; - int countedVehicles = 0; + int vehicleCount = 0; + for (Entity entity : chunk.getEntities()) { + final EntityType type = entity.getType(); + + if (VEHICLE_TYPES.contains(type)) { + vehicleCount++; + if (vehicleCount <= maxVehiclesPerChunk) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed vehicle " + + entity.getType() + " at x:" + entity.getLocation().getX() + + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxVehiclesPerChunk); + }, null); + + continue; + } + if (entity instanceof Vehicle) { - countedVehicles++; - if (countedVehicles > maxVehiclesPerChunk) { - entity.getScheduler().run(plugin, kill -> entity.remove(), null); - } + VEHICLE_TYPES.add(type); // Add type of vehicle to HashSet for faster operation in the future + vehicleCount++; + if (vehicleCount <= maxVehiclesPerChunk) continue; + + entity.getScheduler().run(plugin, kill -> { + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed vehicle " + + entity.getType() + " at x:" + entity.getLocation().getX() + + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxVehiclesPerChunk); + }, null); } } }); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VillagerLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VillagerLimit.java index 86fce877a..7dc1d0143 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VillagerLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VillagerLimit.java @@ -22,11 +22,11 @@ public class VillagerLimit implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; - private ScheduledTask scheduledTask; private final List removalPriority = new ArrayList<>(); + private ScheduledTask scheduledTask; + private final long checkPeriod; private final int maxVillagersPerChunk; private final boolean logIsEnabled; - private final long checkPeriod; public VillagerLimit() { shouldEnable(); @@ -34,16 +34,19 @@ public VillagerLimit() { Config config = AnarchyExploitFixes.getConfiguration(); this.maxVillagersPerChunk = config.getInt("chunk-limits.entity-limits.villager-limit.max-villagers-per-chunk", 25); this.logIsEnabled = config.getBoolean("chunk-limits.entity-limits.villager-limit.log-removals", false); - this.checkPeriod = config.getInt("chunk-limits.entity-limits.villager-limit.check-period-in-ticks", 600, "check all chunks every x ticks."); + this.checkPeriod = config.getInt("chunk-limits.entity-limits.villager-limit.check-period-in-ticks", 600, + "check all chunks every x ticks."); config.getList("chunk-limits.entity-limits.villager-limit.removal-priority", List.of( "NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER", "FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN" - ), "Professions that are in the top of the list are going to be scheduled for removal first.").forEach(configuredProfession -> { + ), "Professions that are in the top of the list are going to be scheduled for removal first." + ).forEach(configuredProfession -> { try { Villager.Profession profession = Villager.Profession.valueOf(configuredProfession); this.removalPriority.add(profession); } catch (IllegalArgumentException e) { - LogUtils.moduleLog(Level.WARNING, name(), "Villager profession '"+configuredProfession+"' not recognized. Make sure you're using the correct profession enums for your Version."); + LogUtils.moduleLog(Level.WARNING, name(), "Villager profession '"+configuredProfession+"' not recognized. " + + "Make sure you're using the correct profession enums for your Version."); } }); } @@ -75,7 +78,7 @@ public void disable() { if (scheduledTask != null) scheduledTask.cancel(); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onCreateSpawn(CreatureSpawnEvent event) { if (event.getEntityType().equals(EntityType.VILLAGER)) { checkVillagersInChunk(event.getEntity().getChunk()); @@ -86,7 +89,7 @@ private void run() { for (World world : plugin.getServer().getWorlds()) { for (Chunk chunk : world.getLoadedChunks()) { plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), task -> { - Chunk.LoadLevel level = chunk.getLoadLevel(); + final Chunk.LoadLevel level = chunk.getLoadLevel(); if (level.equals(Chunk.LoadLevel.ENTITY_TICKING) || level.equals(Chunk.LoadLevel.TICKING)) { checkVillagersInChunk(chunk); } @@ -114,8 +117,8 @@ private void checkVillagersInChunk(Chunk chunk) { // Remove prioritized villagers that are too many for (int i = 0; i < amount_over_the_limit; i++) { Villager villager = villagers_in_chunk.get(i); - if (logIsEnabled) - LogUtils.moduleLog(Level.INFO, name(), "Removing villager of profession type '"+villager.getProfession()+"' at "+villager.getLocation()); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), + "Removing villager of profession type '"+villager.getProfession()+"' at "+villager.getLocation()); villager.getScheduler().run(plugin, kill -> villager.remove(), null); } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/BlockLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/BlockLimit.java index a85742558..b56b6e05f 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/BlockLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/BlockLimit.java @@ -142,7 +142,8 @@ public BlockLimit() { defaults.put("SKULL_ITEM", 50); } - ConfigSection section = AnarchyExploitFixes.getConfiguration().getConfigSection("chunk-limits.block-limit.max-blocks-per-chunk", defaults, "Attempt to prevent ChunkBan / Client FPS Lag"); + ConfigSection section = AnarchyExploitFixes.getConfiguration().getConfigSection("chunk-limits.block-limit.max-blocks-per-chunk", defaults, + "Attempt to prevent ChunkBan / Client FPS Lag"); for (String configuredMaterial : section.getKeys(false)) { try { Material blockMaterial = Material.valueOf(configuredMaterial); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/CustomEntityLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/CustomEntityLimit.java index 30fbb11ce..5f27e1bc5 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/CustomEntityLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/CustomEntityLimit.java @@ -22,8 +22,8 @@ public class CustomEntityLimit implements AnarchyExploitFixesModule, Listener, R private final AnarchyExploitFixes plugin; private final HashMap entityLimits = new HashMap<>(); - private final boolean logIsEnabled; private final long checkPeriod; + private final boolean logIsEnabled; public CustomEntityLimit() { shouldEnable(); @@ -147,19 +147,19 @@ private void onSpawn(EntitySpawnEvent event) { EntityType spawnedType = event.getEntityType(); if (!entityLimits.containsKey(spawnedType)) return; - final int maxAllowed = entityLimits.get(spawnedType); + final int maxAllowedPerChunk = entityLimits.get(spawnedType); + int entityCount = 0; + for (Entity entity : event.getEntity().getChunk().getEntities()) { - int count = 0; - if (entity.getType().equals(spawnedType)) { - count++; - if (count > maxAllowed) { - entity.remove(); - if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType() - + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowed - ); - } - } + if (!entity.getType().equals(spawnedType)) continue; + + entityCount++; + if (entityCount <= maxAllowedPerChunk) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowedPerChunk); } } @@ -172,21 +172,19 @@ public void run() { Entity[] chunkEntities = chunk.getEntities(); for (Map.Entry limit : entityLimits.entrySet()) { - - final int maxAllowed = limit.getValue(); - int count = 0; + final int maxAllowedPerChunk = limit.getValue(); + int entityCount = 0; for (Entity entity : chunkEntities) { - if (entity.getType().equals(limit.getKey())) { - count++; - if (count > maxAllowed) { - entity.remove(); - if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType() - + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowed - ); - } - } + if (!entity.getType().equals(limit.getKey())) continue; + + entityCount++; + if (entityCount <= maxAllowedPerChunk) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowedPerChunk); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/DroppedItemLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/DroppedItemLimit.java index bf5a395ad..70976e56e 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/DroppedItemLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/DroppedItemLimit.java @@ -20,8 +20,6 @@ import java.util.List; import java.util.logging.Level; -import static me.moomoo.anarchyexploitfixes.utils.LogUtils.moduleLog; - public class DroppedItemLimit implements AnarchyExploitFixesModule, Listener, Runnable { private final AnarchyExploitFixes plugin; @@ -34,10 +32,12 @@ public DroppedItemLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.entity-limits.dropped-item-limit.enable", "Limit the amount of dropped items in a chunk to combat lag.\nBe aware this does not prioritize higher value items."); + config.addComment("chunk-limits.entity-limits.dropped-item-limit.enable", + "Limit the amount of dropped items in a chunk to combat lag.\nBe aware this does not prioritize higher value items."); this.logIsEnabled = config.getBoolean("chunk-limits.entity-limits.dropped-item-limit.log-removals", true); this.maxDroppedItemsPerChunk = config.getInt("chunk-limits.entity-limits.dropped-item-limit.max-dropped-items-per-chunk", 200); - this.checkPeriod = config.getInt("chunk-limits.entity-limits.dropped-item-limit.check-period-in-ticks", 800, "20 ticks = 1 second"); + this.checkPeriod = config.getInt("chunk-limits.entity-limits.dropped-item-limit.check-period-in-ticks", 800, + "20 ticks = 1 second"); this.usingWhitelist = config.getBoolean("chunk-limits.entity-limits.dropped-item-limit.whitelist-specific-item-types", false); List defaults; if (AnarchyExploitFixes.getMCVersion() > 12) { @@ -51,7 +51,8 @@ public DroppedItemLimit() { "PINK_SHULKER_BOX", "PURPLE_SHULKER_BOX", "RED_SHULKER_BOX", "WHITE_SHULKER_BOX", "YELLOW_SHULKER_BOX" ); } - List configuredWhitelistedTypes = config.getList("chunk-limits.entity-limits.dropped-item-limit.whitelisted-types", defaults, "You need to use correct Material enums for your minecraft version here."); + List configuredWhitelistedTypes = config.getList("chunk-limits.entity-limits.dropped-item-limit.whitelisted-types", defaults, + "You need to use correct Material enums for your minecraft version here."); for (String configuredWhitelistedType : configuredWhitelistedTypes) { try { Material whitelistedType = Material.valueOf(configuredWhitelistedType); @@ -83,17 +84,21 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("chunk-limits.entity-limits.dropped-item-limit.enable", false); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onItemDrop(ItemSpawnEvent event) { - int count = 0; + int droppedItemCount = 0; + for (Entity entity : event.getEntity().getChunk().getEntities()) { - if (entity.getType().equals(EntityType.DROPPED_ITEM)) { - count++; - if (count > maxDroppedItemsPerChunk) { - if (usingWhitelist && whitelistedItems.contains(event.getEntity().getItemStack().getType())) continue; - entity.remove(); - } - } + if (!entity.getType().equals(EntityType.DROPPED_ITEM)) continue; + + droppedItemCount++; + if (droppedItemCount <= maxDroppedItemsPerChunk) continue; + if (usingWhitelist && whitelistedItems.contains(((Item) entity).getItemStack().getType())) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed dropped item at" + + " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + " in world " + entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk); } } @@ -103,32 +108,19 @@ public void run() { for (Chunk chunk : world.getLoadedChunks()) { if (!chunk.isLoaded()) continue; - int droppedItems = 0; + int droppedItemCount = 0; + for (Entity entity : chunk.getEntities()) { - if (!chunk.isLoaded()) continue; if (!entity.getType().equals(EntityType.DROPPED_ITEM)) continue; - if (usingWhitelist) { - if (!whitelistedItems.contains(((Item) entity).getItemStack().getType())) { - droppedItems++; - if (droppedItems > maxDroppedItemsPerChunk) { - entity.remove(); - if (logIsEnabled) moduleLog(Level.INFO, name(), "Removed dropped item at" - + " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + " in world "+ entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk - ); - } - } - } else { - droppedItems++; - if (droppedItems > maxDroppedItemsPerChunk) { - entity.remove(); - if (logIsEnabled) moduleLog(Level.INFO, name(), "Removed dropped item at" - + " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + " in world "+ entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk - ); - } - } + droppedItemCount++; + if (droppedItemCount <= maxDroppedItemsPerChunk) continue; + if (usingWhitelist && whitelistedItems.contains(((Item) entity).getItemStack().getType())) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed dropped item at" + + " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + " in world " + entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/ExpBottleLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/ExpBottleLimit.java index b23452e91..be0f89905 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/ExpBottleLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/ExpBottleLimit.java @@ -17,8 +17,10 @@ public class ExpBottleLimit implements AnarchyExploitFixesModule, Listener { public ExpBottleLimit() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.exp-bottle-limit.enable", "Prevent having a shit ton of exp bottles in one chunk then loading it to kill the server."); - this.maxExpBottlePerChunk = config.getInt("chunk-limits.exp-bottle-limit.max-exp-bottle-per-chunk", 25, "Max in a chunk, doesn't limit the actual xp orbs."); + config.addComment("chunk-limits.exp-bottle-limit.enable", + "Prevent having a shit ton of exp bottles in one chunk then loading it to kill the server."); + this.maxExpBottlePerChunk = config.getInt("chunk-limits.exp-bottle-limit.max-exp-bottle-per-chunk", 25, + "Max in a chunk, doesn't limit the actual xp orbs."); } @Override @@ -42,13 +44,16 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("chunk-limits.exp-bottle-limit.enable", true); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onExpBottle(ExpBottleEvent event) { - int count=0; + int expBottleCount=0; + for (Entity entity : event.getEntity().getChunk().getEntities()) { - if (entity.getType().equals(EntityType.THROWN_EXP_BOTTLE)) { - count++; - if (count > maxExpBottlePerChunk) entity.remove(); + if (!entity.getType().equals(EntityType.THROWN_EXP_BOTTLE)) continue; + + expBottleCount++; + if (expBottleCount > maxExpBottlePerChunk) { + entity.remove(); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/FallingBlockLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/FallingBlockLimit.java index bc3431107..5d5646754 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/FallingBlockLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/FallingBlockLimit.java @@ -3,6 +3,7 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.LogUtils; import me.moomoo.anarchyexploitfixes.utils.models.ExpiringSet; import org.bukkit.Chunk; import org.bukkit.entity.Entity; @@ -16,8 +17,6 @@ import java.time.Duration; import java.util.logging.Level; -import static me.moomoo.anarchyexploitfixes.utils.LogUtils.moduleLog; - public class FallingBlockLimit implements AnarchyExploitFixesModule, Listener { private final ExpiringSet checkedChunks; @@ -59,7 +58,7 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("chunk-limits.falling-block-limit.enable", true); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onBlockPhysics(BlockPhysicsEvent event) { Chunk chunk = event.getBlock().getChunk(); if (checkedChunks.contains(chunk)) return; @@ -79,13 +78,13 @@ private void onBlockPhysics(BlockPhysicsEvent event) { checkedChunks.add(chunk); - if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(), + if (logIsEnabled && removed_falling) LogUtils.moduleLog(Level.INFO, name(), "Removed falling blocks at " + event.getSourceBlock().getLocation() + ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk" ); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onChangeBlock(EntityChangeBlockEvent event) { if (!event.getEntityType().equals(EntityType.FALLING_BLOCK)) return; Chunk chunk = event.getBlock().getChunk(); @@ -106,7 +105,7 @@ private void onChangeBlock(EntityChangeBlockEvent event) { checkedChunks.add(chunk); - if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(), + if (logIsEnabled && removed_falling) LogUtils.moduleLog(Level.INFO, name(), "Removed falling blocks at " + event.getBlock().getLocation() + ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk" ); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/MinecartLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/MinecartLimit.java index d12b1552f..c71d218a5 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/MinecartLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/MinecartLimit.java @@ -3,27 +3,43 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.LogUtils; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Vehicle; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.vehicle.VehicleCreateEvent; +import java.util.Arrays; +import java.util.HashSet; +import java.util.logging.Level; +import java.util.stream.Collectors; + public class MinecartLimit implements AnarchyExploitFixesModule, Listener, Runnable { private final AnarchyExploitFixes plugin; - private final int maxMinecartsPerChunk; + private final HashSet MINECART_TYPES; private final long checkPeriod; + private final int maxMinecartsPerChunk; + private final boolean logIsEnabled; public MinecartLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); + this.MINECART_TYPES = Arrays.stream(EntityType.values()) + .filter(type -> type.name().toUpperCase().contains("MINECART")) + .collect(Collectors.toCollection(HashSet::new)); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.minecart-limit.enable", "Limit the amount of minecarts to prevent lag."); + config.addComment("chunk-limits.minecart-limit.enable", + "Limit the amount of minecarts to prevent lag."); + this.logIsEnabled = config.getBoolean("chunk-limits.minecart-limit.log-removals", false); this.maxMinecartsPerChunk = config.getInt("chunk-limits.minecart-limit.max-minecarts-per-chunk", 25); - this.checkPeriod = config.getInt("chunk-limits.minecart-limit.check-period-in-ticks", 400, "200 ticks = 10 seconds."); + this.checkPeriod = config.getInt("chunk-limits.minecart-limit.check-period-in-ticks", 400, + "200 ticks = 10 seconds."); } @Override @@ -47,19 +63,22 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("chunk-limits.minecart-limit.enable", false); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onCreate(VehicleCreateEvent event) { - if (!event.getVehicle().getType().name().contains("MINECART")) return; - - int count = 0; - for (Entity entity : event.getVehicle().getChunk().getEntities()) { - if (entity.getType().name().contains("MINECART")) { - count++; - if (count > maxMinecartsPerChunk) { - event.setCancelled(true); - return; - } - } + Vehicle vehicle = event.getVehicle(); + if (!MINECART_TYPES.contains(vehicle.getType())) return; + + int minecartCount = 0; + + for (Entity entity : vehicle.getChunk().getEntities()) { + if (!MINECART_TYPES.contains(entity.getType())) continue; + minecartCount++; + if (minecartCount <= maxMinecartsPerChunk) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed minecart " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + + " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxMinecartsPerChunk); } } @@ -69,14 +88,18 @@ public void run() { for (Chunk chunk : world.getLoadedChunks()) { if (!chunk.isLoaded()) continue; - int count = 0; + int minecartCount = 0; + for (Entity entity : chunk.getEntities()) { - if (entity.getType().name().contains("MINECART")) { - count++; - if (count > maxMinecartsPerChunk) { - entity.remove(); - } - } + if (!MINECART_TYPES.contains(entity.getType())) continue; + + minecartCount++; + if (minecartCount <= maxMinecartsPerChunk) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed minecart " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + + " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxMinecartsPerChunk); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/NonLivingEntityLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/NonLivingEntityLimit.java index b6405aebc..6cabe65b9 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/NonLivingEntityLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/NonLivingEntityLimit.java @@ -3,6 +3,7 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.LogUtils; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Entity; @@ -13,25 +14,27 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntitySpawnEvent; +import java.util.HashSet; import java.util.logging.Level; -import static me.moomoo.anarchyexploitfixes.utils.LogUtils.moduleLog; - public class NonLivingEntityLimit implements AnarchyExploitFixesModule, Listener, Runnable { private final AnarchyExploitFixes plugin; - private final boolean logIsEnabled; - private final int maxNonLivingEntities; + private final HashSet NON_LIVING_TYPES = new HashSet<>(); private final long checkPeriod; + private final int maxNonLivingEntities; + private final boolean logIsEnabled; public NonLivingEntityLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.entity-limits.non-living-limit.enable", "Limit the amount of non living entities in a chunk to prevent lag (does not affect items)."); + config.addComment("chunk-limits.entity-limits.non-living-limit.enable", + "Limit the amount of non living entities in a chunk to prevent lag (does not affect items)."); this.logIsEnabled = config.getBoolean("chunk-limits.entity-limits.non-living-limit.log-removals", true); this.maxNonLivingEntities = config.getInt("chunk-limits.entity-limits.non-living-limit.max-non-living-per-chunk", 100); - this.checkPeriod = config.getInt("chunk-limits.entity-limits.non-living-limit.check-period-in-ticks", 20, "20 ticks = 1 second"); + this.checkPeriod = config.getInt("chunk-limits.entity-limits.non-living-limit.check-period-in-ticks", 20, + "20 ticks = 1 second"); } @Override @@ -55,21 +58,43 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("chunk-limits.entity-limits.non-living-limit.enable", false); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onSpawn(EntitySpawnEvent event) { if (event.getEntityType().equals(EntityType.DROPPED_ITEM) || event.getEntity() instanceof LivingEntity) return; - int count = 0; + int nonLivingCount = 0; + for (Entity entity : event.getEntity().getChunk().getEntities()) { + final EntityType type = entity.getType(); + + if (NON_LIVING_TYPES.contains(type)) { + nonLivingCount++; + if (nonLivingCount <= maxNonLivingEntities) continue; + + entity.remove(); + event.setCancelled(true); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed non-living entity " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxNonLivingEntities); + + continue; + } + if ( - !entity.getType().equals(EntityType.DROPPED_ITEM) - && !entity.getType().equals(EntityType.ENDER_PEARL) + !type.equals(EntityType.DROPPED_ITEM) + && !type.equals(EntityType.ENDER_PEARL) && !(entity instanceof LivingEntity) ) { - count++; - if (count > maxNonLivingEntities) { - entity.remove(); - } + NON_LIVING_TYPES.add(type); // Add type of non-living to HashSet for faster operation in the future + + nonLivingCount++; + if (nonLivingCount <= maxNonLivingEntities) continue; + + entity.remove(); + event.setCancelled(true); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed non-living entity " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxNonLivingEntities); } } } @@ -80,21 +105,37 @@ public void run() { for (Chunk chunk : world.getLoadedChunks()) { if (!chunk.isLoaded()) continue; - int nonLiving = 0; + int nonLivingCount = 0; + for (Entity entity : chunk.getEntities()) { + final EntityType type = entity.getType(); + + if (NON_LIVING_TYPES.contains(type)) { + nonLivingCount++; + if (nonLivingCount <= maxNonLivingEntities) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed non-living entity " + + entity.getType() + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + + " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxNonLivingEntities); + + continue; + } + if ( - !entity.getType().equals(EntityType.DROPPED_ITEM) - && !entity.getType().equals(EntityType.ENDER_PEARL) - && !(entity instanceof LivingEntity) + !type.equals(EntityType.DROPPED_ITEM) + && !type.equals(EntityType.ENDER_PEARL) + && !(entity instanceof LivingEntity) ) { - nonLiving++; - if (nonLiving > maxNonLivingEntities) { - entity.remove(); - if (logIsEnabled) moduleLog(Level.INFO, name(), "Removed non-living entity " + entity.getType() - + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() - + ", because reached limit of " + maxNonLivingEntities - ); - } + NON_LIVING_TYPES.add(type); + + nonLivingCount++; + if (nonLivingCount <= maxNonLivingEntities) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed non-living entity " + + entity.getType() + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + + " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxNonLivingEntities); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VehicleLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VehicleLimit.java index 2523daa90..d352c444e 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VehicleLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VehicleLimit.java @@ -3,26 +3,43 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.LogUtils; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Vehicle; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.vehicle.VehicleCreateEvent; +import java.util.Arrays; +import java.util.HashSet; +import java.util.logging.Level; +import java.util.stream.Collectors; + public class VehicleLimit implements AnarchyExploitFixesModule, Listener, Runnable { private final AnarchyExploitFixes plugin; - private final int maxVehiclesPerChunk; + private final HashSet VEHICLE_TYPES; private final long checkPeriod; + private final int maxVehiclesPerChunk; + private final boolean logIsEnabled; public VehicleLimit() { shouldEnable(); this.plugin = AnarchyExploitFixes.getInstance(); + this.VEHICLE_TYPES = new HashSet<>(); // Used to cache entity types of vehicles for faster operations + // Prefill with known types; + this.VEHICLE_TYPES.addAll(Arrays.stream(EntityType.values()).filter(entityType -> { + final String name = entityType.name().toUpperCase(); + return name.contains("MINECART") && name.contains("BOAT"); + }).collect(Collectors.toSet())); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("chunk-limits.vehicle-limit.enable", "Limit the amount of vehicles to prevent some lag machines involving boats and a dispenser,"); + config.addComment("chunk-limits.vehicle-limit.enable", + "Limit the amount of vehicles to prevent some lag machines involving boats and a dispenser."); + this.logIsEnabled = config.getBoolean("chunk-limits.vehicle-limit.log-removals", false); this.maxVehiclesPerChunk = config.getInt("chunk-limits.vehicle-limit.max-vehicles-per-chunk", 25); this.checkPeriod = config.getInt("chunk-limits.vehicle-limit.check-period-in-ticks", 400, "200 ticks = 10 seconds."); } @@ -48,16 +65,34 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("chunk-limits.vehicle-limit.enable", false); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onCreate(VehicleCreateEvent event) { - int count = 0; + int vehicleCount = 0; + for (Entity entity : event.getVehicle().getChunk().getEntities()) { + final EntityType type = entity.getType(); + + if (VEHICLE_TYPES.contains(type)) { + vehicleCount++; + if (vehicleCount <= maxVehiclesPerChunk) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed vehicle " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxVehiclesPerChunk); + + continue; + } + if (entity instanceof Vehicle) { - count++; - if (count > maxVehiclesPerChunk) { - event.setCancelled(true); - return; - } + VEHICLE_TYPES.add(type); // Add type of vehicle to HashSet for faster operation in the future + vehicleCount++; + if (vehicleCount <= maxVehiclesPerChunk) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed vehicle " + entity.getType() + + " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxVehiclesPerChunk); } } } @@ -68,13 +103,34 @@ public void run() { for (Chunk chunk : world.getLoadedChunks()) { if (!chunk.isLoaded()) continue; - int countedVehicles = 0; + int vehicleCount = 0; + for (Entity entity : chunk.getEntities()) { + final EntityType type = entity.getType(); + + if (VEHICLE_TYPES.contains(type)) { + vehicleCount++; + if (vehicleCount <= maxVehiclesPerChunk) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed vehicle " + + entity.getType() + " at x:" + entity.getLocation().getX() + + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxVehiclesPerChunk); + + continue; + } + if (entity instanceof Vehicle) { - countedVehicles++; - if (countedVehicles > maxVehiclesPerChunk) { - entity.remove(); - } + VEHICLE_TYPES.add(type); // Add type of vehicle to HashSet for faster operation in the future + vehicleCount++; + if (vehicleCount <= maxVehiclesPerChunk) continue; + + entity.remove(); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Removed vehicle " + + entity.getType() + " at x:" + entity.getLocation().getX() + + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ() + + ", because reached limit of " + maxVehiclesPerChunk); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VillagerLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VillagerLimit.java index 9eb1b8ac6..df705695a 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VillagerLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/chunklimits/VillagerLimit.java @@ -24,9 +24,9 @@ public class VillagerLimit implements AnarchyExploitFixesModule, Runnable, Liste private final AnarchyExploitFixes plugin; private final List removalPriority = new ArrayList<>(); + private final long checkPeriod; private final int maxVillagersPerChunk; private final boolean logIsEnabled; - private final long checkPeriod; public VillagerLimit() { shouldEnable(); @@ -34,19 +34,19 @@ public VillagerLimit() { Config config = AnarchyExploitFixes.getConfiguration(); this.maxVillagersPerChunk = config.getInt("chunk-limits.entity-limits.villager-limit.max-villagers-per-chunk", 25); this.logIsEnabled = config.getBoolean("chunk-limits.entity-limits.villager-limit.log-removals", false); - this.checkPeriod = config.getInt("chunk-limits.entity-limits.villager-limit.check-period-in-ticks", 600, "check all chunks every x ticks."); + this.checkPeriod = config.getInt("chunk-limits.entity-limits.villager-limit.check-period-in-ticks", 600, + "check all chunks every x ticks."); config.getList("chunk-limits.entity-limits.villager-limit.removal-priority", Arrays.asList( "NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER", "FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN" - ), - "Professions that are in the top of the list are going to be scheduled for removal first." - + ), "Professions that are in the top of the list are going to be scheduled for removal first." ).forEach(configuredProfession -> { try { Villager.Profession profession = Villager.Profession.valueOf(configuredProfession); this.removalPriority.add(profession); } catch (IllegalArgumentException e) { - LogUtils.moduleLog(Level.WARNING, name(), "Villager profession '"+configuredProfession+"' not recognized. Make sure youre using the correct profession enums for your Version."); + LogUtils.moduleLog(Level.WARNING, name(), "Villager profession '"+configuredProfession+"' not recognized. " + + "Make sure youre using the correct profession enums for your Version."); } }); } @@ -72,10 +72,10 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("chunk-limits.entity-limits.villager-limit.enable", false); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onCreateSpawn(CreatureSpawnEvent event) { if (event.getEntityType().equals(EntityType.VILLAGER)) { - checkVillagersInChunk(event.getEntity().getChunk()); + this.checkVillagersInChunk(event.getEntity().getChunk()); } } @@ -83,7 +83,7 @@ private void onCreateSpawn(CreatureSpawnEvent event) { public void run() { for (World world : plugin.getServer().getWorlds()) { for (Chunk chunk : world.getLoadedChunks()) { - checkVillagersInChunk(chunk); + if (chunk.isLoaded()) this.checkVillagersInChunk(chunk); } } } @@ -107,8 +107,8 @@ private void checkVillagersInChunk(Chunk chunk) { // Remove prioritized villagers that are too many for (int i = 0; i < amount_over_the_limit; i++) { Villager villager = villagers_in_chunk.get(i); - if (logIsEnabled) - LogUtils.moduleLog(Level.INFO, name(), "Removing villager of profession type '"+villager.getProfession()+"' at "+villager.getLocation()); + if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), + "Removing villager of profession type '"+villager.getProfession()+"' at "+villager.getLocation()); villager.remove(); } }