-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatAPI.java
More file actions
70 lines (55 loc) · 2.15 KB
/
Copy pathRepeatAPI.java
File metadata and controls
70 lines (55 loc) · 2.15 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
package ru.cwcode.cwutils.scheduler.annotationRepeatable;
import org.bukkit.plugin.java.JavaPlugin;
import ru.cwcode.cwutils.reflection.ReflectionUtils;
import ru.cwcode.cwutils.scheduler.Tasks;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Deprecated
public class RepeatAPI {
static HashMap<JavaPlugin, List<Integer>> tasks = new HashMap<>();
public static void init(JavaPlugin plugin, File file) {
tasks.put(plugin, new ArrayList<>());
String packageName = plugin.getClass().getPackage().getName();
int i = 0;
int registered = 0;
long start = System.currentTimeMillis();
try {
for (var clazz : ReflectionUtils.getClasses(file, packageName)) {
i++;
registered += handle(clazz, plugin);
}
plugin.getLogger().info("Scanned " + i + " classes, took " + (System.currentTimeMillis() - start) + "ms, registered " + registered + " tasks");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void registerRepeatable(JavaPlugin plugin, Method method, Repeat annotation) {
tasks.computeIfAbsent(plugin, x -> new ArrayList<>());
int id = new RepeatEntry(method, annotation).schedule(plugin);
tasks.get(plugin).add(id);
}
public static void unregisterAll(JavaPlugin plugin) {
if (!tasks.containsKey(plugin)) return;
for (Integer id : tasks.get(plugin)) {
Tasks.cancelTask(id);
}
}
private static int handle(Class<?> classInfo, JavaPlugin plugin) throws ClassNotFoundException {
int registered = 0;
for (Method method : classInfo.getDeclaredMethods()) {
if (Modifier.isStatic(method.getModifiers()) && method.getParameterCount() == 0) {
Repeat annotation = method.getAnnotation(Repeat.class);
if (annotation != null) {
plugin.getLogger().info("Registered task " + classInfo.getName() + "." + method.getName() + "()");
registerRepeatable(plugin, method, annotation);
registered++;
}
}
}
return registered;
}
}