diff --git a/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java b/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java index ac7a9aaca..f252e620d 100644 --- a/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java @@ -351,12 +351,25 @@ public class UserConnectionImpl implements UserConnection { throw cancelSupplier.apply(ex); } - ByteBuf transformed = wrapper.allocateOutputBuffer(); + writeToBuffer(wrapper, buf); + } + + private void writeToBuffer(final PacketWrapperImpl wrapper, final ByteBuf buf) { + // Instead of allocating a possible unnecessarily large buffer to write the wrapper contents to, + // only allocate the remaining bytes and write the rest to the original buf's head directly. + final int remainingBytes = buf.readableBytes(); + final ByteBuf remainingBuf = buf.alloc().buffer(remainingBytes); try { - wrapper.writeToBuffer(transformed); - buf.clear().writeBytes(transformed); + // Copy before modifying the buffer + remainingBuf.writeBytes(buf, remainingBytes); + + // Reset indexes, write wrapper contents, then the unread bytes + buf.readerIndex(0); + buf.writerIndex(0); + wrapper.writeProcessedValues(buf); + buf.writeBytes(remainingBuf); } finally { - transformed.release(); + remainingBuf.release(); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java index 971640a41..20a52d1d7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java @@ -222,6 +222,13 @@ public class PacketWrapperImpl implements PacketWrapper { @Override public void writeToBuffer(ByteBuf buffer) throws InformativeException { + writeProcessedValues(buffer); + if (inputBuffer != null) { + buffer.writeBytes(inputBuffer); + } + } + + public void writeProcessedValues(ByteBuf buffer) throws InformativeException { if (id != -1) { Types.VAR_INT.writePrimitive(buffer, id); } @@ -238,7 +245,6 @@ public class PacketWrapperImpl implements PacketWrapper { throw createInformativeException(e, packetValue.type(), i); } } - writeRemaining(buffer); } private InformativeException createInformativeException(final Exception cause, final Type type, final int index) { @@ -264,12 +270,6 @@ public class PacketWrapperImpl implements PacketWrapper { packetValues.clear(); } - private void writeRemaining(ByteBuf output) { - if (inputBuffer != null) { - output.writeBytes(inputBuffer); - } - } - @Override public void send(Class protocol, boolean skipCurrentPipeline) throws InformativeException { send0(protocol, skipCurrentPipeline, true); @@ -386,7 +386,7 @@ public class PacketWrapperImpl implements PacketWrapper { } } - public ByteBuf allocateOutputBuffer() { + private ByteBuf allocateOutputBuffer() { if (inputBuffer == null) { return user().getChannel().alloc().buffer(); }