From fbf647913512113c04a31de324348a8388e40da7 Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 3 Aug 2021 19:44:10 +0200 Subject: [PATCH] Fix item meta writing Signed-off-by: TheMode --- src/main/java/net/minestom/server/item/ItemMeta.java | 3 +-- .../server/network/player/NettyPlayerConnection.java | 4 +--- .../java/net/minestom/server/network/socket/Worker.java | 3 +-- src/main/java/net/minestom/server/utils/Utils.java | 5 ++--- .../net/minestom/server/utils/binary/BinaryWriter.java | 7 +++++-- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/minestom/server/item/ItemMeta.java b/src/main/java/net/minestom/server/item/ItemMeta.java index a31518f88..10c9381b0 100644 --- a/src/main/java/net/minestom/server/item/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/ItemMeta.java @@ -153,7 +153,6 @@ public class ItemMeta implements TagReadable, Writeable { w.writeNBT("", nbt); this.cachedBuffer = w.getBuffer(); } - writer.write(cachedBuffer); - this.cachedBuffer.position(0); + writer.write(cachedBuffer.flip()); } } diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index 8b0270c04..0d32488bc 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -211,9 +211,7 @@ public class NettyPlayerConnection extends PlayerConnection { serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component -> GlobalTranslator.render(component, Objects.requireNonNullElseGet(getPlayer().getLocale(), MinestomAdventure::getDefaultLocale))); } - synchronized (tickBufferLock) { - PacketUtils.writeFramedPacket(tickBuffer, serverPacket); - } + attemptWrite(PacketUtils.createFramedPacket(serverPacket)); return; } else if (message instanceof ByteBuffer) { attemptWrite((ByteBuffer) message); diff --git a/src/main/java/net/minestom/server/network/socket/Worker.java b/src/main/java/net/minestom/server/network/socket/Worker.java index 1c2a3dc05..a2bac27ab 100644 --- a/src/main/java/net/minestom/server/network/socket/Worker.java +++ b/src/main/java/net/minestom/server/network/socket/Worker.java @@ -7,7 +7,6 @@ import net.minestom.server.network.player.NettyPlayerConnection; import java.io.IOException; import java.nio.ByteBuffer; -import java.nio.ByteOrder; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; @@ -140,7 +139,7 @@ public class Worker { } private static ByteBuffer allocate(int size) { - return ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()); + return ByteBuffer.allocateDirect(size); } } } diff --git a/src/main/java/net/minestom/server/utils/Utils.java b/src/main/java/net/minestom/server/utils/Utils.java index 7fb846b24..3a2ba5ac2 100644 --- a/src/main/java/net/minestom/server/utils/Utils.java +++ b/src/main/java/net/minestom/server/utils/Utils.java @@ -2,7 +2,6 @@ package net.minestom.server.utils; import it.unimi.dsi.fastutil.shorts.Short2ShortLinkedOpenHashMap; import net.minestom.server.instance.palette.Palette; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -89,14 +88,14 @@ public final class Utils { return result; } - public static void writeVarLong(BinaryWriter writer, long value) { + public static void writeVarLong(ByteBuffer buffer, long value) { do { byte temp = (byte) (value & 0b01111111); value >>>= 7; if (value != 0) { temp |= 0b10000000; } - writer.writeByte(temp); + buffer.put(temp); } while (value != 0); } diff --git a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java index ce800e491..4ef846483 100644 --- a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java +++ b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java @@ -150,7 +150,7 @@ public class BinaryWriter extends OutputStream { * @param l the long to write */ public void writeVarLong(long l) { - Utils.writeVarLong(this, l); + Utils.writeVarLong(buffer, l); } /** @@ -292,7 +292,10 @@ public class BinaryWriter extends OutputStream { } public void write(@NotNull BinaryWriter writer) { - write(writer.getBuffer()); + var buffer = writer.buffer; + final int pos = buffer.position(); + write(buffer.position(0)); + buffer.position(pos); } /**