Support alternate colour codes in plugin messages

This commit is contained in:
Draycia committed 2020-10-10 23:26:47 -07:00
1 parent 267ebc4ca9
commit f6479214e8
4 files changed
+52 -29

No files matched your search

@@ -14,13 +14,13 @@ public class PreChatFormatEvent implements CarbonEvent, Cancellable {
private final ChatUser user;
@NonNull
private ChatChannel chatChannel;
@Nullable
@NonNull
private String format;
@NonNull
private String message;
public PreChatFormatEvent(@NonNull final ChatUser user, @NonNull final ChatChannel chatChannel,
@Nullable final String format, @NonNull final String message) {
@NonNull final String format, @NonNull final String message) {
this.user = user;
this.chatChannel = chatChannel;
@@ -52,12 +52,12 @@ public class PreChatFormatEvent implements CarbonEvent, Cancellable {
this.chatChannel = chatChannel;
}
@Nullable
@NonNull
public String format() {
return this.format;
}
public void format(@Nullable final String format) {
public void format(@NonNull final String format) {
this.format = format;
}
@@ -2,6 +2,7 @@ package net.draycia.carbon.listeners.events;
import net.draycia.carbon.events.CarbonEvents;
import net.draycia.carbon.events.api.PreChatFormatEvent;
import net.draycia.carbon.util.CarbonUtils;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@@ -21,27 +22,7 @@ public class LegacyFormatHandler {
public LegacyFormatHandler() {
CarbonEvents.register(PreChatFormatEvent.class, PostOrders.FIRST, false, event -> {
// Legacy RGB
event.format(this.pattern.matcher(event.format()).replaceAll("<#$1$2$3$4$5$6>"));
// Alternate RGB, TAB (neznamy) && KiteBoard
event.format(this.altPattern.matcher(event.format()).replaceAll("<#$1$2$3$4$5$6>"));
// Legacy Colors
for (final char c : "0123456789abcdef".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextColor color = format.color();
event.format(event.format().replaceAll("[§&]" + c, "<" + color.asHexString() + ">"));
}
// Legacy Formatting
for (final char c : "klmno".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextDecoration decoration = format.decoration();
event.format(event.format().replaceAll("[§&]" + c, "<" + decoration.name() + ">"));
}
event.format(CarbonUtils.translateAlternateColors(event.format()));
});
}
@@ -3,6 +3,7 @@ package net.draycia.carbon.managers;
import de.themoep.minedown.adventure.MineDown;
import me.clip.placeholderapi.PlaceholderAPI;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.util.CarbonUtils;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
@@ -40,18 +41,20 @@ public class AdventureManager {
return TextComponent.empty();
}
String format = CarbonUtils.translateAlternateColors(input);
switch (this.carbonChat.language().getString("formatting.type", "minimessage").toLowerCase()) {
case "minedown":
return MineDown.parse(input, placeholders);
return MineDown.parse(format, placeholders);
case "mojang":
case "mojangson":
case "json":
return this.processMojang(input, placeholders);
return this.processMojang(format, placeholders);
case "minimessage-markdown":
return MiniMessage.markdown().parse(input, placeholders);
return MiniMessage.markdown().parse(format, placeholders);
case "minimessage":
default:
return MiniMessage.get().parse(input, placeholders);
return MiniMessage.get().parse(format, placeholders);
}
}
@@ -29,8 +29,10 @@ import net.draycia.carbon.storage.ChatUser;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.bungeecord.BungeeCordComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
@@ -46,6 +48,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public final class CarbonUtils {
@@ -129,6 +132,42 @@ public final class CarbonUtils {
return TextColor.fromCSSHexString(input);
}
private static final @NonNull Pattern spigotLegacyRGB =
Pattern.compile("[§&]x[§&]([0-9a-f])[§&]([0-9a-f])[§&]([0-9a-f])[§&]([0-9a-f])[§&]([0-9a-f])[§&]([0-9a-f])");
private static final @NonNull Pattern pluginRGB =
Pattern.compile("[§&]#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])");
@NonNull
public static String translateAlternateColors(@NonNull String input) {
// TODO: check if MiniMessage or MineDown
String output = input;
// Legacy RGB
output = spigotLegacyRGB.matcher(output).replaceAll("<#$1$2$3$4$5$6>");
// Alternate RGB, TAB (neznamy) && KiteBoard
output = pluginRGB.matcher(output).replaceAll("<#$1$2$3$4$5$6>");
// Legacy Colors
for (final char c : "0123456789abcdef".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextColor color = format.color();
output = output.replaceAll("[§&]" + c, "<" + color.asHexString() + ">");
}
// Legacy Formatting
for (final char c : "klmno".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextDecoration decoration = format.decoration();
output = output.replaceAll("[§&]" + c, "<" + decoration.name() + ">");
}
return output;
}
@NonNull
public static Argument onlineChatUserArgument() {
return new CustomArgument<>(input -> {