Move and rename ChunkUtils.toSectionRelativeCoordinate, added tests

This commit is contained in:
Németh Noel 2021-12-22 22:13:38 +01:00 committed by TheMode
parent 2d1102e0e8
commit 83d751ecf2
3 changed files with 27 additions and 13 deletions

View File

@ -66,7 +66,7 @@ public class DynamicChunk extends Chunk {
} }
Section section = getSectionAt(y); Section section = getSectionAt(y);
section.blockPalette() section.blockPalette()
.set(toChunkRelativeCoordinate(x), y, toChunkRelativeCoordinate(z), block.stateId()); .set(ChunkUtils.toSectionRelativeCoordinate(x), ChunkUtils.toSectionRelativeCoordinate(y), ChunkUtils.toSectionRelativeCoordinate(z), block.stateId());
final int index = ChunkUtils.getBlockIndex(x, y, z); final int index = ChunkUtils.getBlockIndex(x, y, z);
// Handler // Handler
@ -89,9 +89,9 @@ public class DynamicChunk extends Chunk {
this.chunkCache.invalidate(); this.chunkCache.invalidate();
Section section = getSectionAt(y); Section section = getSectionAt(y);
section.biomePalette().set( section.biomePalette().set(
toChunkRelativeCoordinate(x) / 4, ChunkUtils.toSectionRelativeCoordinate(x) / 4,
toChunkRelativeCoordinate(y) / 4, ChunkUtils.toSectionRelativeCoordinate(y) / 4,
toChunkRelativeCoordinate(z) / 4, biome.id()); ChunkUtils.toSectionRelativeCoordinate(z) / 4, biome.id());
} }
@Override @Override
@ -130,7 +130,7 @@ public class DynamicChunk extends Chunk {
// Retrieve the block from state id // Retrieve the block from state id
final Section section = getSectionAt(y); final Section section = getSectionAt(y);
final int blockStateId = section.blockPalette() final int blockStateId = section.blockPalette()
.get(toChunkRelativeCoordinate(x), y, toChunkRelativeCoordinate(z)); .get(ChunkUtils.toSectionRelativeCoordinate(x), y, ChunkUtils.toSectionRelativeCoordinate(z));
return Objects.requireNonNullElse(Block.fromStateId((short) blockStateId), Block.AIR); return Objects.requireNonNullElse(Block.fromStateId((short) blockStateId), Block.AIR);
} }
@ -138,7 +138,7 @@ public class DynamicChunk extends Chunk {
public @NotNull Biome getBiome(int x, int y, int z) { public @NotNull Biome getBiome(int x, int y, int z) {
final Section section = getSectionAt(y); final Section section = getSectionAt(y);
final int id = section.biomePalette() final int id = section.biomePalette()
.get(toChunkRelativeCoordinate(x) / 4, y / 4, toChunkRelativeCoordinate(z) / 4); .get(ChunkUtils.toSectionRelativeCoordinate(x) / 4, y / 4, ChunkUtils.toSectionRelativeCoordinate(z) / 4);
return MinecraftServer.getBiomeManager().getById(id); return MinecraftServer.getBiomeManager().getById(id);
} }
@ -237,11 +237,4 @@ public class DynamicChunk extends Chunk {
skyLights, blockLights); skyLights, blockLights);
} }
private static int toChunkRelativeCoordinate(int xz) {
xz %= 16;
if (xz < 0) {
xz += Chunk.CHUNK_SECTION_SIZE;
}
return xz;
}
} }

View File

@ -233,4 +233,18 @@ public final class ChunkUtils {
public static int blockIndexToChunkPositionZ(int index) { public static int blockIndexToChunkPositionZ(int index) {
return (index >> 28) & 0xF; // 28-32 bits return (index >> 28) & 0xF; // 28-32 bits
} }
/**
* Converts a global coordinate value to a section coordinate
*
* @param xyz global coordinate
* @return section coordinate
*/
public static int toSectionRelativeCoordinate(int xyz) {
xyz %= 16;
if (xyz < 0) {
xyz += Chunk.CHUNK_SECTION_SIZE;
}
return xyz;
}
} }

View File

@ -76,4 +76,11 @@ public class CoordinateTest {
assertEquals(0, temp.y()); assertEquals(0, temp.y());
assertEquals(0, temp.z()); assertEquals(0, temp.z());
} }
@Test
public void toSectionRelativeCoordinate() {
assertEquals(12, ChunkUtils.toSectionRelativeCoordinate(-20));
assertEquals(5, ChunkUtils.toSectionRelativeCoordinate(5));
assertEquals(4, ChunkUtils.toSectionRelativeCoordinate(20));
}
} }