From 5334f77d49e68853ab80c3497f0f4678f673010f Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 27 Dec 2023 17:30:47 +0100 Subject: [PATCH] Finish up things --- .../chat/TranslatableRewriter1_16.java | 33 ++----------------- .../Protocol1_19_1To1_19_3.java | 2 +- .../Protocol1_19To1_19_1.java | 2 +- .../Protocol1_20_2To1_20_3.java | 6 ++-- .../rewriter/EntityPacketRewriter1_20_3.java | 6 ++-- 5 files changed, 11 insertions(+), 38 deletions(-) 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 76b603951..f37b59f02 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 @@ -17,15 +17,13 @@ */ package com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.chat; -import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; -import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; -import com.viaversion.viaversion.libs.gson.JsonParseException; import com.viaversion.viaversion.libs.gson.JsonPrimitive; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; +import com.viaversion.viaversion.util.ComponentUtil; public class TranslatableRewriter1_16 extends TranslatableRewriter { @@ -76,33 +74,8 @@ public void processText(JsonElement value) { } // show_text as chat component json, show_entity and show_item serialized as snbt - // Let adventure handle all of that - try { - Component component = ChatRewriter.HOVER_GSON_SERIALIZER.deserializeFromTree(object); - JsonObject convertedObject; - try { - convertedObject = (JsonObject) ChatRewriter.HOVER_GSON_SERIALIZER.serializeToTree(component); - } catch (JsonParseException e) { - JsonObject contents = hoverEvent.getAsJsonObject("contents"); - if (contents.remove("tag") == null) { - throw e; // Just rethrow if this is not an item with a tag provided - } - - // Most likely an invalid nbt tag - try again after its removal - component = ChatRewriter.HOVER_GSON_SERIALIZER.deserializeFromTree(object); - convertedObject = (JsonObject) ChatRewriter.HOVER_GSON_SERIALIZER.serializeToTree(component); - } - - // Remove new format - JsonObject processedHoverEvent = convertedObject.getAsJsonObject("hoverEvent"); - processedHoverEvent.remove("contents"); - object.add("hoverEvent", processedHoverEvent); - } catch (Exception e) { - if (!Via.getConfig().isSuppressConversionWarnings()) { - ViaBackwards.getPlatform().getLogger().severe("Error converting hover event component: " + object); - e.printStackTrace(); - } - } + JsonObject convertedObject = (JsonObject) ComponentUtil.convertJson(object, ComponentUtil.SerializerVersion.V1_16, ComponentUtil.SerializerVersion.V1_15); + object.add("hoverEvent", convertedObject.getAsJsonObject("hoverEvent")); } private String getClosestChatColor(int rgb) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java index a843f0cee..b34ccfa56 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java @@ -345,7 +345,7 @@ public void register() { } final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT); - final JsonElement content = unsignedContent != null ? unsignedContent : ComponentUtil.plainTextToJson(plainContent); + final JsonElement content = unsignedContent != null ? unsignedContent : ComponentUtil.plainToJson(plainContent); translatableRewriter.processText(content); final int filterMaskType = wrapper.read(Type.VAR_INT); if (filterMaskType == 2) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java index c05e6e986..7eb343fbc 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java @@ -156,7 +156,7 @@ public void register() { } if (message == null) { // If no decorated or unsigned message is given, use the plain one - message = ComponentUtil.plainTextToJson(plainMessage); + message = ComponentUtil.plainToJson(plainMessage); } final int filterMaskType = wrapper.read(Type.VAR_INT); 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 cefe9cf7f..205d57436 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 @@ -47,10 +47,10 @@ import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundConfigurationPackets1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPackets1_20_3; -import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util.ComponentConverter; import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; +import com.viaversion.viaversion.util.ComponentUtil; import java.util.BitSet; import java.util.UUID; @@ -368,13 +368,13 @@ private PacketHandler resourcePackHandler() { private void convertComponent(final PacketWrapper wrapper) throws Exception { final Tag tag = wrapper.read(Type.TAG); translatableRewriter.processTag(tag); - wrapper.write(Type.COMPONENT, ComponentConverter.tagComponentToJson(tag)); + wrapper.write(Type.COMPONENT, ComponentUtil.tagToJson(tag)); } private void convertOptionalComponent(final PacketWrapper wrapper) throws Exception { final Tag tag = wrapper.read(Type.OPTIONAL_TAG); translatableRewriter.processTag(tag); - wrapper.write(Type.OPTIONAL_COMPONENT, ComponentConverter.tagComponentToJson(tag)); + wrapper.write(Type.OPTIONAL_COMPONENT, ComponentUtil.tagToJson(tag)); } @Override diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_2to1_20_3/rewriter/EntityPacketRewriter1_20_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_2to1_20_3/rewriter/EntityPacketRewriter1_20_3.java index 14e48b32d..3ac8307e5 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_2to1_20_3/rewriter/EntityPacketRewriter1_20_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_2to1_20_3/rewriter/EntityPacketRewriter1_20_3.java @@ -33,7 +33,7 @@ import com.viaversion.viaversion.api.type.types.version.Types1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_20_2; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3; -import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util.ComponentConverter; +import com.viaversion.viaversion.util.ComponentUtil; public final class EntityPacketRewriter1_20_3 extends EntityRewriter { @@ -99,10 +99,10 @@ protected void registerRewrites() { filter().handler((event, meta) -> { final MetaType type = meta.metaType(); if (type == Types1_20_3.META_TYPES.componentType) { - meta.setTypeAndValue(Types1_20_2.META_TYPES.componentType, ComponentConverter.tagComponentToJson(meta.value())); + meta.setTypeAndValue(Types1_20_2.META_TYPES.componentType, ComponentUtil.tagToJson(meta.value())); return; } else if (type == Types1_20_3.META_TYPES.optionalComponentType) { - meta.setTypeAndValue(Types1_20_2.META_TYPES.optionalComponentType, ComponentConverter.tagComponentToJson(meta.value())); + meta.setTypeAndValue(Types1_20_2.META_TYPES.optionalComponentType, ComponentUtil.tagToJson(meta.value())); return; } else if (type == Types1_20_3.META_TYPES.particleType) { final Particle particle = (Particle) meta.getValue();