From 8a1759b6b117e1ab75e5af44e3a037067d5800ca Mon Sep 17 00:00:00 2001 From: hyperdefined Date: Tue, 28 Jan 2025 21:53:39 -0500 Subject: [PATCH] remove/add hashes when enabled/disabled --- .../java/lol/hyper/toolstats/ToolStats.java | 2 +- .../hyper/toolstats/events/InventoryOpen.java | 35 +++++++++-------- .../hyper/toolstats/events/PlayerJoin.java | 39 ++++++++++++------- 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/main/java/lol/hyper/toolstats/ToolStats.java b/src/main/java/lol/hyper/toolstats/ToolStats.java index 54c5840..d8fccd9 100644 --- a/src/main/java/lol/hyper/toolstats/ToolStats.java +++ b/src/main/java/lol/hyper/toolstats/ToolStats.java @@ -112,7 +112,7 @@ public final class ToolStats extends JavaPlugin { */ public final NamespacedKey originType = new NamespacedKey(this, "origin"); - public final int CONFIG_VERSION = 9; + public final int CONFIG_VERSION = 10; public final Logger logger = this.getLogger(); public final File configFile = new File(this.getDataFolder(), "config.yml"); public boolean tokens = false; diff --git a/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java b/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java index 5f0c9cc..637928e 100644 --- a/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java +++ b/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java @@ -19,8 +19,7 @@ package lol.hyper.toolstats.events; import lol.hyper.toolstats.ToolStats; import lol.hyper.toolstats.tools.UUIDDataType; -import org.bukkit.Bukkit; -import org.bukkit.Location; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryOpenEvent; @@ -47,7 +46,7 @@ public class InventoryOpen implements Listener { } Inventory inventory = event.getInventory(); - Location location = event.getInventory().getLocation(); + Player player = (Player) event.getPlayer(); for (ItemStack itemStack : inventory) { if (itemStack == null) { continue; @@ -75,31 +74,35 @@ public class InventoryOpen implements Listener { } } - // generate a hash if the item doesn't have one (if it's enabled in the config) + // generate a hash if the item doesn't have one (and enabled) + // if hashes are disabled and the item has one, remove it. if (toolStats.config.getBoolean("generate-hash-for-items")) { if (!container.has(toolStats.hash, PersistentDataType.STRING)) { - // make sure the item has an owner - if (!container.has(toolStats.itemOwner, new UUIDDataType())) { - continue; + UUID owner = null; + // get the current owner if there is one. + if (container.has(toolStats.itemOwner, new UUIDDataType())) { + owner = container.get(toolStats.itemOwner, new UUIDDataType()); } - UUID owner = container.get(toolStats.itemOwner, new UUIDDataType()); + // if there is no owner, use the player holding it if (owner == null) { - continue; + owner = player.getUniqueId(); } Long timestamp = container.get(toolStats.timeCreated, PersistentDataType.LONG); if (timestamp == null) { - continue; + // if there is no time created, use now + timestamp = System.currentTimeMillis(); } String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp); container.set(toolStats.hash, PersistentDataType.STRING, hash); + itemStack.setItemMeta(itemMeta); + } + } else { + // if hashes are disabled but the item has one, remove it. + if (container.has(toolStats.hash, PersistentDataType.STRING)) { + container.remove(toolStats.hash); + itemStack.setItemMeta(itemMeta); } } - ItemMeta clone = itemMeta.clone(); - if (location != null) { - Bukkit.getRegionScheduler().runDelayed(toolStats, location, scheduledTask -> itemStack.setItemMeta(clone), 1); - } - - } } } diff --git a/src/main/java/lol/hyper/toolstats/events/PlayerJoin.java b/src/main/java/lol/hyper/toolstats/events/PlayerJoin.java index ee0f783..365126a 100644 --- a/src/main/java/lol/hyper/toolstats/events/PlayerJoin.java +++ b/src/main/java/lol/hyper/toolstats/events/PlayerJoin.java @@ -72,24 +72,33 @@ public class PlayerJoin implements Listener { } // generate a hash if the item doesn't have one - if (!container.has(toolStats.hash, PersistentDataType.STRING)) { - // make sure the item has an owner - if (!container.has(toolStats.itemOwner, new UUIDDataType())) { - continue; + if (toolStats.config.getBoolean("generate-hash-for-items")) { + if (!container.has(toolStats.hash, PersistentDataType.STRING)) { + UUID owner = null; + // get the current owner if there is one. + if (container.has(toolStats.itemOwner, new UUIDDataType())) { + owner = container.get(toolStats.itemOwner, new UUIDDataType()); + } + // if there is no owner, use the player holding it + if (owner == null) { + owner = player.getUniqueId(); + } + Long timestamp = container.get(toolStats.timeCreated, PersistentDataType.LONG); + if (timestamp == null) { + // if there is no time created, use now + timestamp = System.currentTimeMillis(); + } + String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp); + container.set(toolStats.hash, PersistentDataType.STRING, hash); + itemStack.setItemMeta(itemMeta); } - UUID owner = container.get(toolStats.itemOwner, new UUIDDataType()); - if (owner == null) { - continue; + } else { + // if hashes are disabled but the item has one, remove it. + if (container.has(toolStats.hash, PersistentDataType.STRING)) { + container.remove(toolStats.hash); + itemStack.setItemMeta(itemMeta); } - Long timestamp = container.get(toolStats.timeCreated, PersistentDataType.LONG); - if (timestamp == null) { - continue; - } - String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp); - container.set(toolStats.hash, PersistentDataType.STRING, hash); } - ItemMeta clone = itemMeta.clone(); - player.getScheduler().runDelayed(toolStats, scheduledTask -> itemStack.setItemMeta(clone), null, 1); } } }