Updated FoliaLib dependency. Added new Folia task methods to SchedulerReport.java. Re-added Bukkit task reports in SchedulerReport.java.

This commit is contained in:
Loving11ish 2024-02-06 02:37:45 +00:00
parent 6edbb60e00
commit 13682383c8
2 changed files with 68 additions and 19 deletions

View File

@ -30,7 +30,7 @@ dependencies {
}
"api"("com.sk89q.worldedit:worldedit-bukkit:${Versions.WORLDEDIT}") { isTransitive = false }
"implementation"("com.google.guava:guava:${Versions.GUAVA}")
"implementation"("com.tcoded:FoliaLib:0.3.1")
"implementation"("com.tcoded:FoliaLib:0.3.2")
"compileOnly"("com.sk89q:commandbook:2.3") { isTransitive = false }
"shadeOnly"("io.papermc:paperlib:1.0.8")
"shadeOnly"("org.bstats:bstats-bukkit:3.0.1")

View File

@ -25,9 +25,8 @@ import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.sk89q.worldedit.util.report.DataReport;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.tcoded.folialib.FoliaLib;
import com.tcoded.folialib.wrapper.task.WrappedTask;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
@ -37,6 +36,8 @@ import java.util.Set;
public class SchedulerReport extends DataReport {
private FoliaLib foliaLib = WorldGuardPlugin.inst().getFoliaLib();
private LoadingCache<Class<?>, Optional<Field>> taskFieldCache = CacheBuilder.newBuilder()
.build(new CacheLoader<Class<?>, Optional<Field>>() {
@Override
@ -51,30 +52,78 @@ public class SchedulerReport extends DataReport {
}
});
private LoadingCache<Class<?>, Optional<Field>> foliaTaskFieldCache = CacheBuilder.newBuilder()
.build(new CacheLoader<Class<?>, Optional<Field>>() {
@Override
public Optional<Field> load(Class<?> clazz) throws Exception {
try {
Field field = clazz.getDeclaredField("run");
field.setAccessible(true);
return Optional.ofNullable(field);
} catch (NoSuchFieldException ignored) {
return Optional.empty();
}
}
});
public SchedulerReport() {
super("Scheduler");
append("Error", "FOLIA VERSION - PLEASE REPORT TO WORLDGUARD");
List<WrappedTask> tasks = foliaLib.getImpl().getAllTasks();
// List<BukkitTask> tasks = Bukkit.getServer().getScheduler().getPendingTasks();
//// WorldGuardPlugin.inst().foliaLib.getImpl().
//
// append("Pending Task Count", tasks.size());
//
// for (BukkitTask task : tasks) {
// Class<?> taskClass = getTaskClass(task);
//
// DataReport report = new DataReport("Task: #" + task.getTaskId());
// report.append("Owner", task.getOwner().getName());
// report.append("Runnable", taskClass != null ? taskClass.getName() : "<Unknown>");
// report.append("Synchronous?", task.isSync());
// append(report.getTitle(), report);
// }
append("Pending Task Count", tasks.size());
for (WrappedTask task : tasks) {
Object handle = getTaskHandle(task);
Class<?> taskClass;
if (foliaLib.isFolia()) {
taskClass = getFoliaTaskClass(handle);
} else {
taskClass = getBukkitTaskClass(handle);
}
DataReport report = new DataReport("Task: #" + handle.hashCode());
report.append("Owner", task.getOwningPlugin().getName());
report.append("Runnable", taskClass != null ? taskClass.getName() : "<Unknown>");
report.append("Synchronous?", !task.isAsync());
append(report.getTitle(), report);
}
}
private Object getTaskHandle(WrappedTask task) {
try {
Field field = task.getClass().getDeclaredField("task");
field.setAccessible(true);
return field.get(task);
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
@Nullable
private Class<?> getTaskClass(BukkitTask task) {
private Class<?> getFoliaTaskClass(Object task) {
try {
Class<?> clazz = task.getClass();
Set<Class<?>> classes = (Set) TypeToken.of(clazz).getTypes().rawTypes();
for (Class<?> type : classes) {
Optional<Field> field = foliaTaskFieldCache.getUnchecked(type);
if (field.isPresent()) {
Object res = field.get().get(task);
return res == null ? null : res.getClass();
}
}
} catch (IllegalAccessException | NoClassDefFoundError ignored) {
}
return null;
}
@SuppressWarnings("unchecked")
@Nullable
private Class<?> getBukkitTaskClass(Object task) {
try {
Class<?> clazz = task.getClass();
Set<Class<?>> classes = (Set) TypeToken.of(clazz).getTypes().rawTypes();