diff --git a/commons-folia/build.gradle.kts b/commons-folia/build.gradle.kts new file mode 100644 index 0000000..c4bea66 --- /dev/null +++ b/commons-folia/build.gradle.kts @@ -0,0 +1,16 @@ +plugins { + `commons-java` + `commons-publish` + `commons-repositories` +} + +dependencies { + api(project(":commons-shared")) + api(project(":commons-bukkit")) + + compileOnlyApi("dev.folia:folia-api:1.20.1-R0.1-SNAPSHOT") +} + +tasks.test { + useJUnitPlatform() +} diff --git a/commons-folia/src/main/java/space/bxteam/commons/folia/scheduler/FoliaScheduledTask.java b/commons-folia/src/main/java/space/bxteam/commons/folia/scheduler/FoliaScheduledTask.java new file mode 100644 index 0000000..6c2270b --- /dev/null +++ b/commons-folia/src/main/java/space/bxteam/commons/folia/scheduler/FoliaScheduledTask.java @@ -0,0 +1,34 @@ +package space.bxteam.commons.folia.scheduler; + +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import org.bukkit.plugin.Plugin; +import space.bxteam.commons.scheduler.Task; + +public class FoliaScheduledTask implements Task { + private final ScheduledTask task; + + public FoliaScheduledTask(final ScheduledTask task) { + this.task = task; + } + + public void cancel() { + this.task.cancel(); + } + + public boolean isCancelled() { + return this.task.isCancelled(); + } + + public Plugin getPlugin() { + return this.task.getOwningPlugin(); + } + + public boolean isCurrentlyRunning() { + final ScheduledTask.ExecutionState state = this.task.getExecutionState(); + return state == ScheduledTask.ExecutionState.RUNNING || state == ScheduledTask.ExecutionState.CANCELLED_RUNNING; + } + + public boolean isRepeatingTask() { + return this.task.isRepeatingTask(); + } +} diff --git a/commons-folia/src/main/java/space/bxteam/commons/folia/scheduler/FoliaScheduler.java b/commons-folia/src/main/java/space/bxteam/commons/folia/scheduler/FoliaScheduler.java new file mode 100644 index 0000000..f97c2e5 --- /dev/null +++ b/commons-folia/src/main/java/space/bxteam/commons/folia/scheduler/FoliaScheduler.java @@ -0,0 +1,149 @@ +package space.bxteam.commons.folia.scheduler; + +import io.papermc.paper.threadedregions.scheduler.AsyncScheduler; +import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler; +import io.papermc.paper.threadedregions.scheduler.RegionScheduler; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; +import space.bxteam.commons.scheduler.Scheduler; +import space.bxteam.commons.scheduler.Task; + +import java.util.concurrent.TimeUnit; + +public class FoliaScheduler implements Scheduler { + final Plugin plugin; + + public FoliaScheduler(Plugin plugin) { + this.plugin = plugin; + } + + private final RegionScheduler regionScheduler = Bukkit.getServer().getRegionScheduler(); + private final GlobalRegionScheduler globalRegionScheduler = Bukkit.getServer().getGlobalRegionScheduler(); + private final AsyncScheduler asyncScheduler = Bukkit.getServer().getAsyncScheduler(); + + @Override + public boolean isGlobalThread() { + return Bukkit.getServer().isGlobalTickThread(); + } + + @Override + public boolean isTickThread() { + return Bukkit.getServer().isPrimaryThread(); + } + + @Override + public boolean isEntityThread(Entity entity) { + return Bukkit.getServer().isOwnedByCurrentRegion(entity); + } + + @Override + public boolean isRegionThread(Location location) { + return Bukkit.getServer().isOwnedByCurrentRegion(location); + } + + @Override + public Task runTask(Runnable runnable) { + return new FoliaScheduledTask(globalRegionScheduler.run(plugin, task -> runnable.run())); + } + + @Override + public Task runTaskLater(Runnable runnable, long delay) { + if (delay <= 0) { + return runTask(runnable); + } + return new FoliaScheduledTask(globalRegionScheduler.runDelayed(plugin, task -> runnable.run(), delay)); + } + + @Override + public Task runTaskTimer(Runnable runnable, long delay, long period) { + delay = getOneIfNotPositive(delay); + return new FoliaScheduledTask(globalRegionScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay, period)); + } + + @Override + public Task runTask(Location location, Runnable runnable) { + return new FoliaScheduledTask(regionScheduler.run(plugin, location, task -> runnable.run())); + } + + @Override + public Task runTaskLater(Location location, Runnable runnable, long delay) { + if (delay <= 0) { + return runTask(runnable); + } + return new FoliaScheduledTask(regionScheduler.runDelayed(plugin, location, task -> runnable.run(), delay)); + } + + @Override + public Task runTaskTimer(Location location, Runnable runnable, long delay, long period) { + delay = getOneIfNotPositive(delay); + return new FoliaScheduledTask(regionScheduler.runAtFixedRate(plugin, location, task -> runnable.run(), delay, period)); + } + + @Override + public Task runTask(Entity entity, Runnable runnable) { + return new FoliaScheduledTask(entity.getScheduler().run(plugin, task -> runnable.run(), null)); + } + + @Override + public Task runTaskLater(Entity entity, Runnable runnable, long delay) { + if (delay <= 0) { + return runTask(entity, runnable); + } + return new FoliaScheduledTask(entity.getScheduler().runDelayed(plugin, task -> runnable.run(), null, delay)); + } + + @Override + public Task runTaskTimer(Entity entity, Runnable runnable, long delay, long period) { + delay = getOneIfNotPositive(delay); + return new FoliaScheduledTask(entity.getScheduler().runAtFixedRate(plugin, task -> runnable.run(), null, delay, period)); + } + + @Override + public Task runTaskAsynchronously(Runnable runnable) { + return new FoliaScheduledTask(asyncScheduler.runNow(plugin, task -> runnable.run())); + } + + @Override + public Task runTaskLaterAsynchronously(Runnable runnable, long delay) { + delay = getOneIfNotPositive(delay); + return new FoliaScheduledTask(asyncScheduler.runDelayed(plugin, task -> runnable.run(), delay * 50L, TimeUnit.MILLISECONDS)); + } + + @Override + public Task runTaskTimerAsynchronously(Runnable runnable, long delay, long period) { + return new FoliaScheduledTask(asyncScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay * 50, period * 50, TimeUnit.MILLISECONDS)); + } + + @Override + public void execute(Runnable runnable) { + globalRegionScheduler.execute(plugin, runnable); + } + + @Override + public void execute(Location location, Runnable runnable) { + regionScheduler.execute(plugin, location, runnable); + } + + @Override + public void execute(Entity entity, Runnable runnable) { + entity.getScheduler().execute(plugin, runnable, null, 1L); + } + + @Override + public void cancelTasks() { + globalRegionScheduler.cancelTasks(plugin); + asyncScheduler.cancelTasks(plugin); + } + + @Override + public void cancelTasks(Plugin plugin) { + globalRegionScheduler.cancelTasks(plugin); + asyncScheduler.cancelTasks(plugin); + } + + private long getOneIfNotPositive(long x) { + return x <= 0 ? 1L : x; + } +} diff --git a/commons-folia/src/main/java/space/bxteam/commons/paper/scheduler/PaperScheduler.java b/commons-folia/src/main/java/space/bxteam/commons/paper/scheduler/PaperScheduler.java new file mode 100644 index 0000000..a82a487 --- /dev/null +++ b/commons-folia/src/main/java/space/bxteam/commons/paper/scheduler/PaperScheduler.java @@ -0,0 +1,16 @@ +package space.bxteam.commons.paper.scheduler; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; +import space.bxteam.commons.folia.scheduler.FoliaScheduler; + +public class PaperScheduler extends FoliaScheduler { + public PaperScheduler(Plugin plugin) { + super(plugin); + } + + @Override + public boolean isGlobalThread() { + return Bukkit.getServer().isPrimaryThread(); + } +} diff --git a/commons-github/build.gradle.kts b/commons-github/build.gradle.kts deleted file mode 100644 index 01628f9..0000000 --- a/commons-github/build.gradle.kts +++ /dev/null @@ -1,11 +0,0 @@ -plugins { - `commons-java` - `commons-publish` - `commons-repositories` -} - -dependencies { - api("org.jetbrains:annotations:26.0.1") - - compileOnly("com.google.code.gson:gson:2.10") -} diff --git a/commons-github/src/main/java/space/bxteam/commons/github/GitCheck.java b/commons-shared/src/main/java/space/bxteam/commons/github/GitCheck.java similarity index 100% rename from commons-github/src/main/java/space/bxteam/commons/github/GitCheck.java rename to commons-shared/src/main/java/space/bxteam/commons/github/GitCheck.java diff --git a/commons-github/src/main/java/space/bxteam/commons/github/GitCheckResult.java b/commons-shared/src/main/java/space/bxteam/commons/github/GitCheckResult.java similarity index 100% rename from commons-github/src/main/java/space/bxteam/commons/github/GitCheckResult.java rename to commons-shared/src/main/java/space/bxteam/commons/github/GitCheckResult.java diff --git a/commons-github/src/main/java/space/bxteam/commons/github/GitRelease.java b/commons-shared/src/main/java/space/bxteam/commons/github/GitRelease.java similarity index 100% rename from commons-github/src/main/java/space/bxteam/commons/github/GitRelease.java rename to commons-shared/src/main/java/space/bxteam/commons/github/GitRelease.java diff --git a/commons-github/src/main/java/space/bxteam/commons/github/GitRepository.java b/commons-shared/src/main/java/space/bxteam/commons/github/GitRepository.java similarity index 100% rename from commons-github/src/main/java/space/bxteam/commons/github/GitRepository.java rename to commons-shared/src/main/java/space/bxteam/commons/github/GitRepository.java diff --git a/commons-github/src/main/java/space/bxteam/commons/github/GitTag.java b/commons-shared/src/main/java/space/bxteam/commons/github/GitTag.java similarity index 100% rename from commons-github/src/main/java/space/bxteam/commons/github/GitTag.java rename to commons-shared/src/main/java/space/bxteam/commons/github/GitTag.java diff --git a/commons-shared/src/main/java/space/bxteam/commons/scheduler/Scheduler.java b/commons-shared/src/main/java/space/bxteam/commons/scheduler/Scheduler.java index 7a8cc8f..5aaf7e8 100644 --- a/commons-shared/src/main/java/space/bxteam/commons/scheduler/Scheduler.java +++ b/commons-shared/src/main/java/space/bxteam/commons/scheduler/Scheduler.java @@ -11,6 +11,7 @@ public interface Scheduler { /** + * Folia: Returns whether the current thread is ticking the global region
* Paper & Bukkit: Returns {@link org.bukkit.Server#isPrimaryThread} */ boolean isGlobalThread(); @@ -23,7 +24,7 @@ default boolean isTickThread() { } /** - * Paper: Returns whether the current thread is ticking a region and that the region + * Folia & Paper: Returns whether the current thread is ticking a region and that the region * being ticked owns the specified entity. Note that this function is the only appropriate method of * checking for ownership of an entity, as retrieving the entity's location is undefined unless the * entity is owned by the current region @@ -35,7 +36,7 @@ default boolean isTickThread() { boolean isEntityThread(Entity entity); /** - * Paper: Returns whether the current thread is ticking a region and that the region + * Folia & Paper: Returns whether the current thread is ticking a region and that the region * being ticked owns the chunk at the specified world and block position as included in the specified location *

* Bukkit: returns {@link org.bukkit.Server#isPrimaryThread} @@ -46,7 +47,7 @@ default boolean isTickThread() { /** * Schedules a task to be executed on the next tick
- * Paper: ...on the global region
+ * Folia & Paper: ...on the global region
* Bukkit: ...on the main thread * * @param runnable The task to execute @@ -55,7 +56,7 @@ default boolean isTickThread() { /** * Schedules a task to be executed after the specified delay in ticks
- * Paper: ...on the global region
+ * Folia & Paper: ...on the global region
* Bukkit: ...on the main thread * * @param runnable The task to execute @@ -65,7 +66,7 @@ default boolean isTickThread() { /** * Schedules a repeating task to be executed after the initial delay with the specified period
- * Paper: ...on the global region
+ * Folia & Paper: ...on the global region
* Bukkit: ...on the main thread * * @param runnable The task to execute @@ -75,7 +76,7 @@ default boolean isTickThread() { Task runTaskTimer(Runnable runnable, long delay, long period); /** - * Paper: Schedules a task to be executed on the region which owns the location on the next tick + * Folia & Paper: Schedules a task to be executed on the region which owns the location on the next tick *

* Bukkit: same as {@link #runTask(Runnable)} * @@ -87,7 +88,7 @@ default Task runTask(Location location, Runnable runnable) { } /** - * Paper: Schedules a task to be executed on the region which owns the location after the + * Folia & Paper: Schedules a task to be executed on the region which owns the location after the * specified delay in ticks *

* Bukkit: same as {@link #runTaskLater(Runnable, long)} @@ -101,7 +102,7 @@ default Task runTaskLater(Location location, Runnable runnable, long delay) { } /** - * Paper: Schedules a repeating task to be executed on the region which owns the location + * Folia & Paper: Schedules a repeating task to be executed on the region which owns the location * after the initial delay with the specified period *

* Bukkit: same as {@link #runTaskTimer(Runnable, long, long)} @@ -116,7 +117,7 @@ default Task runTaskTimer(Location location, Runnable runnable, long delay, long } /** - * Paper: Schedules a task to be executed on the region which owns the location + * Folia & Paper: Schedules a task to be executed on the region which owns the location * of given entity on the next tick *

* Bukkit: same as {@link #runTask(Runnable)} @@ -129,7 +130,7 @@ default Task runTask(Entity entity, Runnable runnable) { } /** - * Paper: Schedules a task to be executed on the region which owns the location + * Folia & Paper: Schedules a task to be executed on the region which owns the location * of given entity after the specified delay in ticks *

* Bukkit: same as {@link #runTaskLater(Runnable, long)} @@ -143,7 +144,7 @@ default Task runTaskLater(Entity entity, Runnable runnable, long delay) { } /** - * Paper: Schedules a repeating task to be executed on the region which owns the + * Folia & Paper: Schedules a repeating task to be executed on the region which owns the * location of given entity after the initial delay with the specified period *

* Bukkit: same as {@link #runTaskTimer(Runnable, long, long)} diff --git a/gradle.properties b/gradle.properties index 5ebc747..d60c988 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,4 +2,4 @@ org.gradle.parallel=true org.gradle.caching=true group=space.bxteam -version=1.2.1 +version=1.3.0 diff --git a/settings.gradle.kts b/settings.gradle.kts index 9f1fa50..ad05a9b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,5 +2,5 @@ rootProject.name = "commons" include(":commons-adventure") include(":commons-bukkit") -include(":commons-github") +include(":commons-folia") include(":commons-shared")