ItemMetadataService.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.gmail.nossr50.metadata;
  2. import com.gmail.nossr50.mcMMO;
  3. import org.bukkit.inventory.ItemStack;
  4. import org.bukkit.inventory.meta.ItemMeta;
  5. import org.bukkit.persistence.PersistentDataContainer;
  6. import org.bukkit.persistence.PersistentDataType;
  7. import org.jetbrains.annotations.NotNull;
  8. import java.util.List;
  9. import static com.gmail.nossr50.metadata.MetadataService.NSK_SUPER_ABILITY_BOOSTED_ITEM;
  10. public class ItemMetadataService {
  11. public final @NotNull String LEGACY_ABILITY_TOOL_LORE = "mcMMO Ability Tool";
  12. public final @NotNull mcMMO pluginRef;
  13. public ItemMetadataService(@NotNull mcMMO pluginRef) {
  14. this.pluginRef = pluginRef;
  15. }
  16. public void setSuperAbilityBoostedItem(@NotNull ItemStack itemStack, int originalDigSpeed) {
  17. if (itemStack.getItemMeta() == null) {
  18. mcMMO.p.getLogger().severe("Can not assign persistent data to an item with null item metadata");
  19. return;
  20. }
  21. ItemMeta itemMeta = itemStack.getItemMeta();
  22. PersistentDataContainer dataContainer = itemMeta.getPersistentDataContainer();
  23. dataContainer.set(NSK_SUPER_ABILITY_BOOSTED_ITEM, PersistentDataType.INTEGER, originalDigSpeed);
  24. itemStack.setItemMeta(itemMeta);
  25. }
  26. public boolean isSuperAbilityBoosted(@NotNull ItemStack itemStack) {
  27. if (itemStack.getItemMeta() == null)
  28. return false;
  29. ItemMeta itemMeta = itemStack.getItemMeta();
  30. //Get container from entity
  31. PersistentDataContainer dataContainer = itemMeta.getPersistentDataContainer();
  32. //If this value isn't null, then the tool can be considered dig speed boosted
  33. Integer boostValue = dataContainer.get(NSK_SUPER_ABILITY_BOOSTED_ITEM, PersistentDataType.INTEGER);
  34. return boostValue != null;
  35. }
  36. public int getSuperAbilityToolOriginalDigSpeed(@NotNull ItemStack itemStack) {
  37. //Get container from entity
  38. ItemMeta itemMeta = itemStack.getItemMeta();
  39. if (itemMeta == null)
  40. return 0;
  41. PersistentDataContainer dataContainer = itemMeta.getPersistentDataContainer();
  42. if (dataContainer.get(NSK_SUPER_ABILITY_BOOSTED_ITEM, PersistentDataType.INTEGER) == null) {
  43. mcMMO.p.getLogger().severe("Value should never be null for a boosted item");
  44. return 0;
  45. } else {
  46. //Too lazy to make a custom data type for this stuff
  47. Integer boostValue = dataContainer.get(NSK_SUPER_ABILITY_BOOSTED_ITEM, PersistentDataType.INTEGER);
  48. return Math.max(boostValue, 0);
  49. }
  50. }
  51. public void removeBonusDigSpeedOnSuperAbilityTool(@NotNull ItemStack itemStack) {
  52. int originalSpeed = getSuperAbilityToolOriginalDigSpeed(itemStack);
  53. ItemMeta itemMeta = itemStack.getItemMeta();
  54. if(itemMeta != null) {
  55. //TODO: can be optimized
  56. if (itemMeta.hasEnchant(mcMMO.p.getEnchantmentMapper().getEfficiency())) {
  57. itemMeta.removeEnchant(mcMMO.p.getEnchantmentMapper().getEfficiency());
  58. }
  59. if (originalSpeed > 0) {
  60. itemMeta.addEnchant(mcMMO.p.getEnchantmentMapper().getEfficiency(), originalSpeed, true);
  61. }
  62. PersistentDataContainer dataContainer = itemMeta.getPersistentDataContainer();
  63. dataContainer.remove(NSK_SUPER_ABILITY_BOOSTED_ITEM); //Remove persistent data
  64. //TODO: needed?
  65. itemStack.setItemMeta(itemMeta);
  66. }
  67. }
  68. public boolean isLegacyAbilityTool(@NotNull ItemStack itemStack) {
  69. ItemMeta itemMeta = itemStack.getItemMeta();
  70. if (itemMeta == null)
  71. return false;
  72. List<String> lore = itemMeta.getLore();
  73. if (lore == null || lore.isEmpty())
  74. return false;
  75. return lore.contains(LEGACY_ABILITY_TOOL_LORE);
  76. }
  77. public @NotNull String getLegacyAbilityToolLore() {
  78. return LEGACY_ABILITY_TOOL_LORE;
  79. }
  80. }