mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-20 15:51:11 +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:
+1
-1
@@ -41,7 +41,7 @@ public class MinestomBlockType implements BlockType {
|
|||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(obj instanceof MinestomBlockType other) {
|
if(obj instanceof MinestomBlockType other) {
|
||||||
return block.id() == other.block.id();
|
return block.stateId() == other.block.stateId();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,40 +10,40 @@ import net.minestom.server.instance.block.Block;
|
|||||||
import net.minestom.server.instance.generator.UnitModifier;
|
import net.minestom.server.instance.generator.UnitModifier;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
|
||||||
public class CachedChunk implements ProtoChunk {
|
public class CachedChunk implements ProtoChunk {
|
||||||
private final int minHeight;
|
private final int minHeight;
|
||||||
private final int maxHeight;
|
private final int maxHeight;
|
||||||
private final Block[][][] blocks;
|
private final Block[] blocks;
|
||||||
|
|
||||||
public CachedChunk(int minHeight, int maxHeight) {
|
public CachedChunk(int minHeight, int maxHeight) {
|
||||||
this.minHeight = minHeight;
|
this.minHeight = minHeight;
|
||||||
this.maxHeight = maxHeight;
|
this.maxHeight = maxHeight;
|
||||||
this.blocks = new Block[16][maxHeight - minHeight + 1][16];
|
this.blocks = new Block[16 * (maxHeight - minHeight + 1) * 16];
|
||||||
|
Arrays.fill(blocks, Block.AIR);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeRelative(UnitModifier modifier) {
|
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
|
@Override
|
||||||
public void setBlock(int x, int y, int z, @NotNull BlockState blockState) {
|
public void setBlock(int x, int y, int z, @NotNull BlockState blockState) {
|
||||||
Block block = (Block) blockState.getHandle();
|
Block block = (Block) blockState.getHandle();
|
||||||
if(block == null) return;
|
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
|
@Override
|
||||||
public @NotNull BlockState getBlock(int x, int y, int z) {
|
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
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user