Skip to content

Commit 7e13159

Browse files
committed
support hex colors in lore (#68)
1 parent 9412dc0 commit 7e13159

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,11 @@
150150
<version>0.4.4</version>
151151
<scope>compile</scope>
152152
</dependency>
153+
<dependency>
154+
<groupId>net.md-5</groupId>
155+
<artifactId>bungeecord-chat</artifactId>
156+
<version>1.19-R0.1-SNAPSHOT</version>
157+
<scope>provided</scope>
158+
</dependency>
153159
</dependencies>
154160
</project>

src/main/java/lol/hyper/toolstats/tools/ItemLore.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import lol.hyper.toolstats.ToolStats;
2121
import org.bukkit.ChatColor;
22-
import org.bukkit.Material;
23-
import org.bukkit.inventory.meta.Damageable;
2422
import org.bukkit.inventory.meta.ItemMeta;
2523
import org.bukkit.persistence.PersistentDataContainer;
2624
import org.bukkit.persistence.PersistentDataType;

src/main/java/lol/hyper/toolstats/tools/config/ConfigTools.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import org.bukkit.ChatColor;
2222
import org.bukkit.Material;
2323

24+
import java.util.regex.Matcher;
2425
import java.util.regex.Pattern;
2526

2627
public class ConfigTools {
2728

2829
private final ToolStats toolStats;
29-
private final Pattern COLOR_CODES = Pattern.compile("(?i)&[0-9A-FK-ORX]");
30+
private final Pattern COLOR_CODES = Pattern.compile("&([0-9a-fk-or])");
31+
private final Pattern HEX_PATTERN = Pattern.compile("&#([A-Fa-f0-9]{6})");
3032

3133
public ConfigTools(ToolStats toolStats) {
3234
this.toolStats = toolStats;
@@ -110,7 +112,7 @@ public boolean checkConfig(Material material, String configName) {
110112
* Gets the lore message from the config.
111113
*
112114
* @param configName The config name, "messages." is already in front.
113-
* @param raw If you want the raw message with the formatting codes and placeholders.
115+
* @param raw If you want the raw message. False if you want no placeholders.
114116
* @return The lore message.
115117
*/
116118
public String getLoreFromConfig(String configName, boolean raw) {
@@ -119,13 +121,26 @@ public String getLoreFromConfig(String configName, boolean raw) {
119121
return null;
120122
}
121123
if (raw) {
122-
return ChatColor.translateAlternateColorCodes('&', lore);
124+
Matcher hexMatcher = HEX_PATTERN.matcher(lore);
125+
while (hexMatcher.find()) {
126+
String hexCode = hexMatcher.group(1);
127+
lore = lore.replaceAll(hexMatcher.group(), net.md_5.bungee.api.ChatColor.of("#" + hexCode).toString());
128+
}
129+
130+
Matcher colorMatcher = COLOR_CODES.matcher(lore);
131+
while (colorMatcher.find()) {
132+
String colorCode = colorMatcher.group(1);
133+
lore = lore.replaceAll("&" + colorCode, ChatColor.getByChar(colorCode).toString());
134+
}
135+
136+
return ChatColor.translateAlternateColorCodes('§', lore);
123137
} else {
124138
// remove all color codes
125139
// this is used to compare the current lore on the item
126140
// Example: [§7Arrows shot: §8] is on the lore
127141
// this will return [Arrows shot: ] so we can match it
128142
lore = COLOR_CODES.matcher(lore).replaceAll("");
143+
lore = HEX_PATTERN.matcher(lore).replaceAll("");
129144
if (lore.contains("{player}")) {
130145
lore = lore.replace("{player}", "");
131146
}

0 commit comments

Comments
 (0)