From 1878e7eb5c400982db36c193b67b5f10915d6be9 Mon Sep 17 00:00:00 2001 From: Roch Blonndiaux Date: Wed, 9 Nov 2022 17:02:29 +0100 Subject: [PATCH] fixed --- .../net/Indyuce/mmoitems/stat/DisplayName.java | 6 +++--- .../java/net/Indyuce/mmoitems/util/ColorUtils.java | 14 +++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/DisplayName.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/DisplayName.java index 91fcadef..4da7a011 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/DisplayName.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/DisplayName.java @@ -33,13 +33,13 @@ public class DisplayName extends StringStat implements GemStoneStat { // Bake String format = data.toString() .replace("", tier != null ? ColorUtils.stripColors(tier.getName()) : "") - .replace("", tier != null ? ChatColor.getLastColors(tier.getName()) : "&f") - .replace("", tier != null ? ColorUtils.stripDecoration(ChatColor.getLastColors(tier.getName())) : "&f"); + .replace("", tier != null ? ColorUtils.getLastColors(tier.getName()) : "&f") + .replace("", tier != null ? ColorUtils.stripDecoration(ColorUtils.getLastColors(tier.getName())) : "&f"); if (tier != null) { System.out.println("Name: " + tier.getName()); System.out.println("Format: " + format); - System.out.println("Last Colors: " + ChatColor.getLastColors(tier.getName()) + " w: " + ColorUtils.stripDecoration(ChatColor.getLastColors(tier.getName()))); + System.out.println("Last Colors: " + ColorUtils.getLastColors(tier.getName()) + " w: " + ColorUtils.stripDecoration(ChatColor.getLastColors(tier.getName()))); System.out.println("===================\n"); } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/ColorUtils.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/ColorUtils.java index f4a38a71..e7cccc41 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/ColorUtils.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/ColorUtils.java @@ -3,6 +3,7 @@ package net.Indyuce.mmoitems.util; import org.bukkit.ChatColor; import org.jetbrains.annotations.NotNull; +import java.util.regex.Matcher; import java.util.regex.Pattern; /** @@ -15,12 +16,23 @@ public class ColorUtils { private static final Pattern STRIP_DECORATION_PATTERN = Pattern.compile("(?i)" + '§' + "[K-O]"); private static final Pattern COLOR_TAG_PATTERN = Pattern.compile("(?i)<.*>"); + private static final Pattern START_COLOR_TAG_PATTERN = Pattern.compile("(?i)<[^/]*>"); + private static final Pattern MINI_MSG_DECORATION_PATTERN = Pattern.compile("(?i)(<|"); public static @NotNull String stripDecoration(@NotNull String input) { - return "%s%s".formatted(ChatColor.RESET, STRIP_DECORATION_PATTERN.matcher(input).replaceAll("")).replace('§', '&'); + return "%s%s".formatted(ChatColor.RESET, MINI_MSG_DECORATION_PATTERN.matcher(STRIP_DECORATION_PATTERN.matcher(input).replaceAll("")).replaceAll("")) + .replace('§', '&'); } public static @NotNull String stripColors(@NotNull String input) { return ChatColor.stripColor(COLOR_TAG_PATTERN.matcher(input).replaceAll("")); } + + public static @NotNull String getLastColors(@NotNull String input) { + Matcher matcher = START_COLOR_TAG_PATTERN.matcher(input); + String lastMatch = null; + while (matcher.find()) + lastMatch = matcher.group(); + return lastMatch == null ? ChatColor.getLastColors(input) : lastMatch; + } }