From 1afa441571d1cb5d3a0871bcf4f132e9c6e7e7e5 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 21 Nov 2021 21:02:06 +0100 Subject: [PATCH] Fix possible NPE in 1.13 component rewriting --- .../protocol1_13to1_12_2/ChatRewriter.java | 6 +- .../Protocol1_13To1_12_2.java | 88 +++++-------------- .../data/ComponentRewriter1_13.java | 4 - .../blockentities/CommandBlockHandler.java | 7 +- 4 files changed, 31 insertions(+), 74 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/ChatRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/ChatRewriter.java index e1ee475ce..6f26af880 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/ChatRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/ChatRewriter.java @@ -25,12 +25,9 @@ import com.viaversion.viaversion.libs.kyori.adventure.text.format.TextDecoration import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.legacyimpl.NBTLegacyHoverEventSerializer; import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ComponentRewriter1_13; -import com.viaversion.viaversion.rewriter.ComponentRewriter; public final class ChatRewriter { public static final GsonComponentSerializer HOVER_GSON_SERIALIZER = GsonComponentSerializer.builder().emitLegacyHoverEvent().legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get()).build(); - private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter1_13(); public static String legacyTextToJsonString(String message, boolean itemData) { Component component = Component.text(builder -> { @@ -63,7 +60,8 @@ public final class ChatRewriter { } } + @Deprecated/*(forRemoval = true)*/ public static void processTranslate(JsonElement value) { - COMPONENT_REWRITER.processText(value); + Via.getManager().getProtocolManager().getProtocol(Protocol1_13To1_12_2.class).getComponentRewriter().processText(value); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 10c77d3e5..c82bc48c3 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -46,6 +46,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.providers.PacketBlockConnectionProvider; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.BlockIdData; +import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ComponentRewriter1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.MappingData; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeData; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.StatisticData; @@ -60,6 +61,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockCon import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.util.ChatColorUtil; import com.viaversion.viaversion.util.GsonUtil; @@ -78,6 +80,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol FORMATTING_CODES = Sets.newHashSet('k', 'l', 'm', 'n', 'o', 'r'); private final EntityRewriter entityRewriter = new MetadataRewriter1_13To1_12_2(this); private final ItemRewriter itemRewriter = new InventoryPackets(this); + private final ComponentRewriter componentRewriter = new ComponentRewriter1_13(this); static { SCOREBOARD_TEAM_NAME_REWRITE.put('0', 'g'); @@ -170,7 +173,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT))); + handler(wrapper -> componentRewriter.processText(wrapper.passthrough(Type.COMPONENT))); } }); @@ -267,28 +270,10 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT))); - } - }); + + componentRewriter.registerBossBar(ClientboundPackets1_12_1.BOSSBAR); + componentRewriter.registerComponentPacket(ClientboundPackets1_12_1.CHAT_MESSAGE); + registerClientbound(ClientboundPackets1_12_1.TAB_COMPLETE, new PacketRemapper() { @Override public void registerMap() { @@ -335,7 +320,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT))); // Title + handler(wrapper -> componentRewriter.processText(wrapper.passthrough(Type.COMPONENT))); // Title } }); @@ -378,12 +363,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT))); - } - }); + componentRewriter.registerComponentPacket(ClientboundPackets1_12_1.DISCONNECT); registerClientbound(ClientboundPackets1_12_1.EFFECT, new PacketRemapper() { @Override @@ -439,22 +419,9 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol wrapper.write(Type.STRING, "viaversion:legacy/" + wrapper.read(Type.VAR_INT))); } }); - registerClientbound(ClientboundPackets1_12_1.COMBAT_EVENT, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Event - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - if (wrapper.get(Type.VAR_INT, 0) == 2) { // Entity dead - wrapper.passthrough(Type.VAR_INT); // Player id - wrapper.passthrough(Type.INT); // Entity id - ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)); - } - } - }); - } - }); + + componentRewriter.registerCombatEvent(ClientboundPackets1_12_1.COMBAT_EVENT); + registerClientbound(ClientboundPackets1_12_1.MAP_DATA, new PacketRemapper() { @Override public void registerMap() { @@ -682,21 +649,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol= 0 && action <= 2) { - ChatRewriter.processTranslate(wrapper.passthrough(Type.COMPONENT)); - } - } - }); - } - }); + componentRewriter.registerTitle(ClientboundPackets1_12_1.TITLE); + // New 0x4C - Stop Sound new SoundRewriter(this).registerSound(ClientboundPackets1_12_1.SOUND); @@ -707,8 +661,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol