Making Iron Doors open and close without redstone

Discussion in 'Plugin Development' started by Perdog, Nov 7, 2011.

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

    Perdog

    I had an idea to make it possible to open iron doors with a lever in your hand, other then having to run redstone everywhere.
    Problem is, I cant get the door to open or close

    Here's my player listener:
    Code:java
    1. public class listener extends PlayerListener {
    2. public void onPlayerInteract (PlayerInteractEvent event) {
    3. ItemStack held = event.getPlayer().getItemInHand();
    4. Action action = event.getAction();
    5. Block clicked = event.getClickedBlock();
    6. if (clicked.getType() == Material.IRON_DOOR_BLOCK) {
    7. Player player = event.getPlayer();
    8. player.sendMessage("Its an iron Door");
    9. if (action == Action.RIGHT_CLICK_BLOCK) {
    10. player.sendMessage("You right clocked it");
    11. if (held.getType() == Material.LEVER) {
    12. player.sendMessage("and your holding a lever");
    13. Door door = (Door) clicked.getState().getData();
    14. if (door.isTopHalf() == true || door.isTopHalf() ==false) {
    15. if (door.isOpen() == false) {
    16. player.sendMessage("Doors closed. Data is: " + door);
    17. door.setOpen(true);
    18. }
    19. else {
    20. player.sendMessage("Doors open. Data is: " + door);
    21. door.setOpen(false);
    22. }
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }


    Side note: Please excuse all the debug messages I added in. They all work, it runs through all the if's and sends me the data, but will not open the door :/
     
  2. Offline

    DDoS

    This condition:

    PHP:
    if (door.isTopHalf() == true || door.isTopHalf() == false) {
    is pointless. It will always return true. You should start by removing it. Also, try updating the block state.
     
  3. Offline

    Perdog

    I threw that in there randomly while trying to get it to work to see if it would do anything (It didn't, as you know lol) Already been taken out. And okay, will give that a try :) thank you

    Updating did not work. To make sure I did it the way you meant, here's the new playerlistener
    Code:java
    1. public class listener extends PlayerListener {
    2. public void onPlayerInteract (PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. ItemStack held = player.getItemInHand();
    5. Action action = event.getAction();
    6. Block clicked = event.getClickedBlock();
    7. if (clicked.getType() == Material.IRON_DOOR_BLOCK) {
    8. player.sendMessage("Its an iron Door");
    9. if (action == Action.RIGHT_CLICK_BLOCK) {
    10. player.sendMessage("You right clocked it");
    11. if (held.getType() == Material.LEVER) {
    12. clicked.getState();
    13. player.sendMessage("and your holding a lever");
    14. Door door = (Door) clicked.getState().getData();
    15. if (door.isOpen() == false) {
    16. player.sendMessage("Doors closed. Data is: " + door);
    17. door.setOpen(true);
    18. clicked.getState().update();
    19. }
    20. else {
    21. player.sendMessage("Doors open. Data is: " + door);
    22. door.setOpen(false);
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  4. Offline

    nisovin

    You shouldn't get a new BlockState and update that, that does nothing. You should update the BlockState you already have (door). Do door.update().

    Edit: Kidding. You'll need to save the state before getting the data. Then you change the data, then update the state.
     
  5. Offline

    Perdog

    How do I go about saving it? In a hashmap or arraylist or something?
     
  6. Offline

    nisovin

    No, just store it in a variable.
    Code:
    public class listener extends PlayerListener {
        public void onPlayerInteract (PlayerInteractEvent event) {
            Player player = event.getPlayer();
            ItemStack held = player.getItemInHand();
            Action action = event.getAction();
            Block clicked = event.getClickedBlock();
            if (clicked.getType() == Material.IRON_DOOR_BLOCK) {
                player.sendMessage("Its an iron Door");
                if (action == Action.RIGHT_CLICK_BLOCK) {
                    player.sendMessage("You right clocked it");
                    if (held.getType() == Material.LEVER) {
                        player.sendMessage("and your holding a lever");
                        BlockState state = clicked.getState();
                        Door door = (Door) state.getData();
                        if (door.isOpen() == false) {
                            player.sendMessage("Doors closed. Data is: " + door);
                            door.setOpen(true);
                        }
                        else {
                            player.sendMessage("Doors open. Data is: " + door);
                            door.setOpen(false);
                        }
                        state.update();
                    }
                }
            }
        }
    }
    
     
  7. Offline

    Perdog

    AH thank you :D Works ... kinda :p Opens like a split door, i.e Click top, top opens, bottom stays closed, and vice versa. I'm sure it won't be hard to also update the above or bellow block :)
     
Thread Status:
Not open for further replies.

Share This Page