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
This commit is contained in:
David Choo 2022-01-14 14:57:59 -05:00 committed by GitHub
parent 3b943f214c
commit a39de7d7d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -309,32 +309,31 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
} }
} }
// At this point we're dealing with Bedrock chunk sections // As of 1.17.10, Bedrock hardcodes to always read 32 biome sections
// As of 1.18, this hardcode was lowered to 25
boolean isNewVersion = session.getUpstream().getProtocolVersion() >= Bedrock_v475.V475_CODEC.getProtocolVersion();
int biomeCount = isNewVersion ? 25 : 32;
int dimensionOffset = (overworld ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT) >> 4; 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; int biomeYOffset = dimensionOffset + i;
if (biomeYOffset < yOffset) { 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); byteBuf.writeBytes(ChunkUtils.EMPTY_BIOME_DATA);
continue; 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 BiomeTranslator.toNewBedrockBiome(session, javaBiomes[i + (dimensionOffset - yOffset)]).writeToNetwork(byteBuf);
// 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);
}
} }
byteBuf.writeByte(0); // Border blocks - Edu edition only byteBuf.writeByte(0); // Border blocks - Edu edition only