Resource Custom Enchants and Essentials (NamespacedKey.Minecraft)

Discussion in 'Spigot Plugin Development' started by TDStuart, Mar 12, 2020.

  1. Important For Anyone Registering Enchantments!
    Add Compatibility with Essentials /enchant and kits! + Other Plugins

    Code (Java):
    public class CustomEnchant extends Enchantment implements Listener{

        public CustomEnchant(String namespace) {
            super(NamespacedKey.minecraft(namespace));
        }
       
        @Override
        public boolean canEnchantItem(ItemStack arg0) {
            return true;
        }

        @Override
        public boolean conflictsWith(Enchantment arg0) {
            return false;
        }

        @Override
        public EnchantmentTarget getItemTarget() {
            return EnchantmentTarget.ARMOR_FEET;
        }

        @Override
        public int getMaxLevel() {
            return 2;
        }

        @Override
        public String getName() {
            return "customenchant";
        }

        @Override
        public int getStartLevel() {
            return 1;
        }

        @Override
        public boolean isCursed() {
            return false;
        }

        @Override
        public boolean isTreasure() {
            return false;
        }
       
    }
     

    This Code above, specifically the NamespacedKey("minecraft", namespace) will allow your enchantment to be compatible with /enchant and kits.

    Make sure NAMESPACE is the same as the getName()

    How Does This Work?
    Instead of registering the enchantment with a NamespacedKey comprised with your plugin and the enchantment name, the NamespacedKey goes to the NamespacedKey.minecraft with your enchantment name. This will allow other plugins to easily access your enchantment. This is how Essentials gets enchantments, so yours will be accessible to essentials.

    Using in Other Plugins:
    Code (Java):
    Enchantment CustomEnchant = Enchantment.getByKey(NamespacedKey.minecraft("customenchant"));

    The code above can be used by any plugin to get your custom enchantment.

    Note :
    Although the method is deprecated one of the moderators said that it shouldn't be.
    Also this will not fix /enchant tab complete not working for custom enchants.
    Also note that any lore you may have wanted to go onto the item (To show item enchantment and level) will not be applied with /enchant but lore that is already on the item will be put on kits.

    Edit 1:
    Changed code to use "NamespacedKey.minecraft(key)", not deprecated.
     
    #1 TDStuart, Mar 12, 2020
    Last edited: Mar 12, 2020