Skip to content

Commit 8578bba

Browse files
perf: reduce object churn by using minestom block states
1 parent d262831 commit 8578bba

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

platforms/minestom/src/main/java/com/dfsek/terra/minestom/chunk/CachedChunk.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,27 @@
1515
public class CachedChunk implements ProtoChunk {
1616
private final int minHeight;
1717
private final int maxHeight;
18-
private final Block[] blocks;
18+
private final MinestomBlockState[] blocks;
1919

2020
public CachedChunk(int minHeight, int maxHeight) {
2121
this.minHeight = minHeight;
2222
this.maxHeight = maxHeight;
23-
this.blocks = new Block[16 * (maxHeight - minHeight + 1) * 16];
24-
Arrays.fill(blocks, Block.AIR);
23+
this.blocks = new MinestomBlockState[16 * (maxHeight - minHeight + 1) * 16];
24+
Arrays.fill(blocks, MinestomBlockState.AIR);
2525
}
2626

2727
public void writeRelative(UnitModifier modifier) {
28-
modifier.setAllRelative((x, y, z) -> blocks[getIndex(x, y + minHeight, z)]);
28+
modifier.setAllRelative((x, y, z) -> blocks[getIndex(x, y + minHeight, z)].block());
2929
}
3030

3131
@Override
3232
public void setBlock(int x, int y, int z, @NotNull BlockState blockState) {
33-
Block block = (Block) blockState.getHandle();
33+
MinestomBlockState minestomBlockState = (MinestomBlockState) blockState;
34+
Block block = minestomBlockState.block();
3435
if(block == null) return;
3536
int index = getIndex(x, y, z);
3637
if(index > blocks.length || index < 0) return;
37-
blocks[index] = block;
38+
blocks[index] = minestomBlockState;
3839
}
3940

4041
private int getIndex(int x, int y, int z) {
@@ -46,7 +47,7 @@ private int getIndex(int x, int y, int z) {
4647
public @NotNull BlockState getBlock(int x, int y, int z) {
4748
int index = getIndex(x, y, z);
4849
if(index > blocks.length || index < 0) return MinestomBlockState.AIR;
49-
return new MinestomBlockState(blocks[index]);
50+
return blocks[index];
5051
}
5152

5253
@Override

0 commit comments

Comments
 (0)