support hex colors in lore (#68)

This commit is contained in:
hyperdefined
2024-08-28 23:20:16 -04:00
parent 9412dc02d7
commit 7e13159056
3 changed files with 24 additions and 5 deletions

View File

@@ -150,5 +150,11 @@
<version>0.4.4</version> <version>0.4.4</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-chat</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -19,8 +19,6 @@ package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats; import lol.hyper.toolstats.ToolStats;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;

View File

@@ -21,12 +21,14 @@ import lol.hyper.toolstats.ToolStats;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class ConfigTools { public class ConfigTools {
private final ToolStats toolStats; private final ToolStats toolStats;
private final Pattern COLOR_CODES = Pattern.compile("(?i)&[0-9A-FK-ORX]"); private final Pattern COLOR_CODES = Pattern.compile("&([0-9a-fk-or])");
private final Pattern HEX_PATTERN = Pattern.compile("&#([A-Fa-f0-9]{6})");
public ConfigTools(ToolStats toolStats) { public ConfigTools(ToolStats toolStats) {
this.toolStats = toolStats; this.toolStats = toolStats;
@@ -110,7 +112,7 @@ public class ConfigTools {
* Gets the lore message from the config. * Gets the lore message from the config.
* *
* @param configName The config name, "messages." is already in front. * @param configName The config name, "messages." is already in front.
* @param raw If you want the raw message with the formatting codes and placeholders. * @param raw If you want the raw message. False if you want no placeholders.
* @return The lore message. * @return The lore message.
*/ */
public String getLoreFromConfig(String configName, boolean raw) { public String getLoreFromConfig(String configName, boolean raw) {
@@ -119,13 +121,26 @@ public class ConfigTools {
return null; return null;
} }
if (raw) { if (raw) {
return ChatColor.translateAlternateColorCodes('&', lore); Matcher hexMatcher = HEX_PATTERN.matcher(lore);
while (hexMatcher.find()) {
String hexCode = hexMatcher.group(1);
lore = lore.replaceAll(hexMatcher.group(), net.md_5.bungee.api.ChatColor.of("#" + hexCode).toString());
}
Matcher colorMatcher = COLOR_CODES.matcher(lore);
while (colorMatcher.find()) {
String colorCode = colorMatcher.group(1);
lore = lore.replaceAll("&" + colorCode, ChatColor.getByChar(colorCode).toString());
}
return ChatColor.translateAlternateColorCodes('§', lore);
} else { } else {
// remove all color codes // remove all color codes
// this is used to compare the current lore on the item // this is used to compare the current lore on the item
// Example: [§7Arrows shot: §8] is on the lore // Example: [§7Arrows shot: §8] is on the lore
// this will return [Arrows shot: ] so we can match it // this will return [Arrows shot: ] so we can match it
lore = COLOR_CODES.matcher(lore).replaceAll(""); lore = COLOR_CODES.matcher(lore).replaceAll("");
lore = HEX_PATTERN.matcher(lore).replaceAll("");
if (lore.contains("{player}")) { if (lore.contains("{player}")) {
lore = lore.replace("{player}", ""); lore = lore.replace("{player}", "");
} }