Solved closing and opening doors

Discussion in 'Plugin Development' started by Yeowza, Mar 2, 2020.

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

    Yeowza

    Could somebody help me out with this? All of the google examples are from 2011 and the stuff is deprecated now. I see the door interface has a setOpen method, https://hub.spigotmc.org/javadocs/spigot/org/bukkit/block/data/type/Door.html, but it doesn't seem to work for me.

    Code:
            if (e.getClickedBlock() != null) {
                if (e.getClickedBlock().getType() == Material.SPRUCE_DOOR) {
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        Door d = (Door) e.getClickedBlock().getBlockData();
                        d.setOpen(false);
                        e.getClickedBlock().setBlockData(d);
                    }
                }  
            }
    No errors or anything, just doesn't work.

    i hate it when you figure out the answer right after posting for help :p

    Ok so I was using the playerInteract event for this and I set the code to run a few server ticks later with a bukkit scheduler and its working now, hope this helps someoneout.. there are a LOT of dud articles on google about this

    Code:
            if (e.getClickedBlock() != null) {
                if (e.getClickedBlock().getType() == Material.SPRUCE_DOOR) {
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        final Block b = e.getClickedBlock();
                        new BukkitRunnable() { @Override public void run() { Door d = (Door) b.getBlockData(); d.setOpen(false); d.setPowered(false); b.setBlockData(d); } }.runTaskLater(Main.plugin, 40);
                    }
                } 
            }
    Need to check if the task is set and cancel it though, i forsee a problem of players just clicking really fast on the door to bug it out

    EDIT, sorry, merged it

    Ok here is final code, checks to make sure player can not spam click the task and make sure sounds are working

    Code:
    // DOORS AUTOMATIC CLOSING after 2 seconds
    if (e.getClickedBlock() != null) {
    if (e.getClickedBlock().getType() == Material.SPRUCE_DOOR) {
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    final Block b = e.getClickedBlock();
    Door d = (Door) b.getBlockData();
    if (!d.isOpen()) {
    if (!b.hasMetadata("door")) {
    b.setMetadata("door", new FixedMetadataValue(Main.plugin, "opened"));
    new BukkitRunnable() {
    @Override public void run() {
    Door d = (Door) b.getBlockData();
    if (d.isOpen()) { Main.world.playSound(b.getLocation(), Sound.BLOCK_WOODEN_DOOR_CLOSE, 10, 1); }
    d.setOpen(false);
    b.setBlockData(d);
    b.removeMetadata("door", Main.plugin);
    }
    }.runTaskLater(Main.plugin, 40);
    }
    }
    }
    }
    }
    Ok here is final code, checks to make sure player can not spam click the task and make sure sounds are working

    Code:
            // DOORS AUTOMATIC CLOSING after 2 seconds
            if (e.getClickedBlock() != null) {
                if (e.getClickedBlock().getType() == Material.SPRUCE_DOOR) {
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        final Block b = e.getClickedBlock();
                        Door d = (Door) b.getBlockData();
                        if (!d.isOpen()) {
                            if (!b.hasMetadata("door")) {
                                b.setMetadata("door", new FixedMetadataValue(Main.plugin, "opened"));
                                new BukkitRunnable() {
                                    @Override public void run() {
                                        Door d = (Door) b.getBlockData();
                                        if (d.isOpen()) { Main.world.playSound(b.getLocation(), Sound.BLOCK_WOODEN_DOOR_CLOSE, 10, 1); }
                                        d.setOpen(false);
                                        b.setBlockData(d);
                                        b.removeMetadata("door", Main.plugin);
                                    }
                                }.runTaskLater(Main.plugin, 40);
                            }
                        }
                    }
                }   
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 3, 2020
Thread Status:
Not open for further replies.

Share This Page