|
18 | 18 | package lol.hyper.toolstats.events; |
19 | 19 |
|
20 | 20 | import lol.hyper.toolstats.ToolStats; |
| 21 | +import net.kyori.adventure.text.Component; |
21 | 22 | import org.bukkit.Material; |
22 | 23 | import org.bukkit.event.EventHandler; |
23 | 24 | import org.bukkit.event.EventPriority; |
@@ -89,6 +90,11 @@ public void onAnvilEvent(PrepareAnvilEvent event) { |
89 | 90 | return; |
90 | 91 | } |
91 | 92 |
|
| 93 | + if (tokenType.equalsIgnoreCase("remove")) { |
| 94 | + remove(event, clone); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
92 | 98 | // if the item is a mining tool |
93 | 99 | if (toolStats.itemChecker.isMineTool(firstSlotMaterial)) { |
94 | 100 | if (firstSlotMaterial.toString().toLowerCase(Locale.ROOT).contains("hoe")) { |
@@ -385,4 +391,155 @@ private void reset(PrepareAnvilEvent event, ItemStack inputItem) { |
385 | 391 | event.setResult(finalItem); |
386 | 392 | event.getView().setRepairCost(toolStats.itemChecker.getCost("reset")); |
387 | 393 | } |
| 394 | + |
| 395 | + /** |
| 396 | + * Remove all stats from an item. |
| 397 | + * |
| 398 | + * @param event The PrepareAnvilEvent event. |
| 399 | + * @param inputItem The input item to remove stats from. |
| 400 | + */ |
| 401 | + private void remove(PrepareAnvilEvent event, ItemStack inputItem) { |
| 402 | + ItemStack finalItem = inputItem.clone(); |
| 403 | + ItemMeta meta = finalItem.getItemMeta(); |
| 404 | + PersistentDataContainer container = meta.getPersistentDataContainer(); |
| 405 | + |
| 406 | + // remove the applied tokens |
| 407 | + if (container.has(toolStats.tokenApplied)) { |
| 408 | + container.remove(toolStats.tokenApplied); |
| 409 | + } |
| 410 | + |
| 411 | + if (container.has(toolStats.playerKills)) { |
| 412 | + Integer playerKills = container.get(toolStats.playerKills, PersistentDataType.INTEGER); |
| 413 | + if (playerKills == null) { |
| 414 | + return; |
| 415 | + } |
| 416 | + |
| 417 | + container.remove(toolStats.playerKills); |
| 418 | + |
| 419 | + String playerKillsFormatted = toolStats.numberFormat.formatInt(playerKills); |
| 420 | + Component lineToRemove = toolStats.configTools.formatLore("kills.player", "{kills}", playerKillsFormatted); |
| 421 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 422 | + finalItem.setItemMeta(meta); |
| 423 | + } |
| 424 | + if (container.has(toolStats.mobKills)) { |
| 425 | + Integer mobKills = container.get(toolStats.mobKills, PersistentDataType.INTEGER); |
| 426 | + if (mobKills == null) { |
| 427 | + return; |
| 428 | + } |
| 429 | + |
| 430 | + container.remove(toolStats.mobKills); |
| 431 | + |
| 432 | + String mobKillsFormatted = toolStats.numberFormat.formatInt(mobKills); |
| 433 | + Component lineToRemove = toolStats.configTools.formatLore("kills.mob", "{kills}", mobKillsFormatted); |
| 434 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 435 | + finalItem.setItemMeta(meta); |
| 436 | + } |
| 437 | + if (container.has(toolStats.blocksMined)) { |
| 438 | + Integer blocksMined = container.get(toolStats.blocksMined, PersistentDataType.INTEGER); |
| 439 | + if (blocksMined == null) { |
| 440 | + return; |
| 441 | + } |
| 442 | + |
| 443 | + container.remove(toolStats.blocksMined); |
| 444 | + |
| 445 | + String blocksMinedFormatted = toolStats.numberFormat.formatInt(blocksMined); |
| 446 | + Component lineToRemove = toolStats.configTools.formatLore("blocks-mined", "{blocks}", blocksMinedFormatted); |
| 447 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 448 | + finalItem.setItemMeta(meta); |
| 449 | + } |
| 450 | + if (container.has(toolStats.cropsHarvested)) { |
| 451 | + Integer cropsHarvested = container.get(toolStats.playerKills, PersistentDataType.INTEGER); |
| 452 | + if (cropsHarvested == null) { |
| 453 | + return; |
| 454 | + } |
| 455 | + |
| 456 | + container.remove(toolStats.cropsHarvested); |
| 457 | + |
| 458 | + String cropsHarvestedFormatted = toolStats.numberFormat.formatInt(cropsHarvested); |
| 459 | + Component lineToRemove = toolStats.configTools.formatLore("crops-harvested", "{crops}", cropsHarvestedFormatted); |
| 460 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 461 | + finalItem.setItemMeta(meta); |
| 462 | + } |
| 463 | + if (container.has(toolStats.fishCaught)) { |
| 464 | + Integer fishCaught = container.get(toolStats.fishCaught, PersistentDataType.INTEGER); |
| 465 | + if (fishCaught == null) { |
| 466 | + return; |
| 467 | + } |
| 468 | + |
| 469 | + container.remove(toolStats.fishCaught); |
| 470 | + |
| 471 | + String fishCaughtFormatted = toolStats.numberFormat.formatInt(fishCaught); |
| 472 | + Component lineToRemove = toolStats.configTools.formatLore("fished.fish-caught", "{fish}", fishCaughtFormatted); |
| 473 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 474 | + finalItem.setItemMeta(meta); |
| 475 | + } |
| 476 | + if (container.has(toolStats.sheepSheared)) { |
| 477 | + Integer sheepSheared = container.get(toolStats.sheepSheared, PersistentDataType.INTEGER); |
| 478 | + if (sheepSheared == null) { |
| 479 | + return; |
| 480 | + } |
| 481 | + |
| 482 | + container.remove(toolStats.sheepSheared); |
| 483 | + |
| 484 | + String sheepShearedFormatted = toolStats.numberFormat.formatInt(sheepSheared); |
| 485 | + Component lineToRemove = toolStats.configTools.formatLore("sheep.sheared", "{sheep}", sheepShearedFormatted); |
| 486 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 487 | + finalItem.setItemMeta(meta); |
| 488 | + } |
| 489 | + if (container.has(toolStats.armorDamage)) { |
| 490 | + Double armorDamage = container.get(toolStats.armorDamage, PersistentDataType.DOUBLE); |
| 491 | + if (armorDamage == null) { |
| 492 | + return; |
| 493 | + } |
| 494 | + |
| 495 | + container.remove(toolStats.armorDamage); |
| 496 | + |
| 497 | + String armorDamageFormatted = toolStats.numberFormat.formatDouble(armorDamage); |
| 498 | + Component lineToRemove = toolStats.configTools.formatLore("damage-taken", "{damage}", armorDamageFormatted); |
| 499 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 500 | + finalItem.setItemMeta(meta); |
| 501 | + } |
| 502 | + if (container.has(toolStats.damageDone)) { |
| 503 | + Double damageDone = container.get(toolStats.damageDone, PersistentDataType.DOUBLE); |
| 504 | + if (damageDone == null) { |
| 505 | + return; |
| 506 | + } |
| 507 | + |
| 508 | + container.remove(toolStats.damageDone); |
| 509 | + |
| 510 | + String damageDoneFormatted = toolStats.numberFormat.formatDouble(damageDone); |
| 511 | + Component lineToRemove = toolStats.configTools.formatLore("damage-done", "{damage}", damageDoneFormatted); |
| 512 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 513 | + finalItem.setItemMeta(meta); |
| 514 | + } |
| 515 | + if (container.has(toolStats.arrowsShot)) { |
| 516 | + Integer arrowsShot = container.get(toolStats.arrowsShot, PersistentDataType.INTEGER); |
| 517 | + if (arrowsShot == null) { |
| 518 | + return; |
| 519 | + } |
| 520 | + |
| 521 | + container.remove(toolStats.arrowsShot); |
| 522 | + |
| 523 | + String arrowsShotFormatted = toolStats.numberFormat.formatInt(arrowsShot); |
| 524 | + Component lineToRemove = toolStats.configTools.formatLore("arrows-shot", "{arrows}", arrowsShotFormatted); |
| 525 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 526 | + finalItem.setItemMeta(meta); |
| 527 | + } |
| 528 | + if (container.has(toolStats.flightTime)) { |
| 529 | + Long flightTime = container.get(toolStats.flightTime, PersistentDataType.LONG); |
| 530 | + if (flightTime == null) { |
| 531 | + return; |
| 532 | + } |
| 533 | + |
| 534 | + container.remove(toolStats.flightTime); |
| 535 | + |
| 536 | + String flightTimeFormatted = toolStats.numberFormat.formatDouble(flightTime); |
| 537 | + Component lineToRemove = toolStats.configTools.formatLore("flight-time", "{time}", flightTimeFormatted); |
| 538 | + meta.lore(toolStats.itemLore.removeLore(meta.lore(), lineToRemove)); |
| 539 | + finalItem.setItemMeta(meta); |
| 540 | + } |
| 541 | + |
| 542 | + event.setResult(finalItem); |
| 543 | + event.getView().setRepairCost(toolStats.itemChecker.getCost("remove")); |
| 544 | + } |
388 | 545 | } |
0 commit comments