25
25
import com .google .common .cache .LoadingCache ;
26
26
import com .sk89q .worldedit .util .report .DataReport ;
27
27
import com .sk89q .worldguard .bukkit .WorldGuardPlugin ;
28
+ import com .tcoded .folialib .FoliaLib ;
28
29
import com .tcoded .folialib .wrapper .task .WrappedTask ;
29
- import org .bukkit .Bukkit ;
30
- import org .bukkit .scheduler .BukkitTask ;
31
30
32
31
import javax .annotation .Nullable ;
33
32
import java .lang .reflect .Field ;
37
36
38
37
public class SchedulerReport extends DataReport {
39
38
39
+ private FoliaLib foliaLib = WorldGuardPlugin .inst ().getFoliaLib ();
40
+
40
41
private LoadingCache <Class <?>, Optional <Field >> taskFieldCache = CacheBuilder .newBuilder ()
41
42
.build (new CacheLoader <Class <?>, Optional <Field >>() {
42
43
@ Override
@@ -51,30 +52,78 @@ public Optional<Field> load(Class<?> clazz) throws Exception {
51
52
}
52
53
});
53
54
55
+ private LoadingCache <Class <?>, Optional <Field >> foliaTaskFieldCache = CacheBuilder .newBuilder ()
56
+ .build (new CacheLoader <Class <?>, Optional <Field >>() {
57
+ @ Override
58
+ public Optional <Field > load (Class <?> clazz ) throws Exception {
59
+ try {
60
+ Field field = clazz .getDeclaredField ("run" );
61
+ field .setAccessible (true );
62
+ return Optional .ofNullable (field );
63
+ } catch (NoSuchFieldException ignored ) {
64
+ return Optional .empty ();
65
+ }
66
+ }
67
+ });
68
+
54
69
public SchedulerReport () {
55
70
super ("Scheduler" );
56
71
57
- append ("Error" , "FOLIA VERSION - PLEASE REPORT TO WORLDGUARD" );
58
-
59
- // List<BukkitTask> tasks = Bukkit.getServer().getScheduler().getPendingTasks();
60
- //// WorldGuardPlugin.inst().foliaLib.getImpl().
61
- //
62
- // append("Pending Task Count", tasks.size());
63
- //
64
- // for (BukkitTask task : tasks) {
65
- // Class<?> taskClass = getTaskClass(task);
66
- //
67
- // DataReport report = new DataReport("Task: #" + task.getTaskId());
68
- // report.append("Owner", task.getOwner().getName());
69
- // report.append("Runnable", taskClass != null ? taskClass.getName() : "<Unknown>");
70
- // report.append("Synchronous?", task.isSync());
71
- // append(report.getTitle(), report);
72
- // }
72
+ List <WrappedTask > tasks = foliaLib .getImpl ().getAllTasks ();
73
+
74
+ append ("Pending Task Count" , tasks .size ());
75
+
76
+ for (WrappedTask task : tasks ) {
77
+ Object handle = getTaskHandle (task );
78
+ Class <?> taskClass ;
79
+ if (foliaLib .isFolia ()) {
80
+ taskClass = getFoliaTaskClass (handle );
81
+ } else {
82
+ taskClass = getBukkitTaskClass (handle );
83
+ }
84
+
85
+ DataReport report = new DataReport ("Task: #" + handle .hashCode ());
86
+ report .append ("Owner" , task .getOwningPlugin ().getName ());
87
+ report .append ("Runnable" , taskClass != null ? taskClass .getName () : "<Unknown>" );
88
+ report .append ("Synchronous?" , !task .isAsync ());
89
+ append (report .getTitle (), report );
90
+ }
91
+ }
92
+
93
+ private Object getTaskHandle (WrappedTask task ) {
94
+ try {
95
+ Field field = task .getClass ().getDeclaredField ("task" );
96
+ field .setAccessible (true );
97
+ return field .get (task );
98
+ } catch (IllegalAccessException | NoSuchFieldException e ) {
99
+ e .printStackTrace ();
100
+ return null ;
101
+ }
102
+ }
103
+
104
+ @ SuppressWarnings ("unchecked" )
105
+ @ Nullable
106
+ private Class <?> getFoliaTaskClass (Object task ) {
107
+ try {
108
+ Class <?> clazz = task .getClass ();
109
+ Set <Class <?>> classes = (Set ) TypeToken .of (clazz ).getTypes ().rawTypes ();
110
+
111
+ for (Class <?> type : classes ) {
112
+ Optional <Field > field = foliaTaskFieldCache .getUnchecked (type );
113
+ if (field .isPresent ()) {
114
+ Object res = field .get ().get (task );
115
+ return res == null ? null : res .getClass ();
116
+ }
117
+ }
118
+ } catch (IllegalAccessException | NoClassDefFoundError ignored ) {
119
+ }
120
+
121
+ return null ;
73
122
}
74
123
75
124
@ SuppressWarnings ("unchecked" )
76
125
@ Nullable
77
- private Class <?> getTaskClass ( BukkitTask task ) {
126
+ private Class <?> getBukkitTaskClass ( Object task ) {
78
127
try {
79
128
Class <?> clazz = task .getClass ();
80
129
Set <Class <?>> classes = (Set ) TypeToken .of (clazz ).getTypes ().rawTypes ();
0 commit comments