support for swapping modes

this makes sure the PDC matches when token mode is off so you can swap without issue
This commit is contained in:
hyperdefined
2025-01-26 18:51:36 -05:00
parent c597b4ffeb
commit 393f9b74c0
4 changed files with 262 additions and 36 deletions

View File

@@ -62,6 +62,19 @@ public class InventoryOpen implements Listener {
} }
PersistentDataContainer container = itemMeta.getPersistentDataContainer(); 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) // 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 (toolStats.config.getBoolean("generate-hash-for-items")) {
if (!container.has(toolStats.hash, PersistentDataType.STRING)) { if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
@@ -85,6 +98,8 @@ public class InventoryOpen implements Listener {
if (location != null) { if (location != null) {
Bukkit.getRegionScheduler().runDelayed(toolStats, location, scheduledTask -> itemStack.setItemMeta(clone), 1); Bukkit.getRegionScheduler().runDelayed(toolStats, location, scheduledTask -> itemStack.setItemMeta(clone), 1);
} }
} }
} }
} }

View File

@@ -58,6 +58,19 @@ public class PlayerJoin implements Listener {
} }
PersistentDataContainer container = itemMeta.getPersistentDataContainer(); 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 // generate a hash if the item doesn't have one
if (!container.has(toolStats.hash, PersistentDataType.STRING)) { if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
// make sure the item has an owner // make sure the item has an owner

View File

@@ -19,6 +19,7 @@ package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats; import lol.hyper.toolstats.ToolStats;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@@ -280,4 +281,65 @@ public class ItemChecker {
return null; 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<String> 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)));
}
} }

View File

@@ -229,14 +229,29 @@ public class ItemLore {
// if they don't exist, then start from 0 // if they don't exist, then start from 0
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "crops-mined");
// check for tokens // check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.cropsHarvested) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "crops-mined"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; return null;
} }
} else {
if (!validToken) {
String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
} }
Integer cropsMined = 0; Integer cropsMined = 0;
@@ -244,6 +259,8 @@ public class ItemLore {
cropsMined = container.get(toolStats.cropsHarvested, PersistentDataType.INTEGER); cropsMined = container.get(toolStats.cropsHarvested, PersistentDataType.INTEGER);
} }
toolStats.logger.info("woooo!!!!!!!!!!");
if (cropsMined == null) { if (cropsMined == null) {
cropsMined = 0; cropsMined = 0;
toolStats.logger.warning(clone + " does not have valid crops-mined set! Resting to zero. This should NEVER happen."); 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(); PersistentDataContainer container = meta.getPersistentDataContainer();
boolean validToken = toolStats.itemChecker.checkTokens(container, "blocks-mined");
// check for tokens // check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.blocksMined) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "blocks-mined"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; 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 // read the current stats from the item
@@ -334,13 +365,28 @@ public class ItemLore {
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens // check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "player-kills");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.playerKills) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "player-kills"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; return null;
} }
} else {
if (!validToken) {
String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
} }
Integer playerKills = 0; Integer playerKills = 0;
@@ -385,13 +431,28 @@ public class ItemLore {
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens // check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "mob-kills");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.mobKills) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "mob-kills"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; return null;
} }
} else {
if (!validToken) {
String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
} }
Integer mobKills = 0; Integer mobKills = 0;
@@ -444,13 +505,28 @@ public class ItemLore {
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens // check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "damage-taken");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.armorDamage) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "damage-taken"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; 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; Double damageTaken = 0.0;
@@ -494,13 +570,28 @@ public class ItemLore {
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens // check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "flight-time");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.flightTime) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "flight-time"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; 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 // read the current stats from the item
@@ -547,13 +638,28 @@ public class ItemLore {
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens // check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "sheep-sheared");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.sheepSheared) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "sheep-sheared"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; return null;
} }
} else {
if (!validToken) {
String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
} }
Integer sheepSheared = 0; Integer sheepSheared = 0;
@@ -597,13 +703,28 @@ public class ItemLore {
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens // check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "arrows-shot");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.arrowsShot) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "arrows-shot"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; 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 // read the current stats from the item
@@ -650,13 +771,28 @@ public class ItemLore {
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens // check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "fish-caught");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) { if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue // if the item has stats but no token, add the token
// if the item does not, ignore if (container.has(toolStats.fishCaught) && !validToken) {
boolean validTokens = toolStats.itemChecker.checkTokens(container, "fish-caught"); String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (!validTokens) { if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null; return null;
} }
} else {
if (!validToken) {
String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
} }
Integer fishCaught = 0; Integer fishCaught = 0;