From fb334c6c1415d89c5928fe06bf965c24b55c1412 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 15 Jul 2022 17:54:01 +0200 Subject: [PATCH] 1.19.1-pre5 --- build.gradle.kts | 2 +- .../Protocol1_18_2To1_19_1.java | 62 +++++++++++++------ .../storage/ReceivedMessagesStorage.java | 57 +++++++++++++++++ gradle/libs.versions.toml | 2 +- 4 files changed, 102 insertions(+), 21 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19_1/storage/ReceivedMessagesStorage.java diff --git a/build.gradle.kts b/build.gradle.kts index 9ab10d1d..2e68f9b9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { allprojects { group = "com.viaversion" - version = "4.4.0-1.19.1-pre4-SNAPSHOT" + version = "4.4.0-1.19.1-pre5-SNAPSHOT" description = "Allow older clients to join newer server versions." } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19_1/Protocol1_18_2To1_19_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19_1/Protocol1_18_2To1_19_1.java index acf340a4..e64e42e2 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19_1/Protocol1_18_2To1_19_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19_1/Protocol1_18_2To1_19_1.java @@ -27,7 +27,9 @@ import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19_1.data.CommandR import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19_1.packets.BlockItemPackets1_19; import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19_1.packets.EntityPackets1_19; import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19_1.storage.DimensionRegistryStorage; +import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19_1.storage.ReceivedMessagesStorage; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -53,8 +55,8 @@ import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18; import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.ClientboundPackets1_19_1; +import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.ServerboundPackets1_19_1; import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.Protocol1_19To1_18_2; -import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ServerboundPackets1_19; import com.viaversion.viaversion.rewriter.CommandRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; @@ -62,7 +64,7 @@ import com.viaversion.viaversion.rewriter.TagRewriter; import java.time.Instant; import java.util.UUID; -public final class Protocol1_18_2To1_19_1 extends BackwardsProtocol { +public final class Protocol1_18_2To1_19_1 extends BackwardsProtocol { public static final BackwardsMappings MAPPINGS = new BackwardsMappings(); private static final UUID ZERO_UUID = new UUID(0, 0); @@ -72,7 +74,7 @@ public final class Protocol1_18_2To1_19_1 extends BackwardsProtocol 64) { + messagesStorage.resetUnacknowledgedCount(); + + // Send chat acknowledgement + final PacketWrapper chatAckPacket = wrapper.create(ServerboundPackets1_19_1.CHAT_ACK); + chatAckPacket.write(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY, messagesStorage.lastSignatures()); + chatAckPacket.write(Type.OPTIONAL_PLAYER_MESSAGE_SIGNATURE, null); + chatAckPacket.sendToServer(Protocol1_18_2To1_19_1.class); + } + } // Send the unsigned message if present, otherwise the signed message - final JsonElement message = wrapper.read(Type.COMPONENT); + JsonElement message = wrapper.read(Type.COMPONENT); // Plain message + JsonElement decoratedMessage = wrapper.read(Type.OPTIONAL_COMPONENT); + if (decoratedMessage != null) { + message = decoratedMessage; + } wrapper.read(Type.LONG); // Timestamp wrapper.read(Type.LONG); // Salt - final int lastSeenPlayers = wrapper.read(Type.VAR_INT); - for (int i = 0; i < lastSeenPlayers; i++) { - wrapper.read(Type.UUID); // Profile uuid - wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); // Last signature - } + wrapper.read(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY); // Last seen final JsonElement unsignedMessage = wrapper.read(Type.OPTIONAL_COMPONENT); - final JsonElement chatMessage = unsignedMessage != null ? unsignedMessage : message; - wrapper.write(Type.COMPONENT, chatMessage); + if (unsignedMessage != null) { + message = unsignedMessage; + } + wrapper.write(Type.COMPONENT, message); final int chatTypeId = wrapper.read(Type.VAR_INT); wrapper.write(Type.BYTE, (byte) 1); - wrapper.write(Type.UUID, sender); + wrapper.write(Type.UUID, signature.uuid()); final JsonElement senderName = wrapper.read(Type.COMPONENT); final JsonElement targetName = wrapper.read(Type.OPTIONAL_COMPONENT); - final JsonElement decoratedMessage = decorateChatMessage(wrapper, chatTypeId, senderName, targetName, chatMessage); + decoratedMessage = decorateChatMessage(wrapper, chatTypeId, senderName, targetName, message); if (decoratedMessage == null) { wrapper.cancel(); } else { wrapper.set(Type.COMPONENT, 0, decoratedMessage); } }); - } }); @@ -260,14 +277,20 @@ public final class Protocol1_18_2To1_19_1 extends BackwardsProtocol { final String message = wrapper.get(Type.STRING, 0); if (!message.isEmpty() && message.charAt(0) == '/') { - wrapper.setPacketType(ServerboundPackets1_19.CHAT_COMMAND); + wrapper.setPacketType(ServerboundPackets1_19_1.CHAT_COMMAND); wrapper.set(Type.STRING, 0, message.substring(1)); wrapper.write(Type.VAR_INT, 0); // No signatures - wrapper.write(Type.BOOLEAN, false); // No signed preview } else { wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, EMPTY_BYTES); // Signature - wrapper.write(Type.BOOLEAN, false); // No signed preview } + wrapper.write(Type.BOOLEAN, false); // No signed preview + + // Write last seen messages - even though we don't actually need to send them since everything will be unsigned, + // we'll try and play nice with sending them anyway. + final ReceivedMessagesStorage messagesStorage = wrapper.user().get(ReceivedMessagesStorage.class); + messagesStorage.resetUnacknowledgedCount(); + wrapper.write(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY, messagesStorage.lastSignatures()); + wrapper.write(Type.OPTIONAL_PLAYER_MESSAGE_SIGNATURE, null); // No last unacknowledged }); } }); @@ -317,6 +340,7 @@ public final class Protocol1_18_2To1_19_1 extends BackwardsProtocol. + */ +package com.viaversion.viabackwards.protocol.protocol1_18_2to1_19_1.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; +import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature; + +import java.util.Arrays; + +public final class ReceivedMessagesStorage implements StorableObject { + private final PlayerMessageSignature[] signatures = new PlayerMessageSignature[5]; + private int size; + private int unacknowledged; + + public void add(final PlayerMessageSignature signature) { + PlayerMessageSignature toPush = signature; + for (int i = 0; i < this.size; ++i) { + final PlayerMessageSignature entry = this.signatures[i]; + this.signatures[i] = toPush; + toPush = entry; + if (entry.uuid().equals(signature.uuid())) { + return; + } + } + + if (this.size < this.signatures.length) { + this.signatures[this.size++] = toPush; + } + } + + public PlayerMessageSignature[] lastSignatures() { + return Arrays.copyOf(this.signatures, size); + } + + public int tickUnacknowledged() { + return unacknowledged++; + } + + public void resetUnacknowledgedCount() { + unacknowledged = 0; + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 433b8141..b4c261f9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,7 @@ metadata.format.version = "1.1" [versions] # ViaVersion -viaver = "4.4.0-1.19.1-pre4-SNAPSHOT" +viaver = "4.4.0-1.19.1-pre5-SNAPSHOT" # Common provided netty = "4.0.20.Final"