|
9 | 9 | import java.util.function.Consumer;
|
10 | 10 |
|
11 | 11 | public class FoliaScheduler implements ProtocolScheduler {
|
12 |
| - private final Object foliaScheduler; |
| 12 | + private final Object foliaRegionScheduler; |
13 | 13 | private final MethodAccessor runAtFixedRate;
|
14 | 14 | private final MethodAccessor runDelayed;
|
15 | 15 | private final MethodAccessor execute;
|
16 | 16 | private final MethodAccessor cancel;
|
| 17 | + |
| 18 | + private final Object foliaAsyncScheduler; |
| 19 | + private final MethodAccessor executeAsync; |
| 20 | + |
17 | 21 | private final Plugin plugin;
|
18 | 22 |
|
19 | 23 | public FoliaScheduler(Plugin plugin) {
|
20 | 24 | this.plugin = plugin;
|
21 | 25 |
|
22 | 26 | MethodAccessor getScheduler = Accessors.getMethodAccessor(Bukkit.getServer().getClass(), "getGlobalRegionScheduler");
|
23 |
| - this.foliaScheduler = getScheduler.invoke(Bukkit.getServer()); |
| 27 | + this.foliaRegionScheduler = getScheduler.invoke(Bukkit.getServer()); |
24 | 28 |
|
25 |
| - this.runAtFixedRate = Accessors.getMethodAccessor(foliaScheduler.getClass(), "runAtFixedRate", Plugin.class, |
| 29 | + this.runAtFixedRate = Accessors.getMethodAccessor(foliaRegionScheduler.getClass(), "runAtFixedRate", Plugin.class, |
26 | 30 | Consumer.class, long.class, long.class);
|
27 |
| - this.execute = Accessors.getMethodAccessor(foliaScheduler.getClass(), "run", Plugin.class, Consumer.class); |
28 |
| - this.runDelayed = Accessors.getMethodAccessor(foliaScheduler.getClass(), "runDelayed", Plugin.class, Consumer.class, long.class); |
| 31 | + this.execute = Accessors.getMethodAccessor(foliaRegionScheduler.getClass(), "run", Plugin.class, Consumer.class); |
| 32 | + this.runDelayed = Accessors.getMethodAccessor(foliaRegionScheduler.getClass(), "runDelayed", Plugin.class, Consumer.class, long.class); |
| 33 | + |
| 34 | + MethodAccessor getAsyncScheduler = Accessors.getMethodAccessor(Bukkit.getServer().getClass(), "getAsyncScheduler"); |
| 35 | + foliaAsyncScheduler = getAsyncScheduler.invoke(Bukkit.getServer()); |
| 36 | + |
| 37 | + this.executeAsync = Accessors.getMethodAccessor(foliaAsyncScheduler.getClass(), "runNow", Plugin.class, Consumer.class); |
29 | 38 |
|
30 | 39 | Class<?> taskClass = MinecraftReflection.getLibraryClass("io.papermc.paper.threadedregions.scheduler.ScheduledTask");
|
31 | 40 | this.cancel = Accessors.getMethodAccessor(taskClass, "cancel");
|
32 | 41 | }
|
33 | 42 |
|
34 | 43 | @Override
|
35 | 44 | public Task scheduleSyncRepeatingTask(Runnable task, long delay, long period) {
|
36 |
| - Object taskHandle = runAtFixedRate.invoke(foliaScheduler, plugin, (Consumer<Object>)(t -> task.run()), delay, period); |
| 45 | + Object taskHandle = runAtFixedRate.invoke(foliaRegionScheduler, plugin, (Consumer<Object>)(t -> task.run()), delay, period); |
37 | 46 | return new FoliaTask(cancel, taskHandle);
|
38 | 47 | }
|
39 | 48 |
|
40 | 49 | @Override
|
41 | 50 | public Task runTask(Runnable task) {
|
42 |
| - Object taskHandle = execute.invoke(foliaScheduler, plugin, (Consumer<Object>)(t -> task.run())); |
| 51 | + Object taskHandle = execute.invoke(foliaRegionScheduler, plugin, (Consumer<Object>)(t -> task.run())); |
43 | 52 | return new FoliaTask(cancel, taskHandle);
|
44 | 53 | }
|
45 | 54 |
|
46 | 55 | @Override
|
47 | 56 | public Task scheduleSyncDelayedTask(Runnable task, long delay) {
|
48 |
| - Object taskHandle = runDelayed.invoke(foliaScheduler, plugin, (Consumer<Object>)(t -> task.run()), delay); |
| 57 | + Object taskHandle = runDelayed.invoke(foliaRegionScheduler, plugin, (Consumer<Object>)(t -> task.run()), delay); |
49 | 58 | return new FoliaTask(cancel, taskHandle);
|
50 | 59 | }
|
| 60 | + |
| 61 | + @Override |
| 62 | + public Task runTaskAsync(Runnable task) { |
| 63 | + Object taskHandle = executeAsync.invoke(foliaAsyncScheduler, plugin, (Consumer<Object>)(t -> task.run())); |
| 64 | + return new FoliaTask(cancel, taskHandle); |
| 65 | + } |
51 | 66 | }
|
0 commit comments