mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
refactor: minestom chunk storage to improve memory efficiency
Replaced 3D array with a 1D array for chunk block storage and adjusted related logic to use calculated indices. Updated block type comparison to use state IDs instead of block IDs for consistency and correctness.
This commit is contained in:
@@ -41,7 +41,7 @@ public class MinestomBlockType implements BlockType {
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof MinestomBlockType other) {
|
||||
return block.id() == other.block.id();
|
||||
return block.stateId() == other.block.stateId();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -10,40 +10,40 @@ import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.instance.generator.UnitModifier;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class CachedChunk implements ProtoChunk {
|
||||
private final int minHeight;
|
||||
private final int maxHeight;
|
||||
private final Block[][][] blocks;
|
||||
private final Block[] blocks;
|
||||
|
||||
public CachedChunk(int minHeight, int maxHeight) {
|
||||
this.minHeight = minHeight;
|
||||
this.maxHeight = maxHeight;
|
||||
this.blocks = new Block[16][maxHeight - minHeight + 1][16];
|
||||
|
||||
for(int x = 0; x < 16; x++) {
|
||||
for(int z = 0; z < 16; z++) {
|
||||
for(int y = 0; y < maxHeight - minHeight + 1; y++) {
|
||||
blocks[x][y][z] = Block.AIR;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.blocks = new Block[16 * (maxHeight - minHeight + 1) * 16];
|
||||
Arrays.fill(blocks, Block.AIR);
|
||||
}
|
||||
|
||||
public void writeRelative(UnitModifier modifier) {
|
||||
modifier.setAllRelative((x, y, z) -> blocks[x][y][z]);
|
||||
modifier.setAllRelative((x, y, z) -> blocks[getIndex(x, y + minHeight, z)]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, @NotNull BlockState blockState) {
|
||||
Block block = (Block) blockState.getHandle();
|
||||
if(block == null) return;
|
||||
blocks[x][y - minHeight][z] = block;
|
||||
blocks[getIndex(x, y, z)] = block;
|
||||
}
|
||||
|
||||
private int getIndex(int x, int y, int z) {
|
||||
int y_normalized = y - minHeight;
|
||||
return x + (z * 16) + (y_normalized * 256);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull BlockState getBlock(int x, int y, int z) {
|
||||
return new MinestomBlockState(blocks[x][y - minHeight][z]);
|
||||
return new MinestomBlockState(blocks[getIndex(x, y, z)]);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user