From 65158455cc841e38aa010e9f4c3f397f65767c9f Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 15 Oct 2023 18:12:39 +1000 Subject: [PATCH] Fix early sending of resource pack Fixes #3476 --- .../Protocol1_20_2To1_20_3.java | 8 +++++ .../Protocol1_20To1_20_2.java | 30 ++++++++++++++----- .../storage/ConfigurationPacketStorage.java | 20 ++++++++++++- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_2to1_20_3/Protocol1_20_2To1_20_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_2to1_20_3/Protocol1_20_2To1_20_3.java index 1907a827..015499f9 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_2to1_20_3/Protocol1_20_2To1_20_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_2to1_20_3/Protocol1_20_2To1_20_3.java @@ -119,6 +119,14 @@ public final class Protocol1_20_2To1_20_3 extends BackwardsProtocol { + wrapper.passthrough(Type.UUID); // Id + + final int action = wrapper.passthrough(Type.VAR_INT); + if (action == 0 || action == 3) { + convertComponent(wrapper); + } + }); registerClientbound(ClientboundPackets1_20_2.PLAYER_CHAT, wrapper -> { wrapper.passthrough(Type.UUID); // Sender wrapper.passthrough(Type.VAR_INT); // Index diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/Protocol1_20To1_20_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/Protocol1_20To1_20_2.java index 9c8e6d9d..ca1b8c84 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/Protocol1_20To1_20_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/Protocol1_20To1_20_2.java @@ -111,9 +111,26 @@ public final class Protocol1_20To1_20_2 extends BackwardsProtocol { + wrapper.setPacketType(ClientboundPackets1_19_4.DISCONNECT); + }); + registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.KEEP_ALIVE.getId(), -1, wrapper -> { + wrapper.setPacketType(ClientboundPackets1_19_4.KEEP_ALIVE); + }); + registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.RESOURCE_PACK.getId(), -1, wrapper -> { + // Send after join. We have to pretend the client accepted, else the server won't continue... + wrapper.user().get(ConfigurationPacketStorage.class).setResourcePack(wrapper); + wrapper.cancel(); + + final PacketWrapper acceptedResponse = wrapper.create(ServerboundConfigurationPackets1_20_2.RESOURCE_PACK); + acceptedResponse.write(Type.VAR_INT, 3); + acceptedResponse.sendToServer(Protocol1_20To1_20_2.class); + + final PacketWrapper downloadedResponse = wrapper.create(ServerboundConfigurationPackets1_20_2.RESOURCE_PACK); + downloadedResponse.write(Type.VAR_INT, 0); + downloadedResponse.sendToServer(Protocol1_20To1_20_2.class); + }); registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.REGISTRY_DATA.getId(), -1, wrapper -> { wrapper.cancel(); @@ -123,18 +140,17 @@ public final class Protocol1_20To1_20_2 extends BackwardsProtocol { - wrapper.cancel(); - final String[] enabledFeatures = wrapper.read(Type.STRING_ARRAY); wrapper.user().get(ConfigurationPacketStorage.class).setEnabledFeatures(enabledFeatures); + wrapper.cancel(); }); registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.UPDATE_TAGS.getId(), -1, wrapper -> { - wrapper.cancel(); wrapper.user().get(ConfigurationPacketStorage.class).addRawPacket(wrapper, ClientboundPackets1_19_4.TAGS); + wrapper.cancel(); }); registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.CUSTOM_PAYLOAD.getId(), -1, wrapper -> { - wrapper.cancel(); wrapper.user().get(ConfigurationPacketStorage.class).addRawPacket(wrapper, ClientboundPackets1_19_4.PLUGIN_MESSAGE); + wrapper.cancel(); }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/storage/ConfigurationPacketStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/storage/ConfigurationPacketStorage.java index ffb6342a..b9ed1791 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/storage/ConfigurationPacketStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/storage/ConfigurationPacketStorage.java @@ -24,6 +24,7 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.protocol.packet.PacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.util.ArrayList; @@ -35,6 +36,11 @@ public final class ConfigurationPacketStorage implements StorableObject { private CompoundTag registry; private String[] enabledFeatures; private boolean finished; + private QueuedPacket resourcePack; + + public void setResourcePack(final PacketWrapper wrapper) throws Exception { + resourcePack = toQueuedPacket(wrapper, ClientboundPackets1_19_4.RESOURCE_PACK); + } public CompoundTag registry() { Preconditions.checkNotNull(registry); @@ -55,15 +61,27 @@ public final class ConfigurationPacketStorage implements StorableObject { } public void addRawPacket(final PacketWrapper wrapper, final PacketType type) throws Exception { + rawPackets.add(toQueuedPacket(wrapper, type)); + } + + private QueuedPacket toQueuedPacket(final PacketWrapper wrapper, final PacketType type) throws Exception { + Preconditions.checkArgument(!wrapper.isCancelled(), "Wrapper should be cancelled AFTER calling toQueuedPacket"); + // It's easier to just copy it to a byte array buffer than to manually read the data final ByteBuf buf = Unpooled.buffer(); //noinspection deprecation wrapper.setId(-1); // Don't write the packet id to the buffer wrapper.writeToBuffer(buf); - rawPackets.add(new QueuedPacket(buf, type)); + return new QueuedPacket(buf, type); } public void sendQueuedPackets(final UserConnection connection) throws Exception { + // Send resource pack at the end + if (resourcePack != null) { + rawPackets.add(resourcePack); + resourcePack = null; + } + for (final QueuedPacket queuedPacket : rawPackets) { try { final PacketWrapper packet = PacketWrapper.create(queuedPacket.packetType(), queuedPacket.buf(), connection);