diff --git a/src/main/java/eu/mathiasl/legacychatfix/LegacyChatFixPacketAdapter.java b/src/main/java/eu/mathiasl/legacychatfix/LegacyChatFixPacketAdapter.java index f449961..4c63a95 100644 --- a/src/main/java/eu/mathiasl/legacychatfix/LegacyChatFixPacketAdapter.java +++ b/src/main/java/eu/mathiasl/legacychatfix/LegacyChatFixPacketAdapter.java @@ -10,11 +10,6 @@ import com.comphenix.protocol.wrappers.WrappedChatComponent; import eu.mathiasl.legacychatfix.config.ConfigManager; import eu.mathiasl.legacychatfix.config.LegacyChatOption; -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.chat.BaseComponent; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; - import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.MemorySection; import org.bukkit.configuration.file.FileConfiguration; @@ -60,14 +55,21 @@ public class LegacyChatFixPacketAdapter extends PacketAdapter { /** * Set the adapter to listen for packets being sent to the client from "system chat". + * * @param plugin A reference to the plugin class responsible for the adapter. */ public LegacyChatFixPacketAdapter(Plugin plugin, ConfigManager configManager) { super(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.SYSTEM_CHAT); - this.legacyColors = getLegacyColors(configManager); + this.legacyColors = getLegacyChatOptions(configManager); } - private HashMap getLegacyColors(ConfigManager configManager) { + /** + * Get the legacy chat options from the config file. + * + * @param configManager The config manager to get the config file from. + * @return A map of legacy color codes to replace. + */ + private HashMap getLegacyChatOptions(ConfigManager configManager) { HashMap legacyColors = new HashMap<>(); FileConfiguration config = configManager.getConfig("config.yml"); @@ -80,13 +82,15 @@ public class LegacyChatFixPacketAdapter extends PacketAdapter { if (!(value instanceof MemorySection)) return; MemorySection valueSection = (MemorySection) value; + String legacyCode = valueSection.getString("legacy_code"); + String modernCode = valueSection.getString("modern_code"); - LegacyChatOption legacyChatOption = new LegacyChatOption( - valueSection.getString("legacy_code"), - valueSection.getString("modern_code") - ); - - legacyColors.put(key, legacyChatOption); + try { + LegacyChatOption legacyChatOption = new LegacyChatOption(legacyCode, modernCode); + legacyColors.put(key, legacyChatOption); + } catch (Exception e) { + plugin.getLogger().warning("Failed to parse legacy color option: legacy_code=" + legacyCode + ", modern_code=" + modernCode); + } }); return legacyColors; @@ -95,7 +99,7 @@ public class LegacyChatFixPacketAdapter extends PacketAdapter { /** * Listen for packets being sent to the client and replace legacy color codes. * - * @param event The packet event being sent. + * @param event The packet event being sent. */ @Override public void onPacketSending(PacketEvent event) { @@ -108,76 +112,26 @@ public class LegacyChatFixPacketAdapter extends PacketAdapter { WrappedChatComponent chatComponent = chatComponents.read(0); // Get the chat component // Convert the chat component to a JSON string String message = chatComponent.getJson(); // Get the message - // Parse the message (to check if it's a chat component) - BaseComponent[] bungeeComponents; - try { - bungeeComponents = ComponentSerializer.parse(message); // Parse the message (to check if it's a chat component - } catch (Exception e) { - plugin.getLogger().warning("Failed to parse chat component: " + message); - return; - } - // If no bungee components are found after all, return - if (bungeeComponents == null || bungeeComponents.length == 0) return; - - // Iterate through the bungee components and replace legacy color codes - for (BaseComponent component : bungeeComponents) { - // If the component is not a text component, continue - if (!(component instanceof TextComponent)) - continue; - - // Replace legacy color codes in the text component - TextComponent textComponent = (TextComponent) component; - replaceLegacyColor(textComponent, legacyColors); - - if (textComponent.getExtra() == null || textComponent.getExtra().isEmpty()) - continue; - - // If the text component has extra components, replace legacy color codes in those as well - for (BaseComponent extra : textComponent.getExtra()) { - if (!(extra instanceof TextComponent)) - continue; - - TextComponent extraTextComponent = (TextComponent) extra; - replaceLegacyColor(extraTextComponent, legacyColors); - } - } - // Write the modified chat components back to the packet - chatComponents.write(0, WrappedChatComponent.fromJson(ComponentSerializer.toString(bungeeComponents))); - } - - /** - * Replaces legacy color code formatting with modern color formatting in - * through the TextComponent and its extra components. - * - * @param textComponent The text component to replace the legacy color codes in. - * @param legacyColors The legacy color codes to replace. - */ - private void replaceLegacyColor(TextComponent textComponent, HashMap legacyColors) { - // Get the text from the text component - String text = textComponent.getText(); - - // Iterate through the legacy color codes and replace them with modern color codes + boolean containedLegacyCoding = false; for (String legacyColorIdentifier : legacyColors.keySet()) { - // If the text does not contain the legacy color code, continue - if (!text.contains(legacyColorIdentifier)) - continue; LegacyChatOption legacyColorOption = legacyColors.get(legacyColorIdentifier); + // If the text does not contain the legacy color code, continue + if (!message.contains(legacyColorOption.getLegacyCode())) + continue; + + containedLegacyCoding = true; // Replace the legacy color code with an empty string - text = text.replace(legacyColorOption.getLegacyCode(), ""); - // Set the text component's text to the modified text - textComponent.setText(text); - // Set the text component's color to "RESET" initially - ChatColor chatColor = ChatColor.WHITE; - try { - // Try to set the text component's color to the modern color code - chatColor = legacyColorOption.getModernCode(); - } catch (IllegalArgumentException e) { - // If the modern color code is invalid, log a warning - this.getPlugin().getLogger().warning("Invalid color code: " + legacyColorOption.getModernCode()); - } - // Set the text component's color to the modern color code - textComponent.setColor(chatColor); + message = message.replace(legacyColorOption.getLegacyCode().replace("\\", "\\\\"), legacyColorOption.getModernCode().toString()); + } + + // No reason to continue if the message doesn't contain any legacy color codes + if (!containedLegacyCoding) return; + // Write the modified chat components back to the packet + try { + chatComponents.write(0, WrappedChatComponent.fromJson(message)); + } catch (Exception e) { + plugin.getLogger().warning("Failed to write chat components back to the packet: " + e.getCause()); } } } diff --git a/src/main/java/eu/mathiasl/legacychatfix/config/LegacyChatOption.java b/src/main/java/eu/mathiasl/legacychatfix/config/LegacyChatOption.java index 6d3f84c..fca5a7a 100644 --- a/src/main/java/eu/mathiasl/legacychatfix/config/LegacyChatOption.java +++ b/src/main/java/eu/mathiasl/legacychatfix/config/LegacyChatOption.java @@ -35,8 +35,8 @@ public class LegacyChatOption { @Override public String toString() { return "LegacyOption[" + - "legacyColor='" + legacyCode + '\'' + - ", modernColor='" + modernCode + '\'' + + "legacyCode='" + legacyCode + '\'' + + ", modernCode='" + modernCode + '\'' + ']'; } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9fdef69..405eed2 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -4,7 +4,7 @@ # - Can be essentially anything as it is just used to check if it is in the chat and replace it accordingly. # - modern_code: The modern formatting code to replace with. # - Must be written according to bungeecoord-chat:Chatcolor! This is the actual formatting code that will be used in the chat. -# Including readable name (e.g., black), the older §# codes (e.g., §0), or even RGB using #RRGGBB (e.g., #000000). +# Including readable name (e.g., black), the older §# codes (e.g., §0). # See: https://github.com/SpigotMC/BungeeCord/blob/master/chat/src/main/java/net/md_5/bungee/api/ChatColor.java legacy_codes: black: @@ -72,7 +72,7 @@ legacy_codes: modern_code: 'obfuscated' reset: legacy_code: '\' - modern_code: 'white' # apparently, 'reset' is not a valid color in modern formatting, so we'll just use white. + modern_code: 'reset' # apparently, 'reset' is not a valid color in modern formatting, so we'll just use black. secondary: legacy_code: '\' modern_code: 'white'