Create custom enchant in 1.11

Discussion in 'Spigot Plugin Development' started by Dyouto, May 15, 2017.

  1. Hi,

    I was wondering how would it be possible to create custom enchant.

    I would to create enchantment for tools and display the enchant in lores.

    I have extended the Enchantment class:

    Code (Text):
    public class CustomEnchant extends Enchantment {
        public static CustomEnchant enchant = new CustomEnchant(197);

        public MultiToolEnchant(int id) {
            super(id);
        }

        public static void load() {
            try {
                try {
                    Field f = Enchantment.class.getDeclaredField("acceptingNew");
                    f.setAccessible(true);
                    f.set(null, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    Enchantment.registerEnchantment(enchant);
                } catch (IllegalArgumentException e) {
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       
       
       
        @Override
        public boolean canEnchantItem(ItemStack item) {
            return item.getType() == Material.DIAMOND_AXE
                || item.getType() == Material.DIAMOND_PICKAXE
                || item.getType() == Material.DIAMOND_SPADE
                || item.getType() == Material.DIAMOND_SWORD;
        }

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

        @Override
        public EnchantmentTarget getItemTarget() {
            return null;
        }

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

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

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

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

        @Override
        public boolean isTreasure() {
            return false;
        }
    }
    And what are the possibilities of ids for the constructor ?
    Code (Text):
    new CustomEnchant(197);
    Thank for your help.
     
  2. I've tried a bit to create custom enchant using this method. If you use id over 255, if a player put an item in an anvil, the client crash.
     
  3. I wouldn't extend the Enchantment class as it would cause problems. I would just use lores and check for the lore when looking for the custom enchant.
     
  4. It's much easier checking for custom enchant in this way.
     
  5. I saw tutorials in 1.8 but nothing for 1.11
     
  6. Most of the custom ench plugins test for a lore on the item, if it 'sees' the lore, it adds the effects that have been configured. I dont know how would you do this if you wanted something with custom behavior, cause this only works with potion effects. I know you COULD make it do something else if it sees the lore, but this is the simple way to make simple custom enchs.
     
  7. Yeah that's what I currently use