Fix Folia compatability
This commit is contained in:
1 parent
c8519f4d74
commit
991e70e676
3 files changed
+11
-19
No files matched your search
+8
-17
@@ -1,55 +1,46 @@
|
||||
package me.caseload.knockbacksync.scheduler;
|
||||
|
||||
import io.github.retrooper.packetevents.util.folia.FoliaScheduler;
|
||||
import io.github.retrooper.packetevents.util.folia.GlobalRegionScheduler;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class FoliaSchedulerAdapter implements SchedulerAdapter {
|
||||
private final Plugin plugin;
|
||||
private GlobalRegionScheduler scheduler = null;
|
||||
|
||||
public FoliaSchedulerAdapter(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
try {
|
||||
// Attempt to find and call the `getGlobalRegionScheduler` method
|
||||
Method getSchedulerMethod = Bukkit.getServer().getClass().getMethod("getGlobalRegionScheduler");
|
||||
scheduler = (GlobalRegionScheduler) getSchedulerMethod.invoke(Bukkit.getServer());
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
plugin.getLogger().severe("Failed to access GlobalRegionScheduler: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractTaskHandle runTask(Runnable task) {
|
||||
return new FoliaTaskHandle(scheduler.run(plugin, scheduledTask -> task.run()));
|
||||
return new FoliaTaskHandle(FoliaScheduler.getGlobalRegionScheduler().run(plugin, scheduledTask -> task.run()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractTaskHandle runTaskAsynchronously(Runnable task) {
|
||||
return new FoliaTaskHandle(scheduler.run(plugin, scheduledTask -> task.run()));
|
||||
return new FoliaTaskHandle(FoliaScheduler.getAsyncScheduler().runNow(plugin, scheduledTask -> task.run()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractTaskHandle runTaskLater(Runnable task, long delayTicks) {
|
||||
return new FoliaTaskHandle(scheduler.runDelayed(plugin, scheduledTask -> task.run(), delayTicks));
|
||||
return new FoliaTaskHandle(FoliaScheduler.getGlobalRegionScheduler().runDelayed(plugin, scheduledTask -> task.run(), delayTicks));
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractTaskHandle runTaskTimer(Runnable task, long delayTicks, long periodTicks) {
|
||||
return new FoliaTaskHandle(scheduler.runAtFixedRate(plugin, scheduledTask -> task.run(), delayTicks, periodTicks));
|
||||
return new FoliaTaskHandle(FoliaScheduler.getGlobalRegionScheduler().runAtFixedRate(plugin, scheduledTask -> task.run(), delayTicks, periodTicks));
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractTaskHandle runTaskLaterAsynchronously(Runnable task, long delay) {
|
||||
return new FoliaTaskHandle(scheduler.runDelayed(plugin, scheduledTask -> task.run(), delay));
|
||||
return new FoliaTaskHandle(FoliaScheduler.getAsyncScheduler().runDelayed(plugin, scheduledTask -> task.run(), delay * 50, TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractTaskHandle runTaskTimerAsynchronously(Runnable task, long delay, long period) {
|
||||
return new FoliaTaskHandle(scheduler.runAtFixedRate(plugin, scheduledTask -> task.run(), delay, period));
|
||||
return new FoliaTaskHandle(FoliaScheduler.getAsyncScheduler().runAtFixedRate(plugin, scheduledTask -> task.run(), delay, period));
|
||||
}
|
||||
|
||||
// Folia takes care of this
|
||||
|
||||
@@ -17,7 +17,8 @@ public class FoliaTaskHandle implements AbstractTaskHandle {
|
||||
MethodHandles.Lookup lookup = MethodHandles.lookup();
|
||||
|
||||
GET_OWNING_PLUGIN = lookup.findVirtual(scheduledTaskClass, "getOwningPlugin", MethodType.methodType(Plugin.class));
|
||||
CANCEL = lookup.findVirtual(scheduledTaskClass, "cancel", MethodType.methodType(void.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);
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ dependencies {
|
||||
modImplementation("net.fabricmc.fabric-api:fabric-api:${rootProject.property("fabric_version")}")
|
||||
|
||||
include(modImplementation("me.lucko:fabric-permissions-api:0.3.1")!!)
|
||||
include(modImplementation("com.github.retrooper:packetevents-fabric:2.6.0-SNAPSHOT")!!)
|
||||
include(modImplementation("com.github.retrooper:packetevents-fabric:2.6.1-SNAPSHOT")!!)
|
||||
include(modImplementation("org.incendo:cloud-fabric:2.0.0-beta.9")!!)
|
||||
|
||||
include(implementation("org.yaml:snakeyaml:2.0")!!)
|
||||
|
||||
Reference in new issue
Block a user