Skip to content

Commit 27a89fd

Browse files
committed
unify owner and timestamp creation
1 parent 70ed50c commit 27a89fd

File tree

9 files changed

+191
-204
lines changed

9 files changed

+191
-204
lines changed

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

Lines changed: 19 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -233,38 +233,6 @@ private void fixItemLore(ItemStack original, Player player) {
233233
origin = -1;
234234
}
235235

236-
// hard code elytras
237-
if (finalItem.getType() == Material.ELYTRA) {
238-
Long flightTime = null;
239-
Long timeCreated = null;
240-
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
241-
timeCreated = container.get(toolStats.timeCreated, PersistentDataType.LONG);
242-
}
243-
if (container.has(toolStats.flightTime, PersistentDataType.LONG)) {
244-
flightTime = container.get(toolStats.flightTime, PersistentDataType.LONG);
245-
}
246-
247-
if (flightTime != null) {
248-
if (toolStats.config.getBoolean("enabled.flight-time")) {
249-
Map<String, String> flightTimeFormatted = toolStats.numberFormat.formatTime(flightTime);
250-
Component line = toolStats.configTools.formatLoreMultiplePlaceholders("flight-time", flightTimeFormatted);
251-
lore.add(line);
252-
}
253-
}
254-
255-
if (timeCreated != null) {
256-
Component timeCreatedLine = toolStats.configTools.formatLore("looted.found-by", "{player}", player.getName());
257-
Component playerOwnerLine = toolStats.configTools.formatLore("looted.found-on", "{date}", toolStats.numberFormat.formatDate(new Date(timeCreated)));
258-
lore.add(timeCreatedLine);
259-
lore.add(playerOwnerLine);
260-
}
261-
262-
finalMeta.lore(lore);
263-
finalItem.setItemMeta(finalMeta);
264-
int slot = player.getInventory().getHeldItemSlot();
265-
player.getInventory().setItem(slot, finalItem);
266-
}
267-
268236
if (container.has(toolStats.droppedBy, PersistentDataType.STRING)) {
269237
if (toolStats.config.getBoolean("enabled.dropped-by")) {
270238
if (container.has(toolStats.droppedBy)) {
@@ -293,82 +261,30 @@ private void fixItemLore(ItemStack original, Player player) {
293261
container.set(toolStats.itemOwner, new UUIDDataType(), player.getUniqueId());
294262
}
295263

296-
// show how the item was created based on the previous lore
297-
switch (origin) {
298-
case 0: {
299-
if (toolStats.configTools.checkConfig(original.getType(), "crafted-by")) {
300-
lore.add(toolStats.configTools.formatLore("crafted.crafted-by", "{player}", ownerName));
301-
}
302-
break;
303-
}
304-
case 2: {
305-
if (toolStats.configTools.checkConfig(original.getType(), "looted-by")) {
306-
lore.add(toolStats.configTools.formatLore("looted.looted-by", "{player}", ownerName));
307-
}
308-
break;
309-
}
310-
case 3: {
311-
if (toolStats.configTools.checkConfig(original.getType(), "traded-by")) {
312-
lore.add(toolStats.configTools.formatLore("traded.traded-by", "{player}", ownerName));
313-
}
314-
break;
315-
}
316-
case 5: {
317-
if (toolStats.configTools.checkConfig(original.getType(), "fished-by")) {
318-
lore.add(toolStats.configTools.formatLore("fished.caught-by", "{player}", ownerName));
319-
}
320-
break;
321-
}
322-
case 6: {
323-
if (toolStats.configTools.checkConfig(original.getType(), "spawned-in-by")) {
324-
lore.add(toolStats.configTools.formatLore("spawned-in.spawned-by", "{player}", ownerName));
325-
}
326-
break;
327-
}
264+
// add the ownership lore
265+
Component ownerLore = toolStats.itemLore.formatOwner(ownerName, origin, original);
266+
if (ownerLore != null) {
267+
lore.add(ownerLore);
328268
}
269+
329270
}
330271
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
331272
Long time = container.get(toolStats.timeCreated, PersistentDataType.LONG);
332273
if (time != null) {
333-
String date = toolStats.numberFormat.formatDate(new Date(time));
334-
// show how when the item was created based on the previous lore
335-
switch (origin) {
336-
case 0: {
337-
if (toolStats.configTools.checkConfig(original.getType(), "crafted-on")) {
338-
lore.add(toolStats.configTools.formatLore("crafted.crafted-on", "{date}", date));
339-
}
340-
break;
341-
}
342-
case 1: {
343-
if (toolStats.config.getBoolean("enabled.dropped-on")) {
344-
lore.add(toolStats.configTools.formatLore("dropped-on", "{date}", date));
345-
}
346-
break;
347-
}
348-
case 2: {
349-
if (toolStats.configTools.checkConfig(original.getType(), "looted-on")) {
350-
lore.add(toolStats.configTools.formatLore("looted.looted-on", "{date}", date));
351-
}
352-
break;
353-
}
354-
case 3: {
355-
if (toolStats.configTools.checkConfig(original.getType(), "traded-on")) {
356-
lore.add(toolStats.configTools.formatLore("traded.traded-on", "{date}", date));
357-
}
358-
break;
359-
}
360-
case 5: {
361-
if (toolStats.configTools.checkConfig(original.getType(), "fished-on")) {
362-
lore.add(toolStats.configTools.formatLore("fished.caught-on", "{date}", date));
363-
}
364-
break;
365-
}
366-
case 6: {
367-
if (toolStats.configTools.checkConfig(original.getType(), "spawned-in-on")) {
368-
lore.add(toolStats.configTools.formatLore("spawned-in.spawned-on", "{date}", date));
369-
}
370-
break;
371-
}
274+
// add the creation time lore
275+
Component creationTimeLore = toolStats.itemLore.formatCreationTime(time, origin, original);
276+
if (creationTimeLore != null) {
277+
lore.add(creationTimeLore);
278+
}
279+
}
280+
}
281+
if (toolStats.config.getBoolean("enabled.flight-time")) {
282+
if (container.has(toolStats.flightTime, PersistentDataType.LONG)) {
283+
Long flightTime = container.get(toolStats.flightTime, PersistentDataType.LONG);
284+
if (flightTime != null) {
285+
Map<String, String> flightTimeFormatted = toolStats.numberFormat.formatTime(flightTime);
286+
Component line = toolStats.configTools.formatLoreMultiplePlaceholders("flight-time", flightTimeFormatted);
287+
lore.add(line);
372288
}
373289
}
374290
}

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ private ItemStack addCraftOrigin(ItemStack itemStack, Player owner) {
125125
if (toolStats.config.getBoolean("normalize-time-creation")) {
126126
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
127127
timeCreated = finalDate.getTime();
128-
} else {
129-
finalDate = new Date(timeCreated);
130128
}
131129
PersistentDataContainer container = meta.getPersistentDataContainer();
132130

@@ -145,29 +143,20 @@ private ItemStack addCraftOrigin(ItemStack itemStack, Player owner) {
145143
}
146144

147145
// if creation date is enabled, add it
148-
if (toolStats.configTools.checkConfig(itemStack.getType(), "crafted-on")) {
146+
Component creationDate = toolStats.itemLore.formatCreationTime(timeCreated, 0, newItem);
147+
if (creationDate != null) {
149148
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
150149
container.set(toolStats.originType, PersistentDataType.INTEGER, 0);
151-
152-
String date = toolStats.numberFormat.formatDate(finalDate);
153-
Component newLine = toolStats.configTools.formatLore("crafted.crafted-on", "{date}", date);
154-
if (newLine == null) {
155-
return null;
156-
}
157-
lore.add(newLine);
150+
lore.add(creationDate);
158151
meta.lore(lore);
159152
}
160153

161-
// if creation owner is enabled, add it
162-
if (toolStats.configTools.checkConfig(itemStack.getType(), "crafted-by")) {
154+
// if ownership is enabled, add it
155+
Component itemOwner = toolStats.itemLore.formatOwner(owner.getName(), 0, newItem);
156+
if (itemOwner != null) {
163157
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
164158
container.set(toolStats.originType, PersistentDataType.INTEGER, 0);
165-
166-
Component newLine = toolStats.configTools.formatLore("crafted.crafted-by", "{player}", owner.getName());
167-
if (newLine == null) {
168-
return null;
169-
}
170-
lore.add(newLine);
159+
lore.add(itemOwner);
171160
meta.lore(lore);
172161
}
173162

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ private ItemStack addCreativeOrigin(ItemStack itemStack, Player owner) {
9292
if (toolStats.config.getBoolean("normalize-time-creation")) {
9393
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
9494
timeCreated = finalDate.getTime();
95-
} else {
96-
finalDate = new Date(timeCreated);
9795
}
9896
PersistentDataContainer container = meta.getPersistentDataContainer();
9997

@@ -111,28 +109,21 @@ private ItemStack addCreativeOrigin(ItemStack itemStack, Player owner) {
111109
lore = new ArrayList<>();
112110
}
113111

114-
if (toolStats.configTools.checkConfig(itemStack.getType(), "spawned-in-on")) {
112+
// if creation date is enabled, add it
113+
Component creationDate = toolStats.itemLore.formatCreationTime(timeCreated, 6, newSpawnedItem);
114+
if (creationDate != null) {
115115
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
116116
container.set(toolStats.originType, PersistentDataType.INTEGER, 6);
117-
118-
String date = toolStats.numberFormat.formatDate(finalDate);
119-
Component newLine = toolStats.configTools.formatLore("spawned-in.spawned-on", "{date}", date);
120-
if (newLine == null) {
121-
return null;
122-
}
123-
lore.add(newLine);
117+
lore.add(creationDate);
124118
meta.lore(lore);
125119
}
126120

127-
if (toolStats.configTools.checkConfig(itemStack.getType(), "spawned-in-by")) {
121+
// if ownership is enabled, add it
122+
Component itemOwner = toolStats.itemLore.formatOwner(owner.getName(), 6, newSpawnedItem);
123+
if (itemOwner != null) {
128124
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
129125
container.set(toolStats.originType, PersistentDataType.INTEGER, 6);
130-
131-
Component newLine = toolStats.configTools.formatLore("spawned-in.spawned-by", "{player}", owner.getName());
132-
if (newLine == null) {
133-
return null;
134-
}
135-
lore.add(newLine);
126+
lore.add(itemOwner);
136127
meta.lore(lore);
137128
}
138129

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package lol.hyper.toolstats.events;
1919

2020
import lol.hyper.toolstats.ToolStats;
21+
import lol.hyper.toolstats.tools.UUIDDataType;
2122
import net.kyori.adventure.text.Component;
2223
import org.bukkit.entity.LivingEntity;
2324
import org.bukkit.entity.Player;
@@ -91,8 +92,6 @@ private ItemStack addLore(ItemStack oldItem, LivingEntity entity) {
9192
if (toolStats.config.getBoolean("normalize-time-creation")) {
9293
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
9394
timeCreated = finalDate.getTime();
94-
} else {
95-
finalDate = new Date(timeCreated);
9695
}
9796

9897
PersistentDataContainer container = meta.getPersistentDataContainer();
@@ -108,12 +107,13 @@ private ItemStack addLore(ItemStack oldItem, LivingEntity entity) {
108107
lore = new ArrayList<>();
109108
}
110109

111-
if (toolStats.config.getBoolean("enabled.dropped-on")) {
112-
container.set(toolStats.originType, PersistentDataType.INTEGER, 1);
110+
// if creation date is enabled, add it
111+
Component creationDate = toolStats.itemLore.formatCreationTime(timeCreated, 1, newItem);
112+
if (creationDate != null) {
113113
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);
114+
container.set(toolStats.originType, PersistentDataType.INTEGER, 1);
115+
lore.add(creationDate);
116+
meta.lore(lore);
117117
}
118118

119119
if (toolStats.config.getBoolean("enabled.dropped-by")) {

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ private ItemStack addLootedOrigin(ItemStack itemStack, Player owner) {
104104
if (toolStats.config.getBoolean("normalize-time-creation")) {
105105
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
106106
timeCreated = finalDate.getTime();
107-
} else {
108-
finalDate = new Date(timeCreated);
109107
}
110108
PersistentDataContainer container = meta.getPersistentDataContainer();
111109

@@ -121,28 +119,21 @@ private ItemStack addLootedOrigin(ItemStack itemStack, Player owner) {
121119
lore = new ArrayList<>();
122120
}
123121

124-
if (toolStats.configTools.checkConfig(newItem.getType(), "looted-on")) {
122+
// if creation date is enabled, add it
123+
Component creationDate = toolStats.itemLore.formatCreationTime(timeCreated, 2, newItem);
124+
if (creationDate != null) {
125125
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
126126
container.set(toolStats.originType, PersistentDataType.INTEGER, 2);
127-
128-
String date = toolStats.numberFormat.formatDate(finalDate);
129-
Component newLine = toolStats.configTools.formatLore("looted.looted-on", "{date}", date);
130-
if (newLine == null) {
131-
return null;
132-
}
133-
lore.add(newLine);
127+
lore.add(creationDate);
134128
meta.lore(lore);
135129
}
136130

137-
if (toolStats.configTools.checkConfig(newItem.getType(), "looted-by")) {
131+
// if ownership is enabled, add it
132+
Component itemOwner = toolStats.itemLore.formatOwner(owner.getName(), 2, newItem);
133+
if (itemOwner != null) {
138134
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
139135
container.set(toolStats.originType, PersistentDataType.INTEGER, 2);
140-
141-
Component newLine = toolStats.configTools.formatLore("looted.looted-by", "{player}", owner.getName());
142-
if (newLine == null) {
143-
return null;
144-
}
145-
lore.add(newLine);
136+
lore.add(itemOwner);
146137
meta.lore(lore);
147138
}
148139

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ private ItemStack addElytraOrigin(ItemStack itemStack, Player owner) {
9595
if (toolStats.config.getBoolean("normalize-time-creation")) {
9696
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
9797
timeCreated = finalDate.getTime();
98-
} else {
99-
finalDate = new Date(timeCreated);
10098
}
10199
PersistentDataContainer container = meta.getPersistentDataContainer();
102100

@@ -123,12 +121,22 @@ private ItemStack addElytraOrigin(ItemStack itemStack, Player owner) {
123121
container.set(toolStats.originType, PersistentDataType.INTEGER, 4);
124122
container.remove(toolStats.newElytra);
125123

126-
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
127-
Component dateCreatedLore = toolStats.configTools.formatLore("looted.found-on", "{date}", formattedDate);
128-
Component itemOwnerLore = toolStats.configTools.formatLore("looted.found-by", "{player}", owner.getName());
129-
lore.add(dateCreatedLore);
130-
lore.add(itemOwnerLore);
131-
meta.lore(lore);
124+
Component creationDate = toolStats.itemLore.formatCreationTime(timeCreated, 4, finalItem);
125+
if (creationDate != null) {
126+
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
127+
container.set(toolStats.originType, PersistentDataType.INTEGER, 4);
128+
lore.add(creationDate);
129+
meta.lore(lore);
130+
}
131+
132+
Component itemOwner = toolStats.itemLore.formatOwner(owner.getName(), 4, finalItem);
133+
if (itemOwner != null) {
134+
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
135+
container.set(toolStats.originType, PersistentDataType.INTEGER, 4);
136+
lore.add(itemOwner);
137+
meta.lore(lore);
138+
}
139+
132140
finalItem.setItemMeta(meta);
133141
return finalItem;
134142
}

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ private ItemStack addFishedOrigin(ItemStack originalItem, Player owner) {
114114
if (toolStats.config.getBoolean("normalize-time-creation")) {
115115
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
116116
timeCreated = finalDate.getTime();
117-
} else {
118-
finalDate = new Date(timeCreated);
119117
}
120118
PersistentDataContainer container = meta.getPersistentDataContainer();
121119

@@ -131,28 +129,21 @@ private ItemStack addFishedOrigin(ItemStack originalItem, Player owner) {
131129
lore = new ArrayList<>();
132130
}
133131

134-
if (toolStats.configTools.checkConfig(newItem.getType(), "fished-on")) {
132+
// if creation date is enabled, add it
133+
Component creationDate = toolStats.itemLore.formatCreationTime(timeCreated, 5, newItem);
134+
if (creationDate != null) {
135135
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
136136
container.set(toolStats.originType, PersistentDataType.INTEGER, 5);
137-
138-
String date = toolStats.numberFormat.formatDate(finalDate);
139-
Component newLine = toolStats.configTools.formatLore("fished.caught-on", "{date}", date);
140-
if (newLine == null) {
141-
return null;
142-
}
143-
lore.add(newLine);
137+
lore.add(creationDate);
144138
meta.lore(lore);
145139
}
146140

147-
if (toolStats.configTools.checkConfig(newItem.getType(), "fished-by")) {
141+
// if ownership is enabled, add it
142+
Component itemOwner = toolStats.itemLore.formatOwner(owner.getName(), 5, newItem);
143+
if (itemOwner != null) {
148144
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
149145
container.set(toolStats.originType, PersistentDataType.INTEGER, 5);
150-
151-
Component newLine = toolStats.configTools.formatLore("fished.caught-by", "{player}", owner.getName());
152-
if (newLine == null) {
153-
return null;
154-
}
155-
lore.add(newLine);
146+
lore.add(itemOwner);
156147
meta.lore(lore);
157148
}
158149

0 commit comments

Comments
 (0)