diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/CustomEntityLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/CustomEntityLimit.java index 8f076bba..afa44b40 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/CustomEntityLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/CustomEntityLimit.java @@ -26,11 +26,12 @@ public class CustomEntityLimit extends AEFModule implements Consumer, Listener { - private ScheduledTask scheduledTask; private final Map entityLimits = new EnumMap<>(EntityType.class); private final long checkPeriod, minChunkAge; private final boolean logIsEnabled, enableChunkAgeSkip, forceLoadEntities; + private ScheduledTask scheduledTask; + public CustomEntityLimit() { super("chunk-limits.entity-limits.custom-limit", false, """ Limit specific entity types per chunk.\s diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/DroppedItemLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/DroppedItemLimit.java index 79b92035..e4a65083 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/DroppedItemLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/DroppedItemLimit.java @@ -23,6 +23,7 @@ import java.time.Duration; import java.util.EnumSet; +import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; @@ -31,13 +32,14 @@ public class DroppedItemLimit extends AEFModule implements Consumer, Listener { - private ScheduledTask scheduledTask; - private final Cache scheduledChecks; private final Set whitelistedTypes; private final long checkPeriod, cleanupDelay; private final int maxDroppedItemsPerChunk; private final boolean logIsEnabled, usingWhitelist, onEntitiesLoad; + private Cache scheduledChecks; + private ScheduledTask scheduledTask; + public DroppedItemLimit() { super("chunk-limits.entity-limits.dropped-item-limit", false, """ Limit the amount of dropped items in a chunk to combat lag.\s @@ -55,7 +57,6 @@ public DroppedItemLimit() { The period in ticks in which all loaded chunks should be regularly\s checked. Keep in mind: A lower number provides more accuracy but is\s also worse for performance."""); - this.scheduledChecks = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(cleanupDelay * 50L)).build(); this.onEntitiesLoad = config.getBoolean(configPath + ".check-on-entities-load", true, """ Runs item check when a chunk's entities are loaded."""); this.usingWhitelist = config.getBoolean(configPath + ".whitelist-specific-item-types", false); @@ -79,16 +80,27 @@ public DroppedItemLimit() { @Override public void enable() { + scheduledChecks = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(cleanupDelay * 50L)).build(); plugin.getServer().getPluginManager().registerEvents(this, plugin); - this.scheduledTask = plugin.getServer().getGlobalRegionScheduler() + scheduledTask = plugin.getServer().getGlobalRegionScheduler() .runAtFixedRate(plugin, this, checkPeriod, checkPeriod); } @Override public void disable() { HandlerList.unregisterAll(this); - if (scheduledTask != null) scheduledTask.cancel(); - scheduledChecks.asMap().forEach((chunk, queuedCheck) -> queuedCheck.cancel()); + if (scheduledTask != null) { + scheduledTask.cancel(); + scheduledTask = null; + } + if (scheduledChecks != null) { + for (Map.Entry entry : scheduledChecks.asMap().entrySet()) { + entry.getValue().cancel(); + } + scheduledChecks.invalidateAll(); + scheduledChecks.cleanUp(); + scheduledChecks = null; + } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/ExpBottleLimit.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/ExpBottleLimit.java index 4401f328..91ee986d 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/ExpBottleLimit.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/ExpBottleLimit.java @@ -19,11 +19,12 @@ public class ExpBottleLimit extends AEFModule implements Consumer, Listener { - private ScheduledTask scheduledTask; private final long checkPeriod; private final int maxExpBottlePerChunk; private final boolean logIsEnabled; + private ScheduledTask scheduledTask; + public ExpBottleLimit() { super("chunk-limits.exp-bottle-limit", true, """ Prevent players from crashing the server or other players by\s diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/CustomEntityLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/CustomEntityLimit.java index fdf8423b..64d20cbb 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/CustomEntityLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/CustomEntityLimit.java @@ -27,6 +27,7 @@ public class CustomEntityLimit extends AEFModule implements Runnable, Listener { private final Map entityLimits = new EnumMap<>(EntityType.class); private final long checkPeriod; private final boolean logIsEnabled; + private BukkitTask bukkitTask; public CustomEntityLimit() { @@ -146,7 +147,10 @@ public void enable() { @Override public void disable() { HandlerList.unregisterAll(this); - if (bukkitTask != null) bukkitTask.cancel(); + if (bukkitTask != null) { + bukkitTask.cancel(); + bukkitTask = null; + } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/DroppedItemLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/DroppedItemLimit.java index 383430bc..b6858f36 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/DroppedItemLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/DroppedItemLimit.java @@ -23,17 +23,19 @@ import java.time.Duration; import java.util.EnumSet; +import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; public class DroppedItemLimit extends AEFModule implements Listener, Runnable { - private final Cache scheduledChecks; private final Set whitelistedTypes; private final long checkPeriod, cleanupDelay; private final int maxDroppedItemsPerChunk; private final boolean logIsEnabled, usingWhitelist, onChunkLoad; + + private Cache scheduledChecks; private BukkitTask bukkitTask; public DroppedItemLimit() { @@ -49,7 +51,6 @@ public DroppedItemLimit() { "has dropped before the check logic will run.\n" + "This improves performance as there will be no check for each single\n" + "item entity that spawns.")); - this.scheduledChecks = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(cleanupDelay * 50L)).build(); this.checkPeriod = config.getInt(configPath + ".check-period-in-ticks", 1200, "The period in ticks in which all loaded chunks should be regularly\n" + "checked. Keep in mind: A lower number provides more accuracy but is\n" + @@ -77,14 +78,27 @@ public DroppedItemLimit() { @Override public void enable() { + scheduledChecks = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(cleanupDelay * 50L)).build(); plugin.getServer().getPluginManager().registerEvents(this, plugin); - bukkitTask = plugin.getServer().getScheduler().runTaskTimer(plugin, this, checkPeriod, checkPeriod); + bukkitTask = plugin.getServer().getScheduler() + .runTaskTimer(plugin, this, checkPeriod, checkPeriod); } @Override public void disable() { HandlerList.unregisterAll(this); - if (bukkitTask != null) bukkitTask.cancel(); + if (bukkitTask != null) { + bukkitTask.cancel(); + bukkitTask = null; + } + if (scheduledChecks != null) { + for (Map.Entry entry : scheduledChecks.asMap().entrySet()) { + entry.getValue().cancel(); + } + scheduledChecks.invalidateAll(); + scheduledChecks.cleanUp(); + scheduledChecks = null; + } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -93,7 +107,7 @@ private void onItemDrop(ItemSpawnEvent event) { final ChunkUID chunkUID = ChunkUID.of(chunk); // Don't create a check task for each spawning item scheduledChecks.get(chunkUID, k -> - plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> { + plugin.getServer().getScheduler().runTaskLater(plugin, () -> { if (!chunk.isLoaded()) return; int droppedItemCount = 0; diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/ExpBottleLimit.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/ExpBottleLimit.java index 3d2cffd6..bd5fa575 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/ExpBottleLimit.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/ExpBottleLimit.java @@ -19,6 +19,7 @@ public class ExpBottleLimit extends AEFModule implements Runnable, Listener { private final long checkPeriod; private final int maxExpBottlePerChunk; private final boolean logIsEnabled; + private BukkitTask bukkitTask; public ExpBottleLimit() { @@ -43,7 +44,10 @@ public void enable() { @Override public void disable() { HandlerList.unregisterAll(this); - if (bukkitTask != null) bukkitTask.cancel(); + if (bukkitTask != null) { + bukkitTask.cancel(); + bukkitTask = null; + } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)