Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Folia support with new API versions #352

Open
wants to merge 5 commits into
base: folia
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
dependencies {
implementation(project(":eco-api"))
implementation(project(path = ":eco-core:core-plugin", configuration = "shadow"))
implementation(project(":eco-core:core-folia"))
implementation(project(":eco-core:core-proxy"))
implementation(project(":eco-core:core-backend"))
implementation(project(path = ":eco-core:core-nms:v1_17_R1", configuration = "reobf"))
Expand Down Expand Up @@ -81,7 +82,7 @@ allprojects {
maven("https://repo.md-5.net/content/groups/public/")

// UltraEconomy
maven("https://repo.techscode.com/repository/maven-releases/")
maven("https://repo.techscode.com/repository/techscode-apis/")

// PlayerPoints
maven("https://repo.rosewooddev.io/repository/public/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class Prerequisite {
* Requires the server to be running an implementation of Folia.
*/
public static final Prerequisite HAS_FOLIA = new Prerequisite(
() -> ClassUtils.exists("io.papermc.paper.threadedregions.scheduler.RegionisedScheduler"),
() -> ClassUtils.exists("io.papermc.paper.threadedregions.scheduler.RegionScheduler"),
"Requires server to be running Folia!"
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.willfp.eco.core.scheduling;

import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

public interface ScheduledTask {
@NotNull Plugin getOwningPlugin();

boolean isRepeatingTask();

@NotNull CancelledState cancel();

@NotNull ExecutionState getExecutionState();

default boolean isCancelled() {
ExecutionState state = this.getExecutionState();
return state == ScheduledTask.ExecutionState.CANCELLED || state == ScheduledTask.ExecutionState.CANCELLED_RUNNING;
}

public static enum ExecutionState {
IDLE,
RUNNING,
FINISHED,
CANCELLED,
CANCELLED_RUNNING;
}

public static enum CancelledState {
CANCELLED_BY_CALLER,
CANCELLED_ALREADY,
RUNNING,
ALREADY_EXECUTED,
NEXT_RUNS_CANCELLED,
NEXT_RUNS_CANCELLED_ALREADY;
}
}
58 changes: 12 additions & 46 deletions eco-api/src/main/java/com/willfp/eco/core/scheduling/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface Scheduler {
* @deprecated Does not work with Folia.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default BukkitTask runLater(@NotNull Runnable runnable,
default ScheduledTask runLater(@NotNull Runnable runnable,
long ticksLater) {
return runLater(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), (int) ticksLater, runnable);
}
Expand All @@ -35,7 +35,7 @@ default BukkitTask runLater(@NotNull Runnable runnable,
* @deprecated Does not work with Folia.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default BukkitTask runLater(long ticksLater,
default ScheduledTask runLater(long ticksLater,
@NotNull Runnable runnable) {
return runLater(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), (int) ticksLater, runnable);
}
Expand All @@ -50,7 +50,7 @@ default BukkitTask runLater(long ticksLater,
* @deprecated Does not work with Folia.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default BukkitTask runTimer(@NotNull Runnable runnable,
default ScheduledTask runTimer(@NotNull Runnable runnable,
long delay,
long repeat) {
return runTimer(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), (int) delay, (int) repeat, runnable);
Expand All @@ -68,7 +68,7 @@ default BukkitTask runTimer(@NotNull Runnable runnable,
* @deprecated Does not work with Folia.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default BukkitTask runTimer(long delay,
default ScheduledTask runTimer(long delay,
long repeat,
@NotNull Runnable runnable) {
return runTimer(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), (int) delay, (int) repeat, runnable);
Expand All @@ -84,7 +84,7 @@ default BukkitTask runTimer(long delay,
* @deprecated Does not work with Folia.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default BukkitTask runAsyncTimer(@NotNull Runnable runnable,
default ScheduledTask runAsyncTimer(@NotNull Runnable runnable,
long delay,
long repeat) {
return runTimerAsync((int) delay, (int) repeat, runnable);
Expand All @@ -102,7 +102,7 @@ default BukkitTask runAsyncTimer(@NotNull Runnable runnable,
* @deprecated Does not work with Folia.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default BukkitTask runAsyncTimer(long delay,
default ScheduledTask runAsyncTimer(long delay,
long repeat,
@NotNull Runnable runnable) {
return runTimerAsync((int) delay, (int) repeat, runnable);
Expand All @@ -116,44 +116,10 @@ default BukkitTask runAsyncTimer(long delay,
* @deprecated Does not work with Folia.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default BukkitTask run(@NotNull Runnable runnable) {
default ScheduledTask run(@NotNull Runnable runnable) {
return run(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), runnable);
}

/**
* Schedule the task to be run repeatedly on a timer.
*
* @param runnable The lambda to run.
* @param delay The amount of ticks to wait before the first execution.
* @param repeat The amount of ticks to wait between executions.
* @return The id of the task.
* @deprecated Not needed.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default int syncRepeating(@NotNull Runnable runnable,
long delay,
long repeat) {
return runTimer(runnable, delay, repeat).getTaskId();
}

/**
* Schedule the task to be ran repeatedly on a timer.
* <p>
* Reordered for better kotlin interop.
*
* @param runnable The lambda to run.
* @param delay The amount of ticks to wait before the first execution.
* @param repeat The amount of ticks to wait between executions.
* @return The id of the task.
* @deprecated Not needed.
*/
@Deprecated(since = "6.53.0", forRemoval = true)
default int syncRepeating(long delay,
long repeat,
@NotNull Runnable runnable) {
return runTimer(runnable, delay, repeat).getTaskId();
}

/**
* Cancel all running tasks from the linked {@link EcoPlugin}.
*/
Expand All @@ -165,7 +131,7 @@ default int syncRepeating(long delay,
* @param task The lambda to run.
* @return The created {@link BukkitTask}.
*/
BukkitTask runAsync(@NotNull Runnable task);
ScheduledTask runAsync(@NotNull Runnable task);

/**
* Run a task.
Expand All @@ -174,7 +140,7 @@ default int syncRepeating(long delay,
* @param task The task.
* @return The created {@link BukkitTask}.
*/
BukkitTask run(@NotNull Location location,
ScheduledTask run(@NotNull Location location,
@NotNull Runnable task);

/**
Expand All @@ -185,7 +151,7 @@ BukkitTask run(@NotNull Location location,
* @param task The task.
* @return The created {@link BukkitTask}.
*/
BukkitTask runLater(@NotNull Location location,
ScheduledTask runLater(@NotNull Location location,
int ticksLater,
@NotNull Runnable task);

Expand All @@ -198,7 +164,7 @@ BukkitTask runLater(@NotNull Location location,
* @param task The task.
* @return The created {@link BukkitTask}.
*/
BukkitTask runTimer(@NotNull Location location,
ScheduledTask runTimer(@NotNull Location location,
int delay,
int repeat,
@NotNull Runnable task);
Expand All @@ -211,7 +177,7 @@ BukkitTask runTimer(@NotNull Location location,
* @param task The task.
* @return The created {@link BukkitTask}.
*/
BukkitTask runTimerAsync(int delay,
ScheduledTask runTimerAsync(int delay,
int repeat,
@NotNull Runnable task);
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
package com.willfp.eco.internal.scheduling

import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.scheduling.ScheduledTask
import com.willfp.eco.core.scheduling.Scheduler
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitTask

class EcoSchedulerSpigot(private val plugin: EcoPlugin) : Scheduler {
override fun runLater(location: Location, ticksLater: Int, task: Runnable): BukkitTask {
return Bukkit.getScheduler().runTaskLater(plugin, task, ticksLater.toLong())
override fun runLater(location: Location, ticksLater: Int, task: Runnable): ScheduledTask {
return TaskAdapter(Bukkit.getScheduler().runTaskLater(plugin, task, ticksLater.toLong()))
}

override fun runTimer(location: Location, delay: Int, repeat: Int, task: Runnable): BukkitTask {
return Bukkit.getScheduler().runTaskTimer(plugin, task, delay.toLong(), repeat.toLong())
override fun runTimer(location: Location, delay: Int, repeat: Int, task: Runnable): ScheduledTask {
return TaskAdapter(Bukkit.getScheduler().runTaskTimer(plugin, task, delay.toLong(), repeat.toLong()), true)
}

override fun run(location: Location, task: Runnable): BukkitTask {
return Bukkit.getScheduler().runTask(plugin, task)
override fun run(location: Location, task: Runnable): ScheduledTask {
return TaskAdapter(Bukkit.getScheduler().runTask(plugin, task))
}

override fun runAsync(task: Runnable): BukkitTask {
return Bukkit.getScheduler().runTaskAsynchronously(plugin, task)
override fun runAsync(task: Runnable): ScheduledTask {
return TaskAdapter(Bukkit.getScheduler().runTaskAsynchronously(plugin, task))
}

override fun runTimerAsync(delay: Int, repeat: Int, task: Runnable): BukkitTask {
return Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, task, delay.toLong(), repeat.toLong())
override fun runTimerAsync(delay: Int, repeat: Int, task: Runnable): ScheduledTask {
return TaskAdapter(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, task, delay.toLong(), repeat.toLong()), true)
}

override fun cancelAll() {
Bukkit.getScheduler().cancelTasks(plugin)
}

// Should we monitor the status of the task?
class TaskAdapter(
private val task: BukkitTask,
private val repeatable: Boolean = false
) : ScheduledTask {
override fun getOwningPlugin(): Plugin = task.owner

override fun isRepeatingTask(): Boolean = repeatable

override fun cancel(): ScheduledTask.CancelledState = when (task.cancel().run { task.isCancelled }) {
true -> ScheduledTask.CancelledState.CANCELLED_ALREADY
false -> ScheduledTask.CancelledState.RUNNING
}

override fun getExecutionState(): ScheduledTask.ExecutionState = when (task.isCancelled) {
true -> ScheduledTask.ExecutionState.CANCELLED
false -> ScheduledTask.ExecutionState.RUNNING
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,85 @@ package com.willfp.eco.internal.scheduling

import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.scheduling.Scheduler
import io.papermc.paper.threadedregions.scheduler.ScheduledTask
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.scheduler.BukkitTask
import org.bukkit.plugin.Plugin
import java.util.concurrent.TimeUnit

class EcoSchedulerFolia(private val plugin: EcoPlugin) : Scheduler {
override fun runLater(runnable: Runnable, ticksLater: Long): BukkitTask {
Bukkit.getGlobalRegionScheduler().runDelayed(plugin, { runnable.run() }, ticksLater)
override fun runLater(runnable: Runnable, ticksLater: Long): com.willfp.eco.core.scheduling.ScheduledTask {
return TaskAdapter(Bukkit.getGlobalRegionScheduler().runDelayed(plugin, { runnable.run() }, ticksLater))
}

override fun runLater(location: Location, ticksLater: Int, task: Runnable): BukkitTask {
Bukkit.getRegionScheduler().runDelayed(plugin, location, { task.run() }, ticksLater.toLong())
override fun runLater(
location: Location,
ticksLater: Int,
task: Runnable
): com.willfp.eco.core.scheduling.ScheduledTask {
return TaskAdapter(Bukkit.getRegionScheduler().runDelayed(plugin, location, { task.run() }, ticksLater.toLong()))
}

override fun runTimer(delay: Long, repeat: Long, runnable: Runnable): BukkitTask {
Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, { runnable.run() }, delay, repeat)
override fun runTimer(delay: Long, repeat: Long, runnable: Runnable): com.willfp.eco.core.scheduling.ScheduledTask {
return TaskAdapter(Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, { runnable.run() }, delay, repeat))
}

override fun runTimer(location: Location, delay: Int, repeat: Int, task: Runnable): BukkitTask {
Bukkit.getRegionScheduler().runAtFixedRate(plugin, location, { task.run() }, delay.toLong(), repeat.toLong())
override fun runTimer(
location: Location,
delay: Int,
repeat: Int,
task: Runnable
): com.willfp.eco.core.scheduling.ScheduledTask {
return TaskAdapter(Bukkit.getRegionScheduler().runAtFixedRate(plugin, location, { task.run() }, delay.toLong(), repeat.toLong()))
}

override fun run(runnable: Runnable): BukkitTask {
Bukkit.getGlobalRegionScheduler().run(plugin) { runnable.run() }
override fun run(runnable: Runnable): com.willfp.eco.core.scheduling.ScheduledTask {
return TaskAdapter(Bukkit.getGlobalRegionScheduler().run(plugin) { runnable.run() })
}

override fun run(location: Location, task: Runnable): BukkitTask {
Bukkit.getRegionScheduler().run(plugin, location) { task.run() }
override fun run(location: Location, task: Runnable): com.willfp.eco.core.scheduling.ScheduledTask {
return TaskAdapter(Bukkit.getRegionScheduler().run(plugin, location) { task.run() })
}

override fun runAsync(task: Runnable): BukkitTask {
Bukkit.getAsyncScheduler().runNow(plugin) { task.run() }
override fun runAsync(task: Runnable): com.willfp.eco.core.scheduling.ScheduledTask {
return TaskAdapter(Bukkit.getAsyncScheduler().runNow(plugin) { task.run() })
}

override fun runTimerAsync(delay: Int, repeat: Int, task: Runnable): BukkitTask {
Bukkit.getAsyncScheduler()
override fun runTimerAsync(delay: Int, repeat: Int, task: Runnable): com.willfp.eco.core.scheduling.ScheduledTask {
return TaskAdapter(Bukkit.getAsyncScheduler()
.runAtFixedRate(plugin, { task.run() }, delay * 50L, repeat * 50L, TimeUnit.MILLISECONDS)
)
}

override fun cancelAll() {
Bukkit.getScheduler().cancelTasks(plugin)
Bukkit.getAsyncScheduler().cancelTasks(plugin)
Bukkit.getGlobalRegionScheduler().cancelTasks(plugin)
}

class TaskAdapter(
private val task: ScheduledTask
) : com.willfp.eco.core.scheduling.ScheduledTask {
override fun getOwningPlugin(): Plugin = task.owningPlugin

override fun isRepeatingTask(): Boolean = task.isRepeatingTask

override fun cancel(): com.willfp.eco.core.scheduling.ScheduledTask.CancelledState = when (task.cancel()) {
ScheduledTask.CancelledState.CANCELLED_BY_CALLER -> com.willfp.eco.core.scheduling.ScheduledTask.CancelledState.CANCELLED_BY_CALLER
ScheduledTask.CancelledState.CANCELLED_ALREADY -> com.willfp.eco.core.scheduling.ScheduledTask.CancelledState.CANCELLED_ALREADY
ScheduledTask.CancelledState.RUNNING -> com.willfp.eco.core.scheduling.ScheduledTask.CancelledState.RUNNING
ScheduledTask.CancelledState.ALREADY_EXECUTED -> com.willfp.eco.core.scheduling.ScheduledTask.CancelledState.ALREADY_EXECUTED
ScheduledTask.CancelledState.NEXT_RUNS_CANCELLED -> com.willfp.eco.core.scheduling.ScheduledTask.CancelledState.NEXT_RUNS_CANCELLED
ScheduledTask.CancelledState.NEXT_RUNS_CANCELLED_ALREADY -> com.willfp.eco.core.scheduling.ScheduledTask.CancelledState.NEXT_RUNS_CANCELLED_ALREADY
}

override fun getExecutionState(): com.willfp.eco.core.scheduling.ScheduledTask.ExecutionState =
when (task.executionState) {
ScheduledTask.ExecutionState.IDLE -> com.willfp.eco.core.scheduling.ScheduledTask.ExecutionState.IDLE
ScheduledTask.ExecutionState.RUNNING -> com.willfp.eco.core.scheduling.ScheduledTask.ExecutionState.RUNNING
ScheduledTask.ExecutionState.FINISHED -> com.willfp.eco.core.scheduling.ScheduledTask.ExecutionState.FINISHED
ScheduledTask.ExecutionState.CANCELLED -> com.willfp.eco.core.scheduling.ScheduledTask.ExecutionState.CANCELLED
ScheduledTask.ExecutionState.CANCELLED_RUNNING -> com.willfp.eco.core.scheduling.ScheduledTask.ExecutionState.CANCELLED_RUNNING
}

}
}
Loading