Smaller workaround for lore italics in 1.13->1.14

This commit is contained in:
KennyTV 2020-10-28 15:19:00 +01:00
parent 510ff4e6a1
commit 69c8cf8ffb
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
2 changed files with 16 additions and 15 deletions

View File

@ -3,27 +3,26 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2;
import com.google.gson.JsonElement;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ComponentRewriter1_13;
import us.myles.ViaVersion.util.GsonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChatRewriter {
private static final BaseComponent[] EMPTY_COMPONENTS = new BaseComponent[0];
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 String fromLegacyTextAsString(String message, ChatColor defaultColor) {
List<BaseComponent> components = new ArrayList<>();
StringBuilder builder = new StringBuilder();
public static String fromLegacyTextAsString(String message, ChatColor defaultColor, boolean lore) {
TextComponent headComponent = new TextComponent();
TextComponent component = new TextComponent();
StringBuilder builder = new StringBuilder();
if (lore) {
// Workaround for all italic lore
headComponent.setItalic(false);
component.setItalic(true);
}
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
@ -44,7 +43,7 @@ public class ChatRewriter {
component = new TextComponent(old);
old.setText(builder.toString());
builder = new StringBuilder();
components.add(old);
headComponent.addExtra(old);
}
if (ChatColor.BOLD.equals(format)) {
component.setBold(true);
@ -71,13 +70,13 @@ public class ChatRewriter {
}
component.setText(builder.toString());
components.add(component);
headComponent.addExtra(component);
return ComponentSerializer.toString(components.toArray(EMPTY_COMPONENTS));
return ComponentSerializer.toString(headComponent);
}
public static JsonElement fromLegacyText(String message, ChatColor defaultColor) {
return GsonUtil.getJsonParser().parse(fromLegacyTextAsString(message, defaultColor));
return GsonUtil.getJsonParser().parse(fromLegacyTextAsString(message, defaultColor, false));
}
public static JsonElement legacyTextToJson(String legacyText) {
@ -85,7 +84,7 @@ public class ChatRewriter {
}
public static String legacyTextToJsonString(String legacyText) {
return fromLegacyTextAsString(legacyText, ChatColor.WHITE);
return fromLegacyTextAsString(legacyText, ChatColor.WHITE, false);
}
public static String jsonTextToLegacy(String value) {

View File

@ -8,6 +8,7 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.common.collect.Sets;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.md_5.bungee.api.ChatColor;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.minecraft.item.Item;
@ -248,7 +249,8 @@ public class InventoryPackets {
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()));
String jsonText = ChatRewriter.fromLegacyTextAsString(((StringTag) loreEntry).getValue(), ChatColor.WHITE, true);
((StringTag) loreEntry).setValue(jsonText);
}
}
}