Returning null when trying to check custom craft ingredients for enchantments

Discussion in 'Plugin Development' started by Swompd, Jul 13, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Swompd

    I'm trying to check if an item put in a crafting table has an enchantment to put on the result. What I'm doing here is making a Map of all of the ingredients, then checking if the ingredient with the '?' symbol (Which is the iron pickaxe) has an unbreaking enchantment on it. It keeps on returning null and I don't know how to fix it.

    Code:
    @SuppressWarnings("unchecked")
        public void redoluitIronRecipe() {
    
            @SuppressWarnings("deprecation")
            ShapedRecipe iDiamondRecipe = new ShapedRecipe(pick);
    
            pick.addUnsafeEnchantment(NEMain.r, 1);
            ItemMeta pickMeta = pick.getItemMeta();
            pickMeta.setLore(Arrays.asList(ChatColor.GRAY + "Redoluit 1"));
            pick.setItemMeta(pickMeta);
    
            Map<Character, ItemStack> ingredients = iDiamondRecipe.getIngredientMap();
    
            iDiamondRecipe.shape(" ^ ", "^?^", " ^ ");
            iDiamondRecipe.setIngredient('^', Material.LAVA_BUCKET);
            iDiamondRecipe.setIngredient('?', Material.IRON_PICKAXE);
         
            if (ingredients.get('?').containsEnchantment(Enchantment.DURABILITY)) {
                System.out.println("Has Unbreaking!");
            } else {
                return;
            }
    
            Bukkit.addRecipe(iDiamondRecipe);
        }
     
    Last edited: Jul 13, 2022
  2. Offline

    gochi9

    Could you show us the code for
    Code:
    iDiamondRecipe.getIngredientMap();
    From what I understood you get a NullPointerException when you check
    Code:
    ingredients.get('?').containsEnchantment(Enchantment.DURABILITY)
    There can be a few things here. First the map itself could be null, second there is no ? key in your map and third the value (ItemStack) for the key ? is null.
     
Thread Status:
Not open for further replies.

Share This Page