Solved EventException

Discussion in 'Plugin Development' started by Markyroson, Jun 30, 2015.

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

    Markyroson

    How do I resolve/catch a bukkit event exception?
     
  2. Offline

    Markyroson

  3. Offline

    Rahat

    Markyroson likes this.
  4. Offline

    Markyroson

    Code:
    try {
            if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
                return;
            }
    
            Block block = (Block) event.getClickedBlock();
            Chest chest = (Chest) block.getState();
    
            if (!chest.getType().equals(Material.CHEST)) {
                System.out.println("NOT A CHEST!");
                return;
            }
           
            //if (!chest.getBlockInventory().getTitle().equals("Infinite Chest")) {
            if (chest.getBlockInventory().getTitle().equals("Infinite Chest")) {
                event.setCancelled(true);
                player.sendMessage("Woo! It works! :)");
                Inventories.getChestInv(player);
            } else if(chest.getBlockInventory().getTitle().equals("The Warrior")) {
                event.setCancelled(true);
                Inventories.LoadOuts.getWarrior(player);
            } else if(chest.getBlockInventory().getTitle().equals("The Knight")) {
                event.setCancelled(true);
                Inventories.LoadOuts.getKnight(player);
            } else if(chest.getBlockInventory().getTitle().equals("The Bowman")) {
                event.setCancelled(true);
                Inventories.LoadOuts.getBowman(player);
            } else if(chest.getBlockInventory().getTitle().equals("The Archer")) {
                event.setCancelled(true);
                Inventories.LoadOuts.getArcher(player);
            } else if(chest.getBlockInventory().getTitle().equals("The Explorer")) {
                event.setCancelled(true);
                Inventories.LoadOuts.getExplorer(player);
            }
        } catch (EventException e) {
            throw new EventException();
        }
     
  5. Let me explain what the EventException is:
    An EventException is thrown by bukkit if a registered listener handles an event, but throws an error. When an EventException is thrown, is always has a cause. If you look down in a stacktrace you can see the original cause, there you can see the exception that caused the EventException and you can catch that one in a try catch statement.
    EDIT: You can find this on the stacktrace page that is stickied on the plugin development page. It's an example of CommandException, it has the same concept as EventException: http://i.imgur.com/ARsQk.jpg
     
    Markyroson likes this.
  6. Offline

    Markyroson

    The thing is is that in the stack trace it points to that segment of code I posted saying that it threw the event exception. yet apparently it cannot? I am confused :-(

    EDIT: It is thrown stictly when placing a chest or right clicking, well....anything other than a chest, the line it directly points to is in the above
    Code:
    Chest chest = (Chest) block.getState();
    The stack trace then says "CraftBlockState cannot be cast to org.bukkit.block.Chest", the thing is however that the code has to be there. Is there any way to simply catch it so that it does not dump the entire stack trace?
     
    Last edited: Jul 1, 2015
  7. Offline

    Markyroson

    I found out the error. I was assuming it was a chest before checking it, thanks for all your help though! :p;):)
     
Thread Status:
Not open for further replies.

Share This Page