Skip to content

Commit 393f9b7

Browse files
committed
support for swapping modes
this makes sure the PDC matches when token mode is off so you can swap without issue
1 parent c597b4f commit 393f9b7

File tree

4 files changed

+262
-36
lines changed

4 files changed

+262
-36
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ public void onOpen(InventoryOpenEvent event) {
6262
}
6363
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
6464

65+
if (toolStats.config.getBoolean("tokens.enabled")) {
66+
// if the token system is on and the item doesn't have stat keys
67+
if (toolStats.itemChecker.keyCheck(container) && !container.has(toolStats.tokenType)) {
68+
// add the tokens
69+
String newTokens = toolStats.itemChecker.addTokensToExisting(itemStack);
70+
if (newTokens == null) {
71+
return;
72+
}
73+
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
74+
itemStack.setItemMeta(itemMeta);
75+
}
76+
}
77+
6578
// generate a hash if the item doesn't have one (if it's enabled in the config)
6679
if (toolStats.config.getBoolean("generate-hash-for-items")) {
6780
if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
@@ -85,6 +98,8 @@ public void onOpen(InventoryOpenEvent event) {
8598
if (location != null) {
8699
Bukkit.getRegionScheduler().runDelayed(toolStats, location, scheduledTask -> itemStack.setItemMeta(clone), 1);
87100
}
101+
102+
88103
}
89104
}
90105
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ public void onJoin(PlayerJoinEvent event) {
5858
}
5959
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
6060

61+
if (toolStats.config.getBoolean("tokens.enabled")) {
62+
// if the token system is on and the item doesn't have stat keys
63+
if (toolStats.itemChecker.keyCheck(container) && !container.has(toolStats.tokenType)) {
64+
// add the tokens
65+
String newTokens = toolStats.itemChecker.addTokensToExisting(itemStack);
66+
if (newTokens == null) {
67+
return;
68+
}
69+
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
70+
itemStack.setItemMeta(itemMeta);
71+
}
72+
}
73+
6174
// generate a hash if the item doesn't have one
6275
if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
6376
// make sure the item has an owner

src/main/java/lol/hyper/toolstats/tools/ItemChecker.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import lol.hyper.toolstats.ToolStats;
2121
import org.bukkit.Material;
22+
import org.bukkit.NamespacedKey;
2223
import org.bukkit.inventory.ItemStack;
2324
import org.bukkit.inventory.PlayerInventory;
2425
import org.bukkit.inventory.meta.ItemMeta;
@@ -280,4 +281,65 @@ public int getCost(String tokenType) {
280281

281282
return null;
282283
}
284+
285+
/**
286+
* Checks the keys of the item and returns the tokens we should add.
287+
* If the server swaps token systems this should allow compatability.
288+
*
289+
* @param item The input item.
290+
* @return The tokens we should add.
291+
*/
292+
public String addTokensToExisting(ItemStack item) {
293+
ItemStack clone = item.clone();
294+
ItemMeta meta = clone.getItemMeta();
295+
if (meta == null) {
296+
return null;
297+
}
298+
PersistentDataContainer container = meta.getPersistentDataContainer();
299+
ArrayList<String> tokens = new ArrayList<>();
300+
if (container.has(toolStats.playerKills)) {
301+
tokens.add("player-kills");
302+
}
303+
if (container.has(toolStats.mobKills)) {
304+
tokens.add("mob-kills");
305+
}
306+
if (container.has(toolStats.blocksMined)) {
307+
tokens.add("blocks-mined");
308+
}
309+
if (container.has(toolStats.cropsHarvested)) {
310+
tokens.add("crops-mined");
311+
}
312+
if (container.has(toolStats.fishCaught)) {
313+
tokens.add("fish-caught");
314+
}
315+
if (container.has(toolStats.sheepSheared)) {
316+
tokens.add("sheep-sheared");
317+
}
318+
if (container.has(toolStats.armorDamage)) {
319+
tokens.add("damage-taken");
320+
}
321+
if (container.has(toolStats.arrowsShot)) {
322+
tokens.add("arrows-shot");
323+
}
324+
if (container.has(toolStats.flightTime)) {
325+
tokens.add("flight-time");
326+
}
327+
if (tokens.isEmpty()) {
328+
return null;
329+
}
330+
331+
return String.join(",", tokens);
332+
}
333+
334+
/**
335+
* Check to see if a given container has our keys for stats.
336+
*
337+
* @param container The container.
338+
* @return True/false if the container has keys.
339+
*/
340+
public boolean keyCheck(PersistentDataContainer container) {
341+
return container.getKeys().stream()
342+
.map(NamespacedKey::getKey)
343+
.anyMatch(key -> toolStats.tokenKeys.stream().anyMatch(tokenKey -> tokenKey.getKey().equalsIgnoreCase(key)));
344+
}
283345
}

0 commit comments

Comments
 (0)