Problem with Crafting Recipes/Inheritance

Discussion in 'Plugin Development' started by thok13, Nov 5, 2013.

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

    thok13

    I'm making a plugin with spells. There's a base class for spells which each spell inherits. Each spell has it's own item with a custom display name and crafting recipe. I add each spell type to a List<> of spells. When trying to add crafting recipes (from an overridden method), I get a null pointer exception. Here is the code:
    The base spell:
    Code:java
    1. public abstract class MagicType
    2. {
    3. public abstract ItemStack getStack();
    4. public abstract ShapedRecipe getRecipe();
    5. public abstract void cast(Player p);
    6. }
    7.  

    Here is an example of a spell:
    Code:java
    1. public class DamageNear extends MagicType
    2. {
    3. Plugin p;
    4. public DamageNear(Plugin p)
    5. {
    6. this.p = p;
    7. }
    8. @Override
    9. public ItemStack getStack()
    10. {
    11. return BloodMagics.setDisplay(new ItemStack(Material.REDSTONE_TORCH_ON), ChatColor.RED + "Damage Nearby", "Damage all nearby mobs, excluding other vampires");
    12. }
    13. @Override
    14. public ShapedRecipe getRecipe()
    15. {
    16. ShapedRecipe recipe = new ShapedRecipe(this.getStack());
    17. recipe.shape(new String[]{"A","B"});
    18. recipe.setIngredient('A', Material.ENDER_PEARL);
    19. recipe.setIngredient('B', Material.REDSTONE_TORCH_ON);
    20. return recipe;
    21. }
    22. @Override
    23. public void cast(Player p)
    24. {
    25. p.getWorld().playSound(p.getLocation(), Sound.FIZZ, 1, 1);
    26. p.getWorld().playEffect(p.getEyeLocation(), Effect.SMOKE, 30);
    27. int rad = 5;
    28. if(VPlayer.get(p).isBloodlusting())
    29. {
    30. rad = 7;
    31. }
    32. for(Entity en : p.getNearbyEntities(rad, rad, rad))
    33. {
    34. if(en instanceof LivingEntity)
    35. {
    36. LivingEntity le = (LivingEntity)en;
    37. if(!BloodMagics.isVampire(le))
    38. {
    39. le.getWorld().playEffect(le.getLocation(), Effect.MOBSPAWNER_FLAMES, 0);
    40. le.damage(3.0, p);
    41. }
    42. }
    43. }
    44. }
    45. }
    46.  

    Here's the code for adding recipes:
    Code:java
    1. for(MagicType m : magic)
    2. {
    3. plugin.getServer().addRecipe(m.getRecipe());
    4. }

    I've been doing java for a while, I've just gotten rusty, so some help would be appreciated.

    Also I can post the error from the log if needed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    metalhedd

    Post the actual stack trace, as well as the source for BloodMagics.setDisplay
     
  3. Offline

    thok13

    Also I should have mentioned that this is a vampire plugin, which extends MassiveCraft's vampire plugin/
     
  4. Offline

    metalhedd

    No idea what that plugin is.. but the full error message is pretty important for anyone to attempt to debug this for you
     
  5. Offline

    thok13

    setDisplay:
    Code:java
    1. public static ItemStack setDisplay(ItemStack i, String name, String desc )
    2. {
    3. ItemMeta meta = i.getItemMeta();
    4. meta.setDisplayName(name);
    5. List<String> l = new ArrayList<String>();
    6. l.add(desc);
    7. meta.setLore(l);
    8. i.setItemMeta(meta);
    9. return i;
    10. }

    The error:
    Code:
    2013-11-05 18:04:20 [SEVERE] Error occurred while enabling BetterVampires v0 (Is it up to date?)
    java.lang.NullPointerException
        at tk.thokcraft.bettervampires.BloodMagics.addRecipes(BloodMagics.java:56)
        at tk.thokcraft.bettervampires.Main.onEnable(Main.java:31)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:382)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.java:288)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.java:270)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:618)
        at org.bukkit.Bukkit.reload(Bukkit.java:277)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:24)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:532)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchServerCommand(CraftServer.java:519)
        at net.minecraft.server.v1_6_R3.DedicatedServer.as(DedicatedServer.java:276)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:241)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:493)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:425)
    Line 56:
    Code:java
    1. plugin.getServer().addRecipe(m.getRecipe());
     
  6. Offline

    metalhedd

    Ah... I think you need to chain the recipe methods.. like this;

    Code:java
    1.  
    2. @Override
    3. public ShapedRecipe getRecipe()
    4. {
    5. return new ShapedRecipe(this.getStack()).shape("A","B").setIngredient('A', Material.ENDER_PEARL).setIngredient('B', Material.REDSTONE_TORCH_ON);
    6.  
    7. }
    8.  


    the reason being that each method returns a new modified recipe, so the one that you're returning has no shape or ingredients defined.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  7. Offline

    thok13

    I just realized the problem:
    BloodMagics.addRecipes();
    BloodMagics.plugin = this;
    Meaning plugin was null...
    Thanks for your help :3

    That was in my OnEnable*

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  8. Offline

    metalhedd

    Okay, that's definitely a problem as well, but I think the one I pointed out is also a real issue, if your recipes don't work, that would be why.
     
Thread Status:
Not open for further replies.

Share This Page