Fix infinite recursion when using chunk-spread (#114)

This commit is contained in:
OmerBenGera 2024-11-22 18:28:46 +02:00
parent 345a88ae74
commit 9bbabb5b15

View File

@ -5,12 +5,17 @@ import com.bgsoftware.wildloaders.api.loaders.LoaderData;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
public class ChunkLoaderChunks { public class ChunkLoaderChunks {
private static final int MAX_DEPTH = 200;
private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin(); private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin();
private ChunkLoaderChunks() { private ChunkLoaderChunks() {
@ -18,7 +23,7 @@ public class ChunkLoaderChunks {
} }
public static List<Chunk> calculateChunks(LoaderData loaderData, UUID whoPlaced, Location original) { public static List<Chunk> calculateChunks(LoaderData loaderData, UUID whoPlaced, Location original) {
List<Chunk> chunkList = new LinkedList<>(); Set<Chunk> chunkList = new LinkedHashSet<>();
if (loaderData.isChunksSpread()) { if (loaderData.isChunksSpread()) {
calculateClaimChunksRecursive(original.getChunk(), whoPlaced, chunkList); calculateClaimChunksRecursive(original.getChunk(), whoPlaced, chunkList);
@ -32,21 +37,32 @@ public class ChunkLoaderChunks {
chunkList.add(original.getWorld().getChunkAt(chunkX + x, chunkZ + z)); 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<Chunk> chunkList) { private static void calculateClaimChunksRecursive(Chunk originalChunk, UUID whoPlaced, Set<Chunk> chunkList) {
if (!plugin.getProviders().hasChunkAccess(whoPlaced, originalChunk)) calculateClaimChunksRecursive(originalChunk, whoPlaced, chunkList, 0);
}
private static void calculateClaimChunksRecursive(Chunk originalChunk, UUID whoPlaced, Set<Chunk> 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; return;
chunkList.add(originalChunk); 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 x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) { for (int z = -1; z <= 1; z++) {
if (x != 0 || z != 0) // We don't want to add the originalChunk again. 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);
} }
} }