open Iron Door on Click

Discussion in 'Plugin Development' started by Razdom, Aug 13, 2013.

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

    Razdom

    i do that but its not work:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Action action = event.getAction();
    4. Block clicked = event.getClickedBlock();
    5. if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
    6. if (clicked.getTypeId() == 71) {
    7. clicked.getState();
    8. Door door = (Door) clicked.getState().getData();
    9. door.setOpen(true);
    10. if (door.isOpen() == false) {
    11. door.setOpen(true);
    12. clicked.getState().update();
    13. }
    14. else {
    15. door.setOpen(false);
    16. clicked.getState().update();
    17. }
    18. }
    19. }
    20. }
     
  2. Offline

    Axe2760

    Before doing anything, you need to check to see if the block is a door.
    Code:
    if (event.getClickedBlock().getType().equals(material for iron door) && event.getAction().equals(Action.RIGHT_CLICK_BLOCK){
      Door door = (Door)event.getClickedBlock().getState();
      if (door.isOpen() == true){
        door.setOpen(false);
    //et cetera, you can do the rest.
    }
    }
    
    Haven't tested it or looked at the javadocs, it may be wrong.

    Edit Major derp. Missed a line in your code, will be more careful :|
     
  3. Offline

    Razdom

    no its not work.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Action action = event.getAction();
    4. Block clicked = event.getClickedBlock();
    5. if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
    6. if (clicked.getTypeId() == 71) {
    7. Door door = (Door) clicked .getState();
    8. if (door.isOpen() == false) {
    9. door.setOpen(true);
    10. clicked.getState().update();
    11. }
    12. else {
    13. door.setOpen(false);
    14. clicked.getState().update();
    15. }
    16. }
    17. }
    18. }


    the error in:
    Code:java
    1. Door door = (Door) clicked.getState();
     
  4. Offline

    danielmiles

    Razdom this seems to work, only for right clicking the bottom, to do both just check if the data is 8 (top door) then get the block below it using block.getRelative(BlockFace.DOWN) and then switch it
    Code:
    public void playerInteract(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block block = event.getClickedBlock();
                if (block.getType() == Material.IRON_DOOR_BLOCK) {
                    if (block.getData() == (byte) 0) {
                        block.setData((byte) 4);
                        return;
                    } else if (block.getData() == (byte) 4) {
                        block.setData((byte) 0);
                        return;
                    }
                }
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page