Solved Detect if a door is being closed or being opened

Discussion in 'Plugin Development' started by Jogy34, Sep 30, 2012.

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

    Jogy34

    I was wondering how I would go about checking if someone was closing a door. I have tried testing in an onPlayerInteractEvent if the block they clicked was a door and if it was then cast the MaterialData of the block to a Door then try to test if the the door is open or if it isn't open with these:
    Code:
    if(door.isOpen())
    {
    //stuff
    }
     
    OR
     
    if(!door.isOpen)
    {
    //Stuff
    }
    
    The first one seemed to almost always return true whether or not I was closing or opening the door and the second one only returned true every 8 or so times I closed the door.

    Anyone know a better way to test this?
     
  2. Offline

    Muddr

    The top and bottom half of the door acts differently to isOpen(). test it with this.
    Code:
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
            BlockState blockState = block.getState();
            Door door = (Door) blockState.getData();
            BlockState otherHalfState;
            event.getPlayer().sendMessage("----------------");
            if (door.isTopHalf()) {
                player.sendMessage("TopHalf");
                otherHalfState = block.getRelative(BlockFace.DOWN).getState();
            }
            else {
                player.sendMessage("BottomHalf");
                otherHalfState = block.getRelative(BlockFace.UP).getState();         
            }
            Door otherHalf = (Door) otherHalfState.getData();
            player.sendMessage("Door open?: " + door.isOpen());
            player.sendMessage("Door (otherHalf) open?: " + otherHalf.isOpen());
     
  3. Offline

    Jogy34

    Thanks. I found out that the top half always returns true and that the bottom half returns the right values.
     
  4. use this:
    Code:
    Block clicked = event.getClickedBlock();
                    Door door2 = (Door) event.getClickedBlock().getState().getData();
                    if (door2.isTopHalf())
                        clicked = clicked.getRelative(BlockFace.DOWN);
    
    that way you will always get the bottom half of the door when you call the clicked block, as long as it's after that statement. Place it above the blockstate and alter it to match yours
     
    kroltan likes this.
  5. You guys should avoid using getState() for such a simple thing, the call is pretty expensive.

    Instead, you can use:
    Code:
    new Door(0, block.getData());
    to generate the Door object.

    It would be even faster to just check the data values directly, see: http://www.minecraftwiki.net/wiki/Data_values#Doors
     
  6. Offline

    Jogy34

    I already fixed this with the help of Muddr
     
  7. Well, if you don't care about the quality of your code then whatever :)
     
    kroltan likes this.
  8. Offline

    sunnydan

    Is there any way to test for the same thing, except if an NPC villager is opening/closing a door instead of a player?
     
Thread Status:
Not open for further replies.

Share This Page