From 90925b4967e81ccb7d0bbd65ad7fef1ddee8e149 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Fri, 24 Apr 2020 11:33:29 +0200 Subject: [PATCH] Block/chunk batching should be much faster --- src/main/java/net/minestom/server/instance/Chunk.java | 9 +++++++++ .../net/minestom/server/instance/batch/BlockBatch.java | 8 ++++++-- .../net/minestom/server/instance/batch/ChunkBatch.java | 6 ++++-- .../packet/server/play/PlayerPositionAndLookPacket.java | 2 ++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 1969722bf..71b87f5e4 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -10,6 +10,7 @@ import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.BlockManager; import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.instance.block.UpdateConsumer; +import net.minestom.server.network.PacketWriterUtils; import net.minestom.server.network.packet.server.play.ChunkDataPacket; import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.PacketUtils; @@ -307,6 +308,14 @@ public class Chunk implements Viewable { setFullDataPacket(buffer); } + // Write the pakcet in the writer thread pools + public void refreshDataPacket(Runnable runnable) { + PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), buf -> { + setFullDataPacket(buf); + runnable.run(); + }); + } + @Override public String toString() { return "Chunk[" + chunkX + ":" + chunkZ + "]"; diff --git a/src/main/java/net/minestom/server/instance/batch/BlockBatch.java b/src/main/java/net/minestom/server/instance/batch/BlockBatch.java index e1806face..2d4ec22fa 100644 --- a/src/main/java/net/minestom/server/instance/batch/BlockBatch.java +++ b/src/main/java/net/minestom/server/instance/batch/BlockBatch.java @@ -66,13 +66,17 @@ public class BlockBatch implements InstanceBatch { for (BlockData data : dataList) { data.apply(chunk); } - chunk.refreshDataPacket(); - instance.sendChunkUpdate(chunk); + + chunk.refreshDataPacket(() -> { + instance.sendChunkUpdate(chunk); + }); + if (isLast) { // data.clear(); if (callback != null) callback.run(); } + } }); } diff --git a/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java b/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java index 73e251dd7..6a7ae8a03 100644 --- a/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java +++ b/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java @@ -67,8 +67,10 @@ public class ChunkBatch implements InstanceBatch { data.apply(chunk); } - chunk.refreshDataPacket(); - instance.sendChunkUpdate(chunk); + chunk.refreshDataPacket(() -> { + instance.sendChunkUpdate(chunk); + }); + if (callback != null) callback.accept(chunk); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerPositionAndLookPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerPositionAndLookPacket.java index e8ed712c3..09a12f40f 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerPositionAndLookPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerPositionAndLookPacket.java @@ -17,8 +17,10 @@ public class PlayerPositionAndLookPacket implements ServerPacket { writer.writeDouble(position.getX()); writer.writeDouble(position.getY()); writer.writeDouble(position.getZ()); + writer.writeFloat(position.getYaw()); writer.writeFloat(position.getPitch()); + writer.writeByte(flags); writer.writeVarInt(teleportId); }