Skip to content

Commit

Permalink
Simplify minimessage conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 16, 2024
1 parent 5de4f21 commit 8789b19
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,17 @@
import org.apiguardian.api.API;
import org.jetbrains.annotations.VisibleForTesting;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Pattern;

/**
* Class for managing parsers and parse text.
*/
@SuppressWarnings({"unused"})
public class PlaceholderUtil {
private static final Pattern LEGACY_HEX_PATTERN = Pattern.compile("[§&]#(?<hex>[0-9a-fA-F]{6})");
private static final Pattern LEGACY_UGLY_HEX_PATTERN = Pattern.compile("([§&])x\\1(?<h1>[0-9a-fA-F])\\1(?<h2>[0-9a-fA-F])\\1(?<h3>[0-9a-fA-F])\\1(?<h4>[0-9a-fA-F])\\1(?<h5>[0-9a-fA-F])\\1(?<h6>[0-9a-fA-F])");
private static final Map<Character, String> MINIMESSAGE_REPLACEMENTS;
private static final List<PlaceholderParser> preParsePlaceholders = new CopyOnWriteArrayList<>();
private static final List<PlaceholderParser> postParsePlaceholders = new CopyOnWriteArrayList<>();

static {
MINIMESSAGE_REPLACEMENTS = new HashMap<>();
MINIMESSAGE_REPLACEMENTS.put('0', "black");
MINIMESSAGE_REPLACEMENTS.put('1', "dark_blue");
MINIMESSAGE_REPLACEMENTS.put('2', "dark_green");
MINIMESSAGE_REPLACEMENTS.put('3', "dark_aqua");
MINIMESSAGE_REPLACEMENTS.put('4', "dark_red");
MINIMESSAGE_REPLACEMENTS.put('5', "dark_purple");
MINIMESSAGE_REPLACEMENTS.put('6', "gold");
MINIMESSAGE_REPLACEMENTS.put('7', "gray");
MINIMESSAGE_REPLACEMENTS.put('8', "dark_gray");
MINIMESSAGE_REPLACEMENTS.put('9', "blue");
MINIMESSAGE_REPLACEMENTS.put('a', "green");
MINIMESSAGE_REPLACEMENTS.put('b', "aqua");
MINIMESSAGE_REPLACEMENTS.put('c', "red");
MINIMESSAGE_REPLACEMENTS.put('d', "light_purple");
MINIMESSAGE_REPLACEMENTS.put('e', "yellow");
MINIMESSAGE_REPLACEMENTS.put('f', "white");

MINIMESSAGE_REPLACEMENTS.put('k', "obfuscated");
MINIMESSAGE_REPLACEMENTS.put('l', "bold");
MINIMESSAGE_REPLACEMENTS.put('m', "strikethrough");
MINIMESSAGE_REPLACEMENTS.put('n', "underline");
MINIMESSAGE_REPLACEMENTS.put('o', "italic");
MINIMESSAGE_REPLACEMENTS.put('r', "reset");
}

private PlaceholderUtil() {
}

Expand Down Expand Up @@ -106,21 +73,8 @@ private static Component parseTextToComponent(final String text) {
@VisibleForTesting
@API(status = API.Status.INTERNAL)
public static String convertMiniMessageString(String str) {
str = LEGACY_HEX_PATTERN.matcher(str).replaceAll("<#${hex}>");
str = LEGACY_UGLY_HEX_PATTERN.matcher(str).replaceAll("<#${h1}${h2}${h3}${h4}${h5}${h6}>");

str = replaceLegacyWithMiniMessage("&", str);
str = replaceLegacyWithMiniMessage("§", str);

return str;
}

private static String replaceLegacyWithMiniMessage(String prefix, String str) {
for (Map.Entry<Character, String> entry : MINIMESSAGE_REPLACEMENTS.entrySet()) {
str = str.replace(prefix + entry.getKey(), "<" + entry.getValue() + ">");
}

return str;
return PistonSerializersRelocated.miniMessage.serialize(PistonSerializersRelocated.sectionRGB.deserialize(
PistonSerializersRelocated.sectionRGB.serialize(PistonSerializersRelocated.ampersandRGB.deserialize(str))));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class PistonSerializersRelocated {
public static final MiniMessage miniMessage = MiniMessage.miniMessage();
public static final LegacyComponentSerializer section = LegacyComponentSerializer.builder().character('§').build();
public static final LegacyComponentSerializer sectionRGB = LegacyComponentSerializer.builder().character('§').hexCharacter('#').hexColors().build();
public static final LegacyComponentSerializer ampersandRGB = LegacyComponentSerializer.builder().character('&').hexCharacter('#').hexColors().build();
public static final GsonComponentSerializer gsonSerializer = GsonComponentSerializer.gson();
public static final GsonComponentSerializer gsonDownSamplingSerializer = GsonComponentSerializer.colorDownsamplingGson();
Expand Down

0 comments on commit 8789b19

Please sign in to comment.