[Util] RepeatableRunnable - Scheduler

Discussion in 'Resources' started by KingFaris11, Jul 31, 2013.

Thread Status:
Not open for further replies.
  1. This thread has the code to a class I made called "RepeatableRunnable". It extends BukkitRunnable and it automatically schedules a repeating task. That task is called x many times and you can choose how many times the onRun method is called.
    In Simple English, this is what you call a repeating scheduler that cancels after it's been run x many times.

    RepeatableRunnable: http://pastebin.com/KAY1iZWU
    RepeatableRunnable alternative: http://pastie.org/8273349

    Parameters: RepeatableRunnable(BukkitScheduler scheduler, Plugin yourPlugin, long delay, long period, long amountOfTimes)

    scheduler - The server's scheduler.
    yourPlugin - Your plugin (as an object). You can use Bukkit.getPluginManager().getPlugin("PluginName").
    delay - Delay in server ticks before executing first repeat.
    period - Period in server ticks of the task.
    amountOfTimes - The amount of times to call the onRun method.

    For example:
    Code:
    new RepeatableRunnable(Bukkit.getServer().getScheduler(), this, 0L, 20L, 6) {
        public void onRun() {
            Bukkit.getServer().broadcastMessage(ChatColor.RED + (this.getMaxRepeats() - this.getRepeats()) + " repeats remaining.");
        }
    }
    
    This would broadcast every second how many repeats are remaining 6 times and would look like:
    5 seconds remaining - 1st run. - 1
    4 seconds remaining - 1st repeat. - 2
    3 seconds remaining - 2nd repeat. - 3
    2 seconds remaining - 3rd repeat. - 4
    1 second remaining - 4th repeat. - 5
    0 seconds remaining - 5th repeat. - 6
    As you can see, it says it 6 times.

    Note: This automatically will run when creating a new instance of RepeatableRunnable without the need of calling a method such as repeatable.startTaskLater();

    Note: Do not use scheduleSyncRepeatingTask with this as it'd end up scheduling 2 repeating tasks, the one you made giving errors after x many times. This RepeatableRunnable class automatically schedules a task.
     
    IDragonfire, foodyling and microgeek like this.
  2. Offline

    Omerrg

    Very Useful Idea! Nicely done. Gonna use it !
     
  3. Thanks =)
     
  4. Offline

    IDragonfire

  5. Offline

    IDragonfire

    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page