- * This method is the improved version of {@link ChatColor#translateAlternateColorCodes(char, String)},
+ * This method is the improved version of Bukkit’s {@code ChatColor.translateAlternateColorCodes(char, String)},
* because it takes into account essentials RGB color code, and {@code altColorChar} escaping (by doubling it).
* Essentials RGB color code are converted to Bungee chat RGB format, so the returned string can be converted
* to component (see {@link Chat#legacyText(Object)}).
@@ -112,7 +103,7 @@ public class ChatColorUtil {
*/
public static String translateAlternateColorCodes(char altColorChar, String textToTranslate)
{
- char colorChar = ChatColor.COLOR_CHAR;
+ char colorChar = LegacyChatFormat.COLOR_CHAR;
StringBuilder acc = new StringBuilder();
char[] b = textToTranslate.toCharArray();
for ( int i = 0; i < b.length; i++ )
@@ -180,7 +171,7 @@ public class ChatColorUtil {
* @return the text fully italic.
*/
public static String forceItalic(String legacyText) {
- return forceFormat(legacyText, ChatColor.ITALIC);
+ return forceFormat(legacyText, TextDecoration.ITALIC);
}
/**
@@ -190,7 +181,7 @@ public class ChatColorUtil {
* @return the text fully bold.
*/
public static String forceBold(String legacyText) {
- return forceFormat(legacyText, ChatColor.BOLD);
+ return forceFormat(legacyText, TextDecoration.BOLD);
}
/**
@@ -200,7 +191,7 @@ public class ChatColorUtil {
* @return the text fully underlined.
*/
public static String forceUnderline(String legacyText) {
- return forceFormat(legacyText, ChatColor.UNDERLINE);
+ return forceFormat(legacyText, TextDecoration.UNDERLINED);
}
/**
@@ -210,7 +201,7 @@ public class ChatColorUtil {
* @return the text fully stroked through.
*/
public static String forceStrikethrough(String legacyText) {
- return forceFormat(legacyText, ChatColor.STRIKETHROUGH);
+ return forceFormat(legacyText, TextDecoration.STRIKETHROUGH);
}
/**
@@ -220,15 +211,16 @@ public class ChatColorUtil {
* @return the text fully obfuscated.
*/
public static String forceObfuscated(String legacyText) {
- return forceFormat(legacyText, ChatColor.MAGIC);
+ return forceFormat(legacyText, TextDecoration.OBFUSCATED);
}
- private static String forceFormat(String legacyText, ChatColor format) {
+ private static String forceFormat(String legacyText, TextFormat format) {
+ String formatStr = LegacyChatFormat.of(format).toString();
return format + legacyText
- .replace(format.toString(), "") // remove previous tag to make the result cleaner
- .replaceAll("§([a-frA-FR\\d])", "§$1" + format);
+ .replace(formatStr, "") // remove previous tag to make the result cleaner
+ .replaceAll("§([a-frA-FR\\d])", "§$1" + formatStr);
}
@@ -243,40 +235,12 @@ public class ChatColorUtil {
* @return the resulting text.
*/
public static String resetToColor(String legacyText, String color) {
- return legacyText.replace(ChatColor.RESET.toString(), color);
+ return legacyText.replace(LegacyChatFormat.RESET.toString(), color);
}
- /**
- * Converts the provided {@link ChatColor} to its Adventure counterpart.
- * @param bungee a BungeeCord {@link ChatColor} instance.
- * @return the {@link TextColor} equivalent to the provided {@link ChatColor}.
- */
- public static TextColor toAdventure(ChatColor bungee) {
- if (bungee == null)
- return null;
- if (bungee.getColor() == null)
- throw new IllegalArgumentException("The provided Bungee ChatColor must be an actual color (not format nor reset).");
- return TextColor.color(bungee.getColor().getRGB());
- }
-
- /**
- * Converts the provided {@link TextColor} to its BungeeCord counterpart.
- * @param col a Adventure {@link TextColor} instance.
- * @return the {@link ChatColor} equivalent to the provided {@link TextColor}.
- */
- public static ChatColor toBungee(TextColor col) {
- if (col == null)
- return null;
- if (col instanceof NamedTextColor) {
- return ChatColor.of(((NamedTextColor) col).toString());
- }
- return ChatColor.of(col.asHexString());
- }
-
-
/**
* Create a color, interpolating between 2 colors.
* @param v0 the value corresponding to color {@code cc0}.
diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatConfig.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatConfig.java
index b98922a..af83108 100644
--- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatConfig.java
+++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatConfig.java
@@ -87,7 +87,7 @@ public class ChatConfig {
*/
public static int getPrefixWidth(boolean console) {
Chat c;
- return prefix == null ? 0 : (c = prefix.get()) == null ? 0 : ChatUtil.componentWidth(c.getAdv(), console);
+ return prefix == null ? 0 : (c = prefix.get()) == null ? 0 : ChatUtil.componentWidth(c.get(), console);
}
diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatStatic.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatStatic.java
index ce4606d..53c2168 100644
--- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatStatic.java
+++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatStatic.java
@@ -1,7 +1,6 @@
package fr.pandacube.lib.chat;
-import java.util.Objects;
-
+import fr.pandacube.lib.chat.Chat.FormatableChat;
import net.kyori.adventure.text.BlockNBTComponent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentBuilder;
@@ -18,9 +17,8 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
-import net.md_5.bungee.api.chat.BaseComponent;
-import fr.pandacube.lib.chat.Chat.FormatableChat;
+import java.util.Objects;
/**
* Abstract class holding the publicly accessible methods to create an instance of {@link Chat} component.
@@ -33,15 +31,6 @@ public abstract class ChatStatic {
return new FormatableChat(componentToBuilder(c));
}
- /**
- * Creates a {@link FormatableChat} from the provided Bungee {@link BaseComponent}.
- * @param c the {@link BaseComponent}.
- * @return a new {@link FormatableChat}.
- */
- public static FormatableChat chatComponent(BaseComponent c) {
- return new FormatableChat(componentToBuilder(Chat.toAdventure(c)));
- }
-
/**
* Creates a {@link FormatableChat} from the provided {@link ComponentLike}.
* If the provided component is an instance of {@link Chat}, its content will be duplicated, and the provided one
@@ -61,15 +50,6 @@ public abstract class ChatStatic {
return new FormatableChat(Component.text());
}
- /**
- * Creates a {@link FormatableChat} from the provided Bungee {@link BaseComponent BaseComponent[]}.
- * @param c the array of {@link BaseComponent}.
- * @return a new {@link FormatableChat}.
- */
- public static FormatableChat chatComponent(BaseComponent[] c) {
- return chatComponent(Chat.toAdventure(c));
- }
-
diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java
index cf90146..00bf683 100644
--- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java
+++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java
@@ -1,5 +1,17 @@
package fr.pandacube.lib.chat;
+import fr.pandacube.lib.chat.Chat.FormatableChat;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.ComponentLike;
+import net.kyori.adventure.text.TextComponent;
+import net.kyori.adventure.text.TranslatableComponent;
+import net.kyori.adventure.text.TranslationArgument;
+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.format.TextDecoration.State;
+import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -10,19 +22,6 @@ import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
-import net.kyori.adventure.text.Component;
-import net.kyori.adventure.text.ComponentLike;
-import net.kyori.adventure.text.TextComponent;
-import net.kyori.adventure.text.TranslatableComponent;
-import net.kyori.adventure.text.TranslationArgument;
-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.format.TextDecoration.State;
-import net.md_5.bungee.api.ChatColor;
-
-import fr.pandacube.lib.chat.Chat.FormatableChat;
-
import static fr.pandacube.lib.chat.ChatStatic.chat;
/**
@@ -351,7 +350,7 @@ public class ChatUtil {
do {
char c = legacyText.charAt(index);
- if (c == ChatColor.COLOR_CHAR && index < legacyText.length() - 1) {
+ if (c == LegacyComponentSerializer.SECTION_CHAR && index < legacyText.length() - 1) {
currentWord.append(c);
c = legacyText.charAt(++index);
currentWord.append(c);
@@ -482,7 +481,7 @@ public class ChatUtil {
}
if (!row.isEmpty())
spacedRow.then(row.getLast());
- spacedRows.add(spacedRow.getAdv());
+ spacedRows.add(spacedRow.get());
}
return spacedRows;
@@ -504,14 +503,14 @@ public class ChatUtil {
*/
public static Component customWidthSpace(int width, boolean console) {
if (console)
- return Chat.text(" ".repeat(width)).getAdv();
+ return Chat.text(" ".repeat(width)).get();
return switch (width) {
case 0, 1 -> Component.empty();
- case 2 -> Chat.text(".").black().getAdv();
- case 3 -> Chat.text("`").black().getAdv();
- case 6 -> Chat.text(". ").black().getAdv();
- case 7 -> Chat.text("` ").black().getAdv();
- case 11 -> Chat.text("` ").black().getAdv();
+ case 2 -> Chat.text(".").black().get();
+ case 3 -> Chat.text("`").black().get();
+ case 6 -> Chat.text(". ").black().get();
+ case 7 -> Chat.text("` ").black().get();
+ case 11 -> Chat.text("` ").black().get();
default -> {
int nbSpace = width / 4;
int nbBold = width % 4;
@@ -520,13 +519,13 @@ public class ChatUtil {
if (nbBold > 0) {
yield Chat.text(" ".repeat(nbNotBold)).bold(false)
.then(Chat.text(" ".repeat(nbBold)).bold(true))
- .getAdv();
+ .get();
}
else
- yield Chat.text(" ".repeat(nbNotBold)).bold(false).getAdv();
+ yield Chat.text(" ".repeat(nbNotBold)).bold(false).get();
}
else if (nbBold > 0) {
- yield Chat.text(" ".repeat(nbBold)).bold(true).getAdv();
+ yield Chat.text(" ".repeat(nbBold)).bold(true).get();
}
throw new IllegalStateException("Should not be here (width=" + width + "; nbSpace=" + nbSpace + "; nbBold=" + nbBold + "; nbNotBold=" + nbNotBold + ")");
}
diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/LegacyChatFormat.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/LegacyChatFormat.java
new file mode 100644
index 0000000..3bfc866
--- /dev/null
+++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/LegacyChatFormat.java
@@ -0,0 +1,122 @@
+package fr.pandacube.lib.chat;
+
+import net.kyori.adventure.text.format.TextColor;
+import net.kyori.adventure.text.format.TextDecoration;
+import net.kyori.adventure.text.format.TextFormat;
+import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
+import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
+
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public enum LegacyChatFormat {
+
+ BLACK('0'),
+ DARK_BLUE('1'),
+ DARK_GREEN('2'),
+ DARK_AQUA('3'),
+ DARK_RED('4'),
+ DARK_PURPLE('5'),
+ GOLD('6'),
+ GRAY('7'),
+ DARK_GRAY('8'),
+ BLUE('9'),
+ GREEN('a'),
+ AQUA('b'),
+ RED('c'),
+ LIGHT_PURPLE('d'),
+ YELLOW('e'),
+ WHITE('f'),
+ MAGIC('k'),
+ BOLD('l'),
+ STRIKETHROUGH('m'),
+ UNDERLINED('n'),
+ ITALIC('o'),
+ RESET('r');
+
+
+
+
+
+ public static final char COLOR_CHAR = LegacyComponentSerializer.SECTION_CHAR;
+ public static final String COLOR_STR_PREFIX = Character.toString(COLOR_CHAR);
+
+ private static final Map