mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-09-20 20:11:01 +02:00
Fix conversion issues between Legacy and MiniMessage (#5702)
Fixes #5652
This commit is contained in:
parent
d4b72c8af6
commit
1b1b511b61
@ -182,8 +182,10 @@ public class I18n implements net.ess3.api.II18n {
|
||||
}
|
||||
|
||||
final Object[] processedArgs = mutateArgs(objects, arg -> {
|
||||
final String str = arg instanceof AdventureUtil.ParsedPlaceholder ? arg.toString() : AdventureUtil.miniMessage().escapeTags(arg.toString());
|
||||
return AdventureUtil.legacyToMini(str);
|
||||
if (arg instanceof AdventureUtil.ParsedPlaceholder) {
|
||||
return arg.toString();
|
||||
}
|
||||
return AdventureUtil.legacyToMini(AdventureUtil.miniMessage().escapeTags(arg.toString()));
|
||||
});
|
||||
|
||||
return messageFormat.format(processedArgs).replace(' ', ' '); // replace nbsp with a space
|
||||
|
@ -8,18 +8,12 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.Tag;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class AdventureUtil {
|
||||
private static final LegacyComponentSerializer LEGACY_SERIALIZER;
|
||||
private static final Pattern NAMED_PATTERN = Pattern.compile(ChatColor.COLOR_CHAR + "[0-9a-fk-orA-FK-OR]");
|
||||
private static final Pattern HEX_PATTERN = Pattern.compile(ChatColor.COLOR_CHAR + "x((?:" + ChatColor.COLOR_CHAR + "[0-9a-fA-F]){6})");
|
||||
private static final MiniMessage MINI_MESSAGE_NO_TAGS;
|
||||
private static final String LOOKUP = "0123456789abcdefklmnor";
|
||||
private static final String[] MINI_TAGS = new String[] {"black", "dark_blue", "dark_green", "dark_aqua", "dark_red", "dark_purple", "gold", "gray", "dark_gray", "blue", "green", "aqua", "red", "light_purple", "yellow", "white", "obf", "b", "st", "u", "i", "reset"};
|
||||
private static final NamedTextColor[] COLORS = new NamedTextColor[] {NamedTextColor.BLACK, NamedTextColor.DARK_BLUE, NamedTextColor.DARK_GREEN, NamedTextColor.DARK_AQUA, NamedTextColor.DARK_RED, NamedTextColor.DARK_PURPLE, NamedTextColor.GOLD, NamedTextColor.GRAY, NamedTextColor.DARK_GRAY, NamedTextColor.BLUE, NamedTextColor.GREEN, NamedTextColor.AQUA, NamedTextColor.RED, NamedTextColor.LIGHT_PURPLE, NamedTextColor.YELLOW, NamedTextColor.WHITE};
|
||||
private static final NamedTextColor[] COLORS = new NamedTextColor[]{NamedTextColor.BLACK, NamedTextColor.DARK_BLUE, NamedTextColor.DARK_GREEN, NamedTextColor.DARK_AQUA, NamedTextColor.DARK_RED, NamedTextColor.DARK_PURPLE, NamedTextColor.GOLD, NamedTextColor.GRAY, NamedTextColor.DARK_GRAY, NamedTextColor.BLUE, NamedTextColor.GREEN, NamedTextColor.AQUA, NamedTextColor.RED, NamedTextColor.LIGHT_PURPLE, NamedTextColor.YELLOW, NamedTextColor.WHITE};
|
||||
private static IEssentials ess;
|
||||
private static MiniMessage miniMessageInstance;
|
||||
|
||||
@ -29,6 +23,9 @@ public final class AdventureUtil {
|
||||
builder.hexColors();
|
||||
}
|
||||
LEGACY_SERIALIZER = builder.build();
|
||||
|
||||
MINI_MESSAGE_NO_TAGS = MiniMessage.miniMessage();
|
||||
|
||||
miniMessageInstance = createMiniMessageInstance();
|
||||
}
|
||||
|
||||
@ -84,37 +81,16 @@ public final class AdventureUtil {
|
||||
|
||||
/**
|
||||
* Converts a section sign legacy string to a MiniMessage string.
|
||||
*
|
||||
* @param useCustomTags true if gold and red colors should use primary and secondary tags instead.
|
||||
*/
|
||||
public static String legacyToMini(String text, boolean useCustomTags) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
Matcher matcher = HEX_PATTERN.matcher(text);
|
||||
while (matcher.find()) {
|
||||
final String code = matcher.group(1).replace(String.valueOf(ChatColor.COLOR_CHAR), "");
|
||||
matcher.appendReplacement(buffer, "<#" + code + ">");
|
||||
final Component deserializedText = LEGACY_SERIALIZER.deserialize(text);
|
||||
if (useCustomTags) {
|
||||
return miniMessageInstance.serialize(deserializedText);
|
||||
} else {
|
||||
return MINI_MESSAGE_NO_TAGS.serialize(deserializedText);
|
||||
}
|
||||
matcher.appendTail(buffer);
|
||||
|
||||
matcher = NAMED_PATTERN.matcher(buffer.toString());
|
||||
buffer = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
final int format = LOOKUP.indexOf(Character.toLowerCase(matcher.group().charAt(1)));
|
||||
if (format != -1) {
|
||||
String tagName = MINI_TAGS[format];
|
||||
if (useCustomTags) {
|
||||
if (tagName.equals("gold")) {
|
||||
tagName = "primary";
|
||||
} else if (tagName.equals("red")) {
|
||||
tagName = "secondary";
|
||||
}
|
||||
}
|
||||
|
||||
matcher.appendReplacement(buffer, "<" + tagName + ">");
|
||||
}
|
||||
}
|
||||
matcher.appendTail(buffer);
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user