Skip to content

Commit

Permalink
Fix custom message colors config not working (#5705)
Browse files Browse the repository at this point in the history
No linked issue; discussed on Discord with @Evidentsinger14

Also @JRoy, I refuse to believe you ever tested this feature, you're
trolling

---------

Co-authored-by: MD <1917406+mdcfe@users.noreply.github.com>
  • Loading branch information
pop4959 and mdcfe committed Feb 24, 2024
1 parent 09fa646 commit 9be27ad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ public void reload() {
final PluginManager pm = getServer().getPluginManager();
registerListeners(pm);

AdventureUtil.setEss(this);
bukkitAudience = BukkitAudiences.create(this);
}

Expand Down
24 changes: 13 additions & 11 deletions Essentials/src/main/java/com/earth2me/essentials/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
public class Settings implements net.ess3.api.ISettings {
private static final BigDecimal DEFAULT_MAX_MONEY = new BigDecimal("10000000000000");
private static final BigDecimal DEFAULT_MIN_MONEY = new BigDecimal("-10000000000000");
private static final Tag DEFAULT_PRIMARY_COLOR = Tag.styling(NamedTextColor.GOLD);
private static final Tag DEFAULT_SECONDARY_COLOR = Tag.styling(NamedTextColor.RED);
private final transient EssentialsConfiguration config;
private final transient IEssentials ess;
private final transient AtomicInteger reloadCount = new AtomicInteger(0);
Expand Down Expand Up @@ -141,8 +143,8 @@ public class Settings implements net.ess3.api.ISettings {
private double maxProjectileSpeed;
private boolean removeEffectsOnHeal;
private Map<String, String> worldAliases;
private Tag primaryColor = Tag.styling(NamedTextColor.GOLD);
private Tag secondaryColor = Tag.styling(NamedTextColor.RED);
private Tag primaryColor = DEFAULT_PRIMARY_COLOR;
private Tag secondaryColor = DEFAULT_SECONDARY_COLOR;

public Settings(final IEssentials ess) {
this.ess = ess;
Expand Down Expand Up @@ -1970,7 +1972,8 @@ public Tag getPrimaryColor() {

private Tag _getPrimaryColor() {
final String color = config.getString("message-colors.primary", "#ffaa00");
return Tag.styling(_getTagColor(color, NamedTextColor.GOLD));
final TextColor textColor = _getTagColor(color);
return textColor != null ? Tag.styling(textColor) : DEFAULT_PRIMARY_COLOR;
}

@Override
Expand All @@ -1980,24 +1983,23 @@ public Tag getSecondaryColor() {

private Tag _getSecondaryColor() {
final String color = config.getString("message-colors.secondary", "#ff5555");
return Tag.styling(_getTagColor(color, NamedTextColor.RED));
final TextColor textColor = _getTagColor(color);
return textColor != null ? Tag.styling(textColor) : DEFAULT_SECONDARY_COLOR;
}

private TextColor _getTagColor(final String color, final TextColor def) {
private TextColor _getTagColor(final String color) {
try {
if (color.startsWith("#") && color.length() == 7 && NumberUtil.isNumeric(color.substring(1))) {
if (color.startsWith("#") && color.length() == 7 && NumberUtil.isHexadecimal(color.substring(1))) {
return TextColor.color(Color.fromRGB(Integer.decode(color)).asRGB());
}

if (color.length() == 1) {
final NamedTextColor named = AdventureUtil.fromChar(color.charAt(0));
return named != null ? named : def;
return AdventureUtil.fromChar(color.charAt(0));
}

final NamedTextColor named = NamedTextColor.NAMES.value(color.toLowerCase(Locale.ENGLISH));
return named != null ? named : def;
return NamedTextColor.NAMES.value(color.toLowerCase(Locale.ENGLISH));
} catch (IllegalArgumentException ignored) {
}
return def;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,43 @@

public final class AdventureUtil {
private static final LegacyComponentSerializer LEGACY_SERIALIZER;
private static final MiniMessage MINI_MESSAGE_INSTANCE;
private static IEssentials ess;
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 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 IEssentials ess;
private static MiniMessage miniMessageInstance;

static {
final LegacyComponentSerializer.Builder builder = LegacyComponentSerializer.builder().flattener(ComponentFlattener.basic()).useUnusualXRepeatedCharacterHexFormat();
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_16_1_R01)) {
builder.hexColors();
}
LEGACY_SERIALIZER = builder.build();

MINI_MESSAGE_INSTANCE = MiniMessage.builder()
.tags(TagResolver.builder()
.resolvers(TagResolver.standard())
.resolver(TagResolver.resolver("primary", supplyTag(true)))
.resolver(TagResolver.resolver("secondary", supplyTag(false)))
.build())
.build();

miniMessageInstance = createMiniMessageInstance();
}

private AdventureUtil() {
}

public static void setEss(final IEssentials ess) {
AdventureUtil.ess = ess;
miniMessageInstance = createMiniMessageInstance();
}

private static MiniMessage createMiniMessageInstance() {
return MiniMessage.builder()
.tags(TagResolver.builder()
.resolvers(TagResolver.standard())
.resolver(TagResolver.resolver("primary", supplyTag(true)))
.resolver(TagResolver.resolver("secondary", supplyTag(false)))
.build())
.build();
}

public static MiniMessage miniMessage() {
return MINI_MESSAGE_INSTANCE;
return miniMessageInstance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ public static boolean isNumeric(final String sNum) {
return true;
}

public static boolean isHexadecimal(final String sNum) {
try {
Integer.parseInt(sNum, 16);
return true;
} catch (final NumberFormatException e) {
return false;
}
}

/**
* Backport from Guava.
*/
Expand Down

0 comments on commit 9be27ad

Please sign in to comment.