Add Folia providers

This commit is contained in:
Josh Roy 2023-03-27 23:29:42 -04:00
parent bf14b88600
commit 15c48d457a
No known key found for this signature in database
GPG Key ID: 86A69D08540BC29A
6 changed files with 258 additions and 0 deletions

View File

@ -18,6 +18,7 @@ dependencies {
// Providers
api project(':providers:BaseProviders')
api project(':providers:PaperProvider')
api project(':providers:FoliaProvider')
api(project(':providers:NMSReflectionProvider')) {
exclude group: "org.bukkit", module: "bukkit"
}
@ -45,6 +46,7 @@ shadowJar {
include (dependency('org.checkerframework:checker-qual'))
include (project(':providers:BaseProviders'))
include (project(':providers:PaperProvider'))
include (project(':providers:FoliaProvider'))
include (project(':providers:NMSReflectionProvider'))
include (project(':providers:1_8Provider'))
include (project(':providers:1_12Provider'))

View File

@ -0,0 +1,34 @@
package net.ess3.provider;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
public interface SchedulingProvider extends Provider {
void registerInitTask(Runnable runnable);
void runEntityTask(Entity entity, Runnable runnable);
EssentialsTask runEntityTask(Entity entity, Runnable runnable, long delay);
EssentialsTask runEntityTaskRepeating(Entity entity, Runnable runnable, long delay, long period);
void runLocationalTask(Location location, Runnable runnable);
void runLocationalTask(Location location, Runnable runnable, long delay);
EssentialsTask runLocationalTaskRepeating(Location location, Runnable runnable, long delay, long period);
void runGlobalLocationalTask(Runnable runnable, long delay);
EssentialsTask runGlobalLocationalTaskRepeating(Runnable runnable, long delay, long period);
void runAsyncTask(Runnable runnable);
void runAsyncTaskLater(Runnable runnable, long delay);
EssentialsTask runAsyncTaskRepeating(Runnable runnable, long delay, long period);
interface EssentialsTask {
void cancel();
}
}

View File

@ -0,0 +1,91 @@
package net.ess3.provider.providers;
import net.ess3.provider.SchedulingProvider;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
public class BukkitSchedulingProvider implements SchedulingProvider {
private final Plugin plugin;
public BukkitSchedulingProvider(final Plugin plugin) {
this.plugin = plugin;
}
@Override
public void registerInitTask(Runnable runnable) {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, runnable);
}
@Override
public void runEntityTask(Entity entity, Runnable runnable) {
runEntityTask(entity, runnable, 1);
}
@Override
public EssentialsTask runEntityTask(Entity entity, Runnable runnable, long delay) {
return scheduleSyncTask(runnable, delay);
}
@Override
public EssentialsTask runEntityTaskRepeating(Entity entity, Runnable runnable, long delay, long period) {
return scheduleSyncTaskRepeating(runnable, delay, period);
}
@Override
public void runLocationalTask(Location location, Runnable runnable) {
runGlobalLocationalTask(runnable, 1);
}
@Override
public void runLocationalTask(Location location, Runnable runnable, long delay) {
runGlobalLocationalTask(runnable, delay);
}
@Override
public EssentialsTask runLocationalTaskRepeating(Location location, Runnable runnable, long delay, long period) {
return scheduleSyncTaskRepeating(runnable, delay, period);
}
@Override
public void runGlobalLocationalTask(Runnable runnable, long delay) {
scheduleSyncTask(runnable, delay);
}
@Override
public EssentialsTask runGlobalLocationalTaskRepeating(Runnable runnable, long delay, long period) {
return scheduleSyncTaskRepeating(runnable, delay, period);
}
private EssentialsTask scheduleSyncTask(Runnable runnable, long delay) {
final int task = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, runnable, delay);
return () -> plugin.getServer().getScheduler().cancelTask(task);
}
private EssentialsTask scheduleSyncTaskRepeating(Runnable runnable, long delay, long period) {
final BukkitTask task = plugin.getServer().getScheduler().runTaskTimer(plugin, runnable, delay, period);
return task::cancel;
}
@Override
public void runAsyncTask(Runnable runnable) {
runAsyncTaskLater(runnable, 0);
}
@Override
public void runAsyncTaskLater(Runnable runnable, long delay) {
plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, runnable, delay);
}
@Override
public EssentialsTask runAsyncTaskRepeating(Runnable runnable, long delay, long period) {
final BukkitTask task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, period);
return task::cancel;
}
@Override
public String getDescription() {
return "Bukkit Scheduling Provider";
}
}

View File

@ -0,0 +1,19 @@
plugins {
id("essentials.base-conventions")
}
java {
disableAutoTargetJvm()
}
dependencies {
implementation(project(':providers:BaseProviders')) {
exclude(module: 'spigot-api')
}
compileOnly 'dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT'
}
essentials {
injectBukkitApi.set(false)
injectBstats.set(false)
}

View File

@ -0,0 +1,110 @@
package net.ess3.provider.providers;
import io.papermc.paper.threadedregions.RegionizedServerInitEvent;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import net.ess3.provider.SchedulingProvider;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class FoliaSchedulingProvider implements SchedulingProvider {
private final Plugin plugin;
private List<Runnable> initTasks = new ArrayList<>();
public FoliaSchedulingProvider(Plugin plugin) {
this.plugin = plugin;
}
@EventHandler
public void onServerInit(RegionizedServerInitEvent event) {
for (final Runnable tasks : initTasks) {
tasks.run();
}
initTasks = null;
}
@Override
public void registerInitTask(Runnable runnable) {
if (initTasks == null) {
throw new IllegalStateException();
}
initTasks.add(runnable);
}
@Override
public void runEntityTask(Entity entity, Runnable runnable) {
runEntityTask(entity, runnable, 1);
}
@Override
public EssentialsTask runEntityTask(Entity entity, Runnable runnable, long delay) {
final ScheduledTask task = entity.getScheduler().runDelayed(plugin, scheduledTask -> runnable.run(), null, delay);
if (task == null) {
throw new IllegalArgumentException("entity is removed!");
}
return task::cancel;
}
@Override
public EssentialsTask runEntityTaskRepeating(Entity entity, Runnable runnable, long delay, long period) {
final ScheduledTask task = entity.getScheduler().runAtFixedRate(plugin, scheduledTask -> runnable.run(), null, delay, period);
if (task == null) {
throw new IllegalArgumentException("entity is removed!");
}
return task::cancel;
}
@Override
public void runLocationalTask(Location location, Runnable runnable) {
plugin.getServer().getRegionScheduler().execute(plugin, location, runnable);
}
@Override
public void runLocationalTask(Location location, Runnable runnable, long delay) {
plugin.getServer().getRegionScheduler().runDelayed(plugin, location, scheduledTask -> runnable.run(), delay);
}
@Override
public EssentialsTask runLocationalTaskRepeating(Location location, Runnable runnable, long delay, long period) {
final ScheduledTask task = plugin.getServer().getRegionScheduler().runAtFixedRate(plugin, location, scheduledTask -> runnable.run(), delay, period);;
return task::cancel;
}
@Override
public void runGlobalLocationalTask(Runnable runnable, long delay) {
plugin.getServer().getGlobalRegionScheduler().runDelayed(plugin, scheduledTask -> runnable.run(), delay);
}
@Override
public EssentialsTask runGlobalLocationalTaskRepeating(Runnable runnable, long delay, long period) {
final ScheduledTask task = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, scheduledTask -> runnable.run(), delay, period);
return task::cancel;
}
@Override
public void runAsyncTask(Runnable runnable) {
plugin.getServer().getAsyncScheduler().runNow(plugin, scheduledTask -> runnable.run());
}
@Override
public void runAsyncTaskLater(Runnable runnable, long delay) {
plugin.getServer().getAsyncScheduler().runDelayed(plugin, scheduledTask -> runnable.run(), delay * 50L, TimeUnit.MILLISECONDS);
}
@Override
public EssentialsTask runAsyncTaskRepeating(Runnable runnable, long delay, long period) {
final ScheduledTask task = plugin.getServer().getAsyncScheduler().runAtFixedRate(plugin, scheduledTask -> runnable.run(), delay, delay * 50, TimeUnit.MICROSECONDS);
return task::cancel;
}
@Override
public String getDescription() {
return "Folia Scheduling Provider";
}
}

View File

@ -21,6 +21,7 @@ dependencyResolutionManagement {
content { includeGroup("net.kyori") }
content { includeGroup("org.apache.logging.log4j") }
}
mavenLocal()
}
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
}
@ -51,5 +52,6 @@ sequenceOf(
include(":providers:BaseProviders")
include(":providers:NMSReflectionProvider")
include(":providers:PaperProvider")
include(":providers:FoliaProvider")
include(":providers:1_8Provider")
include(":providers:1_12Provider")