이 문서에는 한국어로 번역되지 않은 내용이 담겨 있습니다. 번역을 진행해 주세요.. This article needs to be translated into Korean. |
이 자습서에서는 Bukkit에서 제공하는 스케쥴러를 이용하는 방법을 배울 것입니다. 스케쥴러는 당신의 코드가 나중에 실행되도록 지연시킬 수 있습니다. This is not the same as registering a Listener, a block of code which is executed in response to an event in the game. Blocks of code may also be scheduled to be executed repeatedly, with or without a delay. They will continue to execute until completed, or canceled, or your plugin is disabled.
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 own execution. However, if the BukkitRunnable did not schedule itself for execution, it cannot cancel itself from execution. BukkitRunnables are not schedulers, and do not contain any scheduler logic.
Defining work
Plugins should first extend BukkitRunnable to define work that needs to be done.
예제
This is an example definition of a task that can be scheduled.
import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; public class ExampleTask extends BukkitRunnable { private final JavaPlugin plugin; public ExampleTask(JavaPlugin plugin) { this.plugin = plugin; } public void run() { // What you want to schedule goes here plugin.getServer().broadcastMessage("Welcome to Bukkit! Remember to read the documentation!"); } }
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 instance of the task. The list of methods for BukkitRunnable can be found in the Javadocs for BukkitRunnable. The different run methods all return a BukkitTask object
Warning: | Asynchronous tasks should never access any API in Bukkit |
예제
This is an example of a plugin which registers a listener and when a player joins, schedules a task to be run 20 ticks later.
import org.bukkit.event.EventHandler; import org.bukkit.event.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 class 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; plugin.getServer().getPluginManager().registerEvents(this, plugin); } @EventHandler public void onJoin(PlayerJoinEvent event) { BukkitTask task = new ExampleTask(this).runTaskLater(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 be found in the Javadocs for BukkitScheduler.
Warning: | Asynchronous tasks should never access any API in Bukkit |
BukkitTask
A BukkitTask object is returned whenever a Runnable is scheduled. This object represents the task the scheduled task being executed by the scheduler. For more information see, Javadocs for BukkitTask.
Callable and Future
A Callable given to the scheduler to 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 safe nor guaranteed to be thread safe.
- Asynchronous tasks should never access any API in Bukkit.
- Do not access or modify shared collections from your asynchronous tasks. Normal collections are not thread-safe. This also applies to objects which are not thread safe.
- An asynchronous task can schedule a synchronous task.
- A synchronous task can schedule an asynchronous task.
Language | English • беларуская • Deutsch • español • suomi • français • italiano • 한국어 • Nederlands • norsk • polski • português • русский • lietuvių • čeština |
---|