From 9bbabb5b1504ccd4587f248a607ecbdc02e25280 Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Fri, 22 Nov 2024 18:28:46 +0200 Subject: [PATCH] Fix infinite recursion when using chunk-spread (#114) --- .../wildloaders/utils/ChunkLoaderChunks.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/bgsoftware/wildloaders/utils/ChunkLoaderChunks.java b/src/main/java/com/bgsoftware/wildloaders/utils/ChunkLoaderChunks.java index 1702e8b..e420896 100644 --- a/src/main/java/com/bgsoftware/wildloaders/utils/ChunkLoaderChunks.java +++ b/src/main/java/com/bgsoftware/wildloaders/utils/ChunkLoaderChunks.java @@ -5,12 +5,17 @@ import com.bgsoftware.wildloaders.api.loaders.LoaderData; import org.bukkit.Chunk; import org.bukkit.Location; +import java.util.Collections; +import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; import java.util.UUID; public class ChunkLoaderChunks { + private static final int MAX_DEPTH = 200; + private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin(); private ChunkLoaderChunks() { @@ -18,7 +23,7 @@ public class ChunkLoaderChunks { } public static List calculateChunks(LoaderData loaderData, UUID whoPlaced, Location original) { - List chunkList = new LinkedList<>(); + Set chunkList = new LinkedHashSet<>(); if (loaderData.isChunksSpread()) { calculateClaimChunksRecursive(original.getChunk(), whoPlaced, chunkList); @@ -32,21 +37,32 @@ public class ChunkLoaderChunks { chunkList.add(original.getWorld().getChunkAt(chunkX + x, chunkZ + z)); } - return chunkList; + return chunkList.isEmpty() ? Collections.emptyList() : new LinkedList<>(chunkList); } - private static void calculateClaimChunksRecursive(Chunk originalChunk, UUID whoPlaced, List chunkList) { - if (!plugin.getProviders().hasChunkAccess(whoPlaced, originalChunk)) + private static void calculateClaimChunksRecursive(Chunk originalChunk, UUID whoPlaced, Set chunkList) { + calculateClaimChunksRecursive(originalChunk, whoPlaced, chunkList, 0); + } + + private static void calculateClaimChunksRecursive(Chunk originalChunk, UUID whoPlaced, Set chunkList, int depth) { + if (depth > MAX_DEPTH) { + WildLoadersPlugin.log("Chunk list: " + chunkList); + throw new IllegalStateException("Called calculateClaimChunksRecursive with depth " + depth); + } + + if (chunkList.contains(originalChunk) || !plugin.getProviders().hasChunkAccess(whoPlaced, originalChunk)) return; chunkList.add(originalChunk); - int chunkX = originalChunk.getX(), chunkZ = originalChunk.getZ(); + int chunkX = originalChunk.getX(); + int chunkZ = originalChunk.getZ(); for (int x = -1; x <= 1; x++) { for (int z = -1; z <= 1; z++) { if (x != 0 || z != 0) // We don't want to add the originalChunk again. - calculateClaimChunksRecursive(originalChunk.getWorld().getChunkAt(chunkX + x, chunkZ + z), whoPlaced, chunkList); + calculateClaimChunksRecursive(originalChunk.getWorld().getChunkAt(chunkX + x, chunkZ + z), + whoPlaced, chunkList, depth + 1); } }