Solved Door.setOpen() not working...

Discussion in 'Spigot Plugin Development' started by PandaMan, May 27, 2016.

  1. I am working on a plugin that checks to see if a door was clicked by a user. In my method, I check to see if the block the user clicked is a door and then attempt open it if it is closed, or close it if it is opened (these are IRON DOORS). When I use the .setOpen(<boolean>) method, nothing happens. However, if I use the deprecated .setData() on the block and set the bit to toggle, then it works. I went through the source for Door.java and it looks as if this should work, but it doesn't.

    Doesn't Work....
    Code (Text):
    if(blk.getType() == Material.WOODEN_DOOR || blk.getType() == Material.IRON_DOOR_BLOCK) {
    System.out.println("Player Clicked Door");
    Door door  = (Door) blk.getState().getData();
    if(door.isTopHalf()) {
       System.out.println("Top Half was clicked....getting bottom half");
       door = (Door) blk.getRelative(BlockFace.DOWN).getState().getData();
       blk = blk.getRelative(BlockFace.DOWN);
    }
    System.out.println("IsTopHalf: " + door.isTopHalf());
    System.out.println("Is Opened: " + door.isOpen());

    if(door.isOpen()) {
       System.out.println("Attempting to close door");
       door.setOpen(false);
    } else {
       System.out.println("Attempting to close door");
       door.setOpen(true);
    }
    }
    Does Work...
    Code (Text):
    if(blk.getType() == Material.WOODEN_DOOR || blk.getType() == Material.IRON_DOOR_BLOCK) {
    System.out.println("Player Clicked Door");
    Door door  = (Door) blk.getState().getData();
    if(door.isTopHalf()) {
       System.out.println("Top Half was clicked....getting bottom half");
       door = (Door) blk.getRelative(BlockFace.DOWN).getState().getData();
       blk = blk.getRelative(BlockFace.DOWN);
    }
    System.out.println("IsTopHalf: " + door.isTopHalf());
    System.out.println("Is Opened: " + door.isOpen());

    if(door.isOpen()) {
       System.out.println("Attempting to close door");
       blk.setData((byte) (blk.getData() ^ 0x4));
    } else {
       System.out.println("Attempting to close door");
       blk.setData((byte) (blk.getData() ^ 0x4));
    }
    }
     
  2. Just do Door door = (Door)blk.getState(); instead of using the get data method.
     
  3. So you want make a message on door open.
     
  4. You need to store the blockState to yet from blk, update it via which ever interface is appropriate for that block and then call .update(). What you're doing in the non-working code is getting a _copy_ of the door state and updating it which will _not_ cause it to change in the game.
     
  5. You sir, are a genius! That did it. Thank you very much.