-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoidScheduler.java
More file actions
135 lines (113 loc) · 3.23 KB
/
Copy pathVoidScheduler.java
File metadata and controls
135 lines (113 loc) · 3.23 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
package ru.cwcode.cwutils.scheduler;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.util.function.Supplier;
public class VoidScheduler extends AbstractScheduler {
JavaPlugin registrant = null;
private volatile Runnable action = () -> {};
private volatile Runnable lastlyAction = () -> {};
private volatile Supplier<Boolean> condition = null;
private VoidScheduler() {
}
public static VoidScheduler create() {
return new VoidScheduler();
}
/**
* Действие
*/
public VoidScheduler perform(Runnable action) {
this.action = action;
return this;
}
/**
* Условие, при котором будет совершаться действие
*/
public VoidScheduler until(Supplier<Boolean> condition) {
this.condition = condition;
return this;
}
/**
* Совершать действие асинхронно
*/
public VoidScheduler async() {
this.asyncTask = true;
this.blocked = true;
return this;
}
public VoidScheduler async(boolean async) {
this.asyncTask = async;
this.blocked = async || blocked;
return this;
}
/**
* Не останавливать выполнение, если условие не соблюдено. В таком случае действие не будет выполняться, если условие false
*/
public VoidScheduler infinite() {
this.infinite = true;
return this;
}
public VoidScheduler infinite(boolean infinite) {
this.infinite = infinite;
return this;
}
/**
* Действие, выполняющееся вместо основного, когда условие false
*/
public VoidScheduler otherwise(Runnable lastlyAction) {
this.lastlyAction = lastlyAction;
return this;
}
/**
* Регистрирует и запускает планировщик
*/
public BukkitTask registerTask(JavaPlugin plugin, long delay) {
BukkitTask task;
if (asyncTask) {
task = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::tick, delay, delay);
} else {
task = Bukkit.getScheduler().runTaskTimer(plugin, this::tick, delay, delay);
}
registrant = plugin;
taskId = task.getTaskId();
Tasks.put(id, this);
return task;
}
/**
* Регистрирует и запускает планировщик
*/
public int register(JavaPlugin plugin, long delay) {
return registerTask(plugin, delay).getTaskId();
}
private void tick() {
if (blocked && running) return;
if (condition == null) {
runWithCancelling(action);
} else if (condition.get()) {
run(action);
} else {
runWithCancelling(lastlyAction);
}
}
private void runWithCancelling(Runnable action) {
try {
running = true;
action.run();
if (!infinite) Tasks.cancelTask(id);
} catch (Exception e) {
e.printStackTrace();
} finally {
running = false;
}
}
private void run(Runnable action) {
try {
running = true;
action.run();
} catch (Exception e) {
e.printStackTrace();
} finally {
running = false;
}
}
}