Skip to content

Commit e296c27

Browse files
committed
added comments to better explain what is going on
1 parent adb1c63 commit e296c27

File tree

12 files changed

+138
-16
lines changed

12 files changed

+138
-16
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ public void checkForUpdates() {
152152
}
153153
}
154154

155+
/**
156+
* Checks the config to see if we want to show lore on certain items.
157+
* @param itemStack The item to check.
158+
* @param configName The config we are checking under.
159+
* @return If we want to allow lore or not.
160+
*/
155161
public boolean checkConfig(ItemStack itemStack, String configName) {
156162
String itemName = itemStack.getType().toString().toLowerCase();
157163
String itemType = null;
@@ -202,6 +208,12 @@ public boolean checkConfig(ItemStack itemStack, String configName) {
202208
return false;
203209
}
204210

211+
/**
212+
* Gets the lore message from the config.
213+
* @param configName The config name, "messages." is already in front.
214+
* @param raw If you want the raw message with the formatting codes and placeholders.
215+
* @return The lore message.
216+
*/
205217
public String getLoreFromConfig(String configName, boolean raw) {
206218
String lore = config.getString("messages." + configName);
207219
if (lore == null) {

src/main/java/lol/hyper/toolstats/commands/CommandToolStats.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import lol.hyper.toolstats.UUIDDataType;
2222
import org.bukkit.*;
2323
import org.bukkit.command.Command;
24-
import org.bukkit.command.CommandExecutor;
2524
import org.bukkit.command.CommandSender;
2625
import org.bukkit.command.TabExecutor;
2726
import org.bukkit.entity.Player;
@@ -88,11 +87,16 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
8887
return true;
8988
}
9089

91-
private ItemStack fixItemLore(ItemStack original, Player player) {
90+
/**
91+
* Fixes lore on a given item. This will wipe all lore and reapply our custom ones.
92+
* @param original The item we are fixing.
93+
* @param player The player running the command.
94+
*/
95+
private void fixItemLore(ItemStack original, Player player) {
9296
ItemStack finalItem = original.clone();
9397
ItemMeta finalMeta = finalItem.getItemMeta();
9498
if (finalMeta == null) {
95-
return null;
99+
return;
96100
}
97101
PersistentDataContainer container = finalMeta.getPersistentDataContainer();
98102
List<String> lore = new ArrayList<>();
@@ -101,10 +105,14 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
101105
String lootedByLore = toolStats.getLoreFromConfig("looted.found-by", false);
102106
String tradedByLore = toolStats.getLoreFromConfig("traded.traded-by", false);
103107

108+
// make sure the config messages are not null
104109
if (caughtByLore == null || lootedByLore == null || tradedByLore == null) {
105-
return null;
110+
return;
106111
}
107112

113+
// determine how the item was originally created
114+
// this doesn't get saved, so we just rely on the lore
115+
// if there isn't a tag, default to crafted
108116
String type = "DEFAULT";
109117
if (finalMeta.hasLore()) {
110118
if (finalMeta.getLore() != null) {
@@ -124,6 +132,7 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
124132
if (toolStats.checkConfig(original, "created-by")) {
125133
if (container.has(toolStats.genericOwner, new UUIDDataType())) {
126134
container.set(toolStats.genericOwner, new UUIDDataType(), player.getUniqueId());
135+
// show how the item was created based on the previous lore
127136
switch (type) {
128137
case "DEFAULT": {
129138
lore.add(toolStats.getLoreFromConfig("created.created-by", true).replace("{player}", player.getName()));
@@ -148,8 +157,9 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
148157
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
149158
Long time = container.get(toolStats.timeCreated, PersistentDataType.LONG);
150159
if (time == null) {
151-
return null;
160+
return;
152161
}
162+
// show how when the item was created based on the previous lore
153163
switch (type) {
154164
case "DEFAULT": {
155165
lore.add(toolStats.getLoreFromConfig("created.created-by", true).replace("{date}", format.format(new Date(time))));
@@ -174,7 +184,7 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
174184
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
175185
Integer kills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
176186
if (kills == null) {
177-
return null;
187+
return;
178188
}
179189
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", Integer.toString(kills)));
180190
}
@@ -183,7 +193,7 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
183193
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
184194
Integer kills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
185195
if (kills == null) {
186-
return null;
196+
return;
187197
}
188198
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", Integer.toString(kills)));
189199
}
@@ -192,7 +202,7 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
192202
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
193203
Integer blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
194204
if (blocksMined == null) {
195-
return null;
205+
return;
196206
}
197207
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", Integer.toString(blocksMined)));
198208
}
@@ -201,7 +211,7 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
201211
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
202212
Integer fish = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
203213
if (fish == null) {
204-
return null;
214+
return;
205215
}
206216
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", Integer.toString(fish)));
207217
}
@@ -210,7 +220,7 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
210220
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
211221
Integer sheep = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
212222
if (sheep == null) {
213-
return null;
223+
return;
214224
}
215225
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", Integer.toString(sheep)));
216226
}
@@ -219,14 +229,13 @@ private ItemStack fixItemLore(ItemStack original, Player player) {
219229
if (container.has(toolStats.armorDamage, PersistentDataType.INTEGER)) {
220230
Integer damage = container.get(toolStats.armorDamage, PersistentDataType.INTEGER);
221231
if (damage == null) {
222-
return null;
232+
return;
223233
}
224234
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", Integer.toString(damage)));
225235
}
226236
}
227237
finalMeta.setLore(lore);
228238
finalItem.setItemMeta(finalMeta);
229-
return finalItem;
230239
}
231240

232241
@Nullable

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,21 @@ public void onBreak(BlockBreakEvent event) {
4848
return;
4949
}
5050
Player player = event.getPlayer();
51+
// ignore creative mode
5152
if (player.getGameMode() != GameMode.SURVIVAL) {
5253
return;
5354
}
55+
// if the player mines something with their fist
5456
ItemStack heldItem = player.getInventory().getItem(player.getInventory().getHeldItemSlot());
5557
if (heldItem == null || heldItem.getType() == Material.AIR) {
5658
return;
5759
}
60+
// only check certain items
5861
String itemName = heldItem.getType().toString().toLowerCase();
5962
if (Arrays.stream(validTools).noneMatch(itemName::contains)) {
6063
return;
6164
}
65+
// if it's an item we want, update the stats
6266
updateBlocksMined(heldItem);
6367
}
6468

@@ -67,6 +71,8 @@ private void updateBlocksMined(ItemStack itemStack) {
6771
if (meta == null) {
6872
return;
6973
}
74+
// read the current stats from the item
75+
// if they don't exist, then start from 0
7076
Integer blocksMined = 0;
7177
PersistentDataContainer container = meta.getPersistentDataContainer();
7278
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
@@ -82,17 +88,18 @@ private void updateBlocksMined(ItemStack itemStack) {
8288
String configLore = toolStats.getLoreFromConfig("blocks-mined", false);
8389
String configLoreRaw = toolStats.getLoreFromConfig("blocks-mined", true);
8490

91+
if (configLore == null || configLoreRaw == null) {
92+
toolStats.logger.warning("There is no lore message for messages.blocks-mined!");
93+
return;
94+
}
95+
8596
List<String> lore;
8697
if (meta.hasLore()) {
8798
lore = meta.getLore();
8899
assert lore != null;
89100
boolean hasLore = false;
90101
// we do a for loop like this, we can keep track of index
91102
// this doesn't mess the lore up of existing items
92-
if (configLore == null || configLoreRaw == null) {
93-
toolStats.logger.warning("There is no lore message for messages.blocks-mined!");
94-
return;
95-
}
96103
for (int x = 0; x < lore.size(); x++) {
97104
if (lore.get(x).contains(configLore)) {
98105
hasLore = true;
@@ -109,6 +116,7 @@ private void updateBlocksMined(ItemStack itemStack) {
109116
lore = new ArrayList<>();
110117
lore.add(configLoreRaw.replace("{blocks}", Integer.toString(blocksMined)));
111118
}
119+
// do we add the lore based on the config?
112120
if (toolStats.checkConfig(itemStack, "blocks-mined")) {
113121
meta.setLore(lore);
114122
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ public void onPopulate(ChunkPopulateEvent event) {
5353
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
5454
Chunk chunk = event.getChunk();
5555
for (Entity entity : chunk.getEntities()) {
56+
// if there is a new item frame
5657
if (entity instanceof ItemFrame) {
5758
ItemFrame itemFrame = (ItemFrame) entity;
59+
// if the item frame has an elytra
5860
if (itemFrame.getItem().getType() == Material.ELYTRA) {
5961
ItemStack elytraCopy = itemFrame.getItem();
6062
ItemMeta meta = elytraCopy.getItemMeta();
6163
if (meta == null) {
6264
return;
6365
}
66+
// add the new tag so we know it's new
6467
PersistentDataContainer container = meta.getPersistentDataContainer();
6568
container.set(toolStats.newElytra, PersistentDataType.INTEGER, 1);
6669
elytraCopy.setItemMeta(meta);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ public void onCraft(CraftItemEvent event) {
5656
return;
5757
}
5858
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
59+
// only check for items we want
5960
for (String x : validItems) {
6061
if (name.contains(x)) {
62+
// if the player shift clicks, send them this warning
6163
if (event.isShiftClick()) {
6264
String configMessage = toolStats.config.getString("messages.shift-click-warning.crafting");
6365
if (configMessage != null) {
@@ -66,24 +68,36 @@ public void onCraft(CraftItemEvent event) {
6668
}
6769
}
6870
}
71+
// test the item before setting it
6972
if (addLore(itemStack, player) == null) {
7073
return;
7174
}
75+
// set the result
7276
event.setCurrentItem(addLore(itemStack, player));
7377
}
7478
}
7579
}
7680

81+
/**
82+
* Adds crafted tags to item.
83+
* @param itemStack The item add item to.
84+
* @param owner The player crafting.
85+
* @return A copy of the item with the tags + lore.
86+
*/
7787
private ItemStack addLore(ItemStack itemStack, Player owner) {
88+
// clone the item
7889
ItemStack newItem = itemStack.clone();
7990
ItemMeta meta = newItem.getItemMeta();
8091
if (meta == null) {
8192
return null;
8293
}
94+
// get the current time
8395
long timeCreated = System.currentTimeMillis();
8496
Date finalDate = new Date(timeCreated);
8597
PersistentDataContainer container = meta.getPersistentDataContainer();
8698

99+
// if the item already has the tag
100+
// this is to prevent duplicate tags
87101
if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.genericOwner, PersistentDataType.LONG)) {
88102
return null;
89103
}
@@ -104,12 +118,14 @@ private ItemStack addLore(ItemStack itemStack, Player owner) {
104118
}
105119

106120
List<String> lore;
121+
// get the current lore the item
107122
if (meta.hasLore()) {
108123
lore = meta.getLore();
109124
assert lore != null;
110125
} else {
111126
lore = new ArrayList<>();
112127
}
128+
// do we add the lore based on the config?
113129
if (toolStats.checkConfig(itemStack, "created-date")) {
114130
lore.add(createdOnRaw.replace("{date}", format.format(finalDate)));
115131
}

0 commit comments

Comments
 (0)