fix resolving hex primary/secondary

This commit is contained in:
Christopher Bohn 2024-02-23 22:44:57 -08:00
parent 1929d4110a
commit 88bc1e99ce
3 changed files with 27 additions and 19 deletions

View File

@ -53,6 +53,8 @@ import static com.earth2me.essentials.I18n.tlLiteral;
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 Tag DEFAULT_PRIMARY_COLOR = Tag.styling(NamedTextColor.GOLD);
private 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);
@ -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;
@ -1970,7 +1972,8 @@ public class Settings implements net.ess3.api.ISettings {
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
@ -1980,24 +1983,23 @@ public class Settings implements net.ess3.api.ISettings {
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;
}
}

View File

@ -1,6 +1,6 @@
package com.earth2me.essentials.utils;
import net.ess3.api.IEssentials;
import com.earth2me.essentials.Essentials;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.text.format.NamedTextColor;
@ -9,6 +9,7 @@ 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 org.bukkit.plugin.java.JavaPlugin;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -16,7 +17,6 @@ import java.util.regex.Pattern;
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";
@ -43,10 +43,6 @@ public final class AdventureUtil {
private AdventureUtil() {
}
public static void setEss(final IEssentials ess) {
AdventureUtil.ess = ess;
}
public static MiniMessage miniMessage() {
return MINI_MESSAGE_INSTANCE;
}
@ -141,10 +137,11 @@ public final class AdventureUtil {
}
private static Tag supplyTag(final boolean primary) {
final Essentials ess = JavaPlugin.getPlugin(Essentials.class);
if (primary) {
return ess != null ? ess.getSettings().getPrimaryColor() : Tag.styling(NamedTextColor.GOLD);
return ess.getSettings().getPrimaryColor();
}
return ess != null ? ess.getSettings().getSecondaryColor() : Tag.styling(NamedTextColor.RED);
return ess.getSettings().getSecondaryColor();
}
public static class ParsedPlaceholder {

View File

@ -129,6 +129,15 @@ public final class NumberUtil {
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.
*/