diff --git a/src/main/java/lol/hyper/toolstats/events/CraftItem.java b/src/main/java/lol/hyper/toolstats/events/CraftItem.java index cb5cf3a..54fa333 100644 --- a/src/main/java/lol/hyper/toolstats/events/CraftItem.java +++ b/src/main/java/lol/hyper/toolstats/events/CraftItem.java @@ -121,7 +121,13 @@ public class CraftItem implements Listener { } // get the current time long timeCreated = System.currentTimeMillis(); - Date finalDate = new Date(timeCreated); + Date finalDate; + if (toolStats.config.getBoolean("normalize-time-creation")) { + finalDate = toolStats.numberFormat.normalizeTime(timeCreated); + timeCreated = finalDate.getTime(); + } else { + finalDate = new Date(timeCreated); + } PersistentDataContainer container = meta.getPersistentDataContainer(); // if the item already has the tag diff --git a/src/main/java/lol/hyper/toolstats/events/CreativeEvent.java b/src/main/java/lol/hyper/toolstats/events/CreativeEvent.java index f95ed29..e0de0be 100644 --- a/src/main/java/lol/hyper/toolstats/events/CreativeEvent.java +++ b/src/main/java/lol/hyper/toolstats/events/CreativeEvent.java @@ -88,7 +88,13 @@ public class CreativeEvent implements Listener { } // get the current time long timeCreated = System.currentTimeMillis(); - Date finalDate = new Date(timeCreated); + Date finalDate; + if (toolStats.config.getBoolean("normalize-time-creation")) { + finalDate = toolStats.numberFormat.normalizeTime(timeCreated); + timeCreated = finalDate.getTime(); + } else { + finalDate = new Date(timeCreated); + } PersistentDataContainer container = meta.getPersistentDataContainer(); // if the item already has the tag diff --git a/src/main/java/lol/hyper/toolstats/events/GenerateLoot.java b/src/main/java/lol/hyper/toolstats/events/GenerateLoot.java index 0ad0202..925e50c 100644 --- a/src/main/java/lol/hyper/toolstats/events/GenerateLoot.java +++ b/src/main/java/lol/hyper/toolstats/events/GenerateLoot.java @@ -100,7 +100,13 @@ public class GenerateLoot implements Listener { return null; } long timeCreated = System.currentTimeMillis(); - Date finalDate = new Date(timeCreated); + Date finalDate; + if (toolStats.config.getBoolean("normalize-time-creation")) { + finalDate = toolStats.numberFormat.normalizeTime(timeCreated); + timeCreated = finalDate.getTime(); + } else { + finalDate = new Date(timeCreated); + } PersistentDataContainer container = meta.getPersistentDataContainer(); if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) { diff --git a/src/main/java/lol/hyper/toolstats/events/PickupItem.java b/src/main/java/lol/hyper/toolstats/events/PickupItem.java index 6ff98e3..bcf9557 100644 --- a/src/main/java/lol/hyper/toolstats/events/PickupItem.java +++ b/src/main/java/lol/hyper/toolstats/events/PickupItem.java @@ -91,7 +91,13 @@ public class PickupItem implements Listener { return null; } long timeCreated = System.currentTimeMillis(); - Date finalDate = new Date(timeCreated); + Date finalDate; + if (toolStats.config.getBoolean("normalize-time-creation")) { + finalDate = toolStats.numberFormat.normalizeTime(timeCreated); + timeCreated = finalDate.getTime(); + } else { + finalDate = new Date(timeCreated); + } PersistentDataContainer container = meta.getPersistentDataContainer(); if (!toolStats.config.getBoolean("enabled.elytra-tag")) { diff --git a/src/main/java/lol/hyper/toolstats/events/PlayerFish.java b/src/main/java/lol/hyper/toolstats/events/PlayerFish.java index 1c9d4a4..a17232c 100644 --- a/src/main/java/lol/hyper/toolstats/events/PlayerFish.java +++ b/src/main/java/lol/hyper/toolstats/events/PlayerFish.java @@ -110,7 +110,13 @@ public class PlayerFish implements Listener { return null; } long timeCreated = System.currentTimeMillis(); - Date finalDate = new Date(timeCreated); + Date finalDate; + if (toolStats.config.getBoolean("normalize-time-creation")) { + finalDate = toolStats.numberFormat.normalizeTime(timeCreated); + timeCreated = finalDate.getTime(); + } else { + finalDate = new Date(timeCreated); + } PersistentDataContainer container = meta.getPersistentDataContainer(); if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) { diff --git a/src/main/java/lol/hyper/toolstats/events/VillagerTrade.java b/src/main/java/lol/hyper/toolstats/events/VillagerTrade.java index 60b8eeb..bbf9776 100644 --- a/src/main/java/lol/hyper/toolstats/events/VillagerTrade.java +++ b/src/main/java/lol/hyper/toolstats/events/VillagerTrade.java @@ -127,7 +127,13 @@ public class VillagerTrade implements Listener { return null; } long timeCreated = System.currentTimeMillis(); - Date finalDate = new Date(timeCreated); + Date finalDate; + if (toolStats.config.getBoolean("normalize-time-creation")) { + finalDate = toolStats.numberFormat.normalizeTime(timeCreated); + timeCreated = finalDate.getTime(); + } else { + finalDate = new Date(timeCreated); + } PersistentDataContainer container = meta.getPersistentDataContainer(); if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) { diff --git a/src/main/java/lol/hyper/toolstats/tools/NumberFormat.java b/src/main/java/lol/hyper/toolstats/tools/NumberFormat.java index 1366ae1..fc28dd2 100644 --- a/src/main/java/lol/hyper/toolstats/tools/NumberFormat.java +++ b/src/main/java/lol/hyper/toolstats/tools/NumberFormat.java @@ -22,6 +22,10 @@ import lol.hyper.toolstats.ToolStats; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Date; import java.util.HashMap; import java.util.Locale; @@ -35,6 +39,7 @@ public class NumberFormat { /** * Utility class to format different numbers + * * @param toolStats Plugin instance. */ public NumberFormat(ToolStats toolStats) { @@ -140,8 +145,9 @@ public class NumberFormat { } /** - * Returns a human readable form of time in milliseconds. - * E.g. given 3752348000L outputs 1 years, 5 months, 3 days, 14 hours, 12 minutes, 28 seconds. + * Returns a human-readable form of time in milliseconds. + * E.g. given 3752348000L outputs 1 year, 5 months, 3 days, 14 hours, 12 minutes, 28 seconds. + * * @param time The time in ms. * @return Map with units as keys and time value, e.g. "years" (key) -> 1 (value) */ @@ -155,7 +161,7 @@ public class NumberFormat { long totalSeconds = time / 1000; Map timeUnits = new HashMap<>(); - + long years = totalSeconds / (DAYS_PER_YEAR * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE); if (years > 0) { timeUnits.put("years", Long.toString(years)); @@ -190,7 +196,16 @@ public class NumberFormat { if (seconds > 0 || timeUnits.isEmpty()) { // Always include seconds if everything else is zero timeUnits.put("seconds", Long.toString(seconds)); } - + return timeUnits; } + + public Date normalizeTime(Long time) { + Instant instant = Instant.ofEpochMilli(time); + ZoneId zone = ZoneId.systemDefault(); + + LocalDate localDate = instant.atZone(zone).toLocalDate(); + ZonedDateTime midnight = localDate.atStartOfDay(zone); + return Date.from(midnight.toInstant()); + } } diff --git a/src/main/java/lol/hyper/toolstats/tools/config/versions/Version12.java b/src/main/java/lol/hyper/toolstats/tools/config/versions/Version12.java index eb24d24..625e6f2 100644 --- a/src/main/java/lol/hyper/toolstats/tools/config/versions/Version12.java +++ b/src/main/java/lol/hyper/toolstats/tools/config/versions/Version12.java @@ -93,6 +93,9 @@ public class Version12 { toolStats.config.set("messages.crafted.crafted-by", craftedByMessage); toolStats.config.set("messages.crafted.crafted-on", craftedOnMessage); + toolStats.logger.info("Adding normalize-time-creation"); + toolStats.config.set("normalize-time-creation", false); + // save the config and reload it try { toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml"); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 86f6c69..c501d46 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -265,4 +265,8 @@ number-formats: # This has no use currently, but can be used for future features for dupe detection. generate-hash-for-items: false -config-version: 11 \ No newline at end of file +# Make when items are created at midnight on the date. +# This makes dates for items more "normalized" instead of being at different times. +normalize-time-creation: false + +config-version: 12 \ No newline at end of file