Skip to content

Commit

Permalink
fix: async handler scheduler support for folia
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingrim4 committed Sep 9, 2023
1 parent f0401ac commit c58f9ca
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.bukkit.plugin.Plugin;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType;
Expand All @@ -38,8 +40,6 @@
import com.google.common.base.Function;
import com.google.common.base.Joiner;

import org.bukkit.plugin.Plugin;

/**
* Represents a handler for an asynchronous event.
* <p>
Expand Down Expand Up @@ -328,7 +328,7 @@ public synchronized void start(Function<AsyncRunnable, Void> executor) {
}

private void scheduleAsync(Runnable runnable) {
listener.getPlugin().getServer().getScheduler().runTaskAsynchronously(listener.getPlugin(), runnable);
filterManager.getScheduler().runTaskAsync(runnable);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ public Task scheduleSyncDelayedTask(Runnable task, long delay) {
int taskId = scheduler.scheduleSyncDelayedTask(plugin, task, delay);
return taskId >= 0 ? new DefaultTask(scheduler, taskId) : null;
}

@Override
public Task runTaskAsync(Runnable task) {
int taskId = scheduler.runTaskAsynchronously(plugin, task).getTaskId();
return taskId >= 0 ? new DefaultTask(scheduler, taskId) : null;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.comphenix.protocol.scheduler;

import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;

public class DefaultTask implements Task {
private final int taskId;
Expand Down
31 changes: 23 additions & 8 deletions src/main/java/com/comphenix/protocol/scheduler/FoliaScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,58 @@
import java.util.function.Consumer;

public class FoliaScheduler implements ProtocolScheduler {
private final Object foliaScheduler;
private final Object foliaRegionScheduler;
private final MethodAccessor runAtFixedRate;
private final MethodAccessor runDelayed;
private final MethodAccessor execute;
private final MethodAccessor cancel;

private final Object foliaAsyncScheduler;
private final MethodAccessor executeAsync;

private final Plugin plugin;

public FoliaScheduler(Plugin plugin) {
this.plugin = plugin;

MethodAccessor getScheduler = Accessors.getMethodAccessor(Bukkit.getServer().getClass(), "getGlobalRegionScheduler");
this.foliaScheduler = getScheduler.invoke(Bukkit.getServer());
this.foliaRegionScheduler = getScheduler.invoke(Bukkit.getServer());

this.runAtFixedRate = Accessors.getMethodAccessor(foliaScheduler.getClass(), "runAtFixedRate", Plugin.class,
this.runAtFixedRate = Accessors.getMethodAccessor(foliaRegionScheduler.getClass(), "runAtFixedRate", Plugin.class,
Consumer.class, long.class, long.class);
this.execute = Accessors.getMethodAccessor(foliaScheduler.getClass(), "run", Plugin.class, Consumer.class);
this.runDelayed = Accessors.getMethodAccessor(foliaScheduler.getClass(), "runDelayed", Plugin.class, Consumer.class, long.class);
this.execute = Accessors.getMethodAccessor(foliaRegionScheduler.getClass(), "run", Plugin.class, Consumer.class);
this.runDelayed = Accessors.getMethodAccessor(foliaRegionScheduler.getClass(), "runDelayed", Plugin.class, Consumer.class, long.class);

MethodAccessor getAsyncScheduler = Accessors.getMethodAccessor(Bukkit.getServer().getClass(), "getAsyncScheduler");
foliaAsyncScheduler = getAsyncScheduler.invoke(Bukkit.getServer());

this.executeAsync = Accessors.getMethodAccessor(foliaAsyncScheduler.getClass(), "runNow", Plugin.class, Consumer.class);

Class<?> taskClass = MinecraftReflection.getLibraryClass("io.papermc.paper.threadedregions.scheduler.ScheduledTask");
this.cancel = Accessors.getMethodAccessor(taskClass, "cancel");
}

@Override
public Task scheduleSyncRepeatingTask(Runnable task, long delay, long period) {
Object taskHandle = runAtFixedRate.invoke(foliaScheduler, plugin, (Consumer<Object>)(t -> task.run()), delay, period);
Object taskHandle = runAtFixedRate.invoke(foliaRegionScheduler, plugin, (Consumer<Object>)(t -> task.run()), delay, period);
return new FoliaTask(cancel, taskHandle);
}

@Override
public Task runTask(Runnable task) {
Object taskHandle = execute.invoke(foliaScheduler, plugin, (Consumer<Object>)(t -> task.run()));
Object taskHandle = execute.invoke(foliaRegionScheduler, plugin, (Consumer<Object>)(t -> task.run()));
return new FoliaTask(cancel, taskHandle);
}

@Override
public Task scheduleSyncDelayedTask(Runnable task, long delay) {
Object taskHandle = runDelayed.invoke(foliaScheduler, plugin, (Consumer<Object>)(t -> task.run()), delay);
Object taskHandle = runDelayed.invoke(foliaRegionScheduler, plugin, (Consumer<Object>)(t -> task.run()), delay);
return new FoliaTask(cancel, taskHandle);
}

@Override
public Task runTaskAsync(Runnable task) {
Object taskHandle = executeAsync.invoke(foliaAsyncScheduler, plugin, (Consumer<Object>)(t -> task.run()));
return new FoliaTask(cancel, taskHandle);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.comphenix.protocol.scheduler;

import com.comphenix.protocol.ProtocolLib;
import org.bukkit.plugin.Plugin;

public interface ProtocolScheduler {
Task scheduleSyncRepeatingTask(Runnable task, long delay, long period);

Task runTask(Runnable task);

Task scheduleSyncDelayedTask(Runnable task, long delay);

Task runTaskAsync(Runnable task);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.utility.Closer;
import com.comphenix.protocol.utility.SchedulerUtil;
import org.bukkit.plugin.Plugin;

import java.io.BufferedReader;
Expand Down
72 changes: 0 additions & 72 deletions src/main/java/com/comphenix/protocol/utility/SchedulerUtil.java

This file was deleted.

0 comments on commit c58f9ca

Please sign in to comment.