Search code examples
javastringdelaywait

Java, Delay between sending strings


So, I am having trouble thinking up how to make a delay in between sending a user messages, It would be cool if i could just keep calling the task will different string, and it adds those strings to a list of strings to play when it gets to it (after playing all the messages, and all the 30tick breaks before it).

I cannot use Thread.sleep();, that will stop the whole plugin :/

Here is the method that I am using, but I am failing pretty hard, and cant think of a way to make it work properly, ALL help is greatly appreciated :)

public static void speakBegin(final Player p){
final int messageNumber = 0;

String string1 = "Hey, user! this is message 1!";
//30 tick break
String string2 = "msg 2";
//30 tick break
String string3 = "msg 3";
//30 tick break
String string4 = "msg 4";
//30 tick break
String string5 = "msg 5";
//30 tick break
String string6 = "msg 6";
//30 tick break
String string7 = "msg 7";
//30 tick break
String string8 = "msg 8";
//30 tick break
String string9 = "msg 9";
//30 tick break
String string10 = "this is the final message, user!";
//30 tick break

final int id = plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
    public void run() {
        //Send user string1, then wait 60 ticks and send string2, ect. ect.
        /*
         * eg:
         * p.sendMessage(string + messageNumber); 
         * 
         * <-----I dont know how to do this bit so it plays string1,
         *  then wait, add int to messageNumber,
         *  then play string2 because 'string' + messageNumber would equal 'string2'
         * 
         */

        //messageNumber++;  ?

        //after string10 is played call "plugin.getScheduler().cancelTasks(plugin);"
    }
    //this just waits 20ticks in beginning, then waits 60 tick between messages
}, 20L, 60L).getTaskId();

}


Solution

  • Thread.sleep might be what you want http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html