From cf8d11d530328fd0b28417e9b23fe361b93da351 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Mon, 23 Nov 2020 17:28:07 +0100 Subject: [PATCH] Let's go on an --- .../ViaVersion/api/data/UserConnection.java | 5 +- .../commands/ViaCommandHandler.java | 28 ++++-- .../commands/defaultsubs/DumpSubCmd.java | 9 +- .../protocols/base/BaseProtocol1_7.java | 4 +- .../protocol1_13to1_12_2/ChatRewriter.java | 95 ++++--------------- .../Protocol1_13To1_12_2.java | 85 ++++++++--------- .../packets/InventoryPackets.java | 5 +- .../packets/InventoryPackets.java | 5 +- .../myles/ViaVersion/update/UpdateUtil.java | 3 +- .../myles/ViaVersion/util/ChatColorUtil.java | 56 +++++++++++ .../us/myles/ViaVersion/SpongePlugin.java | 25 +++-- .../sponge/commands/SpongeCommandSender.java | 12 +-- .../us/myles/ViaVersion/VelocityPlugin.java | 24 ++--- .../command/VelocityCommandHandler.java | 14 ++- .../command/VelocityCommandSender.java | 10 +- 15 files changed, 181 insertions(+), 199 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/util/ChatColorUtil.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/data/UserConnection.java b/common/src/main/java/us/myles/ViaVersion/api/data/UserConnection.java index d37b39f69..ffcb3f143 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/data/UserConnection.java +++ b/common/src/main/java/us/myles/ViaVersion/api/data/UserConnection.java @@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; -import net.md_5.bungee.api.ChatColor; import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; @@ -14,6 +13,7 @@ import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.protocols.base.ProtocolInfo; +import us.myles.ViaVersion.util.ChatColorUtil; import us.myles.ViaVersion.util.PipelineUtil; import java.util.Map; @@ -53,7 +53,6 @@ public class UserConnection { } /** - * @param channel * @see #UserConnection(Channel, boolean) */ public UserConnection(@Nullable Channel channel) { @@ -236,7 +235,7 @@ public class UserConnection { pendingDisconnect = true; Via.getPlatform().runSync(() -> { - if (!Via.getPlatform().disconnect(this, ChatColor.translateAlternateColorCodes('&', reason))) { + if (!Via.getPlatform().disconnect(this, ChatColorUtil.translateAlternateColorCodes(reason))) { channel.close(); // =) } }); diff --git a/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java b/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java index fa35133fa..e9cf926d6 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java @@ -1,14 +1,30 @@ package us.myles.ViaVersion.commands; import com.google.common.base.Preconditions; -import net.md_5.bungee.api.ChatColor; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.command.ViaSubCommand; import us.myles.ViaVersion.api.command.ViaVersionCommand; -import us.myles.ViaVersion.commands.defaultsubs.*; +import us.myles.ViaVersion.commands.defaultsubs.AutoTeamSubCmd; +import us.myles.ViaVersion.commands.defaultsubs.DebugSubCmd; +import us.myles.ViaVersion.commands.defaultsubs.DisplayLeaksSubCmd; +import us.myles.ViaVersion.commands.defaultsubs.DontBugMeSubCmd; +import us.myles.ViaVersion.commands.defaultsubs.DumpSubCmd; +import us.myles.ViaVersion.commands.defaultsubs.HelpSubCmd; +import us.myles.ViaVersion.commands.defaultsubs.ListSubCmd; +import us.myles.ViaVersion.commands.defaultsubs.PPSSubCmd; +import us.myles.ViaVersion.commands.defaultsubs.ReloadSubCmd; +import us.myles.ViaVersion.util.ChatColorUtil; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; public abstract class ViaCommandHandler implements ViaVersionCommand { private final Map commandMap; @@ -148,11 +164,7 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { * @return The output String */ public static String color(String string) { - try { - string = ChatColor.translateAlternateColorCodes('&', string); //Dont replace all & with $ like we did before. - } catch (Exception ignored) { - } - return string; + return ChatColorUtil.translateAlternateColorCodes(string); } /** diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java index efe31fed6..ca2030ccb 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.commands.defaultsubs; import com.google.common.io.CharStreams; import com.google.gson.JsonObject; -import net.md_5.bungee.api.ChatColor; import us.myles.ViaVersion.ViaManager; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.command.ViaCommandSender; @@ -60,7 +59,7 @@ public class DumpSubCmd extends ViaSubCommand { try { con = (HttpURLConnection) new URL("https://dump.viaversion.com/documents").openConnection(); } catch (IOException e) { - sender.sendMessage(ChatColor.RED + "Failed to dump, please check the console for more information"); + sender.sendMessage("§4Failed to dump, please check the console for more information"); Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to ViaVersion Dump", e); return; } @@ -75,7 +74,7 @@ public class DumpSubCmd extends ViaSubCommand { out.close(); if (con.getResponseCode() == 429) { - sender.sendMessage(ChatColor.RED + "You can only paste ones every minute to protect our systems."); + sender.sendMessage("§4You can only paste ones every minute to protect our systems."); return; } @@ -87,9 +86,9 @@ public class DumpSubCmd extends ViaSubCommand { if (!output.has("key")) throw new InvalidObjectException("Key is not given in Hastebin output"); - sender.sendMessage(ChatColor.GREEN + "We've made a dump with useful information, report your issue and provide this url: " + getUrl(output.get("key").getAsString())); + sender.sendMessage("§2We've made a dump with useful information, report your issue and provide this url: " + getUrl(output.get("key").getAsString())); } catch (Exception e) { - sender.sendMessage(ChatColor.RED + "Failed to dump, please check the console for more information"); + sender.sendMessage("§4Failed to dump, please check the console for more information"); Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to Hastebin", e); try { if (con.getResponseCode() < 200 || con.getResponseCode() > 400) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java index ee466c346..0f9efa479 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java @@ -5,7 +5,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import io.netty.channel.ChannelFuture; -import net.md_5.bungee.api.ChatColor; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Via; @@ -18,6 +17,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_9to1_8.Protocol1_9To1_8; +import us.myles.ViaVersion.util.ChatColorUtil; import us.myles.ViaVersion.util.GsonUtil; import java.util.List; @@ -168,7 +168,7 @@ public class BaseProtocol1_7 extends SimpleProtocol { if (!wrapper.user().shouldApplyBlockProtocol()) return; PacketWrapper disconnectPacket = new PacketWrapper(0x00, null, wrapper.user()); // Disconnect Packet - Protocol1_9To1_8.FIX_JSON.write(disconnectPacket, ChatColor.translateAlternateColorCodes('&', Via.getConfig().getBlockedDisconnectMsg())); + Protocol1_9To1_8.FIX_JSON.write(disconnectPacket, ChatColorUtil.translateAlternateColorCodes(Via.getConfig().getBlockedDisconnectMsg())); wrapper.cancel(); // cancel current // Send and close 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 a44f4044a..bfe5b8da3 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 @@ -1,96 +1,43 @@ 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.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; +import com.google.gson.JsonParser; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import us.myles.ViaVersion.api.Via; 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; 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 String fromLegacyTextAsString(String message, ChatColor defaultColor, boolean itemData) { - TextComponent headComponent = new TextComponent(); - TextComponent component = new TextComponent(); - StringBuilder builder = new StringBuilder(); - if (itemData) { - // Workaround for all italic lore - headComponent.setItalic(false); - //TODO set first child to italics if it doesn't have a color - } - - for (int i = 0; i < message.length(); i++) { - char c = message.charAt(i); - if (c == ChatColor.COLOR_CHAR) { - if (++i >= message.length()) { - break; - } - c = message.charAt(i); - if (c >= 'A' && c <= 'Z') { - c += 32; - } - ChatColor format = ChatColor.getByChar(c); - if (format == null) { - continue; - } - if (builder.length() > 0) { - TextComponent old = component; - component = new TextComponent(old); - old.setText(builder.toString()); - builder = new StringBuilder(); - headComponent.addExtra(old); - } - if (ChatColor.BOLD.equals(format)) { - component.setBold(true); - } else if (ChatColor.ITALIC.equals(format)) { - component.setItalic(true); - } else if (ChatColor.UNDERLINE.equals(format)) { - component.setUnderlined(true); - } else if (ChatColor.STRIKETHROUGH.equals(format)) { - component.setStrikethrough(true); - } else if (ChatColor.MAGIC.equals(format)) { - component.setObfuscated(true); - } else if (ChatColor.RESET.equals(format)) { - format = defaultColor; - - component = new TextComponent(); - component.setColor(format); - } else { - component = new TextComponent(); - component.setColor(format); - } - continue; + public static String legacyTextToJsonString(String message, boolean itemData) { + Component component = Component.text(builder -> { + if (itemData) { + builder.decoration(TextDecoration.ITALIC, false); } - builder.append(c); - } - - component.setText(builder.toString()); - headComponent.addExtra(component); - - return ComponentSerializer.toString(headComponent); - } - - public static JsonElement fromLegacyText(String message, ChatColor defaultColor) { - return GsonUtil.getJsonParser().parse(fromLegacyTextAsString(message, defaultColor, false)); - } - - public static JsonElement legacyTextToJson(String legacyText) { - return fromLegacyText(legacyText, ChatColor.WHITE); + builder.append(LegacyComponentSerializer.legacySection().deserialize(message)); + }); + return GsonComponentSerializer.gson().serialize(component); } public static String legacyTextToJsonString(String legacyText) { - return fromLegacyTextAsString(legacyText, ChatColor.WHITE, false); + return legacyTextToJsonString(legacyText, false); } - public static String jsonTextToLegacy(String value) { + public static JsonElement legacyTextToJson(String legacyText) { + return JsonParser.parseString(legacyTextToJsonString(legacyText, false)); + } + + public static String jsonToLegacyText(String value) { try { - return TextComponent.toLegacyText(ComponentSerializer.parse(value)); + Component component = GsonComponentSerializer.gson().deserialize(value); + return LegacyComponentSerializer.legacySection().serialize(component); } catch (Exception e) { Via.getPlatform().getLogger().warning("Error converting json text to legacy: " + value); + e.printStackTrace(); return ""; } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 21cc17fff..605d24cc8 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -4,7 +4,6 @@ import com.google.common.collect.Sets; import com.google.common.primitives.Ints; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; -import net.md_5.bungee.api.ChatColor; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; @@ -42,6 +41,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.BlockStorage; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import us.myles.ViaVersion.util.ChatColorUtil; import us.myles.ViaVersion.util.GsonUtil; import java.util.ArrayList; @@ -53,6 +53,34 @@ import java.util.Set; public class Protocol1_13To1_12_2 extends Protocol { public static final MappingData MAPPINGS = new MappingData(); + // These are arbitrary rewrite values, it just needs an invalid color code character. + private static final Map SCOREBOARD_TEAM_NAME_REWRITE = new HashMap<>(); + private static final Set FORMATTING_CODES = Sets.newHashSet('k', 'l', 'm', 'n', 'o', 'r'); + + static { + SCOREBOARD_TEAM_NAME_REWRITE.put('0', 'g'); + SCOREBOARD_TEAM_NAME_REWRITE.put('1', 'h'); + SCOREBOARD_TEAM_NAME_REWRITE.put('2', 'i'); + SCOREBOARD_TEAM_NAME_REWRITE.put('3', 'j'); + SCOREBOARD_TEAM_NAME_REWRITE.put('4', 'p'); + SCOREBOARD_TEAM_NAME_REWRITE.put('5', 'q'); + SCOREBOARD_TEAM_NAME_REWRITE.put('6', 's'); + SCOREBOARD_TEAM_NAME_REWRITE.put('7', 't'); + SCOREBOARD_TEAM_NAME_REWRITE.put('8', 'u'); + SCOREBOARD_TEAM_NAME_REWRITE.put('9', 'v'); + SCOREBOARD_TEAM_NAME_REWRITE.put('a', 'w'); + SCOREBOARD_TEAM_NAME_REWRITE.put('b', 'x'); + SCOREBOARD_TEAM_NAME_REWRITE.put('c', 'y'); + SCOREBOARD_TEAM_NAME_REWRITE.put('d', 'z'); + SCOREBOARD_TEAM_NAME_REWRITE.put('e', '!'); + SCOREBOARD_TEAM_NAME_REWRITE.put('f', '?'); + SCOREBOARD_TEAM_NAME_REWRITE.put('k', '#'); + SCOREBOARD_TEAM_NAME_REWRITE.put('l', '('); + SCOREBOARD_TEAM_NAME_REWRITE.put('m', ')'); + SCOREBOARD_TEAM_NAME_REWRITE.put('n', ':'); + SCOREBOARD_TEAM_NAME_REWRITE.put('o', ';'); + SCOREBOARD_TEAM_NAME_REWRITE.put('r', '/'); + } public Protocol1_13To1_12_2() { super(ClientboundPackets1_12_1.class, ClientboundPackets1_13.class, ServerboundPackets1_12_1.class, ServerboundPackets1_13.class); @@ -115,36 +143,6 @@ public class Protocol1_13To1_12_2 extends Protocol SCOREBOARD_TEAM_NAME_REWRITE = new HashMap<>(); - private static final Set FORMATTING_CODES = Sets.newHashSet(ChatColor.MAGIC, ChatColor.BOLD, ChatColor.STRIKETHROUGH, - ChatColor.UNDERLINE, ChatColor.ITALIC, ChatColor.RESET); - - static { - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.BLACK, 'g'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.DARK_BLUE, 'h'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.DARK_GREEN, 'i'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.DARK_AQUA, 'j'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.DARK_RED, 'p'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.DARK_PURPLE, 'q'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.GOLD, 's'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.GRAY, 't'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.DARK_GRAY, 'u'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.BLUE, 'v'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.GREEN, 'w'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.AQUA, 'x'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.RED, 'y'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.LIGHT_PURPLE, 'z'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.YELLOW, '!'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.WHITE, '?'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.MAGIC, '#'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.BOLD, '('); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.STRIKETHROUGH, ')'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.UNDERLINE, ':'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.ITALIC, ';'); - SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.RESET, '/'); - } - @Override protected void registerPackets() { MetadataRewriter metadataRewriter = new MetadataRewriter1_13To1_12_2(this); @@ -625,9 +623,9 @@ public class Protocol1_13To1_12_2 extends Protocol -1; index--) { char section = input.charAt(index); - if (section == ChatColor.COLOR_CHAR && index < length - 1) { + if (section == ChatColorUtil.COLOR_CHAR && index < length - 1) { char c = input.charAt(index + 1); - ChatColor color = ChatColor.getByChar(c); - if (color != null && !FORMATTING_CODES.contains(color)) { - return color; + if (ChatColorUtil.isColorCode(c) && !FORMATTING_CODES.contains(c)) { + return c; } } } - - return ChatColor.RESET; + return 'r'; } protected String rewriteTeamMemberName(String name) { // The Display Name is just colours which overwrites the suffix // It also overwrites for ANY colour in name but most plugins // will just send colour as 'invisible' character - if (ChatColor.stripColor(name).isEmpty()) { + if (ChatColorUtil.stripColor(name).isEmpty()) { StringBuilder newName = new StringBuilder(); for (int i = 1; i < name.length(); i += 2) { char colorChar = name.charAt(i); - Character rewrite = SCOREBOARD_TEAM_NAME_REWRITE.get(ChatColor.getByChar(colorChar)); + Character rewrite = SCOREBOARD_TEAM_NAME_REWRITE.get(colorChar); if (rewrite == null) { rewrite = colorChar; } - newName.append(ChatColor.COLOR_CHAR).append(rewrite); + newName.append(ChatColorUtil.COLOR_CHAR).append(rewrite); } name = newName.toString(); } 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 21a567922..f7f579811 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 @@ -9,7 +9,6 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.google.common.base.Joiner; import com.google.common.primitives.Ints; -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; @@ -304,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.fromLegacyTextAsString(name.getValue(), ChatColor.WHITE, true)); + name.setValue(ChatRewriter.legacyTextToJsonString(name.getValue(), true)); } } // ench is now Enchantments and now uses identifiers @@ -561,7 +560,7 @@ public class InventoryPackets { if (display.get("Name") instanceof StringTag) { StringTag name = display.get("Name"); StringTag via = display.remove(NBT_TAG_NAME + "|Name"); - name.setValue(via != null ? via.getValue() : ChatRewriter.jsonTextToLegacy(name.getValue())); + name.setValue(via != null ? via.getValue() : ChatRewriter.jsonToLegacyText(name.getValue())); } } 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 5b1638eeb..c56d43906 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 @@ -4,7 +4,6 @@ import com.github.steveice10.opennbt.tag.builtin.*; 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 +247,7 @@ 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) { - String jsonText = ChatRewriter.fromLegacyTextAsString(((StringTag) loreEntry).getValue(), ChatColor.WHITE, true); + String jsonText = ChatRewriter.legacyTextToJsonString(((StringTag) loreEntry).getValue(), true); ((StringTag) loreEntry).setValue(jsonText); } } @@ -275,7 +274,7 @@ public class InventoryPackets { } else { for (Tag loreEntry : lore) { if (loreEntry instanceof StringTag) { - ((StringTag) loreEntry).setValue(ChatRewriter.jsonTextToLegacy(((StringTag) loreEntry).getValue())); + ((StringTag) loreEntry).setValue(ChatRewriter.jsonToLegacyText(((StringTag) loreEntry).getValue())); } } } diff --git a/common/src/main/java/us/myles/ViaVersion/update/UpdateUtil.java b/common/src/main/java/us/myles/ViaVersion/update/UpdateUtil.java index 6628bf805..9d989dde5 100644 --- a/common/src/main/java/us/myles/ViaVersion/update/UpdateUtil.java +++ b/common/src/main/java/us/myles/ViaVersion/update/UpdateUtil.java @@ -2,7 +2,6 @@ package us.myles.ViaVersion.update; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; -import net.md_5.bungee.api.ChatColor; import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.util.GsonUtil; @@ -18,7 +17,7 @@ import java.util.UUID; public class UpdateUtil { - public static final String PREFIX = ChatColor.GREEN + "" + ChatColor.BOLD + "[ViaVersion] " + ChatColor.GREEN; + public static final String PREFIX = "§a§l[ViaVersion] §a"; private static final String URL = "https://api.spiget.org/v2/resources/"; private static final int PLUGIN = 19254; private static final String LATEST_VERSION = "/versions/latest"; diff --git a/common/src/main/java/us/myles/ViaVersion/util/ChatColorUtil.java b/common/src/main/java/us/myles/ViaVersion/util/ChatColorUtil.java new file mode 100644 index 000000000..7e764a744 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/util/ChatColorUtil.java @@ -0,0 +1,56 @@ +package us.myles.ViaVersion.util; + +import it.unimi.dsi.fastutil.ints.Int2IntMap; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; + +import java.util.regex.Pattern; + +// Based on https://github.com/SpigotMC/BungeeCord/blob/master/chat/src/main/java/net/md_5/bungee/api/ChatColor.java +public class ChatColorUtil { + + public static final String ALL_CODES = "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx"; + public static final char COLOR_CHAR = '§'; + public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + COLOR_CHAR + "[0-9A-FK-ORX]"); + private static final Int2IntMap COLOR_ORDINALS = new Int2IntOpenHashMap(); + private static int ordinalCounter; + + static { + addColorOrindal('0', '9'); + addColorOrindal('a', 'f'); + addColorOrindal('k', 'o'); + addColorOrindal('r'); + } + + public static boolean isColorCode(char c) { + return COLOR_ORDINALS.containsKey(c); + } + + public static int getColorOrdinal(char c) { + return COLOR_ORDINALS.getOrDefault(c, -1); + } + + public static String translateAlternateColorCodes(String s) { + char[] chars = s.toCharArray(); + for (int i = 0; i < chars.length - 1; i++) { + if (chars[i] == '&' && ALL_CODES.indexOf(chars[i + 1]) > -1) { + chars[i] = COLOR_CHAR; + chars[i + 1] = Character.toLowerCase(chars[i + 1]); + } + } + return new String(chars); + } + + public static String stripColor(final String input) { + return STRIP_COLOR_PATTERN.matcher(input).replaceAll(""); + } + + private static void addColorOrindal(int from, int to) { + for (int c = from; c <= to; c++) { + addColorOrindal(c); + } + } + + private static void addColorOrindal(int colorChar) { + COLOR_ORDINALS.put(colorChar, ordinalCounter++); + } +} diff --git a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java index ddd0bc068..b3b475548 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java +++ b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java @@ -2,8 +2,7 @@ package us.myles.ViaVersion; import com.google.gson.JsonObject; import com.google.inject.Inject; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.spongepowered.api.Game; import org.spongepowered.api.config.DefaultConfig; import org.spongepowered.api.entity.living.player.Player; @@ -25,13 +24,19 @@ import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.sponge.commands.SpongeCommandHandler; import us.myles.ViaVersion.sponge.commands.SpongeCommandSender; -import us.myles.ViaVersion.sponge.platform.*; +import us.myles.ViaVersion.sponge.platform.SpongeTaskId; +import us.myles.ViaVersion.sponge.platform.SpongeViaAPI; +import us.myles.ViaVersion.sponge.platform.SpongeViaConfig; +import us.myles.ViaVersion.sponge.platform.SpongeViaInjector; +import us.myles.ViaVersion.sponge.platform.SpongeViaLoader; import us.myles.ViaVersion.sponge.util.LoggerWrapper; import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.VersionInfo; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; import java.util.logging.Logger; @Plugin(id = "viaversion", @@ -164,16 +169,8 @@ public class SpongePlugin implements ViaPlatform { @Override public void sendMessage(UUID uuid, String message) { - game.getServer().getPlayer(uuid) - .ifPresent(player -> - player.sendMessage( - TextSerializers.JSON.deserialize( - ComponentSerializer.toString( - TextComponent.fromLegacyText(message) // Hacky way to fix links - ) - ) - ) - ); + String serialized = LegacyComponentSerializer.legacySection().serialize(LegacyComponentSerializer.legacySection().deserialize(message)); + game.getServer().getPlayer(uuid).ifPresent(player -> player.sendMessage(TextSerializers.JSON.deserialize(serialized))); // Hacky way to fix links //TODO ?? } @Override diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/commands/SpongeCommandSender.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/commands/SpongeCommandSender.java index e45f90014..6aa2b117f 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/commands/SpongeCommandSender.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/commands/SpongeCommandSender.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion.sponge.commands; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.text.serializer.TextSerializers; import org.spongepowered.api.util.Identifiable; @@ -23,13 +22,8 @@ public class SpongeCommandSender implements ViaCommandSender { @Override public void sendMessage(String msg) { - source.sendMessage( - TextSerializers.JSON.deserialize( - ComponentSerializer.toString( - TextComponent.fromLegacyText(msg) // Hacky way to fix links - ) - ) - ); + String serialized = LegacyComponentSerializer.legacySection().serialize(LegacyComponentSerializer.legacySection().deserialize(msg)); + source.sendMessage(TextSerializers.JSON.deserialize(serialized)); // Hacky way to fix links //TODO ?? } @Override diff --git a/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java b/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java index 9505745b7..436d2f29b 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java +++ b/velocity/src/main/java/us/myles/ViaVersion/VelocityPlugin.java @@ -10,9 +10,7 @@ import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; -import net.kyori.text.serializer.gson.GsonComponentSerializer; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.slf4j.Logger; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.command.ViaCommandSender; @@ -26,7 +24,11 @@ import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.VersionInfo; import us.myles.ViaVersion.velocity.command.VelocityCommandHandler; import us.myles.ViaVersion.velocity.command.VelocityCommandSender; -import us.myles.ViaVersion.velocity.platform.*; +import us.myles.ViaVersion.velocity.platform.VelocityTaskId; +import us.myles.ViaVersion.velocity.platform.VelocityViaAPI; +import us.myles.ViaVersion.velocity.platform.VelocityViaConfig; +import us.myles.ViaVersion.velocity.platform.VelocityViaInjector; +import us.myles.ViaVersion.velocity.platform.VelocityViaLoader; import us.myles.ViaVersion.velocity.service.ProtocolDetectorService; import us.myles.ViaVersion.velocity.util.LoggerWrapper; @@ -65,7 +67,7 @@ public class VelocityPlugin implements ViaPlatform { public void onProxyInit(ProxyInitializeEvent e) { PROXY = proxy; VelocityCommandHandler commandHandler = new VelocityCommandHandler(); - PROXY.getCommandManager().register(commandHandler, "viaver", "vvvelocity", "viaversion"); + PROXY.getCommandManager().register("viaver", commandHandler, "vvvelocity", "viaversion"); api = new VelocityViaAPI(); conf = new VelocityViaConfig(configDir.toFile()); logger = new LoggerWrapper(loggerslf4j); @@ -152,21 +154,13 @@ public class VelocityPlugin implements ViaPlatform { @Override public void sendMessage(UUID uuid, String message) { - PROXY.getPlayer(uuid).ifPresent(it -> it.sendMessage( - GsonComponentSerializer.INSTANCE.deserialize( - ComponentSerializer.toString(TextComponent.fromLegacyText(message)) // Fixes links - ) - )); + PROXY.getPlayer(uuid).ifPresent(it -> it.sendMessage(LegacyComponentSerializer.legacySection().deserialize(message))); } @Override public boolean kickPlayer(UUID uuid, String message) { return PROXY.getPlayer(uuid).map(it -> { - it.disconnect( - GsonComponentSerializer.INSTANCE.deserialize( - ComponentSerializer.toString(TextComponent.fromLegacyText(message)) - ) - ); + it.disconnect(LegacyComponentSerializer.legacySection().deserialize(message)); return true; }).orElse(false); } diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/command/VelocityCommandHandler.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/command/VelocityCommandHandler.java index 9c23a01e5..3a60557a9 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/command/VelocityCommandHandler.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/command/VelocityCommandHandler.java @@ -1,14 +1,12 @@ package us.myles.ViaVersion.velocity.command; -import com.velocitypowered.api.command.Command; -import com.velocitypowered.api.command.CommandSource; -import org.checkerframework.checker.nullness.qual.NonNull; +import com.velocitypowered.api.command.SimpleCommand; import us.myles.ViaVersion.commands.ViaCommandHandler; import us.myles.ViaVersion.velocity.command.subs.ProbeSubCmd; import java.util.List; -public class VelocityCommandHandler extends ViaCommandHandler implements Command { +public class VelocityCommandHandler extends ViaCommandHandler implements SimpleCommand { public VelocityCommandHandler() { try { registerSubCommand(new ProbeSubCmd()); @@ -18,12 +16,12 @@ public class VelocityCommandHandler extends ViaCommandHandler implements Command } @Override - public void execute(@NonNull CommandSource source, String[] args) { - onCommand(new VelocityCommandSender(source), args); + public void execute(Invocation invocation) { + onCommand(new VelocityCommandSender(invocation.source()), invocation.arguments()); } @Override - public List suggest(@NonNull CommandSource source, String[] currentArgs) { - return onTabComplete(new VelocityCommandSender(source), currentArgs); + public List suggest(Invocation invocation) { + return onTabComplete(new VelocityCommandSender(invocation.source()), invocation.arguments()); } } diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/command/VelocityCommandSender.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/command/VelocityCommandSender.java index e699f743d..bab9ea795 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/command/VelocityCommandSender.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/command/VelocityCommandSender.java @@ -2,9 +2,7 @@ package us.myles.ViaVersion.velocity.command; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.proxy.Player; -import net.kyori.text.serializer.gson.GsonComponentSerializer; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import us.myles.ViaVersion.api.command.ViaCommandSender; import java.util.UUID; @@ -23,11 +21,7 @@ public class VelocityCommandSender implements ViaCommandSender { @Override public void sendMessage(String msg) { - source.sendMessage( - GsonComponentSerializer.INSTANCE.deserialize( - ComponentSerializer.toString(TextComponent.fromLegacyText(msg)) // Fixes links - ) - ); + source.sendMessage(LegacyComponentSerializer.legacySection().deserialize(msg)); } @Override