-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add folia and paper schedulers, move github module to shared
- Loading branch information
Showing
13 changed files
with
229 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
34 changes: 34 additions & 0 deletions
34
commons-folia/src/main/java/space/bxteam/commons/folia/scheduler/FoliaScheduledTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
commons-folia/src/main/java/space/bxteam/commons/folia/scheduler/FoliaScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
commons-folia/src/main/java/space/bxteam/commons/paper/scheduler/PaperScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ org.gradle.parallel=true | |
org.gradle.caching=true | ||
|
||
group=space.bxteam | ||
version=1.2.1 | ||
version=1.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters