diff --git a/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java b/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java index ae6ff3b..5f0c9cc 100644 --- a/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java +++ b/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java @@ -62,6 +62,19 @@ public class InventoryOpen implements Listener { } PersistentDataContainer container = itemMeta.getPersistentDataContainer(); + if (toolStats.config.getBoolean("tokens.enabled")) { + // if the token system is on and the item doesn't have stat keys + if (toolStats.itemChecker.keyCheck(container) && !container.has(toolStats.tokenType)) { + // add the tokens + String newTokens = toolStats.itemChecker.addTokensToExisting(itemStack); + if (newTokens == null) { + return; + } + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + itemStack.setItemMeta(itemMeta); + } + } + // generate a hash if the item doesn't have one (if it's enabled in the config) if (toolStats.config.getBoolean("generate-hash-for-items")) { if (!container.has(toolStats.hash, PersistentDataType.STRING)) { @@ -85,6 +98,8 @@ public class InventoryOpen implements Listener { 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 70ecaeb..ee0f783 100644 --- a/src/main/java/lol/hyper/toolstats/events/PlayerJoin.java +++ b/src/main/java/lol/hyper/toolstats/events/PlayerJoin.java @@ -58,6 +58,19 @@ public class PlayerJoin implements Listener { } PersistentDataContainer container = itemMeta.getPersistentDataContainer(); + if (toolStats.config.getBoolean("tokens.enabled")) { + // if the token system is on and the item doesn't have stat keys + if (toolStats.itemChecker.keyCheck(container) && !container.has(toolStats.tokenType)) { + // add the tokens + String newTokens = toolStats.itemChecker.addTokensToExisting(itemStack); + if (newTokens == null) { + return; + } + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + itemStack.setItemMeta(itemMeta); + } + } + // generate a hash if the item doesn't have one if (!container.has(toolStats.hash, PersistentDataType.STRING)) { // make sure the item has an owner diff --git a/src/main/java/lol/hyper/toolstats/tools/ItemChecker.java b/src/main/java/lol/hyper/toolstats/tools/ItemChecker.java index daf8a9f..31cd14d 100644 --- a/src/main/java/lol/hyper/toolstats/tools/ItemChecker.java +++ b/src/main/java/lol/hyper/toolstats/tools/ItemChecker.java @@ -19,6 +19,7 @@ package lol.hyper.toolstats.tools; import lol.hyper.toolstats.ToolStats; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.ItemMeta; @@ -280,4 +281,65 @@ public class ItemChecker { return null; } + + /** + * Checks the keys of the item and returns the tokens we should add. + * If the server swaps token systems this should allow compatability. + * + * @param item The input item. + * @return The tokens we should add. + */ + public String addTokensToExisting(ItemStack item) { + ItemStack clone = item.clone(); + ItemMeta meta = clone.getItemMeta(); + if (meta == null) { + return null; + } + PersistentDataContainer container = meta.getPersistentDataContainer(); + ArrayList tokens = new ArrayList<>(); + if (container.has(toolStats.playerKills)) { + tokens.add("player-kills"); + } + if (container.has(toolStats.mobKills)) { + tokens.add("mob-kills"); + } + if (container.has(toolStats.blocksMined)) { + tokens.add("blocks-mined"); + } + if (container.has(toolStats.cropsHarvested)) { + tokens.add("crops-mined"); + } + if (container.has(toolStats.fishCaught)) { + tokens.add("fish-caught"); + } + if (container.has(toolStats.sheepSheared)) { + tokens.add("sheep-sheared"); + } + if (container.has(toolStats.armorDamage)) { + tokens.add("damage-taken"); + } + if (container.has(toolStats.arrowsShot)) { + tokens.add("arrows-shot"); + } + if (container.has(toolStats.flightTime)) { + tokens.add("flight-time"); + } + if (tokens.isEmpty()) { + return null; + } + + return String.join(",", tokens); + } + + /** + * Check to see if a given container has our keys for stats. + * + * @param container The container. + * @return True/false if the container has keys. + */ + public boolean keyCheck(PersistentDataContainer container) { + return container.getKeys().stream() + .map(NamespacedKey::getKey) + .anyMatch(key -> toolStats.tokenKeys.stream().anyMatch(tokenKey -> tokenKey.getKey().equalsIgnoreCase(key))); + } } diff --git a/src/main/java/lol/hyper/toolstats/tools/ItemLore.java b/src/main/java/lol/hyper/toolstats/tools/ItemLore.java index 3fb5927..3948c7b 100644 --- a/src/main/java/lol/hyper/toolstats/tools/ItemLore.java +++ b/src/main/java/lol/hyper/toolstats/tools/ItemLore.java @@ -229,14 +229,29 @@ public class ItemLore { // if they don't exist, then start from 0 PersistentDataContainer container = meta.getPersistentDataContainer(); + // check for tokens + boolean validToken = toolStats.itemChecker.checkTokens(container, "crops-mined"); // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "crops-mined"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.cropsHarvested) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } Integer cropsMined = 0; @@ -244,6 +259,8 @@ public class ItemLore { cropsMined = container.get(toolStats.cropsHarvested, PersistentDataType.INTEGER); } + toolStats.logger.info("woooo!!!!!!!!!!"); + if (cropsMined == null) { cropsMined = 0; toolStats.logger.warning(clone + " does not have valid crops-mined set! Resting to zero. This should NEVER happen."); @@ -280,14 +297,28 @@ public class ItemLore { } PersistentDataContainer container = meta.getPersistentDataContainer(); + boolean validToken = toolStats.itemChecker.checkTokens(container, "blocks-mined"); // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "blocks-mined"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.blocksMined) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } // read the current stats from the item @@ -334,13 +365,28 @@ public class ItemLore { PersistentDataContainer container = meta.getPersistentDataContainer(); // check for tokens + boolean validToken = toolStats.itemChecker.checkTokens(container, "player-kills"); + // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "player-kills"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.playerKills) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } Integer playerKills = 0; @@ -385,13 +431,28 @@ public class ItemLore { PersistentDataContainer container = meta.getPersistentDataContainer(); // check for tokens + boolean validToken = toolStats.itemChecker.checkTokens(container, "mob-kills"); + // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "mob-kills"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.mobKills) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } Integer mobKills = 0; @@ -444,13 +505,28 @@ public class ItemLore { PersistentDataContainer container = meta.getPersistentDataContainer(); // check for tokens + boolean validToken = toolStats.itemChecker.checkTokens(container, "damage-taken"); + // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "damage-taken"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.armorDamage) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } Double damageTaken = 0.0; @@ -494,13 +570,28 @@ public class ItemLore { PersistentDataContainer container = meta.getPersistentDataContainer(); // check for tokens + boolean validToken = toolStats.itemChecker.checkTokens(container, "flight-time"); + // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "flight-time"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.flightTime) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } // read the current stats from the item @@ -547,13 +638,28 @@ public class ItemLore { PersistentDataContainer container = meta.getPersistentDataContainer(); // check for tokens + boolean validToken = toolStats.itemChecker.checkTokens(container, "sheep-sheared"); + // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "sheep-sheared"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.sheepSheared) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } Integer sheepSheared = 0; @@ -597,13 +703,28 @@ public class ItemLore { PersistentDataContainer container = meta.getPersistentDataContainer(); // check for tokens + boolean validToken = toolStats.itemChecker.checkTokens(container, "arrows-shot"); + // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "arrows-shot"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.arrowsShot) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } // read the current stats from the item @@ -650,13 +771,28 @@ public class ItemLore { PersistentDataContainer container = meta.getPersistentDataContainer(); // check for tokens + boolean validToken = toolStats.itemChecker.checkTokens(container, "fish-caught"); + // check for tokens if (toolStats.config.getBoolean("tokens.enabled")) { - // if the item has this token, then continue - // if the item does not, ignore - boolean validTokens = toolStats.itemChecker.checkTokens(container, "fish-caught"); - if (!validTokens) { + // if the item has stats but no token, add the token + if (container.has(toolStats.fishCaught) && !validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } + + // the item does not have a valid token + if (!validToken) { return null; } + } else { + if (!validToken) { + String newTokens = toolStats.itemChecker.addTokensToExisting(clone); + if (newTokens != null) { + container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens); + } + } } Integer fishCaught = 0;