Delay in for-loop

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

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

    KevTheCrafter

    Hello,
    i had a plugin that reads from the config an StringList and than goes in a for-loop.
    Code:
                if(stringlist[0].equalsIgnoreCase("DELAY")){
                    Thread.sleep( Long.parseLong(stringlist[1]) );
                }else{
    //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

    YoFuzzy3

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

    You have to make a BukkitRunnable:
    Code:java
    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:
    Code:
    Bukkit.getScheduler().runTaskLater(<plugin instance>, new Runnable() {
        ...
    }, <delay>);
    ... or you run a BukkitRunnable, but then you write it like that:
    Code:
    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

    skipperguy12

    Heres more info on Schedular :3

    http://wiki.bukkit.org/Scheduler_Programming

    Here's the javadoc on it: http://jd.bukkit.org/apidocs/org/bukkit/scheduler/BukkitScheduler.html

    More specifically, you'll want to look at this: http://jd.bukkit.org/apidocs/org/bu...uleSyncRepeatingTask(org.bukkit.plugin.Plugin, java.lang.Runnable, long, long)


    Also, take a look at this, their accomplishing the same as what you want:
    http://forums.bukkit.org/threads/making-a-method-that-is-called-every-12-hours.118298/
     
  5. Offline

    YoFuzzy3

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

    fireblast709

    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

    YoFuzzy3

    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. http://forums.bukkit.org/threads/co...under-50-challenge.33264/page-14#post-1469289
     
  9. Offline

    fireblast709

    then just cancel the id?
    Code:java
    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:
    Code:
    new BukkitRunnable() {
        @Override
        public void run() {
            if (someAbortConditionIsMet) {
                this.cancel();
            } 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

    YoFuzzy3

    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
    http://forums.bukkit.org/threads/co...under-50-challenge.33264/page-14#post-1469289
     
Thread Status:
Not open for further replies.

Share This Page