From 5f59a57a789e41491550200c8eea697416586509 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Fri, 14 May 2021 11:04:45 +0200 Subject: [PATCH] Fix legacy hover conversion Fixes ViaVersion/ViaVersion#2471 --- build.gradle.kts | 2 +- .../chat/TagSerializer.java | 1 + .../chat/TranslatableRewriter1_16.java | 46 ++++++------------- gradle/libs.versions.toml | 2 +- 4 files changed, 16 insertions(+), 35 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c7a49689..f681cdf5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { allprojects { group = "com.viaversion" - version = "4.0.0-21w19a" + version = "4.0.0-21w19a-SNAPSHOT" description = "Allow older clients to join newer server versions." } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TagSerializer.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TagSerializer.java index 5ded0fb3..e9bc4dd2 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TagSerializer.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TagSerializer.java @@ -32,6 +32,7 @@ import java.util.regex.Pattern; /** * Utility class to serialize a JsonObject with Minecraft's CompoundTag serialization */ +@Deprecated public class TagSerializer { private static final Pattern PLAIN_TEXT = Pattern.compile("[A-Za-z0-9._+-]+"); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java index 69105462..0d12ac10 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java @@ -22,6 +22,8 @@ import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.libs.gson.JsonPrimitive; +import com.viaversion.viaversion.libs.kyori.adventure.text.Component; +import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; public class TranslatableRewriter1_16 extends TranslatableRewriter { @@ -48,6 +50,7 @@ public class TranslatableRewriter1_16 extends TranslatableRewriter { super(protocol); } + @Override public void processText(JsonElement value) { super.processText(value); if (!value.isJsonObject()) return; @@ -63,41 +66,18 @@ public class TranslatableRewriter1_16 extends TranslatableRewriter { object.addProperty("color", closestChatColor); } } - } - @Override - protected void handleHoverEvent(JsonObject hoverEvent) { - // Don't call super, convert and process contents here - JsonElement contentsElement = hoverEvent.remove("contents"); - if (contentsElement == null) return; + JsonObject hoverEvent = object.getAsJsonObject("hoverEvent"); + if (hoverEvent != null) { + // show_text as chat component + // show_entity and show_item serialized as nbt + // Let adventure handle all of that + Component component = ChatRewriter.HOVER_GSON_SERIALIZER.deserializeFromTree(object); + JsonObject processedHoverEvent = ((JsonObject) ChatRewriter.HOVER_GSON_SERIALIZER.serializeToTree(component)).getAsJsonObject("hoverEvent"); - // show_text as chat component - // show_entity and show_item serialized as nbt - String action = hoverEvent.getAsJsonPrimitive("action").getAsString(); - switch (action) { - case "show_text": - processText(contentsElement); - hoverEvent.add("value", contentsElement); - break; - case "show_item": - JsonObject item = contentsElement.getAsJsonObject(); - JsonElement count = item.remove("count"); - item.addProperty("Count", count != null ? count.getAsByte() : 1); - - hoverEvent.addProperty("value", TagSerializer.toString(item)); - break; - case "show_entity": - JsonObject entity = contentsElement.getAsJsonObject(); - JsonObject name = entity.getAsJsonObject("name"); - if (name != null) { - processText(name); - entity.addProperty("name", name.toString()); - } - - JsonObject hoverObject = new JsonObject(); - hoverObject.addProperty("text", TagSerializer.toString(entity)); - hoverEvent.add("value", hoverObject); - break; + // Remove new format + processedHoverEvent.remove("contents"); + object.add("hoverEvent", processedHoverEvent); } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index af43ed05..0a249a76 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,7 @@ metadata.format.version = "1.0" [versions] # ViaVersion -viaver = "4.0.0-21w19a" +viaver = "4.0.0-21w19a-SNAPSHOT" # Common provided netty = "4.0.20.Final"