diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/ChatRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/ChatRewriter.java index e858d5cf0..c70eb5e38 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/ChatRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/ChatRewriter.java @@ -20,7 +20,7 @@ public class ChatRewriter { private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter1_13(); // Based on https://github.com/SpigotMC/BungeeCord/blob/master/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java - public static JsonElement fromLegacyText(String message, ChatColor defaultColor) { + public static String fromLegacyTextAsString(String message, ChatColor defaultColor) { List components = new ArrayList<>(); StringBuilder builder = new StringBuilder(); TextComponent component = new TextComponent(); @@ -61,23 +61,9 @@ public class ChatRewriter { component = new TextComponent(); component.setColor(format); - // ViaVersion start - Items have style default to italic - component.setBold(false); - component.setItalic(false); - component.setUnderlined(false); - component.setStrikethrough(false); - component.setObfuscated(false); - // ViaVersion end } else { component = new TextComponent(); component.setColor(format); - // ViaVersion start- Items have style default to italic - component.setBold(false); - component.setItalic(false); - component.setUnderlined(false); - component.setStrikethrough(false); - component.setObfuscated(false); - // ViaVersion end } continue; } @@ -87,14 +73,21 @@ public class ChatRewriter { component.setText(builder.toString()); components.add(component); - final String serializedComponents = ComponentSerializer.toString(components.toArray(EMPTY_COMPONENTS)); - return GsonUtil.getJsonParser().parse(serializedComponents); + return ComponentSerializer.toString(components.toArray(EMPTY_COMPONENTS)); + } + + public static JsonElement fromLegacyText(String message, ChatColor defaultColor) { + return GsonUtil.getJsonParser().parse(fromLegacyTextAsString(message, defaultColor)); } public static JsonElement legacyTextToJson(String legacyText) { return fromLegacyText(legacyText, ChatColor.WHITE); } + public static String legacyTextToJsonString(String legacyText) { + return fromLegacyTextAsString(legacyText, ChatColor.WHITE); + } + public static String jsonTextToLegacy(String value) { return TextComponent.toLegacyText(ComponentSerializer.parse(value)); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java index a9bd3eec0..7b2df4706 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java @@ -303,7 +303,7 @@ public class InventoryPackets { if (display.get("Name") instanceof StringTag) { StringTag name = display.get("Name"); display.put(new StringTag(NBT_TAG_NAME + "|Name", name.getValue())); - name.setValue(ChatRewriter.legacyTextToJson(name.getValue()).toString()); + name.setValue(ChatRewriter.legacyTextToJsonString(name.getValue())); } } // ench is now Enchantments and now uses identifiers diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/BannerHandler.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/BannerHandler.java index a8ca133f5..8926958a5 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/BannerHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/BannerHandler.java @@ -55,7 +55,7 @@ public class BannerHandler implements BlockEntityProvider.BlockEntityHandler { Tag name = tag.get("CustomName"); if (name instanceof StringTag) { - ((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()).toString()); + ((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue())); } return blockId; diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/CommandBlockHandler.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/CommandBlockHandler.java index 3218b5721..15d743236 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/CommandBlockHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/CommandBlockHandler.java @@ -14,7 +14,7 @@ public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandl public int transform(UserConnection user, CompoundTag tag) { Tag name = tag.get("CustomName"); if (name instanceof StringTag) { - ((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()).toString()); + ((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue())); } Tag out = tag.get("LastOutput"); if (out instanceof StringTag) { 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 80d8dd157..15226777c 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 @@ -1,6 +1,5 @@ package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets; -import com.github.steveice10.opennbt.conversion.ConverterRegistry; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.DoubleTag; import com.github.steveice10.opennbt.tag.builtin.ListTag; @@ -237,20 +236,19 @@ public class InventoryPackets { if (item == null) return; item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.getIdentifier())); - CompoundTag tag; - if ((tag = item.getTag()) != null) { - // Display Lore now uses JSON - Tag displayTag = tag.get("display"); - if (displayTag instanceof CompoundTag) { - CompoundTag display = (CompoundTag) displayTag; - Tag loreTag = display.get("Lore"); - if (loreTag instanceof ListTag) { - ListTag lore = (ListTag) loreTag; - display.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|Lore", ConverterRegistry.convertToValue(lore))); - for (Tag loreEntry : lore) { - if (loreEntry instanceof StringTag) { - ((StringTag) loreEntry).setValue(ChatRewriter.legacyTextToJson(((StringTag) loreEntry).getValue()).toString()); - } + if (item.getTag() == null) return; + + // Display Lore now uses JSON + Tag displayTag = item.getTag().get("display"); + if (displayTag instanceof CompoundTag) { + CompoundTag display = (CompoundTag) displayTag; + Tag loreTag = display.get("Lore"); + if (loreTag instanceof ListTag) { + ListTag lore = (ListTag) loreTag; + display.put(new ListTag(NBT_TAG_NAME + "|Lore", lore.clone().getValue())); // Save old lore + for (Tag loreEntry : lore) { + if (loreEntry instanceof StringTag) { + ((StringTag) loreEntry).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) loreEntry).getValue())); } } } @@ -261,32 +259,26 @@ public class InventoryPackets { if (item == null) return; item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.getIdentifier())); - CompoundTag tag; - if ((tag = item.getTag()) != null) { - // Display Name now uses JSON - Tag displayTag = tag.get("display"); - if (displayTag instanceof CompoundTag) { - CompoundTag display = (CompoundTag) displayTag; - Tag loreTag = display.get("Lore"); - if (loreTag instanceof ListTag) { - ListTag lore = (ListTag) loreTag; - ListTag via = display.get(NBT_TAG_NAME + "|Lore"); - if (via != null) { - display.put(ConverterRegistry.convertToTag("Lore", ConverterRegistry.convertToValue(via))); - } else { - for (Tag loreEntry : lore) { - if (loreEntry instanceof StringTag) { - ((StringTag) loreEntry).setValue( - ChatRewriter.jsonTextToLegacy( - ((StringTag) loreEntry).getValue() - ) - ); - } + if (item.getTag() == null) return; + + // Display Name now uses JSON + Tag displayTag = item.getTag().get("display"); + if (displayTag instanceof CompoundTag) { + CompoundTag display = (CompoundTag) displayTag; + Tag loreTag = display.get("Lore"); + if (loreTag instanceof ListTag) { + ListTag lore = (ListTag) loreTag; + ListTag savedLore = display.remove(NBT_TAG_NAME + "|Lore"); + if (savedLore != null) { + display.put(new ListTag("Lore", savedLore.getValue())); + } else { + for (Tag loreEntry : lore) { + if (loreEntry instanceof StringTag) { + ((StringTag) loreEntry).setValue(ChatRewriter.jsonTextToLegacy(((StringTag) loreEntry).getValue())); } } - display.remove(NBT_TAG_NAME + "|Lore"); } } } } -} +} \ No newline at end of file