From 5818052417e9755a3d0f33c366c69183a5fccb59 Mon Sep 17 00:00:00 2001 From: Aleksey Terzi Date: Thu, 4 Aug 2022 18:55:20 -0600 Subject: [PATCH] A little bit of refactoring --- .../world/locations/chunkmeta/ChunkCoord.java | 41 +++++++++---------- .../chunkmeta/WorldChunkMetaManager.java | 12 +++--- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/ChunkCoord.java b/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/ChunkCoord.java index b9f59d931..e1046230c 100644 --- a/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/ChunkCoord.java +++ b/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/ChunkCoord.java @@ -13,15 +13,16 @@ import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.api.ChunkMetaViewTrac import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.stat.LoadStatisticManager; public class ChunkCoord extends XZWCoord { + private static final long INVALID_TIME = -1; /** * When was this chunk last loaded in Minecraft as UNIX timestamp */ - private long lastLoadingTime; + private long lastLoadedTime; /** * When was this chunk last unloaded in Minecraft as UNIX timestamp */ - private long lastUnloadingTime; + private long lastUnloadedTime; /** * Each ChunkMeta belongs to one plugin, they are identified by the plugin id */ @@ -37,8 +38,8 @@ public class ChunkCoord extends XZWCoord { super(x, z, worldID); this.world = world; this.chunkMetas = new TreeMap<>(); - this.lastLoadingTime = -1; - this.lastUnloadingTime = -1; + this.lastLoadedTime = INVALID_TIME; + this.lastUnloadedTime = INVALID_TIME; } /** @@ -106,24 +107,20 @@ public class ChunkCoord extends XZWCoord { } } - /** - * @return When was the minecraft chunk (the block data) this object is tied - * last loaded (UNIX timestamp) - */ - long getLastMCLoadingTime() { - return lastLoadingTime; - } - /** * @return When was the minecraft chunk (the block data) this object is tied * last unloaded (UNIX timestamp) */ - long getLastMCUnloadingTime() { - return lastUnloadingTime; + long getLastUnloadedTime() { + return lastUnloadedTime; } - void clearLastMCUnloadingTime() { - lastUnloadingTime = -1; + void clearUnloaded() { + lastUnloadedTime = INVALID_TIME; + } + + boolean isUnloaded() { + return lastUnloadedTime > lastLoadedTime; } ChunkMetaLoadStatus getMetaIfLoaded(short pluginID, boolean alwaysLoaded) { @@ -208,8 +205,8 @@ public class ChunkCoord extends XZWCoord { * loaded */ void minecraftChunkLoaded() { - boolean hasBeenLoadedBefore = this.lastLoadingTime != -1; - this.lastLoadingTime = System.currentTimeMillis(); + boolean hasBeenLoadedBefore = this.lastLoadedTime != INVALID_TIME; + this.lastLoadedTime = System.currentTimeMillis(); if (hasBeenLoadedBefore) { for (ChunkMeta meta : chunkMetas.values()) { try { @@ -222,10 +219,10 @@ public class ChunkCoord extends XZWCoord { } public boolean isChunkLoaded() { - if (this.lastUnloadingTime > 0) { - return this.lastUnloadingTime < this.lastLoadingTime; + if (this.lastUnloadedTime > 0) { + return this.lastUnloadedTime < this.lastLoadedTime; } else { - return this.lastLoadingTime > 0; + return this.lastLoadedTime > 0; } } @@ -234,7 +231,7 @@ public class ChunkCoord extends XZWCoord { * unloaded */ void minecraftChunkUnloaded() { - this.lastUnloadingTime = System.currentTimeMillis(); + this.lastUnloadedTime = System.currentTimeMillis(); for (ChunkMeta meta : chunkMetas.values()) { try { meta.handleChunkUnload(); diff --git a/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/WorldChunkMetaManager.java b/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/WorldChunkMetaManager.java index f02489eff..20bb1baf0 100644 --- a/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/WorldChunkMetaManager.java +++ b/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/WorldChunkMetaManager.java @@ -106,7 +106,7 @@ public class WorldChunkMetaManager { if (populate) { // Prevent removal from metas in case we load at the same time // as it unloads - value.clearLastMCUnloadingTime(); + value.clearUnloaded(); } return value; } @@ -223,11 +223,11 @@ public class WorldChunkMetaManager { ChunkCoord coord; while((coord = unloadingQueue.poll()) != null) { - if (coord.getLastMCUnloadingTime() == -1) { + if (!coord.isUnloaded()) { continue; } - if (System.currentTimeMillis() - coord.getLastMCUnloadingTime() > UNLOAD_DELAY) { + if (System.currentTimeMillis() - coord.getLastUnloadedTime() > UNLOAD_DELAY) { unloadChunkCoord(coord); } else { if (readdList == null) { @@ -245,7 +245,7 @@ public class WorldChunkMetaManager { private void unloadChunkCoord(ChunkCoord coord) { // make sure chunk hasnt loaded again since - if (coord.getLastMCUnloadingTime() <= coord.getLastMCLoadingTime()) { + if (!coord.isUnloaded()) { return; } @@ -266,9 +266,9 @@ public class WorldChunkMetaManager { // coord is up for garbage collection at this point and all of its data has been // written to the db synchronized (metas) { - if (coord.getLastMCUnloadingTime() > coord.getLastMCLoadingTime()) { + if (coord.isUnloaded()) { metas.remove(coord); - coord.clearLastMCUnloadingTime(); + coord.clearUnloaded(); } } }