How to Stop Bukkitrunnable and Enable It Again
This tutorial will guide you lot in using the scheduler provided by bukkit. It will allow you to defer the execution of lawmaking to a later time. This is not the aforementioned as registering a Listener, a cake of lawmaking which is executed in response to an event in the game. Blocks of lawmaking may likewise be scheduled to exist executed repeatedly at a stock-still interval, with or without a delay. They will continue to execute until completed, or canceled, or your plugin is disabled.
When using BukkitRunnable, with a separate form, scheduling work occurs in 2 steps for the programmer.
- Define the piece of work to be washed, run across the section Defining Work
- Notify Bukkit when the work should be executed, run across the section Scheduling Piece of work
Alternatively, piece of work tin can be scheduled directly with the scheduler, this also occurs in two steps for the programmer.
- Define the piece of work to be done, in a Runnable or Callable
- So straight scheduling the work with the Bukkit Scheduler, see the section BukkitScheduler
Contents
- 1 BukkitRunnable
- ane.one Defining piece of work
- 1.1.i Basic Example
- 1.1.2 Self-Canceling Case
- 1.2 Scheduling Work
- 1.ii.1 Example
- ane.ii.2 Cocky-Canceling Example
- 1.2.3 Anonymous BukkitRunnable Example
- ane.one Defining piece of work
- 2 BukkitScheduler
- two.1 Instance
- 2.ii Repeating Example
- ii.iii BukkitTask
- ii.4 Callable and Future
- 3 Tips for thread safety
BukkitRunnable
BukkitRunnable is an abstract implementation of Runnable. It also supports additional operations that a Runnable is not capable of, most conveniently, BukkitRunnables can schedule and cancel their ain execution. All the same, if the BukkitRunnable did not schedule itself for execution, it cannot cancel itself from execution. BukkitRunnables are not schedulers, do not contain any scheduler logic, and are non expensive to create. Plugins should prefer defining a BukkitRunnable and calling the appropriate run method over direct scheduling a Runnable with the BukkitScheduler.
- For more information on BukkitRunnable, see the BukkitRunnable JavaDocs
Defining work
Plugins should get-go extend BukkitRunnable to define work that needs to be done. In other words, the definition of the run method is what you want executed in accordance to a schedule.
Basic Example
This is an example definition of a chore that can be scheduled.
import org.bukkit.Bukkit ; import org.bukkit.plugin.coffee.JavaPlugin ; import org.bukkit.scheduler.BukkitRunnable ; public form ExampleTask extends BukkitRunnable { private last JavaPlugin plugin ; public ExampleTask ( JavaPlugin plugin ) { this . plugin = plugin ; } @Override public void run () { // What you want to schedule goes here plugin . getServer (). broadcastMessage ( "Welcome to Bukkit! Retrieve to read the documentation!" ); } }
Self-Canceling Example
This is an example of a definition of a task that will cancel itself when it has executed the specified number of times
import org.bukkit.plugin.java.JavaPlugin ; import org.bukkit.scheduler.BukkitRunnable ; public class ExampleSelfCancelingTask extends BukkitRunnable { private final JavaPlugin plugin ; private int counter ; public ExampleSelfCancelingTask ( JavaPlugin plugin , int counter ) { this . plugin = plugin ; if ( counter <= 0 ) { throw new IllegalArgumentException ( "counter must be greater than 0" ); } else { this . counter = counter ; } } @Override public void run () { // What you want to schedule goes hither if ( counter > 0 ) { plugin . getServer (). broadcastMessage ( "Embark greeting in " + counter --); } else { plugin . getServer (). broadcastMessage ( "Welcome to Bukkit! Remember to read the documentation!" ); this . cancel (); } } }
Scheduling Work
After defining the task, the plugin needs to schedule the task. BukkitRunnables are scheduled when the desired run method is invoked on an example of the chore. The list of methods for BukkitRunnable can be plant in the Javadocs for BukkitRunnable. The different run methods all return a BukkitTask object
| | Asynchronous tasks should never access any API in Bukkit |
Example
This is an example of a plugin which registers a listener and when a actor joins, schedules a task to be run twenty ticks subsequently.
import org.bukkit.result.EventHandler ; import org.bukkit.event.Listener ; import org.bukkit.event.player.PlayerJoinEvent ; import org.bukkit.plugin.coffee.JavaPlugin ; import org.bukkit.scheduler.BukkitRunnable ; import org.bukkit.scheduler.BukkitTask ; public final course ExamplePlugin extends JavaPlugin { @Override public void onEnable () { new ExampleListener ( this ); } } class ExampleListener implements Listener { private final ExamplePlugin plugin ; public ExampleListener ( ExamplePlugin plugin ) { this . plugin = plugin ; this . plugin . getServer (). getPluginManager (). registerEvents ( this , plugin ); } @EventHandler public void onJoin ( PlayerJoinEvent event ) { // Create the task and schedule to run information technology once, after xx ticks BukkitTask task = new ExampleTask ( this . plugin ). runTaskLater ( this . plugin , twenty ); } }
Self-Canceling Example
This is example takes the to a higher place ExampleSelfCancelingTask and schedules it to run every 20 ticks subsequently waiting 10 ticks.
import org.bukkit.event.EventHandler ; import org.bukkit.event.Listener ; import org.bukkit.effect.role player.PlayerJoinEvent ; import org.bukkit.plugin.java.JavaPlugin ; import org.bukkit.scheduler.BukkitRunnable ; import org.bukkit.scheduler.BukkitTask ; public final class ExamplePlugin extends JavaPlugin { @Override public void onEnable () { new ExampleListener ( this ); } } form ExampleListener implements Listener { private final ExamplePlugin plugin ; public ExampleListener ( ExamplePlugin plugin ) { this . plugin = plugin ; this . plugin . getServer (). getPluginManager (). registerEvents ( this , plugin ); } @EventHandler public void onJoin ( PlayerJoinEvent effect ) { // Create the chore and schedule BukkitTask chore = new ExampleSelfCancelingTask ( this . plugin , v ). runTaskTimer ( this . plugin , 10 , twenty ); } }
Anonymous BukkitRunnable Case
An anonymous BukkitRunnable will allow you to schedule a job, without creating a new class with a proper name. This examples combines the above two basic examples.
import org.bukkit.issue.EventHandler ; import org.bukkit.effect.Listener ; import org.bukkit.event.player.PlayerJoinEvent ; import org.bukkit.plugin.java.JavaPlugin ; import org.bukkit.scheduler.BukkitRunnable ; import org.bukkit.scheduler.BukkitTask ; public final form ExamplePlugin extends JavaPlugin { @Override public void onEnable () { new ExampleListener ( this ); } } class ExampleListener implements Listener { individual terminal ExamplePlugin plugin ; public ExampleListener ( ExamplePlugin plugin ) { this . plugin = plugin ; this . plugin . getServer (). getPluginManager (). registerEvents ( this , plugin ); } @EventHandler public void onJoin ( PlayerJoinEvent consequence ) { // Create the task anonymously and schedule to run it once, after 20 ticks new BukkitRunnable () { @Override public void run () { // What you want to schedule goes here plugin . getServer (). broadcastMessage ( "Welcome to Bukkit! Remember to read the documentation!" ); } }. runTaskLater ( this . plugin , 20 ); } }
BukkitScheduler
The BukkitScheduler allows plugins to schedule a Runnable and/or a Callable , for execution at a later time. The list of methods for BukkitScheduler can exist found in the Javadocs for BukkitScheduler.
-
Note: Plugins should not schedule a BukkitRunnable with the scheduler, instead they should run the BukkitRunnable with the desired schedule.
Example
Example for directly scheduling an bearding Runnable with the BukkitScheduler to run after twenty ticks.
import org.bukkit.Bukkit ; import org.bukkit.plugin.java.JavaPlugin ; import org.bukkit.scheduler.BukkitScheduler ; public final form ExamplePlugin extends JavaPlugin { public void onEnable () { BukkitScheduler scheduler = getServer (). getScheduler (); scheduler . scheduleSyncDelayedTask ( this , new Runnable () { @Override public void run () { // Do something } }, 20L ); } }
Repeating Example
Case for directly scheduling an anonymous Runnable with the BukkitScheduler to run every xx ticks, forever, starting from when y'all schedule it.
import org.bukkit.Bukkit ; import org.bukkit.plugin.java.JavaPlugin ; import org.bukkit.scheduler.BukkitScheduler ; public final form ExamplePlugin extends JavaPlugin { public void onEnable () { BukkitScheduler scheduler = getServer (). getScheduler (); scheduler . scheduleSyncRepeatingTask ( this , new Runnable () { @Override public void run () { // Do something } }, 0 50 , 20L ); } }
| | Asynchronous tasks should never admission any API in Bukkit |
BukkitTask
A BukkitTask object is returned whenever a Runnable is scheduled. This object represents the chore the scheduled chore being executed by the scheduler. For more information run into, Javadocs for BukkitTask.
Callable and Future
A Callable given to the scheduler to telephone call synchronously returns a Future. These are standard Java classes, for more information see, Javadocs for Callable and Javadocs for Future
Tips for thread safety
The Bukkit API, with the exception of the scheduler package, is not thread rubber nor guaranteed to be thread safe.
- Asynchronous tasks should never access whatever API in Bukkit.
- Do not access or change shared collections from your asynchronous tasks. Normal collections are not thread-prophylactic. This also applies to objects which are non thread safe.
- An asynchronous chore tin schedule a synchronous job.
- A synchronous task can schedule an asynchronous chore.
- If you want to schedule something at a fixed time, by calculating how many ticks until that point in time, you should use an asynchronous chore. If yous don't, lag will increase the delay.
| Language | English • беларуская • Deutsch • español • suomi • français • italiano • 한국어 • Nederlands • norsk • polski • português • русский • lietuvių • čeština |
|---|
Source: https://bukkit.fandom.com/wiki/Scheduler_Programming
0 Response to "How to Stop Bukkitrunnable and Enable It Again"
Post a Comment