Door opening event

Discussion in 'Plugin Development' started by Nagetier, Mar 30, 2012.

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

    Nagetier

    Hey guys,
    just a short question (I guess)

    Is there a Listener or Event getting activated as soon as a door or trapdoor status changes? I'd like to monitor every door getting opened or closed by a player and/or redstone.

    How do I manage this? Thanks in advance!
     
  2. Offline

    SirTyler

    As far as I know there is no event for opening or closing a door, you could however listen for when the play clicks on the door and when redstone activates the door.
     
  3. Offline

    Nagetier

    Thanks for your advice, should be working but I somehow made a mistake, throws me a bunch of errors when opening a door :-/

    Code:
        @EventHandler
        public void onPlayerDoorOpen(PlayerInteractEvent event)
        {
            Action action = event.getAction();
            Block clicked = event.getClickedBlock();
                 
            //Left or Right click?
            if ((action == Action.RIGHT_CLICK_BLOCK) || (action == Action.LEFT_CLICK_BLOCK))
            {
                //Door Block?
                if((clicked.getType() == Material.IRON_DOOR) || (clicked.getType() == Material.WOODEN_DOOR) || (clicked.getType() == Material.TRAP_DOOR))
                {
                    log.info("click");
                 
                }
                else{    }
            }
            else{    }
        }
    The console says: "Could not pass PlayerEvent...."
    What is it I'm doing wrong?
     
  4. Offline

    nisovin

    Please post the whole error message. And tell us which line it says the error is on.
     
  5. Offline

    Nagetier

    Sorry my bad, forgot to define the Logger ;)

    But now I got another problem, I checked if the clicked block is a door, well it is a door, and my code calls another method which should be getting the facing of the door and the materials in front and behind, I'm stuck at the point of transfering the Block or the Material to the class Door.

    That's what I got so far, I found the getState() somwhere here in the forums, but I'm not quiet shure if its the right thing... I couldn't find a code snippet doing something similiar.
    Code:
        private void doorCheck(Block doorblock)
        {
            Door door = doorblock.getState();
            Location loc = doorblock.getLocation();
                   
            door.setOpen(false);
        }
    Obviously this isn't working at all...

    (doorblock is the block touched by the player and identified as a door as seen in the code snippet above)
     
  6. Offline

    Sabersamus

    try if(doorblock.getState() instanceof Door){
     
  7. Offline

    Nagetier

    Where am I supposed to use that and how, I'm sorry, pretty new to java :-/
    Is this a method to check wether my block is a door or not, instead of checking every single material?
     
  8. Offline

    Sabersamus

    where it says Door door = doorblock.getState(); you dont know thatsits a door, yet, so above it put

    if(doorBlock.getState() instanceof Door){
    Door door = (Door)doorblock.getState();
    Location loc = door.getLocation();
    door.setOpen(false);
    }
    }
     
  9. Offline

    Nagetier

    Ah thank you very much, this is working, but it turns out, that the door I right click is not a door, why? I'm pretty sure I'm clicking that door ;)
     
  10. Offline

    Technius

    You need to get the material data from the door.
    Code:java
    1. Door d = (Door) btype.getNewData(block.getData());

    Make sure you have a block variable.
     
  11. Offline

    Nagetier

    Whats the difference between your code and mine?
    Code:java
    1. Door door = (Door)doorblock.getState();


    //Well I see, your code is working and mine not :D but what is your code actually doing, please try to help me understand this a little more :-/
     
Thread Status:
Not open for further replies.

Share This Page