How to toggle Doors and gates

Discussion in 'Spigot Plugin Development' started by timyboy, Jul 2, 2016.

  1. Hi there. I am making a little plugin which is supposed to toggle doors 4 seconds after you open them. I just don't know how to toggle doors and gates. I am using the player interact event for this.
     
  2. I believe what you can do is run a delayed task once the user right clicks the block and then cast the block data to (Openable) and close it that way. (Correct me if I'm wrong).
     
    • Agree Agree x 1
  3. If you look here - https://hub.spigotmc.org/javadocs/spigot/org/bukkit/material/Door.html
    The door class represents a door in your game, and implements Openable. This means that the door class has a way to control whether it is open or not.
    If you get the block that the user interacted with, and you know for sure that it is a door (by checking the Material probably), then you can cast it to a Door object.
    I am not sure how exactly this would work, but I assume the event fires before the door's state updates, meaning that, if the door was closed before, and the player interacts with it, the state of the door would be closed when the event is called, and if the door was open, the state would be open.
    If that is the case, I suggest you then check whether the door is open, meaning that the player is attempting to close the door, and in that case do nothing (Imagine the player opened the door and then closed after himself like a good citizen. But then your plugin came in and change the state of the door after 4 seconds - the door would still stay closed, but it might glitch (not sure)).
    In the other case, where the door state is closed, use the setOpen method and schedule it with the bukkit scheduler to run later in 4 seconds (80 ticks)
     
  4. So, I read the spigotmc javadoc about doors, but still don't really know how to use it. Could someone give an example?
     
  5. FormallyMyles

    Supporter

    Code (Java):
    BlockState state = block.getState();
    MaterialData data = state.getData();
    Openable openable = (Openable) data;
    if (!openable.isOpen()) {
        openable.setOpen(true);
        state.update();
    }
    Should help, where block is a block you've got in your world that's a door.
     
    • Useful Useful x 1
  6. So I now have
    Block block = e.getClickedBlock();
    BlockState state = block .getState();
    MaterialData data = state.getData();
    Openable openable = (Openable) data;
    if (!openable.isOpen()) {
    p.sendMessage("Info van deur: " + openable);
    openable.setOpen(true);
    state.update();
    }

    How can I make it so it also works when you click on the top half of the block?
     
  7. FormallyMyles

    Supporter

    Listen for the interact event? then detect if the block below it's a door part and the current part is a door part?