-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathCraftTask.java
More file actions
137 lines (114 loc) · 3.41 KB
/
CraftTask.java
File metadata and controls
137 lines (114 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.bukkit.craftbukkit.scheduler;
import java.util.function.Consumer;
import io.papermc.paper.util.concurrent.TickBoundTask;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
public class CraftTask implements BukkitTask, Runnable, TickBoundTask {
private volatile CraftTask next = null;
public static final int ERROR = 0;
public static final int NO_REPEATING = -1;
public static final int CANCEL = -2;
public static final int PROCESS_FOR_FUTURE = -3;
public static final int DONE_FOR_FUTURE = -4;
/**
* -1 means no repeating <br>
* -2 means cancel <br>
* -3 means processing for Future <br>
* -4 means done for Future <br>
* Never 0 <br>
* >0 means number of ticks to wait between each execution
*/
private volatile long period;
private long nextRun;
public final Runnable rTask;
public final Consumer<BukkitTask> cTask;
private final Plugin plugin;
private final int id;
private final long createdAt = System.nanoTime();
CraftTask() {
this(null, null, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING);
}
CraftTask(final Object task) {
this(null, task, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING);
}
CraftTask(final Plugin plugin, final Object task, final int id, final long period) {
this.plugin = plugin;
if (task instanceof Runnable) {
this.rTask = (Runnable) task;
this.cTask = null;
} else if (task instanceof Consumer) {
this.cTask = (Consumer<BukkitTask>) task;
this.rTask = null;
} else if (task == null) {
// Head or Future task
this.rTask = null;
this.cTask = null;
} else {
throw new AssertionError("Illegal task class " + task);
}
this.id = id;
this.period = period;
}
@Override
public final int getTaskId() {
return this.id;
}
@Override
public final Plugin getOwner() {
return this.plugin;
}
@Override
public boolean isSync() {
return true;
}
@Override
public void run() {
if (this.rTask != null) {
this.rTask.run();
} else {
this.cTask.accept(this);
}
}
public long getCreatedAt() {
return this.createdAt;
}
long getPeriod() {
return this.period;
}
void setPeriod(long period) {
this.period = period;
}
public long getNextRun() {
return this.nextRun;
}
public void setNextRun(long nextRun) {
this.nextRun = nextRun;
}
CraftTask getNext() {
return this.next;
}
void setNext(CraftTask next) {
this.next = next;
}
Class<?> getTaskClass() {
return (this.rTask != null) ? this.rTask.getClass() : ((this.cTask != null) ? this.cTask.getClass() : null);
}
@Override
public boolean isCancelled() {
return (this.period == CraftTask.CANCEL);
}
@Override
public void cancel() {
Bukkit.getScheduler().cancelTask(this.id);
}
/**
* This method properly sets the status to cancelled, synchronizing when required.
*
* @return false if it is a craft future task that has already begun execution, true otherwise
*/
boolean cancel0() {
this.setPeriod(CraftTask.CANCEL);
return true;
}
}