3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 00:50:13 +01:00

Smaller workaround for lore italics in 1.13->1.14

Dieser Commit ist enthalten in:
KennyTV 2020-10-28 15:19:00 +01:00
Ursprung 510ff4e6a1
Commit 69c8cf8ffb
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
2 geänderte Dateien mit 16 neuen und 15 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

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