Solved Prevent applying Mending to certain items with certain NBT tag

Discussion in 'Spigot Plugin Development' started by tetratheta, Jul 31, 2021.

  1. I want to prevent applying Mending to certain items with certain NBT tag. Let's just say that the NBT tag is 'NoMending: 1b'.
    If any item has 'NoMending: 1b' NBT tag, it should not be applied with Mending.

    AFAIK, there are only two ways to apply enchantments to item. Enchanting Table and Anvil.

    For Enchanting Table part, I've found 'EnchantItemEvent' which is cancellable. But I'm not sure if this will prevent enchanting the item in Enchanting Table. What I want is, when player puts the item in Enchanting Table, it should not display 'enchant preview'-like thing in right side. It should be displayed as if we put stick(which is not enchantable item) in Enchanting Table. Right side should not be 'activated'.

    And for Anvil part, I've found 'PrepareAnvilEvent' (this isn't cancellable though). I'm not sure if I set the result via 'setResult' to AIR, will it be displayed as 'Not Appliable' with red X mark.

    In conclusion, I want them to be displayed as 'You cannot do this' as if we try to enchant stick(or any other non-enchantable item). For Anvil, it should display big red X mark, and for Enchanting Table, it should not 'activate' right side of the GUI.

    Are these possible? And if there any other way to apply enchantment to item other than Enchanting Table and Anvil, I want to prevent them too.
     
  2. You should try it out and see...
     
  3. You cant get mending from an enchanting table
     
  4. When the item matches (has the right tag) :

    • For PrepareAnvilEvent, you can do event.setResult(null);, I'm not sure it will display the red mark but it's a kind of cancel. Thus you can get PrepareAnvilEvent#getInventory() to get the current AnvilInventory and then modify the maximumRepairCost.

    • I think Mending is not available for Enchanting table. In case another plugin or datapack allows Mending in enchantment : there are two events : PrepareItemEnchantEvent (when the player puts an item on the table) and EnchantItemEvent (when the enchantment is done by click). Both are cancellable so I don't think you need to do tricks, try it by yourself. If it does not work is intended, you can get EnchantmentOffer and modify the cost. If you want to keep enchantment but only ignore Mending, you can just check if an offer contains Mending, if yes remove it.
     
    • Like Like x 1
    • Agree Agree x 1