Solved Add Enchantment as Soon as Item is Crafted

Discussion in 'Spigot Plugin Development' started by CrazyTaco, Jul 20, 2018.

  1. I have the following code:

    Code (Text):
        @EventHandler
        public void onCraft(CraftItemEvent e) {
           
            if(e.getRecipe().getResult().getType() == Material.DIAMOND_BOOTS) {
               
                e.getRecipe().getResult().addEnchantment(Enchantment.PROTECTION_FALL, 5);
               
            }
           
        }
    For some reason nothing happens when I use this code. Anyone have any ideas?
     
  2. Try:
    Code (Text):
    ItemStack result = e.getRecipe().getResult().clone();
    ItemMeta meta = e.getRecipe().getItemMeta();
    meta.addEnchant(Enchantment.PROTECTION_FALL, 4, false);
    result.setItemMeta(meta);
    event.setResult(result);
     
    • Informative Informative x 1
  3. on event.setResult(), result cant be an ItemStack, it has to be an object called a Result.
     
  4. Oh. Sry, try this :D

    Code (Text):
    @EventHandler
        public void on(PrepareItemCraftEvent e) {
            if(e.getRecipe().getResult().getType() == Material.DIAMOND_BOOTS) {
                ItemStack result = e.getRecipe().getResult().clone();
                ItemMeta meta = result.getItemMeta();
                meta.addEnchant(Enchantment.PROTECTION_FALL, 4, false);
                result.setItemMeta(meta);
                e.getInventory().setResult(result);
            }
        }
     
    • Winner Winner x 1
  5. Wow this worked better than I even thought, thanks so much man!
     
  6. No problem, now make the "Solved" tag, before the title :)
     
  7. Forgot, new to Spigot Forums haha.

    Thank you so much!
     
  8. why do you clone the item? I never understood why you have to clone; I only know it wouldn't work without that..
     
  9. When i call the "clone" method, then get i a new itemstack with the same settings.
    Example: When the result has custom nbt tags, and you will create a new, then you must set the custom nbt tag.
    When you call the "clone" method, then not ;)
     
  10. what the heck is NBTTag ._. still don't understand you