Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colors class changes from minimessage branch #6132

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/com/palmergames/bukkit/towny/TownyMessaging.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public static void sendTitleMessageToNation(Nation nation, String title, String

/**
* Send the player a Title message
*
* <p>
* As of MC 1.18 a null title will mean the message is never sent, so we are
* changing empty Strings to " ".
*
Expand Down Expand Up @@ -787,7 +787,7 @@ public static void sendPrefixedNationMessage(Nation nation, Translatable message
LOGGER.info(ChatTools.stripColour("[Nation Msg] " + StringMgmt.remUnderscore(nation.getName()) + ": " + message.translate()));

for (Player player : TownyAPI.getInstance().getOnlinePlayers(nation))
sendMessage(player, Translation.translateTranslatables(player, "", Translatable.of("default_nation_prefix", StringMgmt.remUnderscore(nation.getName())), message));
sendMessage(player, Translatable.of("default_nation_prefix", StringMgmt.remUnderscore(nation.getName())).append(message));
}

/**
Expand All @@ -801,7 +801,7 @@ public static void sendPrefixedTownMessage(Town town, Translatable message) {
LOGGER.info(ChatTools.stripColour("[Town Msg] " + StringMgmt.remUnderscore(town.getName()) + ": " + message.translate()));

for (Player player : TownyAPI.getInstance().getOnlinePlayers(town))
sendMessage(player, Translation.translateTranslatables(player, "", Translatable.of("default_town_prefix", StringMgmt.remUnderscore(town.getName())), message));
sendMessage(player, Translatable.of("default_town_prefix", StringMgmt.remUnderscore(town.getName())).append(message));
}

/**
Expand Down
144 changes: 104 additions & 40 deletions src/com/palmergames/bukkit/util/Colors.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@

import net.kyori.adventure.text.format.NamedTextColor;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Colors {

private static final Map<String, String> legacyLookupMap = new HashMap<>();
private static final Pattern legacyPattern = Pattern.compile("[§&][0-9a-fk-or]");

/*
* Legacy colors
*/

public static final String Black = "\u00A70";
public static final String Navy = "\u00A71";
public static final String Green = "\u00A72";
Expand All @@ -29,57 +42,82 @@ public class Colors {
public static final String Yellow = "\u00A7e";
public static final String White = "\u00A7f";

/*
* Minimessage colors
*/

public static final String DARK_RED = "<dark_red>";
public static final String RED = "<red>";
public static final String GOLD = "<gold>";
public static final String YELLOW = "<yellow>";
public static final String DARK_GREEN = "<dark_green>";
public static final String GREEN = "<green>";
public static final String DARK_AQUA = "<dark_aqua>";
public static final String AQUA = "<aqua>";
public static final String DARK_BLUE = "<dark_blue>";
public static final String BLUE = "<blue>";
public static final String LIGHT_PURPLE = "<light_purple>";
public static final String DARK_PURPLE = "<dark_purple>";
public static final String WHITE = "<white>";
public static final String GRAY = "<gray>";
public static final String DARK_GRAY = "<dark_gray>";
public static final String BLACK = "<black>";

public static String strip(String line) {
return ChatColor.stripColor(line);
return ChatColor.stripColor(line); // TODO: Strip minimessage tags once dependency is added
}

public static String translateColorCodes(String str) {
String out = ChatColor.translateAlternateColorCodes('&', str);
return StringMgmt.translateHexColors(out);
return StringMgmt.translateHexColors(ChatColor.translateAlternateColorCodes('&', str));
}

public static String translateLegacyCharacters(String input) {
final Matcher matcher = legacyPattern.matcher(input);

while (matcher.find()) {
String legacy = matcher.group();
input = input.replace(legacy, legacyLookupMap.getOrDefault(legacy.substring(1), legacy));
}

return input;
}

private static final Function<String, String> modernHexFunction = (hex) -> "<" + hex + ">";

/**
* Converts non-minimessage hex formats to minimessage.
* @param input The input that may or may not contain hex.
* @return The input, with the minimessage hex format.
*/
public static String translateLegacyHex(String input) {
return StringMgmt.translateHexColors(input, modernHexFunction);
}

/**
* @param colorCode The color code.
* @param colorCode A legacy or MiniMessage color code.
* @return the {@link NamedTextColor} for the entered color string, or null if it is invalid.
*/
@Nullable
public static NamedTextColor toNamedTextColor(String colorCode) {
switch(colorCode) {
case "\u00A70":
return NamedTextColor.BLACK;
case "\u00A71":
return NamedTextColor.DARK_BLUE;
case "\u00A72":
return NamedTextColor.DARK_GREEN;
case "\u00A73":
return NamedTextColor.DARK_AQUA;
case "\u00A74":
return NamedTextColor.DARK_RED;
case "\u00A75":
return NamedTextColor.DARK_PURPLE;
case "\u00A76":
return NamedTextColor.GOLD;
case "\u00A77":
return NamedTextColor.GRAY;
case "\u00A78":
return NamedTextColor.DARK_GRAY;
case "\u00A79":
return NamedTextColor.BLUE;
case "\u00A7a":
return NamedTextColor.GREEN;
case "\u00A7b":
return NamedTextColor.AQUA;
case "\u00A7c":
return NamedTextColor.RED;
case "\u00A7d":
return NamedTextColor.LIGHT_PURPLE;
case "\u00A7e":
return NamedTextColor.YELLOW;
case "\u00A7f":
return NamedTextColor.WHITE;
default:
return null;
}
return switch (colorCode) {
case "\u00A70", BLACK -> NamedTextColor.BLACK;
case "\u00A71", DARK_BLUE -> NamedTextColor.DARK_BLUE;
case "\u00A72", DARK_GREEN -> NamedTextColor.DARK_GREEN;
case "\u00A73", DARK_AQUA -> NamedTextColor.DARK_AQUA;
case "\u00A74", DARK_RED -> NamedTextColor.DARK_RED;
case "\u00A75", DARK_PURPLE -> NamedTextColor.DARK_PURPLE;
case "\u00A76", GOLD -> NamedTextColor.GOLD;
case "\u00A77", GRAY -> NamedTextColor.GRAY;
case "\u00A78", DARK_GRAY -> NamedTextColor.DARK_GRAY;
case "\u00A79", BLUE -> NamedTextColor.BLUE;
case "\u00A7a", GREEN -> NamedTextColor.GREEN;
case "\u00A7b", AQUA -> NamedTextColor.AQUA;
case "\u00A7c", RED -> NamedTextColor.RED;
case "\u00A7d", LIGHT_PURPLE -> NamedTextColor.LIGHT_PURPLE;
case "\u00A7e", YELLOW -> NamedTextColor.YELLOW;
case "\u00A7f", WHITE -> NamedTextColor.WHITE;
default -> null;
};
}

public static String colorTown(String townName) {
Expand All @@ -105,4 +143,30 @@ public static String getTownColor() {
public static String getNationColor() {
return TownySettings.getPAPIFormattingKing();
}

static {
legacyLookupMap.put("4", DARK_RED);
legacyLookupMap.put("c", RED);
legacyLookupMap.put("6", GOLD);
legacyLookupMap.put("e", YELLOW);
legacyLookupMap.put("2", DARK_GREEN);
legacyLookupMap.put("a", GREEN);
legacyLookupMap.put("3", DARK_AQUA);
legacyLookupMap.put("b", AQUA);
legacyLookupMap.put("1", DARK_BLUE);
legacyLookupMap.put("9", BLUE);
legacyLookupMap.put("d", LIGHT_PURPLE);
legacyLookupMap.put("5", DARK_PURPLE);
legacyLookupMap.put("f", WHITE);
legacyLookupMap.put("7", GRAY);
legacyLookupMap.put("8", DARK_GRAY);
legacyLookupMap.put("0", BLACK);

legacyLookupMap.put("k", "<obfuscated>");
legacyLookupMap.put("l", "<bold>");
legacyLookupMap.put("m", "<strikethrough>");
legacyLookupMap.put("n", "<underline>");
legacyLookupMap.put("o", "<italic>");
legacyLookupMap.put("r", "<reset>");
}
}
38 changes: 16 additions & 22 deletions src/com/palmergames/util/StringMgmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -26,34 +28,26 @@

public class StringMgmt {

public static final Pattern hexPattern = Pattern.compile("(?<!\\\\)(#[a-fA-F0-9]{6})");
public static final Pattern ampersandPattern = Pattern.compile("(?<!\\\\)(&#[a-fA-F0-9]{6})");
public static final Pattern bracketPattern = Pattern.compile("(?<!\\\\)\\{(#[a-fA-F0-9]{6})}");
public static String translateHexColors(String str) {
public static final Pattern hexPattern = Pattern.compile("(?<!\\\\)((&|\\{|)#[a-fA-F0-9]{6})(}|)");
public static final @Deprecated Pattern ampersandPattern = Pattern.compile("(?<!\\\\)(&#[a-fA-F0-9]{6})");
public static final @Deprecated Pattern bracketPattern = Pattern.compile("(?<!\\\\)\\{(#[a-fA-F0-9]{6})}");

private static final Function<String, String> legacyHexFunction = (hex) -> ChatColor.of(hex).toString();

final Matcher hexMatcher = hexPattern.matcher(str);
final Matcher ampMatcher = ampersandPattern.matcher(str);
final Matcher bracketMatcher = bracketPattern.matcher(str);
public static String translateHexColors(String string) {
return translateHexColors(string, legacyHexFunction);
}

@ApiStatus.Internal
public static String translateHexColors(String string, Function<String, String> hexFunction) {
final Matcher hexMatcher = hexPattern.matcher(string);

while (hexMatcher.find()) {
String hex = hexMatcher.group();
str = str.replace(hex, ChatColor.of(hex).toString());
}

while (ampMatcher.find()) {
String hex = ampMatcher.group().replace("&", "");
str = str.replace(hex, ChatColor.of(hex).toString());
str = str.replace("&", "");
}

while (bracketMatcher.find()) {
String hex = bracketMatcher.group().replace("{", "").replace("}", "");
str = str.replace(hex, ChatColor.of(hex).toString());
str = str.replace("{", "").replace("}", "");
string = string.replace(hex, hexFunction.apply(hex.replaceAll("[&{}]", "")));
}

return str;
return string;
}

public static String join(Collection<?> args) {
Expand Down