Skip to content

Commit

Permalink
only check dimension to unload on world tick
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilzinsel64 committed May 23, 2024
1 parent 690bafa commit 1848f7d
Showing 1 changed file with 6 additions and 27 deletions.
33 changes: 6 additions & 27 deletions src/main/java/de/pilz/alternativechunkloading/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ public void onWorldLoad(WorldEvent.Load event) {
((ChunkProviderServer) chunkProvider).loadChunkOnProvideRequest = false;
}
}

if (ConfigGeneral.autoUnloadDimensions) {
checkDimensionToUnload(world);
}
}
}

Expand Down Expand Up @@ -74,13 +70,10 @@ public void onChunkUnforce(UnforceChunkEvent event) {
WorldServer world = (WorldServer) event.ticket.world;

// Prevent chunks to be auto loaded
var coordMap = getPendingForcedChunksForWorld((WorldServer) event.ticket.world);
var coordMap = getPendingForcedChunksForWorld(world);
if (coordMap.containsKey(event.location)) {
coordMap.remove(event.location);
}

// Check dimension to auto unload
checkDimensionToUnload(world);
}
}

Expand All @@ -89,14 +82,6 @@ public void onChunkLoad(ChunkEvent.Load event) {
pendingUnloadDimensions.remove(event.world);
}

@SubscribeEvent
public void onChunkUnload(ChunkEvent.Load event) {
// Check dimension to unload
if (ConfigGeneral.autoUnloadDimensions && !event.world.isRemote && event.world instanceof WorldServer) {
checkDimensionToUnload((WorldServer) event.world);
}
}

@SubscribeEvent
public void onWorldTick(WorldTickEvent event) {
if (event.side == Side.SERVER && event.world instanceof WorldServer) {
Expand All @@ -111,13 +96,9 @@ public void onWorldTick(WorldTickEvent event) {
}

private void checkDimensionToUnload(WorldServer world) {
// Check if already pending
if (pendingUnloadDimensions.containsKey(world)) {
return;
}

// Check blacklist
if (ConfigGeneral.isOnAutoUnloadDimensionBlacklist(world.provider.dimensionId)) {
if (!ConfigGeneral.autoUnloadDimensions
|| ConfigGeneral.isOnAutoUnloadDimensionBlacklist(world.provider.dimensionId)) {
return;
}

Expand All @@ -127,7 +108,7 @@ private void checkDimensionToUnload(WorldServer world) {
return;
}

// Put on list
// Put on list or reset timer
pendingUnloadDimensions.put(world, 0);
}

Expand Down Expand Up @@ -167,10 +148,8 @@ private void unloadDimension(WorldServer world) {
Integer ticksWaited = pendingUnloadDimensions.getOrDefault(world, -1);

if (ticksWaited == -1) {
return;
}

if (ticksWaited >= ConfigGeneral.ticksBeforeUnloadDimension) {
checkDimensionToUnload(world);
} else if (ticksWaited >= ConfigGeneral.ticksBeforeUnloadDimension) {
// Unload dimension
pendingUnloadDimensions.remove(world);
DimensionManager.unloadWorld(world.provider.dimensionId);
Expand Down

0 comments on commit 1848f7d

Please sign in to comment.