How to add cooldown to this event?

Discussion in 'Spigot Plugin Help' started by szymek1223, Jul 12, 2021.

  1. How can I add 1 second cooldown to this event? The point is for the block to turn into air after 1 second after the player enters the block

    package tntrun.events;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import tntrun.main.Main;
    public class onPlayerMove implements Listener {

    static Main plugin;
    public onPlayerMove(Main m) {
    plugin = m;
    }
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {

    Player p = event.getPlayer();


    if(p.getLocation().subtract(0, 1, 0).getBlock().getType() == Material.SAND) {
    p.getLocation().subtract(0, 1, 0).getBlock ().setType(Material.AIR);
    p.getLocation().subtract(0, 2, 0).getBlock().setType(Material.AIR);



    }
    }

    }
     
  2. Please use the code block provided by Spigot

    Use BukkitRunable for this
     
  3. I tried but it didn't work. Would you be able to do something like this?
     
  4. Because it does not work?
     
  5. ??
     
  6. This work for me :coffee:
    Although maybe there is a better way

    Code (Java):
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {

            Player p = event.getPlayer();

            if(p.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.SAND) return;

            new BukkitRunnable() {
                @Override
                public void run() {
                    p.getLocation().subtract(0, 1, 0).getBlock ().setType(Material.AIR);
                    p.getLocation().subtract(0, 2, 0).getBlock().setType(Material.AIR);
                }
            }.runTaskLater(plugin, 40);
        }
     

  7. It works, but I don't think you understood me exactly. I mean, the player will stand on some block and after a second it turns into air and so with each block
     
  8. Strahan

    Benefactor

    Only if you stand perfectly still, heh. You are holding reference to p so when that runnable fires 2 seconds later, it will destroy whatever is under the player then. It will not do what you think it will do.

    That is what his code would do, albeit not necessarily to the blocks intended.