From dad71f117949c83178e268c135b1ebefd0cf3cc9 Mon Sep 17 00:00:00 2001 From: Myles Date: Tue, 23 Apr 2019 10:30:21 +0100 Subject: [PATCH] Rewrite inventory names which use .name syntax --- .../InventoryNameRewriter.java | 45 +++++++++++++++++++ .../packets/InventoryPackets.java | 4 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/InventoryNameRewriter.java diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/InventoryNameRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/InventoryNameRewriter.java new file mode 100644 index 000000000..253cae5b1 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/InventoryNameRewriter.java @@ -0,0 +1,45 @@ +package us.myles.ViaVersion.protocols.protocol1_14to1_13_2; + +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.TranslatableComponent; +import net.md_5.bungee.chat.ComponentSerializer; + +public class InventoryNameRewriter { + public static String processTranslate(String value) { + BaseComponent[] components = ComponentSerializer.parse(value); + for (BaseComponent component : components) { + processTranslate(component); + } + if (components.length == 1) { + return ComponentSerializer.toString(components[0]); + } else { + return ComponentSerializer.toString(components); + } + } + + private static void processTranslate(BaseComponent component) { + if (component instanceof TranslatableComponent) { + String oldTranslate = ((TranslatableComponent) component).getTranslate(); + + // Mojang decided to remove .name from inventory titles + if (oldTranslate.startsWith("block.") && oldTranslate.endsWith(".name")) { + ((TranslatableComponent) component).setTranslate(oldTranslate.substring(0, oldTranslate.length() - 5)); + } + if (((TranslatableComponent) component).getWith() != null) { + for (BaseComponent baseComponent : ((TranslatableComponent) component).getWith()) { + processTranslate(baseComponent); + } + } + } + if (component.getHoverEvent() != null) { + for (BaseComponent baseComponent : component.getHoverEvent().getValue()) { + processTranslate(baseComponent); + } + } + if (component.getExtra() != null) { + for (BaseComponent baseComponent : component.getExtra()) { + processTranslate(baseComponent); + } + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java index b36c2b9b4..055ba8961 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java @@ -12,6 +12,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.InventoryNameRewriter; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker; @@ -38,9 +39,10 @@ public class InventoryPackets { public void handle(PacketWrapper wrapper) throws Exception { Short windowsId = wrapper.read(Type.UNSIGNED_BYTE); String type = wrapper.read(Type.STRING); - String title = wrapper.read(Type.STRING); + String title = InventoryNameRewriter.processTranslate(wrapper.read(Type.STRING)); Short slots = wrapper.read(Type.UNSIGNED_BYTE); + if (type.equals("EntityHorse")) { wrapper.setId(0x1F); int entityId = wrapper.read(Type.INT);