From 9fde0379b0707595efcb9290bfac2bd03077e30c Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Thu, 21 Apr 2022 16:07:00 +0300 Subject: [PATCH] Fixed cast errors when using custom world generators --- .../nms/v1_17_R1/NMSChunksImpl.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/v1_17_R1/src/main/java/com/bgsoftware/superiorskyblock/nms/v1_17_R1/NMSChunksImpl.java b/v1_17_R1/src/main/java/com/bgsoftware/superiorskyblock/nms/v1_17_R1/NMSChunksImpl.java index 91367d7d0..a6c609b53 100644 --- a/v1_17_R1/src/main/java/com/bgsoftware/superiorskyblock/nms/v1_17_R1/NMSChunksImpl.java +++ b/v1_17_R1/src/main/java/com/bgsoftware/superiorskyblock/nms/v1_17_R1/NMSChunksImpl.java @@ -63,6 +63,7 @@ import org.bukkit.entity.Player; import org.bukkit.generator.ChunkGenerator; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -86,6 +87,8 @@ public final class NMSChunksImpl implements NMSChunks { LightEngineThreaded.class, Object.class, "theLightEngine"); private static final ReflectField> LIGHT_ENGINE_EXECUTOR = new ReflectField<>( LightEngineThreaded.class, ThreadedMailbox.class, "e"); + private static final ReflectField CHUNK_BIOME_STORAGE = new ReflectField<>( + Chunk.class, BiomeStorage.class, Modifier.PRIVATE, 1); private static CalculatedChunk calculateChunk(ChunkPosition chunkPosition, ChunkSection[] chunkSections) { KeyMap blockCounts = KeyMapImpl.createHashMap(); @@ -164,10 +167,28 @@ private static void removeBlocks(Chunk chunk) { RegionLimitedWorldAccess region = new RegionLimitedWorldAccess(worldServer, Collections.singletonList(chunk), ChunkStatus.f, 0); - chunkGenerator.buildBase(region, chunk); + try { + chunkGenerator.buildBase(region, chunk); + } catch (ClassCastException error) { + ProtoChunk protoChunk = NMSUtils.createProtoChunk(chunk.getPos(), worldServer); + chunkGenerator.buildBase(region, protoChunk); + + // Load chunk sections from proto chunk to the actual chunk + for (int i = 0; i < protoChunk.getSections().length && i < chunk.getSections().length; ++i) { + chunk.getSections()[i] = protoChunk.getSections()[i]; + } + + // Load biomes from proto chunk to the actual chunk + if (protoChunk.getBiomeIndex() != null) + CHUNK_BIOME_STORAGE.set(chunk, protoChunk.getBiomeIndex()); + + // Load tile entities from proto chunk to the actual chunk + protoChunk.y().forEach(((blockPosition, tileEntity) -> chunk.setTileEntity(tileEntity))); + } } } + @Override public void setBiome(List chunkPositions, Biome biome, Collection playersToUpdate) { if (chunkPositions.isEmpty())