/
Galaxi.java
184 lines (167 loc) · 5.24 KB
/
Galaxi.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package net.ME1312.Galaxi;
import net.ME1312.Galaxi.Library.UniversalFile;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Plugin.PluginInfo;
import net.ME1312.Galaxi.Plugin.PluginManager;
import net.ME1312.Galaxi.Plugin.Task;
import java.lang.reflect.InvocationTargetException;
import java.net.URLStreamHandler;
import java.util.*;
import java.util.concurrent.TimeUnit;
public abstract class Galaxi {
private HashMap<UUID, Timer> schedule = new HashMap<UUID, Timer>();
private HashMap<String, URLStreamHandler> protocols = new HashMap<String, URLStreamHandler>();
/**
* Gets the GalaxiEngine API Methods
*
* @return GalaxiEngine API
*/
public static Galaxi getInstance() {
return (Galaxi) Util.getDespiteException(() -> Class.forName("net.ME1312.Galaxi.Engine.GalaxiEngine").getDeclaredMethod("getInstance").invoke(null), null);
}
/**
* Get the Plugin Manager
*
* @return Plugin Manager
*/
public abstract PluginManager getPluginManager();
/**
* Schedule a task
*
* @param builder SubTaskBuilder
* @return Task ID
*/
public UUID schedule(Task builder) {
if (Util.isNull(builder)) throw new NullPointerException();
UUID sid = Util.getNew(schedule.keySet(), UUID::randomUUID);
TimerTask task = new TimerTask() {
@Override
public void run() {
try {
builder.run();
} catch (Throwable e) {
builder.plugin().getLogger().error.println(new InvocationTargetException(e, "Unhandled exception while running Task " + sid.toString()));
}
if (builder.repeat() <= 0) schedule.remove(sid);
}
};
schedule.put(sid, new Timer(builder.plugin().getName() + "::Scheduled_Task(" + sid + ')'));
if (builder.repeat() > 0) {
if (builder.delay() > 0) {
schedule.get(sid).scheduleAtFixedRate(task, builder.delay(), builder.repeat());
} else {
schedule.get(sid).scheduleAtFixedRate(task, new Date(), builder.repeat());
}
} else {
if (builder.delay() > 0) {
schedule.get(sid).schedule(task, builder.delay());
} else {
new Thread(task, builder.plugin().getName() + "::Scheduled_Task(" + sid + ')').start();
}
}
return sid;
}
/**
* Schedule a task
*
* @param plugin Plugin Scheduling
* @param run What to run
* @return Task ID
*/
public UUID schedule(PluginInfo plugin, Runnable run) {
return schedule(plugin, run, -1L);
}
/**
* Schedule a task
*
* @param plugin Plugin Scheduling
* @param run What to Run
* @param delay Task Delay
* @return Task ID
*/
public UUID schedule(PluginInfo plugin, Runnable run, long delay) {
return schedule(plugin, run, delay, -1L);
}
/**
* Schedule a task
*
* @param plugin Plugin Scheduling
* @param run What to Run
* @param delay Task Delay
* @param repeat Task Repeat Interval
* @return Task ID
*/
public UUID schedule(PluginInfo plugin, Runnable run, long delay, long repeat) {
return schedule(plugin, run, TimeUnit.MILLISECONDS, delay, repeat);
}
/**
* Schedule a task
*
* @param plugin Plugin Scheduling
* @param run What to Run
* @param unit TimeUnit to use
* @param delay Task Delay
* @param repeat Task Repeat Interval
* @return Task ID
*/
public UUID schedule(PluginInfo plugin, Runnable run, TimeUnit unit, long delay, long repeat) {
if (Util.isNull(plugin, run, unit, delay, repeat)) throw new NullPointerException();
return schedule(new Task(plugin) {
@Override
public void run() {
run.run();
}
}.delay(unit.toMillis(delay)).repeat(unit.toMillis(repeat)));
}
/**
* Cancel a task
*
* @param sid Task ID
*/
public void cancelTask(UUID sid) {
if (Util.isNull(sid)) throw new NullPointerException();
if (schedule.keySet().contains(sid)) {
schedule.get(sid).cancel();
schedule.remove(sid);
}
}
/**
* Registers a Protocol
*
* @param generator Stream Generator
* @param handles Protocol Handles
*/
public void addProtocol(URLStreamHandler generator, String... handles) {
for (String handle : handles) {
protocols.put(handle.toLowerCase(), generator);
}
}
/**
* Unregisters a Protocol
*
* @param handles Protocol Handles
*/
public void removeProtocol(String... handles) {
for (String handle : handles) {
protocols.remove(handle.toLowerCase());
}
}
/**
* Gets the Runtime Directory
*
* @return Directory
*/
public abstract UniversalFile getRuntimeDirectory();
/**
* Gets the Galaxi Engine Info
*
* @return Galaxi Engine Info
*/
public abstract PluginInfo getAppInfo();
/**
* Gets the Galaxi Engine Info
*
* @return Galaxi Engine Info
*/
public abstract PluginInfo getEngineInfo();
}