mirror of
https://github.com/BG-Software-LLC/WildLoaders.git
synced 2025-01-08 19:28:11 +01:00
Fix infinite recursion when using chunk-spread (#114)
This commit is contained in:
parent
345a88ae74
commit
9bbabb5b15
@ -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<Chunk> calculateChunks(LoaderData loaderData, UUID whoPlaced, Location original) {
|
||||
List<Chunk> chunkList = new LinkedList<>();
|
||||
Set<Chunk> 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<Chunk> chunkList) {
|
||||
if (!plugin.getProviders().hasChunkAccess(whoPlaced, originalChunk))
|
||||
private static void calculateClaimChunksRecursive(Chunk originalChunk, UUID whoPlaced, Set<Chunk> chunkList) {
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user