Waiting X seconds after running code?

Discussion in 'Plugin Development' started by Xp10d3, Feb 19, 2020.

  1. Offline


    Okay, yes; I know this is a noob question. I feel stupid but I did some research on how to do this. I know all about the Bukkit Scheduler, the Runnables, etc. But that is not necessarily what I want. I want to wait x seconds (lets say, 5 seconds) AFTER running code. Basically my goal is to have a "pressure plate" effect. You get to a location, it runs a command once, then once you move out of that location you can go back to that location and it'll run the command again.
    Player moves to 50, 75, 3. It runs a command. Player moves to 51, 75, 5. It doesn't do anything. Player moves back to 50, 75 , 3 and it runs a command.

    package xp10d3.replace.corelia;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    public class PlayerListeners implements Listener {
        private Core core;
        public PlayerListeners (Core core) {
            this.core = core;
        public void savePlayerLocation(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            core.getConfig().set(player.getUniqueId().toString() + ".lastlocation.x", player.getLocation().getX());
            core.getConfig().set(player.getUniqueId().toString() + ".lastlocation.y", player.getLocation().getY());
            core.getConfig().set(player.getUniqueId().toString() + ".lastlocation.z", player.getLocation().getZ());
            core.getConfig().set(player.getUniqueId().toString() + ".lastlocation.world", player.getLocation().getWorld().getName());
        public void checkLocation(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            double locationX = core.getConfig().getDouble(player.getUniqueId().toString() + ".lastlocation.x");
            double locationY = core.getConfig().getDouble(player.getUniqueId().toString() + ".lastlocation.y");
            double locationZ = core.getConfig().getDouble(player.getUniqueId().toString() + ".lastlocation.z");
            String locationWorld = core.getConfig().getString(player.getUniqueId().toString() + ".lastlocation.world");
            ConfigurationSection command = core.getConfig().getConfigurationSection("command.commands");
            for (String key : command.getKeys(false)) {
                double x = core.getConfig().getDouble("command.commands." + key + ".x");
                double y = core.getConfig().getDouble("command.commands." + key + ".y");
                double z = core.getConfig().getDouble("command.commands." + key + ".z");
                String world = core.getConfig().getString("command.commands." + key + ".world");
                if (x < locationX + 1 && x > locationX - 1 && y < locationY + 1 && y > locationY - 1 && z < locationZ + 1 && z > locationZ - 1&& world.equals(locationWorld)) {
                    core.getServer().dispatchCommand(core.getServer().getConsoleSender(), key.replace("%player%", player.getName()));
                    Data now = new Data();
                    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                    core.logFile(/*"[" + format.format(now) + "]" + */" Dispatched command " + "'" + key + "'" + " at X value " + locationX + " Y value " + locationY + " Z value " + locationZ + " in world " + locationWorld + ".");
  2. Offline


    1. First, run the code you want
    2. Then, create a new BukkitRunnable. Set the delay for this runnable to be 20*5 (or 5 seconds). If you want the task to run only once after 5 seconds, use runTaskLater. If you want to check every 5 seconds until something happens (like the player gets off the pressure plate), use runTaskTimer.
    3. Inside the runnable, put the code you want to run after 5 seconds. If you want the task to stop running, use the cancel() method to stop the runnable.
  3. Offline


    I mean I want to run the code inside the runnable BEFORE the five seconds. Just that I kinda want to the opposite. Run the code, then wait five seconds. Not run the code after five seconds. Or is that what the runnables do...? :O
  4. Offline


    So sorry you want to...


    ..like a cool down? Because you can store the current system time and compare back to it to check how much time has elapsed since said system time was stored

    Or... After code has been run, start a 5second timer and if code tries to run check if timer is still active, if it is, cancel code ?
    Xp10d3 likes this.
  5. Offline


    Yes. Basically. It's like a pressure plate; you are at a location then wait 1 tick and check if the player is at that location. If they are, then wait another tick and then check again. If they aren't then wait till the player is at that location. That is basically what I want, so yes; in a sense a cooldown. Sorry, I probably didn't explain well enough :(


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Feb 20, 2020
  6. Offline


  7. Offline


  8. Offline


    @Xp10d3 You will need scheduler, try to find a way depend of waht you want to achieve
    Xp10d3 likes this.
  9. Offline


    Thanks; I searched that up. How would I use it then? I've never used a scheduler before. Mostly just Bukkit Runnables :/
  10. Online

    timtower Moderator Moderator

    They are the same.
  11. Offline


    Oh. So a Bukkit Runnable is exactly what I want? Cause if I'm not mistaken it waits 5 seconds before running the code, not after like I want.
  12. Online

    timtower Moderator Moderator

    If you delay a runnable with 100 ticks then it waits 5 seconds before running.
    You use runTaskLater for that.
  13. Offline


    Okay. Thanks :) Very helpful. I'll try it out!

Share This Page