Skip to content

Commit 1368238

Browse files
committed
Updated FoliaLib dependency. Added new Folia task methods to SchedulerReport.java. Re-added Bukkit task reports in SchedulerReport.java.
1 parent 6edbb60 commit 1368238

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

worldguard-bukkit/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dependencies {
3030
}
3131
"api"("com.sk89q.worldedit:worldedit-bukkit:${Versions.WORLDEDIT}") { isTransitive = false }
3232
"implementation"("com.google.guava:guava:${Versions.GUAVA}")
33-
"implementation"("com.tcoded:FoliaLib:0.3.1")
33+
"implementation"("com.tcoded:FoliaLib:0.3.2")
3434
"compileOnly"("com.sk89q:commandbook:2.3") { isTransitive = false }
3535
"shadeOnly"("io.papermc:paperlib:1.0.8")
3636
"shadeOnly"("org.bstats:bstats-bukkit:3.0.1")

worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/util/report/SchedulerReport.java

+68-19
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
import com.google.common.cache.LoadingCache;
2626
import com.sk89q.worldedit.util.report.DataReport;
2727
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
28+
import com.tcoded.folialib.FoliaLib;
2829
import com.tcoded.folialib.wrapper.task.WrappedTask;
29-
import org.bukkit.Bukkit;
30-
import org.bukkit.scheduler.BukkitTask;
3130

3231
import javax.annotation.Nullable;
3332
import java.lang.reflect.Field;
@@ -37,6 +36,8 @@
3736

3837
public class SchedulerReport extends DataReport {
3938

39+
private FoliaLib foliaLib = WorldGuardPlugin.inst().getFoliaLib();
40+
4041
private LoadingCache<Class<?>, Optional<Field>> taskFieldCache = CacheBuilder.newBuilder()
4142
.build(new CacheLoader<Class<?>, Optional<Field>>() {
4243
@Override
@@ -51,30 +52,78 @@ public Optional<Field> load(Class<?> clazz) throws Exception {
5152
}
5253
});
5354

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+
5469
public SchedulerReport() {
5570
super("Scheduler");
5671

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;
73122
}
74123

75124
@SuppressWarnings("unchecked")
76125
@Nullable
77-
private Class<?> getTaskClass(BukkitTask task) {
126+
private Class<?> getBukkitTaskClass(Object task) {
78127
try {
79128
Class<?> clazz = task.getClass();
80129
Set<Class<?>> classes = (Set) TypeToken.of(clazz).getTypes().rawTypes();

0 commit comments

Comments
 (0)