Skip to content

Commit 2c971da

Browse files
committed
unify lore management
1 parent 2e0ee18 commit 2c971da

File tree

10 files changed

+317
-312
lines changed

10 files changed

+317
-312
lines changed

src/main/java/lol/hyper/toolstats/ToolStats.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import lol.hyper.githubreleaseapi.GitHubReleaseAPI;
2222
import lol.hyper.toolstats.commands.CommandToolStats;
2323
import lol.hyper.toolstats.events.*;
24+
import lol.hyper.toolstats.tools.ItemLore;
2425
import lol.hyper.toolstats.tools.NumberFormat;
2526
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2627
import org.bstats.bukkit.Metrics;
@@ -69,6 +70,7 @@ public final class ToolStats extends JavaPlugin {
6970
public SheepShear sheepShear;
7071
public VillagerTrade villagerTrade;
7172
public CommandToolStats commandToolStats;
73+
public ItemLore itemLore;
7274

7375
public final Logger logger = this.getLogger();
7476
public final File configFile = new File(this.getDataFolder(), "config.yml");
@@ -99,6 +101,7 @@ public void onEnable() {
99101
sheepShear = new SheepShear(this);
100102
villagerTrade = new VillagerTrade(this);
101103
commandToolStats = new CommandToolStats(this);
104+
itemLore = new ItemLore(this);
102105

103106
Bukkit.getServer().getPluginManager().registerEvents(blocksMined, this);
104107
Bukkit.getServer().getPluginManager().registerEvents(chunkPopulate, this);

src/main/java/lol/hyper/toolstats/events/BlocksMined.java

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
import lol.hyper.toolstats.ToolStats;
2121
import lol.hyper.toolstats.tools.ItemChecker;
22-
import org.bukkit.Bukkit;
2322
import org.bukkit.GameMode;
24-
import org.bukkit.Material;
2523
import org.bukkit.entity.Player;
2624
import org.bukkit.event.EventHandler;
2725
import org.bukkit.event.EventPriority;
@@ -33,7 +31,6 @@
3331
import org.bukkit.persistence.PersistentDataContainer;
3432
import org.bukkit.persistence.PersistentDataType;
3533

36-
import java.util.ArrayList;
3734
import java.util.List;
3835

3936
public class BlocksMined implements Listener {
@@ -60,8 +57,8 @@ public void onBreak(BlockBreakEvent event) {
6057
if (!ItemChecker.isMineTool(heldItem.getType())) {
6158
return;
6259
}
63-
// if it's an item we want, update the stats
64-
Bukkit.getScheduler().runTaskLater(toolStats, () -> updateBlocksMined(heldItem), 1);
60+
// update the blocks mined
61+
updateBlocksMined(heldItem);
6562
}
6663

6764
private void updateBlocksMined(ItemStack playerTool) {
@@ -86,40 +83,17 @@ private void updateBlocksMined(ItemStack playerTool) {
8683
blocksMined++;
8784
container.set(toolStats.genericMined, PersistentDataType.INTEGER, blocksMined);
8885

89-
String configLore = toolStats.getLoreFromConfig("blocks-mined", false);
90-
String configLoreRaw = toolStats.getLoreFromConfig("blocks-mined", true);
86+
String blocksMinedFormatted = toolStats.numberFormat.formatInt(blocksMined);
87+
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{blocks}", blocksMinedFormatted, "blocks-mined");
9188

92-
if (configLore == null || configLoreRaw == null) {
93-
toolStats.logger.warning("There is no lore message for messages.blocks-mined!");
89+
// if the list returned null, don't add it
90+
if (newLore == null) {
9491
return;
9592
}
9693

97-
List<String> lore;
98-
String newLine = configLoreRaw.replace("{blocks}", toolStats.numberFormat.formatInt(blocksMined));
99-
if (meta.hasLore()) {
100-
lore = meta.getLore();
101-
boolean hasLore = false;
102-
// we do a for loop like this, we can keep track of index
103-
// this doesn't mess the lore up of existing items
104-
for (int x = 0; x < lore.size(); x++) {
105-
if (lore.get(x).contains(configLore)) {
106-
hasLore = true;
107-
lore.set(x, newLine);
108-
break;
109-
}
110-
}
111-
// if the item has lore but doesn't have the tag, add it
112-
if (!hasLore) {
113-
lore.add(newLine);
114-
}
115-
} else {
116-
// if the item has no lore, create a new list and add the string
117-
lore = new ArrayList<>();
118-
lore.add(newLine);
119-
}
12094
// do we add the lore based on the config?
12195
if (toolStats.checkConfig(playerTool, "blocks-mined")) {
122-
meta.setLore(lore);
96+
meta.setLore(newLore);
12397
}
12498
playerTool.setItemMeta(meta);
12599
}

0 commit comments

Comments
 (0)