diff --git a/src/main/java/net/minestom/server/entity/pathfinding/AStarPathfinder.java b/src/main/java/net/minestom/server/entity/pathfinding/AStarPathfinder.java index 0e7cb8370..b4cfacdc6 100644 --- a/src/main/java/net/minestom/server/entity/pathfinding/AStarPathfinder.java +++ b/src/main/java/net/minestom/server/entity/pathfinding/AStarPathfinder.java @@ -21,6 +21,7 @@ public class AStarPathfinder { public static LinkedList getPath(Instance instance, BlockPosition start, BlockPosition end, int maxCheck) { + long time = System.nanoTime(); List open = new ArrayList<>(); List closed = new ArrayList<>(); @@ -37,6 +38,7 @@ public class AStarPathfinder { closed.add(current); if (isTargetNode(end, current)) { + System.out.println("found in: " + (System.nanoTime() - time) + " ns"); return buildPath(current); } diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 835b5e7cb..4716ae94e 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -37,9 +37,9 @@ public class Chunk implements Viewable { private Biome biome; private int chunkX, chunkZ; - // blocks id based on coord - private short[][][] blocksId = new short[CHUNK_SIZE_X][CHUNK_SIZE_Y][CHUNK_SIZE_Z]; - private short[][][] customBlocksId = new short[CHUNK_SIZE_X][CHUNK_SIZE_Y][CHUNK_SIZE_Z]; + // blocks id based on coord, see Chunk#getBlockIndex + private short[] blocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; + private short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; // Used to get all blocks with data (no null) // Key is still chunk coord @@ -98,7 +98,7 @@ public class Chunk implements Viewable { } else { // Block has been deleted, clear cache and return - this.blocksId[x][y][z] = 0; // Set to air + this.blocksId[getBlockIndex(x, y, z)] = 0; // Set to air //this.blocks.remove(index); this.blocksData.remove(index); @@ -147,17 +147,17 @@ public class Chunk implements Viewable { } public short getBlockId(int x, int y, int z) { - short id = blocksId[x][y][z]; + short id = blocksId[getBlockIndex(x, y, z)]; return id; } public short getCustomBlockId(int x, int y, int z) { - short id = customBlocksId[x][y][z]; + short id = customBlocksId[getBlockIndex(x, y, z)]; return id; } public CustomBlock getCustomBlock(int x, int y, int z) { - short id = customBlocksId[x][y][z]; + short id = customBlocksId[getBlockIndex(x, y, z)]; return id != 0 ? BLOCK_MANAGER.getBlock(id) : null; } @@ -167,8 +167,8 @@ public class Chunk implements Viewable { } protected void refreshBlockValue(int x, int y, int z, short blockId, short customId) { - this.blocksId[x][y][z] = blockId; - this.customBlocksId[x][y][z] = customId; + this.blocksId[getBlockIndex(x, y, z)] = blockId; + this.customBlocksId[getBlockIndex(x, y, z)] = customId; } protected void refreshBlockValue(int x, int y, int z, short blockId) { @@ -340,4 +340,9 @@ public class Chunk implements Viewable { public Set getViewers() { return Collections.unmodifiableSet(viewers); } + + private int getBlockIndex(int x, int y, int z) { + int index = (((y * 16) + x) * 16) + z; + return index; + } } \ No newline at end of file diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 3e60527d1..4af028753 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -106,10 +106,12 @@ public class InstanceContainer extends Instance { public void refreshBlockId(int x, int y, int z, short blockId) { Chunk chunk = getChunkAt(x, z); synchronized (chunk) { - byte chunkX = (byte) (x % 16); - byte chunkY = (byte) y; - byte chunkZ = (byte) (z % 16); - int index = SerializerUtils.chunkCoordToIndex(chunkX, chunkY, chunkZ); + int chunkX = x % 16; + int chunkY = y; + int chunkZ = z % 16; + + chunkX = ChunkUtils.refreshChunkXZ(chunkX); + chunkZ = ChunkUtils.refreshChunkXZ(chunkZ); chunk.refreshBlockValue(chunkX, chunkY, chunkZ, blockId);