diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index faf515919..b57277d6c 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -135,10 +135,8 @@ public class DynamicChunk extends Chunk { ByteBuffer chunkPacket = cachedChunkBuffer; ByteBuffer lightPacket = cachedLightBuffer; if (lastChange > cachedPacketTime || (chunkPacket == null || lightPacket == null)) { - final var tempChunk = PacketUtils.createFramedPacket(createChunkPacket()); - chunkPacket = ByteBuffer.allocate(tempChunk.position()).put(tempChunk.flip()); - final var tempLight = PacketUtils.createFramedPacket(createLightPacket()); - lightPacket = ByteBuffer.allocate(tempLight.position()).put(tempLight.flip()); + chunkPacket = PacketUtils.allocateTrimmedPacket(createChunkPacket()); + lightPacket = PacketUtils.allocateTrimmedPacket(createLightPacket()); this.cachedChunkBuffer = chunkPacket; this.cachedLightBuffer = lightPacket; this.cachedPacketTime = lastChange; diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 4ee1b9c71..954e35797 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -180,4 +180,9 @@ public final class PacketUtils { public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) { return createFramedPacket(BUFFER.get().clear(), packet); } + + public static ByteBuffer allocateTrimmedPacket(@NotNull ServerPacket packet) { + final var temp = PacketUtils.createFramedPacket(packet); + return ByteBuffer.allocate(temp.position()).put(temp.flip()); + } }