From 7cd5514b90e47ae23dce33743a52cca87372e1d6 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Thu, 29 Aug 2024 21:46:43 +0200 Subject: [PATCH] Add PacketWrapper#sendFutureRaw, cleanup login disconnect handling. (#4129) Removes the special handling in Protocol1_8To1_9 and always send the correct/expected data by the client in ServerboundBaseProtocol1_7 itself. Also prevent sending the packet through the protocol pipeline since packet/format changes should also be handled inside the base protocol. --- .../api/protocol/packet/PacketWrapper.java | 8 +++++++ .../protocol/packet/PacketWrapperImpl.java | 21 ++++++++++++++++++- .../base/v1_7/ServerboundBaseProtocol1_7.java | 12 +++++++++-- .../protocols/v1_8to1_9/Protocol1_8To1_9.java | 9 +------- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketWrapper.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketWrapper.java index 8cfdf07fb..d02e7bd58 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketWrapper.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketWrapper.java @@ -236,6 +236,14 @@ public interface PacketWrapper { */ void sendRaw() throws InformativeException; + /** + * Sends this packet to the associated user, submitted to netty's event loop. + * Unlike {@link #sendFuture(Class)}, this method does not handle the pipeline with packet id and data changes. + * + * @throws InformativeException if it fails to write + */ + ChannelFuture sendFutureRaw() throws InformativeException; + /** * Sends this packet to the associated user, submitted to netty's event loop. * Unlike {@link #send(Class)}, this method does not handle the pipeline with packet id and data changes. 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 bacf3e035..ca562eb41 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 @@ -340,7 +340,7 @@ public class PacketWrapperImpl implements PacketWrapper { } return user().sendRawPacketFuture(output); } - return user().getChannel().newFailedFuture(new RuntimeException("Tried to send cancelled packet")); + return cancelledFuture(); } @Override @@ -348,6 +348,21 @@ public class PacketWrapperImpl implements PacketWrapper { sendRaw(true); } + @Override + public ChannelFuture sendFutureRaw() throws InformativeException { + if (isCancelled()) { + return cancelledFuture(); + } + + ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); + try { + writeToBuffer(output); + return user().sendRawPacketFuture(output.retain()); + } finally { + output.release(); + } + } + @Override public void scheduleSendRaw() throws InformativeException { sendRaw(false); @@ -371,6 +386,10 @@ public class PacketWrapperImpl implements PacketWrapper { } } + private ChannelFuture cancelledFuture() { + return user().getChannel().newFailedFuture(new RuntimeException("Tried to send cancelled packet")); + } + @Override public PacketWrapperImpl create(int packetId) { return new PacketWrapperImpl(packetId, null, user()); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/base/v1_7/ServerboundBaseProtocol1_7.java b/common/src/main/java/com/viaversion/viaversion/protocols/base/v1_7/ServerboundBaseProtocol1_7.java index 2c7bd2802..a355ef171 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/base/v1_7/ServerboundBaseProtocol1_7.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/base/v1_7/ServerboundBaseProtocol1_7.java @@ -17,6 +17,7 @@ */ package com.viaversion.viaversion.protocols.base.v1_7; +import com.google.gson.JsonObject; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.ProtocolInfo; import com.viaversion.viaversion.api.connection.UserConnection; @@ -31,6 +32,7 @@ import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; import com.viaversion.viaversion.protocols.base.packet.BaseClientboundPacket; import com.viaversion.viaversion.protocols.base.packet.BasePacketTypesProvider; import com.viaversion.viaversion.protocols.base.packet.BaseServerboundPacket; +import com.viaversion.viaversion.protocols.v1_8to1_9.Protocol1_8To1_9; import com.viaversion.viaversion.util.ChatColorUtil; import com.viaversion.viaversion.util.ComponentUtil; import io.netty.channel.ChannelFuture; @@ -62,10 +64,16 @@ public class ServerboundBaseProtocol1_7 extends AbstractProtocol user.getChannel().close()); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/Protocol1_8To1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/Protocol1_8To1_9.java index c658d7826..1796fd814 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/Protocol1_8To1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/Protocol1_8To1_9.java @@ -83,14 +83,7 @@ public class Protocol1_8To1_9 extends AbstractProtocol { - if (wrapper.isReadable(Types.COMPONENT, 0)) { - // Already written as component in the base protocol - return; - } - - STRING_TO_JSON.write(wrapper, wrapper.read(Types.STRING)); - }); + registerClientbound(State.LOGIN, ClientboundLoginPackets.LOGIN_DISCONNECT, wrapper -> STRING_TO_JSON.write(wrapper, wrapper.read(Types.STRING))); // Other Handlers SpawnPacketRewriter1_9.register(this);