Fix reloading on Folia

This commit is contained in:
Axionize committed 2024-11-29 20:25:15 -05:00
1 parent 26c72b5490
commit 58f61073c6
1 file changed
+5 -32
@@ -1,42 +1,19 @@
package me.caseload.knockbacksync.scheduler;
import io.github.retrooper.packetevents.util.folia.TaskWrapper;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class FoliaTaskHandle implements AbstractTaskHandle {
private static final MethodHandle GET_OWNING_PLUGIN;
private static final MethodHandle CANCEL;
static {
try {
Class<?> scheduledTaskClass = Class.forName("io.papermc.paper.threadedregions.scheduler.ScheduledTask");
MethodHandles.Lookup lookup = MethodHandles.lookup();
GET_OWNING_PLUGIN = lookup.findVirtual(scheduledTaskClass, "getOwningPlugin", MethodType.methodType(Plugin.class));
CANCEL = lookup.findVirtual(scheduledTaskClass, "cancel", MethodType.methodType(
Class.forName("io.papermc.paper.threadedregions.scheduler.ScheduledTask$CancelledState")));
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException("Failed to initialize FoliaTaskHandle reflection", e);
}
}
private final Object scheduledTask; // Store as Object instead of ScheduledTask
private final TaskWrapper scheduledTask; // Store as Object instead of ScheduledTask
private boolean cancelled;
public FoliaTaskHandle(@NotNull Object scheduledTask) {
public FoliaTaskHandle(@NotNull TaskWrapper scheduledTask) {
this.scheduledTask = scheduledTask;
}
public Plugin getOwner() {
try {
return (Plugin) GET_OWNING_PLUGIN.invoke(scheduledTask);
} catch (Throwable e) {
throw new RuntimeException("Failed to invoke getOwningPlugin", e);
}
return scheduledTask.getOwner();
}
@Override
@@ -46,11 +23,7 @@ public class FoliaTaskHandle implements AbstractTaskHandle {
@Override
public void cancel() {
try {
CANCEL.invoke(scheduledTask);
} catch (Throwable e) {
throw new RuntimeException("Failed to invoke cancel", e);
}
scheduledTask.cancel();
this.cancelled = true;
}
}