|
19 | 19 |
|
20 | 20 | import lol.hyper.toolstats.ToolStats; |
21 | 21 | import org.bukkit.Material; |
| 22 | +import org.bukkit.NamespacedKey; |
22 | 23 | import org.bukkit.inventory.ItemStack; |
23 | 24 | import org.bukkit.inventory.PlayerInventory; |
24 | 25 | import org.bukkit.inventory.meta.ItemMeta; |
@@ -280,4 +281,65 @@ public int getCost(String tokenType) { |
280 | 281 |
|
281 | 282 | return null; |
282 | 283 | } |
| 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 | + } |
283 | 345 | } |
0 commit comments