Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
BukkitRunnable.java
1 package org.bukkit.scheduler;
2 
3 import org.bukkit.Bukkit;
4 import org.bukkit.plugin.Plugin;
5 
6 /**
7  * This class is provided as an easy way to handle scheduling tasks.
8  */
9 public abstract class BukkitRunnable implements Runnable {
10  private int taskId = -1;
11 
12  /**
13  * Attempts to cancel this task.
14  *
15  * @throws IllegalStateException if task was not scheduled yet
16  */
17  public synchronized void cancel() throws IllegalStateException {
19  }
20 
21  /**
22  * Schedules this in the Bukkit scheduler to run on next tick.
23  *
24  * @param plugin the reference to the plugin scheduling task
25  * @return a BukkitTask that contains the id number
26  * @throws IllegalArgumentException if plugin is null
27  * @throws IllegalStateException if this was already scheduled
28  * @see BukkitScheduler#runTask(Plugin, Runnable)
29  */
30  public synchronized BukkitTask runTask(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
31  checkState();
32  return setupId(Bukkit.getScheduler().runTask(plugin, this));
33  }
34 
35  /**
36  * <b>Asynchronous tasks should never access any API in Bukkit. Great care
37  * should be taken to assure the thread-safety of asynchronous tasks.</b>
38  * <p>
39  * Schedules this in the Bukkit scheduler to run asynchronously.
40  *
41  * @param plugin the reference to the plugin scheduling task
42  * @return a BukkitTask that contains the id number
43  * @throws IllegalArgumentException if plugin is null
44  * @throws IllegalStateException if this was already scheduled
45  * @see BukkitScheduler#runTaskAsynchronously(Plugin, Runnable)
46  */
47  public synchronized BukkitTask runTaskAsynchronously(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
48  checkState();
49  return setupId(Bukkit.getScheduler().runTaskAsynchronously(plugin, this));
50  }
51 
52  /**
53  * Schedules this to run after the specified number of server ticks.
54  *
55  * @param plugin the reference to the plugin scheduling task
56  * @param delay the ticks to wait before running the task
57  * @return a BukkitTask that contains the id number
58  * @throws IllegalArgumentException if plugin is null
59  * @throws IllegalStateException if this was already scheduled
60  * @see BukkitScheduler#runTaskLater(Plugin, Runnable, long)
61  */
62  public synchronized BukkitTask runTaskLater(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
63  checkState();
64  return setupId(Bukkit.getScheduler().runTaskLater(plugin, this, delay));
65  }
66 
67  /**
68  * <b>Asynchronous tasks should never access any API in Bukkit. Great care
69  * should be taken to assure the thread-safety of asynchronous tasks.</b>
70  * <p>
71  * Schedules this to run asynchronously after the specified number of
72  * server ticks.
73  *
74  * @param plugin the reference to the plugin scheduling task
75  * @param delay the ticks to wait before running the task
76  * @return a BukkitTask that contains the id number
77  * @throws IllegalArgumentException if plugin is null
78  * @throws IllegalStateException if this was already scheduled
79  * @see BukkitScheduler#runTaskLaterAsynchronously(Plugin, Runnable, long)
80  */
81  public synchronized BukkitTask runTaskLaterAsynchronously(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
82  checkState();
83  return setupId(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, this, delay));
84  }
85 
86  /**
87  * Schedules this to repeatedly run until cancelled, starting after the
88  * specified number of server ticks.
89  *
90  * @param plugin the reference to the plugin scheduling task
91  * @param delay the ticks to wait before running the task
92  * @param period the ticks to wait between runs
93  * @return a BukkitTask that contains the id number
94  * @throws IllegalArgumentException if plugin is null
95  * @throws IllegalStateException if this was already scheduled
96  * @see BukkitScheduler#runTaskTimer(Plugin, Runnable, long, long)
97  */
98  public synchronized BukkitTask runTaskTimer(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
99  checkState();
100  return setupId(Bukkit.getScheduler().runTaskTimer(plugin, this, delay, period));
101  }
102 
103  /**
104  * <b>Asynchronous tasks should never access any API in Bukkit. Great care
105  * should be taken to assure the thread-safety of asynchronous tasks.</b>
106  * <p>
107  * Schedules this to repeatedly run asynchronously until cancelled,
108  * starting after the specified number of server ticks.
109  *
110  * @param plugin the reference to the plugin scheduling task
111  * @param delay the ticks to wait before running the task for the first
112  * time
113  * @param period the ticks to wait between runs
114  * @return a BukkitTask that contains the id number
115  * @throws IllegalArgumentException if plugin is null
116  * @throws IllegalStateException if this was already scheduled
117  * @see BukkitScheduler#runTaskTimerAsynchronously(Plugin, Runnable, long,
118  * long)
119  */
120  public synchronized BukkitTask runTaskTimerAsynchronously(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
121  checkState();
122  return setupId(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this, delay, period));
123  }
124 
125  /**
126  * Gets the task id for this runnable.
127  *
128  * @return the task id that this runnable was scheduled as
129  * @throws IllegalStateException if task was not scheduled yet
130  */
131  public synchronized int getTaskId() throws IllegalStateException {
132  final int id = taskId;
133  if (id == -1) {
134  throw new IllegalStateException("Not scheduled yet");
135  }
136  return id;
137  }
138 
139  private void checkState() {
140  if (taskId != -1) {
141  throw new IllegalStateException("Already scheduled as " + taskId);
142  }
143  }
144 
145  private BukkitTask setupId(final BukkitTask task) {
146  this.taskId = task.getTaskId();
147  return task;
148  }
149 }
BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable task, long delay)
BukkitTask runTaskAsynchronously(Plugin plugin, Runnable task)
synchronized BukkitTask runTaskTimer(Plugin plugin, long delay, long period)
synchronized BukkitTask runTask(Plugin plugin)
synchronized BukkitTask runTaskLaterAsynchronously(Plugin plugin, long delay)
synchronized BukkitTask runTaskAsynchronously(Plugin plugin)
synchronized BukkitTask runTaskLater(Plugin plugin, long delay)
BukkitTask runTaskTimer(Plugin plugin, Runnable task, long delay, long period)
static BukkitScheduler getScheduler()
Definition: Bukkit.java:215
synchronized BukkitTask runTaskTimerAsynchronously(Plugin plugin, long delay, long period)
BukkitTask runTask(Plugin plugin, Runnable task)
BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable task, long delay, long period)
BukkitTask runTaskLater(Plugin plugin, Runnable task, long delay)