NullPointerException on recipes load

Discussion in 'Plugin Development' started by TheRedCat, Apr 2, 2020.

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

    TheRedCat

    I'm making plugin which adds stone generators, but I have a problem with custom recipes. I can craft only coal tier generator and other not works. Could you help me?

    Here's some info:


    logs:
    Code:
    java.lang.NullPointerException: null
        at org.bukkit.inventory.ShapedRecipe.getShape(ShapedRecipe.java:194) ~[patched_1.15.2.jar:git-Paper-121]
        at org.bukkit.craftbukkit.v1_15_R1.inventory.CraftShapedRecipe.fromBukkitRecipe(CraftShapedRecipe.java:33) ~[patched_1.15.2.jar:git-Paper-121]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.addRecipe(CraftServer.java:1143) ~[patched_1.15.2.jar:git-Paper-121]
        at me.rudykot.stonegen.StoneGenPlugin.loadRecipes(StoneGenPlugin.java:95) ~[?:?]
        at me.rudykot.stonegen.StoneGenPlugin.onEnable(StoneGenPlugin.java:45) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[patched_1.15.2.jar:git-Paper-121]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) ~[patched_1.15.2.jar:git-Paper-121]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:435) ~[patched_1.15.2.jar:git-Paper-121]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:470) ~[patched_1.15.2.jar:git-Paper-121]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:384) ~[patched_1.15.2.jar:git-Paper-121]
        at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:482) ~[patched_1.15.2.jar:git-Paper-121]
        at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:298) ~[patched_1.15.2.jar:git-Paper-121]
        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:885) ~[patched_1.15.2.jar:git-Paper-121]
        at java.lang.Thread.run(Thread.java:830) [?:?]
    Plugin main:
    Code:
    /*
    *
    * This source code is under CC BY-NC-SA 4.0 license
    *
    * you can check license details at:
    *
    * https://creativecommons.org/licenses/by-nc-sa/4.0/
    *
    */
    
    
    package me.rudykot.stonegen;
    
    import java.util.Map;
    
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import me.rudykot.stonegen.random.RandomChooser;
    
    public class StoneGenPlugin extends JavaPlugin{
       
        public static FileConfiguration config;
       
        public static ItemStack coalGen, lapisGen, diamondGen;
       
        public static Map<Material,RandomChooser<ItemStack>> choosers;
        @Override
        public void onEnable() {
            saveDefaultConfig();
            config =getConfig();
           
            coalGen = getConfigItem(Material.COAL_ORE,"generators.items");
            lapisGen = getConfigItem(Material.LAPIS_ORE,"generators.items");
            diamondGen = getConfigItem(Material.DIAMOND_ORE,"generators.items");
           
            loadRecipes();
           
            getServer().getPluginManager().registerEvents(new EventListener(), this);
           
        }
       
        @Override
        public void onDisable() {
            // TODO Auto-generated method stub
            super.onDisable();
        }
       
        public static ItemStack getConfigItem(Material m,String path) {
            String realpath=path+'.'+m.toString();
            ItemStack i=new ItemStack(m);
            ItemMeta im=i.getItemMeta();
           
            im.setDisplayName(config.getString(realpath+".name"));
            im.setLore(config.getStringList(realpath+".lore"));
           
            if(config.getBoolean(realpath+".enchanted")) {
                i.addEnchantment(Enchantment.DURABILITY, 1);
            }
           
            i.setItemMeta(im);
            return i;
        }
       
    
        public void loadRecipes() {
            NamespacedKey coalkey = new NamespacedKey(this, "generator_I");
           
            ShapedRecipe IGen = new ShapedRecipe(coalkey,coalGen);
            IGen.shape("rir", "iSi", "rPr");
            IGen.setIngredient('S', Material.STONE);
            IGen.setIngredient('i', Material.IRON_INGOT);
            IGen.setIngredient('r', Material.REDSTONE);
            IGen.setIngredient('P', Material.PISTON);
    
           
            getServer().addRecipe(IGen);  // This is working ^^^
           
            NamespacedKey lapiskey = new NamespacedKey(this, "generator_II");
           
            ShapedRecipe IIGen = new ShapedRecipe(lapiskey,lapisGen);
            IGen.shape("RGR", "GIG", "RPR");
            IGen.setIngredient('G', Material.COAL_ORE);
            IGen.setIngredient('I', Material.IRON_BLOCK);
            IGen.setIngredient('R', Material.REDSTONE_BLOCK);
            IGen.setIngredient('P', Material.PISTON);
    
           
            getServer().addRecipe(IIGen);
           
            NamespacedKey diamondkey = new NamespacedKey(this, "generator_III");
           
            ShapedRecipe IIIGen = new ShapedRecipe(diamondkey, diamondGen);
            IGen.shape("dGd", "GIG", "dPd");
            IGen.setIngredient('G', Material.LAPIS_ORE);
            IGen.setIngredient('I', Material.IRON_BLOCK);
            IGen.setIngredient('d', Material.DIAMOND);
            IGen.setIngredient('P', Material.PISTON);
    
           
            getServer().addRecipe(IIIGen);
           
        }
       
    }
    
     
  2. Offline

    BrittleMind

    I think you have copied and pasted your code and forgot to edit the variable names.
    You never set the shape or ingredient for "IIGen" & "IIIGen"

    Edit: I see how long ago this was posted now, ouch
     
Thread Status:
Not open for further replies.

Share This Page