Improve 1.13->1.14 lore conversion, remove noisy workaround for a non-issue in legacy to json conversion

If people want to have white, unformatted text in their lore, they should do exactly that; add a white color, not reset to default
Fixes #2158
This commit is contained in:
KennyTV 2020-10-28 14:36:00 +01:00
parent a6043c14cd
commit 510ff4e6a1
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
5 changed files with 43 additions and 58 deletions

View File

@ -20,7 +20,7 @@ public class ChatRewriter {
private static final ComponentRewriter COMPONENT_REWRITER = new ComponentRewriter1_13(); 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 // 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<BaseComponent> components = new ArrayList<>(); List<BaseComponent> components = new ArrayList<>();
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
TextComponent component = new TextComponent(); TextComponent component = new TextComponent();
@ -61,23 +61,9 @@ public class ChatRewriter {
component = new TextComponent(); component = new TextComponent();
component.setColor(format); 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 { } else {
component = new TextComponent(); component = new TextComponent();
component.setColor(format); 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; continue;
} }
@ -87,14 +73,21 @@ public class ChatRewriter {
component.setText(builder.toString()); component.setText(builder.toString());
components.add(component); components.add(component);
final String serializedComponents = ComponentSerializer.toString(components.toArray(EMPTY_COMPONENTS)); return ComponentSerializer.toString(components.toArray(EMPTY_COMPONENTS));
return GsonUtil.getJsonParser().parse(serializedComponents); }
public static JsonElement fromLegacyText(String message, ChatColor defaultColor) {
return GsonUtil.getJsonParser().parse(fromLegacyTextAsString(message, defaultColor));
} }
public static JsonElement legacyTextToJson(String legacyText) { public static JsonElement legacyTextToJson(String legacyText) {
return fromLegacyText(legacyText, ChatColor.WHITE); return fromLegacyText(legacyText, ChatColor.WHITE);
} }
public static String legacyTextToJsonString(String legacyText) {
return fromLegacyTextAsString(legacyText, ChatColor.WHITE);
}
public static String jsonTextToLegacy(String value) { public static String jsonTextToLegacy(String value) {
return TextComponent.toLegacyText(ComponentSerializer.parse(value)); return TextComponent.toLegacyText(ComponentSerializer.parse(value));
} }

View File

@ -303,7 +303,7 @@ public class InventoryPackets {
if (display.get("Name") instanceof StringTag) { if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name"); StringTag name = display.get("Name");
display.put(new StringTag(NBT_TAG_NAME + "|Name", name.getValue())); 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 // ench is now Enchantments and now uses identifiers

View File

@ -55,7 +55,7 @@ public class BannerHandler implements BlockEntityProvider.BlockEntityHandler {
Tag name = tag.get("CustomName"); Tag name = tag.get("CustomName");
if (name instanceof StringTag) { if (name instanceof StringTag) {
((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()).toString()); ((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue()));
} }
return blockId; return blockId;

View File

@ -14,7 +14,7 @@ public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandl
public int transform(UserConnection user, CompoundTag tag) { public int transform(UserConnection user, CompoundTag tag) {
Tag name = tag.get("CustomName"); Tag name = tag.get("CustomName");
if (name instanceof StringTag) { 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"); Tag out = tag.get("LastOutput");
if (out instanceof StringTag) { if (out instanceof StringTag) {

View File

@ -1,6 +1,5 @@
package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets; 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.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag; import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.ListTag;
@ -237,20 +236,19 @@ public class InventoryPackets {
if (item == null) return; if (item == null) return;
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.getIdentifier())); item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.getIdentifier()));
CompoundTag tag; if (item.getTag() == null) return;
if ((tag = item.getTag()) != null) {
// Display Lore now uses JSON // Display Lore now uses JSON
Tag displayTag = tag.get("display"); Tag displayTag = item.getTag().get("display");
if (displayTag instanceof CompoundTag) { if (displayTag instanceof CompoundTag) {
CompoundTag display = (CompoundTag) displayTag; CompoundTag display = (CompoundTag) displayTag;
Tag loreTag = display.get("Lore"); Tag loreTag = display.get("Lore");
if (loreTag instanceof ListTag) { if (loreTag instanceof ListTag) {
ListTag lore = (ListTag) loreTag; ListTag lore = (ListTag) loreTag;
display.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|Lore", ConverterRegistry.convertToValue(lore))); display.put(new ListTag(NBT_TAG_NAME + "|Lore", lore.clone().getValue())); // Save old lore
for (Tag loreEntry : lore) { for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) { if (loreEntry instanceof StringTag) {
((StringTag) loreEntry).setValue(ChatRewriter.legacyTextToJson(((StringTag) loreEntry).getValue()).toString()); ((StringTag) loreEntry).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) loreEntry).getValue()));
}
} }
} }
} }
@ -261,32 +259,26 @@ public class InventoryPackets {
if (item == null) return; if (item == null) return;
item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.getIdentifier())); item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.getIdentifier()));
CompoundTag tag; if (item.getTag() == null) return;
if ((tag = item.getTag()) != null) {
// Display Name now uses JSON // Display Name now uses JSON
Tag displayTag = tag.get("display"); Tag displayTag = item.getTag().get("display");
if (displayTag instanceof CompoundTag) { if (displayTag instanceof CompoundTag) {
CompoundTag display = (CompoundTag) displayTag; CompoundTag display = (CompoundTag) displayTag;
Tag loreTag = display.get("Lore"); Tag loreTag = display.get("Lore");
if (loreTag instanceof ListTag) { if (loreTag instanceof ListTag) {
ListTag lore = (ListTag) loreTag; ListTag lore = (ListTag) loreTag;
ListTag via = display.get(NBT_TAG_NAME + "|Lore"); ListTag savedLore = display.remove(NBT_TAG_NAME + "|Lore");
if (via != null) { if (savedLore != null) {
display.put(ConverterRegistry.convertToTag("Lore", ConverterRegistry.convertToValue(via))); display.put(new ListTag("Lore", savedLore.getValue()));
} else { } else {
for (Tag loreEntry : lore) { for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) { if (loreEntry instanceof StringTag) {
((StringTag) loreEntry).setValue( ((StringTag) loreEntry).setValue(ChatRewriter.jsonTextToLegacy(((StringTag) loreEntry).getValue()));
ChatRewriter.jsonTextToLegacy(
((StringTag) loreEntry).getValue()
)
);
}
} }
} }
display.remove(NBT_TAG_NAME + "|Lore");
} }
} }
} }
} }
} }