Rewrite the code for replacing legacy codes in chat
- Just simply replace the text in the JSON as building with TextComponents is unneccesary and causes more issues...
This commit is contained in:
parent
13c12ced63
commit
a564da45be
|
@ -10,11 +10,6 @@ import com.comphenix.protocol.wrappers.WrappedChatComponent;
|
||||||
import eu.mathiasl.legacychatfix.config.ConfigManager;
|
import eu.mathiasl.legacychatfix.config.ConfigManager;
|
||||||
import eu.mathiasl.legacychatfix.config.LegacyChatOption;
|
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.ConfigurationSection;
|
||||||
import org.bukkit.configuration.MemorySection;
|
import org.bukkit.configuration.MemorySection;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
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".
|
* 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.
|
* @param plugin A reference to the plugin class responsible for the adapter.
|
||||||
*/
|
*/
|
||||||
public LegacyChatFixPacketAdapter(Plugin plugin, ConfigManager configManager) {
|
public LegacyChatFixPacketAdapter(Plugin plugin, ConfigManager configManager) {
|
||||||
super(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.SYSTEM_CHAT);
|
super(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.SYSTEM_CHAT);
|
||||||
this.legacyColors = getLegacyColors(configManager);
|
this.legacyColors = getLegacyChatOptions(configManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<String, LegacyChatOption> 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<String, LegacyChatOption> getLegacyChatOptions(ConfigManager configManager) {
|
||||||
HashMap<String, LegacyChatOption> legacyColors = new HashMap<>();
|
HashMap<String, LegacyChatOption> legacyColors = new HashMap<>();
|
||||||
|
|
||||||
FileConfiguration config = configManager.getConfig("config.yml");
|
FileConfiguration config = configManager.getConfig("config.yml");
|
||||||
|
@ -80,13 +82,15 @@ public class LegacyChatFixPacketAdapter extends PacketAdapter {
|
||||||
if (!(value instanceof MemorySection)) return;
|
if (!(value instanceof MemorySection)) return;
|
||||||
|
|
||||||
MemorySection valueSection = (MemorySection) value;
|
MemorySection valueSection = (MemorySection) value;
|
||||||
|
String legacyCode = valueSection.getString("legacy_code");
|
||||||
|
String modernCode = valueSection.getString("modern_code");
|
||||||
|
|
||||||
LegacyChatOption legacyChatOption = new LegacyChatOption(
|
try {
|
||||||
valueSection.getString("legacy_code"),
|
LegacyChatOption legacyChatOption = new LegacyChatOption(legacyCode, modernCode);
|
||||||
valueSection.getString("modern_code")
|
legacyColors.put(key, legacyChatOption);
|
||||||
);
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().warning("Failed to parse legacy color option: legacy_code=" + legacyCode + ", modern_code=" + modernCode);
|
||||||
legacyColors.put(key, legacyChatOption);
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return legacyColors;
|
return legacyColors;
|
||||||
|
@ -95,7 +99,7 @@ public class LegacyChatFixPacketAdapter extends PacketAdapter {
|
||||||
/**
|
/**
|
||||||
* Listen for packets being sent to the client and replace legacy color codes.
|
* 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
|
@Override
|
||||||
public void onPacketSending(PacketEvent event) {
|
public void onPacketSending(PacketEvent event) {
|
||||||
|
@ -108,76 +112,26 @@ public class LegacyChatFixPacketAdapter extends PacketAdapter {
|
||||||
WrappedChatComponent chatComponent = chatComponents.read(0); // Get the chat component
|
WrappedChatComponent chatComponent = chatComponents.read(0); // Get the chat component
|
||||||
// Convert the chat component to a JSON string
|
// Convert the chat component to a JSON string
|
||||||
String message = chatComponent.getJson(); // Get the message
|
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
|
boolean containedLegacyCoding = false;
|
||||||
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<String, LegacyChatOption> 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
|
|
||||||
for (String legacyColorIdentifier : legacyColors.keySet()) {
|
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);
|
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
|
// Replace the legacy color code with an empty string
|
||||||
text = text.replace(legacyColorOption.getLegacyCode(), "");
|
message = message.replace(legacyColorOption.getLegacyCode().replace("\\", "\\\\"), legacyColorOption.getModernCode().toString());
|
||||||
// Set the text component's text to the modified text
|
}
|
||||||
textComponent.setText(text);
|
|
||||||
// Set the text component's color to "RESET" initially
|
// No reason to continue if the message doesn't contain any legacy color codes
|
||||||
ChatColor chatColor = ChatColor.WHITE;
|
if (!containedLegacyCoding) return;
|
||||||
try {
|
// Write the modified chat components back to the packet
|
||||||
// Try to set the text component's color to the modern color code
|
try {
|
||||||
chatColor = legacyColorOption.getModernCode();
|
chatComponents.write(0, WrappedChatComponent.fromJson(message));
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (Exception e) {
|
||||||
// If the modern color code is invalid, log a warning
|
plugin.getLogger().warning("Failed to write chat components back to the packet: " + e.getCause());
|
||||||
this.getPlugin().getLogger().warning("Invalid color code: " + legacyColorOption.getModernCode());
|
|
||||||
}
|
|
||||||
// Set the text component's color to the modern color code
|
|
||||||
textComponent.setColor(chatColor);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,8 +35,8 @@ public class LegacyChatOption {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "LegacyOption[" +
|
return "LegacyOption[" +
|
||||||
"legacyColor='" + legacyCode + '\'' +
|
"legacyCode='" + legacyCode + '\'' +
|
||||||
", modernColor='" + modernCode + '\'' +
|
", modernCode='" + modernCode + '\'' +
|
||||||
']';
|
']';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
# - 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.
|
# - 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.
|
# - 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
|
# See: https://github.com/SpigotMC/BungeeCord/blob/master/chat/src/main/java/net/md_5/bungee/api/ChatColor.java
|
||||||
legacy_codes:
|
legacy_codes:
|
||||||
black:
|
black:
|
||||||
|
@ -72,7 +72,7 @@ legacy_codes:
|
||||||
modern_code: 'obfuscated'
|
modern_code: 'obfuscated'
|
||||||
reset:
|
reset:
|
||||||
legacy_code: '\<reset>'
|
legacy_code: '\<reset>'
|
||||||
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:
|
secondary:
|
||||||
legacy_code: '\<secondary>'
|
legacy_code: '\<secondary>'
|
||||||
modern_code: 'white'
|
modern_code: 'white'
|
||||||
|
|
Loading…
Reference in New Issue