Skip to content

Commit

Permalink
Fix ArrayIndexOutOfBoundsException for worlds lower than -64 (GeyserM…
Browse files Browse the repository at this point in the history
…C#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
  • Loading branch information
davchoo authored Jan 14, 2022
1 parent 3b943f2 commit a39de7d
Showing 1 changed file with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,32 +309,31 @@ public void translate(GeyserSession session, ClientboundLevelChunkWithLightPacke
}
}

// 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;
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);
}

// 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);
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;
}

BiomeTranslator.toNewBedrockBiome(session, javaBiomes[i + (dimensionOffset - yOffset)]).writeToNetwork(byteBuf);
}

byteBuf.writeByte(0); // Border blocks - Edu edition only
Expand Down

0 comments on commit a39de7d

Please sign in to comment.