A little bit of refactoring

This commit is contained in:
Aleksey Terzi
2022-08-04 18:55:20 -06:00
parent 1b6f872199
commit 5818052417
2 changed files with 25 additions and 28 deletions

View File

@@ -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();

View File

@@ -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();
}
}
}