Skip to content

Commit b2ef154

Browse files
committed
add dropped-by
1 parent bc8f494 commit b2ef154

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public final class ToolStats extends JavaPlugin {
9393
* Key for arrows shot.
9494
*/
9595
public final NamespacedKey arrowsShot = new NamespacedKey(this, "arrows-shot");
96+
/**
97+
* Key for arrows shot.
98+
*/
99+
public final NamespacedKey droppedBy = new NamespacedKey(this, "dropped-by");
96100
/**
97101
* Key for tracking flight time.
98102
*/

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,17 @@ private void fixItemLore(ItemStack original, Player player) {
266266
player.getInventory().setItem(slot, finalItem);
267267
}
268268

269+
if (container.has(toolStats.droppedBy, PersistentDataType.STRING)) {
270+
if (toolStats.config.getBoolean("enabled.dropped-by")) {
271+
if (container.has(toolStats.droppedBy)) {
272+
String droppedBy = container.get(toolStats.droppedBy, PersistentDataType.STRING);
273+
lore.add(toolStats.configTools.formatLore("dropped-by", "{name}", droppedBy));
274+
} else {
275+
player.sendMessage(Component.text("Unable to set 'dropped-by', as this item has no record of it."));
276+
}
277+
}
278+
}
279+
269280
if (container.has(toolStats.itemOwner, new UUIDDataType())) {
270281
UUID owner = container.get(toolStats.itemOwner, new UUIDDataType());
271282
String ownerName = null;
@@ -329,6 +340,12 @@ private void fixItemLore(ItemStack original, Player player) {
329340
}
330341
break;
331342
}
343+
case 1: {
344+
if (toolStats.config.getBoolean("enabled.dropped-on")) {
345+
lore.add(toolStats.configTools.formatLore("dropped-on", "{date}", date));
346+
}
347+
break;
348+
}
332349
case 2: {
333350
if (toolStats.configTools.checkConfig(original.getType(), "looted-on")) {
334351
lore.add(toolStats.configTools.formatLore("looted.looted-on", "{date}", date));

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.bukkit.persistence.PersistentDataContainer;
3131
import org.bukkit.persistence.PersistentDataType;
3232

33+
import java.util.ArrayList;
34+
import java.util.Date;
3335
import java.util.List;
3436
import java.util.UUID;
3537

@@ -84,19 +86,44 @@ private ItemStack addLore(ItemStack oldItem, LivingEntity entity) {
8486
return null;
8587
}
8688

87-
if (!toolStats.config.getBoolean("enabled.dropped-by")) {
88-
return null;
89+
long timeCreated = System.currentTimeMillis();
90+
Date finalDate;
91+
if (toolStats.config.getBoolean("normalize-time-creation")) {
92+
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
93+
timeCreated = finalDate.getTime();
94+
} else {
95+
finalDate = new Date(timeCreated);
8996
}
9097

9198
PersistentDataContainer container = meta.getPersistentDataContainer();
92-
container.set(toolStats.originType, PersistentDataType.INTEGER, 1);
9399
String mobName = toolStats.config.getString("messages.mob." + entity.getType());
94100
if (mobName == null) {
95101
mobName = entity.getName();
96102
}
97-
Component newLine = toolStats.configTools.formatLore("dropped-by", "{name}", mobName);
98-
List<Component> newLore = toolStats.itemLore.addItemLore(meta, newLine);
99-
meta.lore(newLore);
103+
104+
List<Component> lore;
105+
if (meta.hasLore()) {
106+
lore = meta.lore();
107+
} else {
108+
lore = new ArrayList<>();
109+
}
110+
111+
if (toolStats.config.getBoolean("enabled.dropped-on")) {
112+
container.set(toolStats.originType, PersistentDataType.INTEGER, 1);
113+
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
114+
String date = toolStats.numberFormat.formatDate(finalDate);
115+
Component droppedOn = toolStats.configTools.formatLore("dropped-on", "{date}", date);
116+
lore.add(droppedOn);
117+
}
118+
119+
if (toolStats.config.getBoolean("enabled.dropped-by")) {
120+
container.set(toolStats.originType, PersistentDataType.INTEGER, 1);
121+
container.set(toolStats.droppedBy, PersistentDataType.STRING, mobName);
122+
Component droppedBy = toolStats.configTools.formatLore("dropped-by", "{name}", mobName);
123+
lore.add(droppedBy);
124+
}
125+
126+
meta.lore(lore);
100127
newItem.setItemMeta(meta);
101128
return newItem;
102129
}

src/main/java/lol/hyper/toolstats/tools/config/versions/Version12.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public void update() {
8080
toolStats.config.set("enabled.traded-tag", null);
8181
toolStats.config.set("enabled.spawned-in", null);
8282

83+
toolStats.logger.info("Adding enabled.dropped-on");
84+
boolean droppedBy = toolStats.config.getBoolean("enabled.dropped-by");
85+
toolStats.config.set("enabled.dropped-on", droppedBy);
86+
87+
toolStats.logger.info("Adding messages.dropped-on");
88+
toolStats.config.set("messages.dropped-on", "&7Dropped on: &8{date}");
89+
8390

8491
// rename crafted to crafted here
8592
// copy the old ones first

src/main/resources/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ enabled:
208208
sheep-sheared: true
209209
armor-damage: true
210210
dropped-by: true
211+
dropped-on: true
211212
elytra-tag: true
212213
arrows-shot: true
213214
flight-time: true
@@ -238,7 +239,8 @@ messages:
238239
blocks-mined: "&7Blocks mined: &8{blocks}"
239240
crops-harvested: "&7Crops harvested: &8{crops}"
240241
sheep-sheared: "&7Sheep sheared: &8{sheep}"
241-
dropped-by: "&7Dropped by: &8{name}" # name will be player/mob name
242+
dropped-by: "&7Dropped by: &8{name}" # name will be mob name
243+
dropped-on: "&7Dropped on: &8{date}"
242244
damage-taken: "&7Damage taken: &8{damage}"
243245
arrows-shot: "&7Arrows shot: &8{arrows}"
244246
flight-time: "&7Flight time: &8{years}y {months}m {days}d {hours}h {minutes}m {seconds}s"

0 commit comments

Comments
 (0)