Help with PrepareItemCraftEvent

Discussion in 'Plugin Development' started by Atmoist, Oct 13, 2020.

  1. Offline

    Atmoist

    Code:
    @EventHandler
       
        public void onPrepareItemCraft(PrepareItemCraftEvent event) {
           
                CraftingInventory inv = event.getInventory();
                ItemStack[] items = inv.getMatrix();
                if (!items[1].getItemMeta().getDisplayName().equals(ItemManager.darkemerald.getItemMeta().getDisplayName())
                        || !items[2].getType().equals(Material.NETHERITE_INGOT)
                        || !items[5].getType().equals(Material.STICK)
                        || !items[8].getType().equals(Material.STICK)){
                    event.getInventory().setResult(new ItemStack(Material.AIR));
                } else {
                    event.getInventory().setResult(new ItemStack(ItemManager.scythe));
                }
           
        }
    This is the code I'm using to try and create a custom recipe using a custom item (darkemerald). I'm new to making plugins and am not really sure what I'm doing wrong. I have no errors in eclipse, when I try to add things to a crafting interface in game, however, I get an error saying org.bukkit.event.EventException: null. How can I go about fixing this? Is there a better way to to go about adding custom items to custom recipes?
     
  2. Offline

    Strahan

    The null is because you are assuming all those elements are in the items array, when they may not be. Also you are assuming the item in slot 1 has meta/display name when it may not.

    Lastly, the default is to not have a crafting target so there is no need to do the negative check and setting of air, just check if the ingredients are correct then set the custom item.
     

Share This Page