Skip to content

Commit

Permalink
cleanup leftover chunklimit modules
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 31, 2024
1 parent 510ddd5 commit 519dcaa
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

public class CustomEntityLimit extends AEFModule implements Consumer<ScheduledTask>, Listener {

private ScheduledTask scheduledTask;
private final Map<EntityType, Integer> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,13 +32,14 @@

public class DroppedItemLimit extends AEFModule implements Consumer<ScheduledTask>, Listener {

private ScheduledTask scheduledTask;
private final Cache<ChunkUID, ScheduledTask> scheduledChecks;
private final Set<Material> whitelistedTypes;
private final long checkPeriod, cleanupDelay;
private final int maxDroppedItemsPerChunk;
private final boolean logIsEnabled, usingWhitelist, onEntitiesLoad;

private Cache<ChunkUID, ScheduledTask> 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
Expand All @@ -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);
Expand All @@ -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<ChunkUID, ScheduledTask> entry : scheduledChecks.asMap().entrySet()) {
entry.getValue().cancel();
}
scheduledChecks.invalidateAll();
scheduledChecks.cleanUp();
scheduledChecks = null;
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

public class ExpBottleLimit extends AEFModule implements Consumer<ScheduledTask>, 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CustomEntityLimit extends AEFModule implements Runnable, Listener {
private final Map<EntityType, Integer> entityLimits = new EnumMap<>(EntityType.class);
private final long checkPeriod;
private final boolean logIsEnabled;

private BukkitTask bukkitTask;

public CustomEntityLimit() {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChunkUID, Integer> scheduledChecks;
private final Set<Material> whitelistedTypes;
private final long checkPeriod, cleanupDelay;
private final int maxDroppedItemsPerChunk;
private final boolean logIsEnabled, usingWhitelist, onChunkLoad;

private Cache<ChunkUID, BukkitTask> scheduledChecks;
private BukkitTask bukkitTask;

public DroppedItemLimit() {
Expand All @@ -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" +
Expand Down Expand Up @@ -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<ChunkUID, BukkitTask> entry : scheduledChecks.asMap().entrySet()) {
entry.getValue().cancel();
}
scheduledChecks.invalidateAll();
scheduledChecks.cleanUp();
scheduledChecks = null;
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down

0 comments on commit 519dcaa

Please sign in to comment.