From a39de7d7d245b4bef3b845452dfcc00e9999232d Mon Sep 17 00:00:00 2001 From: David Choo Date: Fri, 14 Jan 2022 14:57:59 -0500 Subject: [PATCH] Fix ArrayIndexOutOfBoundsException for worlds lower than -64 (#2759) * Fix ArrayIndexOutOfBoundsException for worlds lower than -64 `chunkSize` is Java section count while `sectionCount` is the Bedrock section count * Send biomes for air sections while also staying within limits .-. * Move protocol version check to variable --- .../JavaLevelChunkWithLightTranslator.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaLevelChunkWithLightTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaLevelChunkWithLightTranslator.java index 75e0390c3..97b826473 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaLevelChunkWithLightTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaLevelChunkWithLightTranslator.java @@ -309,32 +309,31 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator= Bedrock_v475.V475_CODEC.getProtocolVersion(); + int biomeCount = isNewVersion ? 25 : 32; int dimensionOffset = (overworld ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT) >> 4; - for (int i = 0; i < chunkSize; i++) { + for (int i = 0; i < biomeCount; i++) { int biomeYOffset = dimensionOffset + i; if (biomeYOffset < yOffset) { - // Ignore this biome section since it goes above or below the height of the Java world + // Ignore this biome section since it goes below the height of the Java world byteBuf.writeBytes(ChunkUtils.EMPTY_BIOME_DATA); continue; } - BiomeTranslator.toNewBedrockBiome(session, javaBiomes[i + (dimensionOffset - yOffset)]).writeToNetwork(byteBuf); - } + if (biomeYOffset >= (chunkSize + yOffset)) { + // This biome section goes above the height of the Java world + if (isNewVersion) { + // A header that says to carry on the biome data from the previous chunk + // This notably fixes biomes in the End + byteBuf.writeByte((127 << 1) | 1); + } else { + byteBuf.writeBytes(ChunkUtils.EMPTY_BIOME_DATA); + } + continue; + } - // As of 1.17.10, Bedrock hardcodes to always read 32 biome sections - // As of 1.18, this hardcode was lowered to 25 - if (session.getUpstream().getProtocolVersion() >= Bedrock_v475.V475_CODEC.getProtocolVersion()) { - int remainingEmptyBiomes = 25 - chunkSize; - for (int i = 0; i < remainingEmptyBiomes; i++) { - // A header that says to carry on the biome data from the previous chunk - // This notably fixes biomes in the End - byteBuf.writeByte((127 << 1) | 1); - } - } else { - int remainingEmptyBiomes = 32 - chunkSize; - for (int i = 0; i < remainingEmptyBiomes; i++) { - byteBuf.writeBytes(ChunkUtils.EMPTY_BIOME_DATA); - } + BiomeTranslator.toNewBedrockBiome(session, javaBiomes[i + (dimensionOffset - yOffset)]).writeToNetwork(byteBuf); } byteBuf.writeByte(0); // Border blocks - Edu edition only