Skip to content

Commit

Permalink
add folia and paper schedulers, move github module to shared
Browse files Browse the repository at this point in the history
  • Loading branch information
NONPLAYT committed Dec 5, 2024
1 parent e97e102 commit bbfaf7c
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 24 deletions.
16 changes: 16 additions & 0 deletions commons-folia/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
11 changes: 0 additions & 11 deletions commons-github/build.gradle.kts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public interface Scheduler {
/**
* <b>Folia</b>: Returns whether the current thread is ticking the global region <br>
* <b>Paper & Bukkit</b>: Returns {@link org.bukkit.Server#isPrimaryThread}
*/
boolean isGlobalThread();
Expand All @@ -23,7 +24,7 @@ default boolean isTickThread() {
}

/**
* <b>Paper</b>: Returns whether the current thread is ticking a region and that the region
* <b>Folia & Paper</b>: 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
Expand All @@ -35,7 +36,7 @@ default boolean isTickThread() {
boolean isEntityThread(Entity entity);

/**
* <b>Paper</b>: Returns whether the current thread is ticking a region and that the region
* <b>Folia & Paper</b>: 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
* <p>
* <b>Bukkit</b>: returns {@link org.bukkit.Server#isPrimaryThread}
Expand All @@ -46,7 +47,7 @@ default boolean isTickThread() {

/**
* Schedules a task to be executed on the next tick <br>
* <b>Paper</b>: ...on the global region <br>
* <b>Folia & Paper</b>: ...on the global region <br>
* <b>Bukkit</b>: ...on the main thread
*
* @param runnable The task to execute
Expand All @@ -55,7 +56,7 @@ default boolean isTickThread() {

/**
* Schedules a task to be executed after the specified delay in ticks <br>
* <b>Paper</b>: ...on the global region <br>
* <b>Folia & Paper</b>: ...on the global region <br>
* <b>Bukkit</b>: ...on the main thread
*
* @param runnable The task to execute
Expand All @@ -65,7 +66,7 @@ default boolean isTickThread() {

/**
* Schedules a repeating task to be executed after the initial delay with the specified period <br>
* <b>Paper</b>: ...on the global region <br>
* <b>Folia & Paper</b>: ...on the global region <br>
* <b>Bukkit</b>: ...on the main thread
*
* @param runnable The task to execute
Expand All @@ -75,7 +76,7 @@ default boolean isTickThread() {
Task runTaskTimer(Runnable runnable, long delay, long period);

/**
* <b>Paper</b>: Schedules a task to be executed on the region which owns the location on the next tick
* <b>Folia & Paper</b>: Schedules a task to be executed on the region which owns the location on the next tick
* <p>
* <b>Bukkit</b>: same as {@link #runTask(Runnable)}
*
Expand All @@ -87,7 +88,7 @@ default Task runTask(Location location, Runnable runnable) {
}

/**
* <b>Paper</b>: Schedules a task to be executed on the region which owns the location after the
* <b>Folia & Paper</b>: Schedules a task to be executed on the region which owns the location after the
* specified delay in ticks
* <p>
* <b>Bukkit</b>: same as {@link #runTaskLater(Runnable, long)}
Expand All @@ -101,7 +102,7 @@ default Task runTaskLater(Location location, Runnable runnable, long delay) {
}

/**
* <b>Paper</b>: Schedules a repeating task to be executed on the region which owns the location
* <b>Folia & Paper</b>: Schedules a repeating task to be executed on the region which owns the location
* after the initial delay with the specified period
* <p>
* <b>Bukkit</b>: same as {@link #runTaskTimer(Runnable, long, long)}
Expand All @@ -116,7 +117,7 @@ default Task runTaskTimer(Location location, Runnable runnable, long delay, long
}

/**
* <b>Paper</b>: Schedules a task to be executed on the region which owns the location
* <b>Folia & Paper</b>: Schedules a task to be executed on the region which owns the location
* of given entity on the next tick
* <p>
* <b>Bukkit</b>: same as {@link #runTask(Runnable)}
Expand All @@ -129,7 +130,7 @@ default Task runTask(Entity entity, Runnable runnable) {
}

/**
* <b>Paper</b>: Schedules a task to be executed on the region which owns the location
* <b>Folia & Paper</b>: Schedules a task to be executed on the region which owns the location
* of given entity after the specified delay in ticks
* <p>
* <b>Bukkit</b>: same as {@link #runTaskLater(Runnable, long)}
Expand All @@ -143,7 +144,7 @@ default Task runTaskLater(Entity entity, Runnable runnable, long delay) {
}

/**
* <b>Paper</b>: Schedules a repeating task to be executed on the region which owns the
* <b>Folia & Paper</b>: 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
* <p>
* <b>Bukkit</b>: same as {@link #runTaskTimer(Runnable, long, long)}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ org.gradle.parallel=true
org.gradle.caching=true

group=space.bxteam
version=1.2.1
version=1.3.0
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ rootProject.name = "commons"

include(":commons-adventure")
include(":commons-bukkit")
include(":commons-github")
include(":commons-folia")
include(":commons-shared")

0 comments on commit bbfaf7c

Please sign in to comment.