Delay in for-loop

Discussion in 'Plugin Development' started by KevTheCrafter, Dec 25, 2012.

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


    i had a plugin that reads from the config an StringList and than goes in a for-loop.
                    Thread.sleep( Long.parseLong(stringlist[1]) );
    //Spawn Firework
    This is in the loop. I split the String in array 'stringlist'. If stringlist[0] is delay, it should sleep for stringlist[1] millisecounds.
    Is stringlist[0] not delay, it should spawn a firework thats defined in the stringlist-array..

    But when i use Thread.sleep all Rockets starts but its laggy and they all explode at the same time in the end.. Thats bad!

    Sry for my bad Englisch.. I'm from germany..
    Greets, Kev
  2. Offline


    Thread.sleep() will pause the main server thread.

    You have to make a BukkitRunnable:
    1. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(<plugin instance>, new BukkitRunnable(){
    2. public void run(){
    3. // Do stuff
    4. }
    5. }, <delay in ticks>);
  3. On a side note, a BukkitRunnable used like this doesn't make that much sense.
    Either you do it traditionally:
    Bukkit.getScheduler().runTaskLater(<plugin instance>, new Runnable() {
    }, <delay>);
    ... or you run a BukkitRunnable, but then you write it like that:
    new BukkitRunnable() {
    }.runTaskLater(<plugin instance>, <delay>);
    Your way is not broken, but doesn't really make use of the simplicity that a BukkitRunnable provides you.
  4. Offline


    Heres more info on Schedular :3

    Here's the javadoc on it:

    More specifically, you'll want to look at this:, java.lang.Runnable, long, long)

    Also, take a look at this, their accomplishing the same as what you want:
  5. Offline


    What do you mean by the "simplicity?" Your way works exactly the same.
  6. Offline


    No the second codeblock uses a BukkitRunnable, you just schedule a task using a normal Runnable. BukkitRunnable has the nice feature of being able to cancel itself from the inside using cancel() ;3
  7. Offline

    Sagacious_Zed Bukkit Docs

    To add to that, a BukkitRunnable actually can't cancel itself if it didn't schedule itself.
  8. Offline


    I tried the cancel() function in my last entry to the 50 line plugin chellenge but it couldn't cancel it because apparently it "wasn't scheuled yet." So I had to make a task id for the BukkitRunnable and then cancel that specific task, but that means you can only have 1 running at a time.
  9. Offline


    then just cancel the id?
    1. Bukkit.getScheduler().cancelTask(this.getTaskId());
  10. That's exactly because of what Sagacious_Zed pointed out:
    Basically, you are creating a BukkitRunnable, but you don't use it to schedule the task. You simply schedule a new task using Bukkit's old API method. The class can only cancel itself if you scheduled it using its own methods (which is what I did in my second code example in my former post).

    Granted, for delayed/non-repeating tasks, there's no huge advantage, but the cancel() thingy comes in really handy for repeating ones:
    new BukkitRunnable() {
        public void run() {
            if (someAbortConditionIsMet) {
            } else {
                // do something
    }.runTaskTimer(<plugin instance>, whatever, whatever);
    No storing of any task ids involved! But this only works if you called "runTaskTimer" on the BukkitRunnable itself.
    YoFuzzy3 likes this.
  11. Offline


    I tried that before you posted it, it also throws the same error.

    That works! Thank you so much! I updated my 50 line plugin entry. :3
Thread Status:
Not open for further replies.

Share This Page