Chest still opens when event is cancelled.

Discussion in 'Plugin Development' started by agodwin987, Mar 17, 2013.

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

    agodwin987

    So I have this code:
    Code:
    if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && event.getClickedBlock().getType().equals(Material.CHEST) && chestGroup.contains(p.getName())){
                event.setCancelled(true);
                Location loc = event.getClickedBlock().getLocation();
                Chest chest = (Chest) loc.getBlock().getState();
                Inventory inv = chest.getInventory();
                p.openInventory(inv);
                p.sendMessage("You opened an invisible chest");
            }
    The issue is, the chest still opens event though the event is cancelled. The interesting part of this is that the event is cancelled, because when the the chest code is removed the chest does not open. Any ideas on how I could stop this from happening?
     
  2. Offline

    Tirelessly

    Try setting the priority to highest
     
  3. Offline

    BajanAmerican

    @EventHandler
    public void onPlayerRightClickEvent(Action event){
    CODEEEEEEEE
    }
     
  4. Offline

    Codisimus

    This is the only way I have been able to do it without it glitching. not ideal but you don't really have another option.
     
    BajanAmerican likes this.
  5. Offline

    agodwin987

    The issue isn't that the event isn't being cancelled, because when the code looks like this
    Code:
    if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && event.getClickedBlock().getType().equals(Material.CHEST) && chestGroup.contains(p.getName())){
                event.setCancelled(true);
                Location loc = event.getClickedBlock().getLocation();
                //Chest chest = (Chest) loc.getBlock().getState();
                //Inventory inv = chest.getInventory();
                //p.openInventory(inv);
                p.sendMessage("You opened an invisible chest");
            }
    The chest does not open. Opening the inventory seems to open the chest.
     
  6. Offline

    Adzwoolly

    I don't know if this is just me but...
    aren't you just cancelling the event and then getting the cancelled chest location, getting the cancelled chest, getting the cancelled chest's inventory and then making the player open it?
    So essentially opening the cancelled chest as normal?
    If I am completely wrong, please tell me...
     
  7. Offline

    agodwin987

    The point of this is to hide the opening animation and sound effect by getting the location of the chest and opening the inventory. It is designed so invisible admins can open chests without players seeing it.
     
  8. Offline

    Adzwoolly

    OK, thank you. I thought I was missing something!
     
  9. DONT use .equals. Use ==.
    .equals is for strings (and some other things)

    For example:
    Code:
    event.getClickedBlock().getType() == Material.CHEST
     
  10. Offline

    Suraski

    Grave digging, but no it's not... .equals is used to compare objects, not just strings.
     
    Zach_1919 likes this.
Thread Status:
Not open for further replies.

Share This Page