Skip to content

Commit 9ee1f77

Browse files
committed
handle lore better for tokens
1 parent 61f87a9 commit 9ee1f77

File tree

3 files changed

+73
-50
lines changed

3 files changed

+73
-50
lines changed

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

+33
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
2525
import org.bukkit.Material;
2626

27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.List;
2730
import java.util.regex.Matcher;
2831
import java.util.regex.Pattern;
2932

@@ -162,4 +165,34 @@ public Component format(String configName) {
162165

163166
return component.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
164167
}
168+
169+
/**
170+
* Get the token item's lore from config.
171+
*
172+
* @param tokenType The type.
173+
* @return The lore.
174+
*/
175+
public List<Component> getTokenLore(String tokenType) {
176+
List<String> raw = toolStats.config.getStringList("tokens.data." + tokenType + ".lore");
177+
if (raw.isEmpty()) {
178+
return Collections.emptyList();
179+
}
180+
181+
List<Component> finalLore = new ArrayList<>();
182+
for (String line : raw) {
183+
Component component;
184+
// if we match the old color codes, then format them as so
185+
Matcher hexMatcher = CONFIG_HEX_PATTERN.matcher(line);
186+
Matcher colorMatcher = COLOR_CODES.matcher(line);
187+
if (hexMatcher.find() || colorMatcher.find()) {
188+
component = LegacyComponentSerializer.legacyAmpersand().deserialize(line);
189+
} else {
190+
// otherwise format them normally
191+
component = MiniMessage.miniMessage().deserialize(line);
192+
}
193+
component = component.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
194+
finalLore.add(component);
195+
}
196+
return finalLore;
197+
}
165198
}

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

+20-40
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ public ItemStack playerKills() {
4444

4545
// set the title and lore
4646
Component title = toolStats.configTools.format("tokens.data.player-kills.title");
47-
Component lore = toolStats.configTools.format("tokens.data.player-kills.lore");
47+
List<Component> lore = toolStats.configTools.getTokenLore("player-kills");
4848
tokenMeta.displayName(title);
49-
List<Component> newLore = new ArrayList<>();
50-
newLore.add(lore);
51-
tokenMeta.lore(newLore);
49+
tokenMeta.lore(lore);
5250

5351
// set the PDC
5452
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "player-kills");
@@ -64,11 +62,9 @@ public ItemStack mobKills() {
6462

6563
// set the title and lore
6664
Component title = toolStats.configTools.format("tokens.data.mob-kills.title");
67-
Component lore = toolStats.configTools.format("tokens.data.mob-kills.lore");
65+
List<Component> lore = toolStats.configTools.getTokenLore("mob-kills");
6866
tokenMeta.displayName(title);
69-
List<Component> newLore = new ArrayList<>();
70-
newLore.add(lore);
71-
tokenMeta.lore(newLore);
67+
tokenMeta.lore(lore);
7268

7369
// set the PDC
7470
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "mob-kills");
@@ -84,11 +80,9 @@ public ItemStack blocksMined() {
8480

8581
// set the title and lore
8682
Component title = toolStats.configTools.format("tokens.data.blocks-mined.title");
87-
Component lore = toolStats.configTools.format("tokens.data.blocks-mined.lore");
83+
List<Component> lore = toolStats.configTools.getTokenLore("blocks-mined");
8884
tokenMeta.displayName(title);
89-
List<Component> newLore = new ArrayList<>();
90-
newLore.add(lore);
91-
tokenMeta.lore(newLore);
85+
tokenMeta.lore(lore);
9286

9387
// set the PDC
9488
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "blocks-mined");
@@ -104,11 +98,9 @@ public ItemStack cropsMined() {
10498

10599
// set the title and lore
106100
Component title = toolStats.configTools.format("tokens.data.crops-mined.title");
107-
Component lore = toolStats.configTools.format("tokens.data.crops-mined.lore");
101+
List<Component> lore = toolStats.configTools.getTokenLore("crops-mined");
108102
tokenMeta.displayName(title);
109-
List<Component> newLore = new ArrayList<>();
110-
newLore.add(lore);
111-
tokenMeta.lore(newLore);
103+
tokenMeta.lore(lore);
112104

113105
// set the PDC
114106
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "crops-mined");
@@ -124,11 +116,9 @@ public ItemStack fishCaught() {
124116

125117
// set the title and lore
126118
Component title = toolStats.configTools.format("tokens.data.fish-caught.title");
127-
Component lore = toolStats.configTools.format("tokens.data.fish-caught.lore");
119+
List<Component> lore = toolStats.configTools.getTokenLore("fight-caught");
128120
tokenMeta.displayName(title);
129-
List<Component> newLore = new ArrayList<>();
130-
newLore.add(lore);
131-
tokenMeta.lore(newLore);
121+
tokenMeta.lore(lore);
132122

133123
// set the PDC
134124
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "fish-caught");
@@ -144,11 +134,9 @@ public ItemStack sheepSheared() {
144134

145135
// set the title and lore
146136
Component title = toolStats.configTools.format("tokens.data.sheep-sheared.title");
147-
Component lore = toolStats.configTools.format("tokens.data.sheep-sheared.lore");
137+
List<Component> lore = toolStats.configTools.getTokenLore("sheep-sheared");
148138
tokenMeta.displayName(title);
149-
List<Component> newLore = new ArrayList<>();
150-
newLore.add(lore);
151-
tokenMeta.lore(newLore);
139+
tokenMeta.lore(lore);
152140

153141
// set the PDC
154142
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "sheep-sheared");
@@ -164,11 +152,9 @@ public ItemStack damageTaken() {
164152

165153
// set the title and lore
166154
Component title = toolStats.configTools.format("tokens.data.damage-taken.title");
167-
Component lore = toolStats.configTools.format("tokens.data.damage-taken.lore");
155+
List<Component> lore = toolStats.configTools.getTokenLore("damage-taken");
168156
tokenMeta.displayName(title);
169-
List<Component> newLore = new ArrayList<>();
170-
newLore.add(lore);
171-
tokenMeta.lore(newLore);
157+
tokenMeta.lore(lore);
172158

173159
// set the PDC
174160
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "damage-taken");
@@ -184,11 +170,9 @@ public ItemStack arrowsShot() {
184170

185171
// set the title and lore
186172
Component title = toolStats.configTools.format("tokens.data.arrows-shot.title");
187-
Component lore = toolStats.configTools.format("tokens.data.arrows-shot.lore");
173+
List<Component> lore = toolStats.configTools.getTokenLore("arrows-shot");
188174
tokenMeta.displayName(title);
189-
List<Component> newLore = new ArrayList<>();
190-
newLore.add(lore);
191-
tokenMeta.lore(newLore);
175+
tokenMeta.lore(lore);
192176

193177
// set the PDC
194178
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "arrows-shot");
@@ -204,11 +188,9 @@ public ItemStack flightTime() {
204188

205189
// set the title and lore
206190
Component title = toolStats.configTools.format("tokens.data.flight-time.title");
207-
Component lore = toolStats.configTools.format("tokens.data.flight-time.lore");
191+
List<Component> lore = toolStats.configTools.getTokenLore("flight-time");
208192
tokenMeta.displayName(title);
209-
List<Component> newLore = new ArrayList<>();
210-
newLore.add(lore);
211-
tokenMeta.lore(newLore);
193+
tokenMeta.lore(lore);
212194

213195
// set the PDC
214196
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "flight-time");
@@ -224,11 +206,9 @@ public ItemStack resetToken() {
224206

225207
// set the title and lore
226208
Component title = toolStats.configTools.format("tokens.data.reset.title");
227-
Component lore = toolStats.configTools.format("tokens.data.reset.lore");
209+
List<Component> lore = toolStats.configTools.getTokenLore("reset");
228210
tokenMeta.displayName(title);
229-
List<Component> newLore = new ArrayList<>();
230-
newLore.add(lore);
231-
tokenMeta.lore(newLore);
211+
tokenMeta.lore(lore);
232212

233213
// set the PDC
234214
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "reset");

src/main/resources/config.yml

+20-10
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,53 @@ tokens:
66
data:
77
player-kills:
88
title: "&7ToolStats: &8Player Kills Token"
9-
lore: "&8Combine with a melee or ranged weapon in an anvil to track player kills."
9+
lore:
10+
- "&8Combine with a melee or ranged weapon in an anvil to track player kills."
1011
levels: 1
1112
mob-kills:
1213
title: "&7ToolStats: &8Mob Kills Token"
13-
lore: "&8Combine with a melee or ranged weapon in an anvil to track mob kills."
14+
lore:
15+
- "&8Combine with a melee or ranged weapon in an anvil to track mob kills."
1416
levels: 1
1517
blocks-mined:
1618
title: "&7ToolStats: &8Blocks Mined Token"
17-
lore: "&8Combine with a pickaxe, axe, shovel, or shears in an anvil to track blocks mined."
19+
lore:
20+
- "&8Combine with a pickaxe, axe, shovel, or shears in an anvil to track blocks mined."
1821
levels: 1
1922
crops-mined:
2023
title: "&7ToolStats: &8Crops Mined Token"
21-
lore: "&8Combine with a hoe in an anvil to track crops broken."
24+
lore:
25+
- "&8Combine with a hoe in an anvil to track crops broken."
2226
levels: 1
2327
fish-caught:
2428
title: "&7ToolStats: &8Fish Caught Token"
25-
lore: "&8Combine with a fishing rod in an anvil to track fish caught."
29+
lore:
30+
- "&8Combine with a fishing rod in an anvil to track fish caught."
2631
levels: 1
2732
sheep-sheared:
2833
title: "&7ToolStats: &8Sheep Sheared Token"
29-
lore: "&8Combine with shears in an anvil to track sheep sheared."
34+
lore:
35+
- "&8Combine with shears in an anvil to track sheep sheared."
3036
levels: 1
3137
damage-taken:
3238
title: "&7ToolStats: &8Damage Taken Token"
33-
lore: "&8Combine with an armor piece in an anvil to track damage taken."
39+
lore:
40+
- "&8Combine with an armor piece in an anvil to track damage taken."
3441
levels: 1
3542
arrows-shot:
3643
title: "&7ToolStats: &8Arrows Shot Token"
37-
lore: "&8Combine with a bow or crossbow in an anvil to track arrows shot."
44+
lore:
45+
- "&8Combine with a bow or crossbow in an anvil to track arrows shot."
3846
levels: 1
3947
flight-time:
4048
title: "&7ToolStats: &8Flight Time Token"
41-
lore: "&8Combine with an elytra in an anvil to track flight time."
49+
lore:
50+
- "&8Combine with an elytra in an anvil to track flight time."
4251
levels: 1
4352
reset:
4453
title: "&7ToolStats: &8Reset Token"
45-
lore: "&8Combine in an anvil with to reset ALL stats for this item. Tokens on this item stay."
54+
lore:
55+
- "&8Combine in an anvil with to reset ALL stats for this item. Tokens on this item stay."
4656
levels: 1
4757

4858
enabled:

0 commit comments

Comments
 (0)