Merge pull request #50 from D3v1s0m/2.0.0

fix commands being executed on wrong thread in folia
This commit is contained in:
Pyr 2023-05-11 09:49:55 +01:00 committed by GitHub
commit cccf2ea133
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

@ -60,6 +60,11 @@ public final class Reflections {
.withClassName("AsyncScheduler")
.setStrict(FoliaUtil.isFolia())).get();
public static final Class<?> GLOBAL_REGION_SCHEDULER_CLASS = new ClassReflection(
new ReflectionBuilder("io.papermc.paper.threadedregions.scheduler")
.withClassName("GlobalRegionScheduler")
.setStrict(FoliaUtil.isFolia())).get();
public static final Class<?> SCHEDULED_TASK_CLASS = new ClassReflection(
new ReflectionBuilder("io.papermc.paper.threadedregions.scheduler")
.withClassName("ScheduledTask")
@ -71,6 +76,12 @@ public final class Reflections {
.withExpectResult(ASYNC_SCHEDULER_CLASS)
.setStrict(FoliaUtil.isFolia()));
public static final ReflectionLazyLoader<Method> FOLIA_GET_GLOBAL_REGION_SCHEDULER = new MethodReflection(
new ReflectionBuilder(Bukkit.class)
.withMethodName("getGlobalRegionScheduler")
.withExpectResult(GLOBAL_REGION_SCHEDULER_CLASS)
.setStrict(FoliaUtil.isFolia()));
public static final ReflectionLazyLoader<Method> FOLIA_RUN_NOW = new MethodReflection(
new ReflectionBuilder(ASYNC_SCHEDULER_CLASS)
.withMethodName("runNow")
@ -92,6 +103,13 @@ public final class Reflections {
.withExpectResult(SCHEDULED_TASK_CLASS)
.setStrict(FoliaUtil.isFolia()));
public static final ReflectionLazyLoader<Method> FOLIA_RUN_NOW_GLOBAL = new MethodReflection(
new ReflectionBuilder(GLOBAL_REGION_SCHEDULER_CLASS)
.withMethodName("runNow")
.withParameterTypes(Plugin.class, Consumer.class)
.withExpectResult(SCHEDULED_TASK_CLASS)
.setStrict(FoliaUtil.isFolia()));
public static final ReflectionLazyLoader<Method> FOLIA_TELEPORT_ASYNC = new MethodReflection(
new ReflectionBuilder(Entity.class)
.withMethodName("teleportAsync")

@ -14,7 +14,12 @@ public class FoliaScheduler extends TaskScheduler {
@Override
public void runSync(Runnable runnable) {
// TODO: Figure out which scheduler to use for running commands
try {
Object scheduler = Reflections.FOLIA_GET_GLOBAL_REGION_SCHEDULER.get().invoke(null);
Reflections.FOLIA_RUN_NOW_GLOBAL.get().invoke(scheduler, plugin, (Consumer<Object>) o -> runnable.run());
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
@Override