PrepareItemCraftEvent Bug

Discussion in 'Plugin Development' started by TheChugBug, May 12, 2014.

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

    TheChugBug

    When I try cancelling the PrepareItemCraftEvent, it throws me an error. This is my code:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void onCraft(PrepareItemCraftEvent e) {
    3. final ItemStack[] mat = e.getInventory().getMatrix();
    4. if (e.getInventory().getResult().equals(setName(new ItemStack(Material.BREAD), ChatColor.WHITE + "Superbread", null))) {
    5. if (mat[4].getType() == Material.SEEDS && mat[4].getItemMeta().getDisplayName().equals("Superseeds")) {
    6. e.getInventory().setResult(setName(new ItemStack(Material.BREAD), ChatColor.WHITE + "Superbread", null));
    7. } else {
    8. e.getInventory().setResult(null);
    9. }
    10. }
    11. }
    12.  

    Please help!

    (I also tried with
    Code:java
    1. e.getInventory.setResult(new ItemStack(Material.Air))
    )

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

    metalhedd

    did you READ the error? did you understand it? what about it didn't you understand?
     
  3. Offline

    TheChugBug

    This is my error: http://pastebin.com/ZCGqpsNW
    I'm fairly new to plugin development and I don't know how to decipher errors well but I know the error comes from line 8 in the code I showed.
     
  4. Offline

    metalhedd

    is that line 105 of EatListener.java? can you post the full stack trace when you change it to
    Code:
    new ItemStack(Material.AIR)
     
  5. Offline

    TheChugBug

    It's line 110 of the class.
     
  6. Offline

    metalhedd

    your error is coming from line 105
     
  7. Offline

    TheChugBug

    Line 105 is this:
    Code:java
    1. if (mat[3].getType() == Material.SEEDS && mat[3].getItemMeta().getDisplayName().equals("Superseeds")
     
  8. Offline

    metalhedd

    Code:
    .getItemMeta().getDisplayName()
    Both of these method calls can return null. you need to check for null first.
     
  9. Offline

    TheChugBug

    Thank you, it works now!
     
  10. Offline

    Garris0n

    You should check out what hasItemMeta and getItemMeta actually do, if I read correctly it will only return null for an air ItemStack.
     
  11. Offline

    metalhedd


    You're right, getItemMeta will create a new instance if there isn't one defined. doesn't matter if it's air or not, any Itemstack. still best to check hasItemMeta first.
     
  12. Offline

    Garris0n

  13. Offline

    metalhedd

Thread Status:
Not open for further replies.

Share This Page