diff --git a/server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessorBuilder.java b/server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessorBuilder.java index ed8b20e15..26b2868de 100644 --- a/server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessorBuilder.java +++ b/server/src/test/java/com/soulfiremc/test/utils/TestBlockAccessorBuilder.java @@ -20,13 +20,16 @@ import com.soulfiremc.server.data.BlockState; import com.soulfiremc.server.data.BlockType; import com.soulfiremc.server.protocol.bot.block.BlockAccessor; -import com.soulfiremc.server.util.VectorHelper; -import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import org.cloudburstmc.math.vector.Vector3i; + +import java.util.Map; public class TestBlockAccessorBuilder { - private final Long2ObjectMap blocks = new Long2ObjectOpenHashMap<>(); + private final Map blocks = new Object2ObjectOpenHashMap<>(); private final BlockState defaultBlock; + private int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE, minZ = Integer.MAX_VALUE; + private int maxX = Integer.MIN_VALUE, maxY = Integer.MIN_VALUE, maxZ = Integer.MIN_VALUE; public TestBlockAccessorBuilder() { this(BlockState.forDefaultBlockType(BlockType.AIR)); @@ -37,17 +40,41 @@ public TestBlockAccessorBuilder(BlockState defaultBlock) { } public void setBlockAt(int x, int y, int z, BlockType block) { - blocks.put(VectorHelper.asLong(x, y, z), BlockState.forDefaultBlockType(block)); + blocks.put(Vector3i.from(x, y, z), BlockState.forDefaultBlockType(block)); + minX = Math.min(minX, x); + minY = Math.min(minY, y); + minZ = Math.min(minZ, z); + maxX = Math.max(maxX, x); + maxY = Math.max(maxY, y); + maxZ = Math.max(maxZ, z); } public BlockAccessor build() { - return new TestBlockAccessor(blocks, defaultBlock); + int sizeX = maxX - minX + 1; + int sizeY = maxY - minY + 1; + int sizeZ = maxZ - minZ + 1; + BlockState[][][] arrayBlocks = new BlockState[sizeX][sizeY][sizeZ]; + + for (Map.Entry entry : blocks.entrySet()) { + Vector3i pos = entry.getKey(); + arrayBlocks[pos.getX() - minX][pos.getY() - minY][pos.getZ() - minZ] = entry.getValue(); + } + + return new TestBlockAccessor(arrayBlocks, defaultBlock, -minX, -minY, -minZ); } - private record TestBlockAccessor(Long2ObjectMap blocks, BlockState defaultBlock) implements BlockAccessor { + private record TestBlockAccessor(BlockState[][][] blocks, BlockState defaultBlock, int offsetX, int offsetY, int offsetZ) implements BlockAccessor { @Override public BlockState getBlockState(int x, int y, int z) { - return blocks.getOrDefault(VectorHelper.asLong(x, y, z), defaultBlock); + int adjX = x + offsetX; + int adjY = y + offsetY; + int adjZ = z + offsetZ; + if (adjX < 0 || adjX >= blocks.length || adjY < 0 || adjY >= blocks[0].length || adjZ < 0 || adjZ >= blocks[0][0].length) { + return defaultBlock; + } + + BlockState value = blocks[adjX][adjY][adjZ]; + return value != null ? value : defaultBlock; } } }