From de3293a3d3cedee321cb163d8efb9747edaddd4c Mon Sep 17 00:00:00 2001 From: novucs Date: Tue, 10 Jan 2017 16:57:41 +0000 Subject: [PATCH] Force immediate recalculation on chunk unload If scheduled a tick after, this forces the chunk to be loaded back in. Potentially causing a horrific loop of death. --- .../net/novucs/ftop/manager/WorthManager.java | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/net/novucs/ftop/manager/WorthManager.java b/core/src/main/java/net/novucs/ftop/manager/WorthManager.java index 42a328a..2840382 100644 --- a/core/src/main/java/net/novucs/ftop/manager/WorthManager.java +++ b/core/src/main/java/net/novucs/ftop/manager/WorthManager.java @@ -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 spawners = new EnumMap<>(EntityType.class); - Map 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 spawners = new EnumMap<>(EntityType.class); + Map 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()); } /**