Skip to content

Commit

Permalink
Force immediate recalculation on chunk unload
Browse files Browse the repository at this point in the history
If scheduled a tick after, this forces the chunk to be loaded back in. Potentially causing a horrific loop of death.
  • Loading branch information
novucs committed Jan 10, 2017
1 parent 1d6eaec commit de3293a
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions core/src/main/java/net/novucs/ftop/manager/WorthManager.java
Expand Up @@ -387,32 +387,41 @@ private void recalculate(ChunkWorth chunkWorth, ChunkPos pos, Chunk chunk, Recal
chunkWorth.setNextRecalculation(Long.MAX_VALUE);

// Schedule this chunk to be recalculated on a separate thread.
if (reason == RecalculateReason.UNLOAD) {
forceRecalculate(pos, chunk);
}

// Occasionally block updates are not updated in the chunk on the
// same tick, getting the chunk snapshot in the next tick fixes
// this issue.
plugin.getServer().getScheduler().runTask(plugin, () -> {
// Clear the recalculate queue in the event of multiple block
// changes in the same tick.
recalculateQueue.row(pos).clear();

// Update the chunk spawner worth on the main thread, unfortunately
// there is no better method of doing this. Same with chests.
Map<EntityType, Integer> spawners = new EnumMap<>(EntityType.class);
Map<Material, Integer> materials = new EnumMap<>(Material.class);

if (plugin.getSettings().isEnabled(WorthType.SPAWNER)) {
set(pos, WorthType.SPAWNER, getSpawnerWorth(chunk, spawners));
}
// this issue. This does not apply to chunk unloads.
else {
plugin.getServer().getScheduler().runTask(plugin, () ->
forceRecalculate(pos, chunk));
}
}

if (plugin.getSettings().isEnabled(WorthType.CHEST)) {
set(pos, WorthType.CHEST, getChestWorth(chunk, materials, spawners));
}
private void forceRecalculate(ChunkPos pos, Chunk chunk) {
// Clear the recalculate queue in the event of multiple block
// changes in the same tick.
recalculateQueue.row(pos).clear();

setSpawners(pos, spawners);
materialsQueue.row(pos).putAll(materials);
// Update the chunk spawner worth on the main thread, unfortunately
// there is no better method of doing this. Same with chests.
Map<EntityType, Integer> spawners = new EnumMap<>(EntityType.class);
Map<Material, Integer> materials = new EnumMap<>(Material.class);

plugin.getChunkWorthTask().queue(chunk.getChunkSnapshot());
});
if (plugin.getSettings().isEnabled(WorthType.SPAWNER)) {
set(pos, WorthType.SPAWNER, getSpawnerWorth(chunk, spawners));
}

if (plugin.getSettings().isEnabled(WorthType.CHEST)) {
set(pos, WorthType.CHEST, getChestWorth(chunk, materials, spawners));
}

setSpawners(pos, spawners);
materialsQueue.row(pos).putAll(materials);

plugin.getChunkWorthTask().queue(chunk.getChunkSnapshot());
}

/**
Expand Down

0 comments on commit de3293a

Please sign in to comment.