org.bukkit.event.EventException but plugin works fine

Discussion in 'Plugin Development' started by Jimfutsu, Apr 20, 2014.

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

    Jimfutsu

    Hello, I have created a plugin called sword arrow, I plan to release it on bukkit dev soon, but currently, on my test server it says
    Code:
    [15:07:41 ERROR]: Could not pass event PlayerInteractEvent to SwordArrow v1.0
    org.bukkit.event.EventException
    I have seen plugins use catchIOexception is that what gets rid of the org.bukkit.event.EventException message?

    I will not release code unless absolutely necessary.

    Thanks!
     
  2. Offline

    ButterSquidz

    Could you show us the full stack trace?
     
  3. Offline

    Plo124

  4. Offline

    Wizehh

    You can only catch something if it has a chance of being thrown. Anyway, I'm almost positive that your problem is this: you're calling a get method on something which is null; for example, 'Item.getDisplayName()' will return null (and throw an eventexception) if the display name of the item doesn't exist.
     
  5. Offline

    Jimfutsu

    Wizehh Plo124 ButterSquidz I can give the code, as long as no one will copy it. and Wizehh, how do I fix it?

    package me.jimfutsu.BukkitProject;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    public class SwordArrow extends JavaPlugin implements Listener{
    public void onEnable(){
    getServer().getPluginManager().registerEvents(this, this);
    }

    public void onDisable(){
    }

    @EventHandler
    public void onRightClick(PlayerInteractEvent e)
    {
    Player p = e.getPlayer();
    if ((e.getAction() == Action.RIGHT_CLICK_AIR) || ((e.getAction() == Action.RIGHT_CLICK_BLOCK))) {
    if (p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Diamond Sword - Bow")){
    p.launchProjectile(Arrow.class);
    }
    else if (p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GRAY + "Iron Sword - Bow")) {
    p.launchProjectile(Arrow.class);
    }
    else if (p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GRAY + "Stone Sword - Bow")) {
    p.launchProjectile(Arrow.class);
    }
    else if (p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GRAY + "Wood Sword - Bow")) {
    p.launchProjectile(Arrow.class);
    }
    }
    }

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player p = (Player) sender;
    if(commandLabel.equalsIgnoreCase("SwordArrow")) {
    if (args.length == 0){
    p.sendMessage(ChatColor.RED + "Command Usage");
    p.sendMessage(ChatColor.RED + "/SwordArrow give Dsword - gives player a diamond sword that shoots arrows.");
    p.sendMessage(ChatColor.RED + "/SwordArrow give Isword - gives player an iron sword that shoots arrows.");
    p.sendMessage(ChatColor.RED + "/SwordArrow give Ssword - gives player a stone sword that shoots arrows.");
    p.sendMessage(ChatColor.RED + "/SwordArrow give Wsword - gives player a wood sword that shoots arrows.");
    }
    else if (args.length == 2) {
    if (args[0].equalsIgnoreCase("give")) {
    if (args[1].equalsIgnoreCase("Dsword")) {
    ItemStack Dsword = new ItemStack(Material.DIAMOND_SWORD, 1);
    ItemMeta DswordI = Dsword.getItemMeta();
    DswordI.setDisplayName(ChatColor.GREEN + "Diamond Sword - Bow");
    Dsword.setItemMeta(DswordI);
    p.getInventory().addItem(Dsword);
    }
    else if (args[1].equalsIgnoreCase("Isword")){
    ItemStack Isword = new ItemStack(Material.IRON_SWORD, 1);
    ItemMeta IswordI = Isword.getItemMeta();
    IswordI.setDisplayName(ChatColor.GRAY + "Iron Sword - Bow");
    Isword.setItemMeta(IswordI);
    p.getInventory().addItem(Isword);
    }
    else if (args[1].equalsIgnoreCase("Ssword")){
    ItemStack Ssword = new ItemStack(Material.STONE_SWORD, 1);
    ItemMeta SswordI = Ssword.getItemMeta();
    SswordI.setDisplayName(ChatColor.GRAY + "Stone Sword - Bow");
    Ssword.setItemMeta(SswordI);
    p.getInventory().addItem(Ssword);
    }
    else if (args[1].equalsIgnoreCase("Wsword")){
    ItemStack Wsword = new ItemStack(Material.WOOD_SWORD, 1);
    ItemMeta WswordI = Wsword.getItemMeta();
    WswordI.setDisplayName(ChatColor.GRAY + "Wood Sword - Bow");
    Wsword.setItemMeta(WswordI);
    p.getInventory().addItem(Wsword);
    }

    }
    }
    }
    return true;
    }
    }

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

    Plo124

    Jimfutsu
    Sorry for the longish wait

    The main problem here is that you are not checking if the item has a displayname first.

    Put this code under the Player p = e.getPlayer() line;
    Code:java
    1. if (p.getItemInHand().getType() == Material.AIR || p.getItemInHand() == null) return;
    2. if (!(p.getItemInHand().hasItemMeta() && p.getItemInHand().getItemMeta().hasDisplayName())) return;
     
  7. Offline

    Jimfutsu

    Plo124

    May I ask what the return does? and isn't it supposed to be continued with a {?
     
  8. Offline

    Garris0n

    Return exits the method. You don't need the braces for one line, although it's generally a nicer format to put the "content" on an indented line after the if statement.
     
  9. Offline

    Plo124

    Jimfutsu
    Return stops running the code underneath,
    You dont need the { }'s because there is only that 1 line of code to execute;
     
  10. Offline

    Zethariel

    For future reference, dear OP, if you need help from someone and they can help you, you can assume that they can write better code than you (ie. bug free), hence the chance of them "stealing" anything is miniscule, borderline nill.
     
  11. Offline

    Jimfutsu

  12. Offline

    Plo124

    Zethariel
    I get lots of bugs in my code,
    But now that I know how to read stacktraces I can isolate the problem to 1 line.
     
Thread Status:
Not open for further replies.

Share This Page