Solved "Cannot have null choice" error with custom crafting recipe

Discussion in 'Spigot Plugin Help' started by AppleSauceTea, Jun 19, 2021.

Thread Status:
Not open for further replies.
  1. I am trying to make custom crafting recipes for my plugin but when I try to launch the server I use to test the plugin I get this error:

    Code (Java):
    [20:01:12 ERROR]: Error occurred while enabling RPGItems v1.0 (Is it up to date?)
    java.lang.IllegalArgumentException: Cannot have null choice
            at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.inventory.RecipeChoice$ExactChoice.<init>(RecipeChoice.java:168) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.inventory.RecipeChoice$ExactChoice.<init>(RecipeChoice.java:157) ~[patched_1.16.5.jar:git-Paper-778]
            at com.applesaucetea.rpgitems.items.RPGItemsItems.createSentinelHammer(RPGItemsItems.java:53) ~[?:?]
            at com.applesaucetea.rpgitems.items.RPGItemsItems.init(RPGItemsItems.java:22) ~[?:?]
            at com.applesaucetea.rpgitems.RPGItems.onEnable(RPGItems.java:21) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:518) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:432) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.loadWorld(MinecraftServer.java:594) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.DedicatedServer.init(DedicatedServer.java:298) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1069) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291) ~[patched_1.16.5.jar:git-Paper-778]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_261]
    Here is the class in which I create the items and custom recipes:
    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.*;
    import org.bukkit.inventory.meta.ItemMeta;

    import java.util.ArrayList;
    import java.util.List;

    public class RPGItemsItems {

        public static ItemStack sentinelHammer;
        public static ItemStack basicAlloy;
        public static ItemStack refinedAlloy;

        public static void init() {
            createSentinelHammer();
            createBasicAlloy();
            createRefinedAlloy();
        }

        private static void createSentinelHammer() {
            ItemStack item = new ItemStack(Material.IRON_AXE, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("§9Sentinel Hammer");
            List<String> lore = new ArrayList<>();
            lore.add("§7This hammer was used by the Sentinels of old");
            lore.add("§7and is crafted with Refined Alloys.");
            lore.add("");
            lore.add("§6§lItem Ability: Sentinel Smite §c§l(RIGHT CLICK)");
            lore.add("§7Generate a powerful explosion that kills entities nearby.");
            lore.add("§bCooldown: 30 seconds");
            lore.add("");
            lore.add("§9§lRARE ITEM");
            meta.setLore(lore);
            meta.setUnbreakable(true);
            meta.addEnchant(Enchantment.LUCK, 1, true);
            meta.addEnchant(Enchantment.DAMAGE_ALL, 7, true);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            item.setItemMeta(meta);
            sentinelHammer = item;

            //Shaped Recipe
            ShapedRecipe shr = new ShapedRecipe(NamespacedKey.minecraft("sentinelhammer"), item);
            shr.shape(" RR", " SR", " S ");
            shr.setIngredient('B', new RecipeChoice.ExactChoice(refinedAlloy));
            shr.setIngredient('S', Material.STICK);
            Bukkit.getServer().addRecipe(shr);
        }

        private static void createBasicAlloy() {
            ItemStack item = new ItemStack(Material.IRON_NUGGET, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("§fBasic Alloy");
            List<String> lore = new ArrayList<>();
            lore.add("§7An unrefined alloy which can");
            lore.add("§7be refined through smelting");
            lore.add("");
            lore.add("§f§lCOMMON ITEM");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.LUCK, 1, true);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            item.setItemMeta(meta);
            basicAlloy = item;

            //Shapeless Recipe
            ShapelessRecipe ba = new ShapelessRecipe(NamespacedKey.minecraft("basicalloy"), item);
            ba.addIngredient(2, Material.IRON_INGOT);
            ba.addIngredient(2, Material.DIAMOND);
            Bukkit.getServer().addRecipe(ba);
        }

        private static void createRefinedAlloy() {
            ItemStack item = new ItemStack(Material.IRON_INGOT, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("§aRefined Alloy");
            List<String> lore = new ArrayList<>();
            lore.add("§7A refined alloy used in");
            lore.add("§7many recipes.");
            lore.add("");
            lore.add("§a§lUNCOMMON ITEM");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.LUCK, 1, true);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            item.setItemMeta(meta);
            refinedAlloy = item;

            //Furnace Recipe
            FurnaceRecipe ra = new FurnaceRecipe(NamespacedKey.minecraft("refinedalloy"), item, new RecipeChoice.ExactChoice(basicAlloy), 5.0f, 10 * 20);
            Bukkit.getServer().addRecipe(ra);
        }
    }
    Does anyone know how to fix this? I am new to Java and Plugins and I cannot seem to figure this out.
     
  2. refinedAlloy is null
     
  3. what could I do to fix this?
     
  4. By assigning an instance.
     
  5. Code (Java):
    ShapedRecipe shr = new ShapedRecipe(NamespacedKey.minecraft("sentinelhammer"), item);
    shr.shape(" RR", " SR", " S ");
    shr.setIngredient('B', new RecipeChoice.ExactChoice(refinedAlloy));
    shr.setIngredient('S', Material.STICK);
    Bukkit.getServer().addRecipe(shr);
    You do not set an ingredient for 'R' in #createSentinelHammer
     
  6. Thank you so much for pointing that out! However, I still get the same error
    what would the code be for this?
     
  7. Code (Java):
    shr.setIngredient('B', new RecipeChoice.ExactChoice(refinedAlloy));
    Also just realized that this is in #createSentinelHammer, which is called before #createRefinedAlloy, hence why refinedAlloy is null.
     
  8. THANK YOU SO MUCH! It finaly works! So sorry to bother you with this. You have taught a valuable lesson for my future coding extravaganzas.
     
Thread Status:
Not open for further replies.