diff --git a/src/main/java/com/gamingmesh/jobs/config/ExploitProtectionManager.java b/src/main/java/com/gamingmesh/jobs/config/ExploitProtectionManager.java index 980d9a5f..ffb954fb 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ExploitProtectionManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ExploitProtectionManager.java @@ -28,7 +28,29 @@ public class ExploitProtectionManager { private static final String NAMETIME = "Time"; private static final String NAMEPAID = "Paid"; - private HashMap> map = new HashMap<>(); + private HashMap> map = new HashMap<>(); + + class chunkData { + private CMIChunkPersistentDataContainer container; + private long lastClean = 0; + + chunkData(CMIChunkPersistentDataContainer container) { + this.container = container; + lastClean = System.currentTimeMillis(); + } + + public long getLastClean() { + return lastClean; + } + + public void setLastClean(long lastClean) { + this.lastClean = lastClean; + } + + public CMIChunkPersistentDataContainer getContainer() { + return container; + } + } private CMIChunkPersistentDataContainer getPDC(Block block) { if (block == null) @@ -39,14 +61,21 @@ public class ExploitProtectionManager { private CMIChunkPersistentDataContainer getPDC(Chunk chunk) { if (chunk == null) return null; - HashMap world = map.computeIfAbsent(chunk.getWorld().getName(), x -> new HashMap<>()); - return world.computeIfAbsent(chunk.getX() + ":" + chunk.getZ(), x -> new CMIChunkPersistentDataContainer(NAMEGENERAL, chunk)); + chunkData data = getChunkData(chunk); + return data == null ? null : data.getContainer(); + } + + private chunkData getChunkData(Chunk chunk) { + if (chunk == null) + return null; + HashMap world = map.computeIfAbsent(chunk.getWorld().getName(), x -> new HashMap<>()); + return world.computeIfAbsent(chunk.getX() + ":" + chunk.getZ(), x -> new chunkData(new CMIChunkPersistentDataContainer(NAMEGENERAL, chunk))); } public void removePDC(Chunk chunk) { if (!Jobs.getGCManager().useNewBlockProtection) return; - HashMap world = map.get(chunk.getWorld().getName()); + HashMap world = map.get(chunk.getWorld().getName()); if (world == null) return; world.remove(chunk.getX() + ":" + chunk.getZ()); @@ -309,17 +338,25 @@ public class ExploitProtectionManager { try { - CMIChunkPersistentDataContainer pdc = getPDC(chunk); + chunkData pdc = getChunkData(chunk); - Set keys = pdc.getKeys(); + if (pdc == null) + return; + + // Delay to clean it up once more to prevent rapid updates of same chunk + if (pdc.getLastClean() + (30 * 1000L) > System.currentTimeMillis()) + return; + + Set keys = pdc.getContainer().getKeys(); for (NamespacedKey one : keys) { - Long time = deconvertLong(pdc.getInt(one.getKey(), NAMETIME)); + Long time = deconvertLong(pdc.getContainer().getInt(one.getKey(), NAMETIME)); if (time != null && time < System.currentTimeMillis()) { - pdc.remove(one.getKey()); + pdc.getContainer().remove(one.getKey()); } } - pdc.save(); + pdc.getContainer().save(); + pdc.setLastClean(System.currentTimeMillis()); } catch (Throwable e) { e.printStackTrace();