How can I make a 5 seconds delay?

Discussion in 'Plugin Development' started by craftminer502, Mar 27, 2013.

Thread Status:
Not open for further replies.
  1. What I need to do is make a 5 seconds delay. An example would be that when I run the /spawn command I will be teleported to spawn instantly but I need there to be a 5 seconds delay before it happens.

    Example:
    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
        sender.teleportToSpawn(); //It happens instantly.
    }
     
  2. Offline

    RainoBoy97

    BukkitRunnables :)
     
  3. Offline

    TheUpdater

    Dont Thing That Helps so Mutch lol...
     
  4. Offline

    PHILLIPS_71

  5. Offline

    bcnobel

    Use this:
    Code:
    Timer yourtimer = new Timer(true); 
    yourtimer.schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            sender.teleportToSpawn();
        }
    }, 5000);
    
     
  6. Offline

    microgeek

    Why not use a BukkitRunnable?
     
  7. Offline

    bcnobel

    There's not any reason not to use a bukkit runnable instead of this, and vice versa.
    It does exactly the same thing.
    Just figured I'd show another option.
     
  8. Offline

    Hoolean

    Always better to use Bukkits built in scheduler otherwise you will run into seemingly unexplained problems.

    Code:
    getServer().getScheduler().scheduleSyncDelayedTask(new Runnable() {
     
        @Override
        public void run() {
            //code
        }
     
    }, 20*secondsgohere);
     
  9. Offline

    bcnobel

    Why would that be then?
    Doesn't Java supply good enough utilities (the one that the BukkitRunnable is built on)?
     
  10. Offline

    Hoolean

    That is Bukkit's official one and it is better because it complies with ticks. Else, you will be running tasks in alternative threads (asynchronously), which can lead to errors :p
     
  11. Thanks for the good replies. I now have a working timer or whatever you will call it.
     
    MrBluebear3 likes this.
  12. Offline

    bcnobel

    Ok, thanks for the clarification. I had not thought of the issues it can cause because of asynchronous thread errors.
    I will keep that in mind in my future plugin projects.
     
    MrBluebear3 likes this.
Thread Status:
Not open for further replies.

Share This Page