Run some code after X amount of time

Discussion in 'Plugin Development' started by ThunderWaffeMC, Aug 5, 2013.

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

    ThunderWaffeMC

    How can I make it so after a certain amount of time the code is run. So for example when a player logs in it waits 10 seconds and then sends a message.

    What I want to do is something like this:

    Code:java
    1.  
    2. if(worldOne.getPlayers().size() <= 0) { //if a world has less than 0 people or 0 people in it
    3. //wait 10 seconds
    4. player.sendMessage("test"); //send the message "test"
    5. }
    6.  


    Thanks in advance!
     
  2. Offline

    lycano

    If its time based then use the scheduler http://wiki.bukkit.org/Scheduler_Programming

    If its command or action based like "did something x times" then you would have to implement your own logic when this happens and execute your code or run the scheduler.

    For your example you would need to listen in PlayerQuitEvent and simply check if the PlayerOnline count is below your limit. ( < 1 cause i believe that the player that is quitting still counts as player but you might have to check that ).

    You can run your code then in a scheduler or simply leave it in the event .. its up to you.
     
    ThunderWaffeMC likes this.
  3. Offline

    Retherz_

    Code:java
    1.  
    2. if(worldOne.getPlayers().size() <= 0) {
    3. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    4. public void run() {
    5. player.sendMessage("test"); //send the message "test"
    6. }
    7. }, 200L);
    8. }
     
    ThunderWaffeMC likes this.
  4. Offline

    ThunderWaffeMC

    What does 200L mean? Is it 200 ticks?
     
  5. Offline

    Henzz

    ThunderWaffeMC
    Yes

    Edit: If you wanted it in seconds just replace it with 20 * (amount of seconds)
     
    ThunderWaffeMC likes this.
Thread Status:
Not open for further replies.

Share This Page