From 393d0031c74e183343e445cd051efd9cba9eee7c Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 12 Mar 2023 12:11:12 +0000 Subject: [PATCH] Added folia support --- .../com/willfp/eco/core/Prerequisite.java | 14 +- .../willfp/eco/core/scheduling/Scheduler.java | 128 ++++++++++++++---- .../internal/factory/EcoRunnableFactory.kt | 2 +- .../eco/internal/scheduling/EcoScheduler.kt | 51 ------- .../internal/scheduling/EcoSchedulerSpigot.kt | 33 +++++ eco-core/core-folia/build.gradle | 7 + .../internal/scheduling/EcoSchedulerFolia.kt | 30 ++++ eco-core/core-plugin/build.gradle | 3 +- .../com/willfp/eco/internal/spigot/EcoImpl.kt | 6 +- settings.gradle.kts | 3 +- 10 files changed, 193 insertions(+), 84 deletions(-) delete mode 100644 eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoScheduler.kt create mode 100644 eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoSchedulerSpigot.kt create mode 100644 eco-core/core-folia/build.gradle create mode 100644 eco-core/core-folia/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoSchedulerFolia.kt diff --git a/eco-api/src/main/java/com/willfp/eco/core/Prerequisite.java b/eco-api/src/main/java/com/willfp/eco/core/Prerequisite.java index eeee7984..57709b87 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/Prerequisite.java +++ b/eco-api/src/main/java/com/willfp/eco/core/Prerequisite.java @@ -27,7 +27,7 @@ public class Prerequisite { */ public static final Prerequisite HAS_PAPER = new Prerequisite( () -> ClassUtils.exists("com.destroystokyo.paper.event.block.BeaconEffectEvent"), - "Requires server to be running paper (or a fork)" + "Requires server to be running paper" ); /** @@ -84,7 +84,7 @@ public class Prerequisite { @Deprecated(since = "6.49.0", forRemoval = true) public static final Prerequisite HAS_BUNGEECORD = new Prerequisite( () -> ClassUtils.exists("net.md_5.bungee.api.event.ServerConnectedEvent"), - "Requires server to be running BungeeCord (or a fork)" + "Requires server to be running BungeeCord" ); /** @@ -95,7 +95,15 @@ public class Prerequisite { @Deprecated(since = "6.49.0", forRemoval = true) public static final Prerequisite HAS_VELOCITY = new Prerequisite( () -> ClassUtils.exists("com.velocitypowered.api.event.player.ServerConnectedEvent"), - "Requires server to be running Velocity (or a fork)" + "Requires server to be running Velocity" + ); + + /** + * 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"), + "Requires server to be running Folia!" ); /** diff --git a/eco-api/src/main/java/com/willfp/eco/core/scheduling/Scheduler.java b/eco-api/src/main/java/com/willfp/eco/core/scheduling/Scheduler.java index 7adced35..8eef562b 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/scheduling/Scheduler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/scheduling/Scheduler.java @@ -1,6 +1,8 @@ package com.willfp.eco.core.scheduling; import com.willfp.eco.core.EcoPlugin; +import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; @@ -14,9 +16,13 @@ public interface Scheduler { * @param runnable The lambda to run. * @param ticksLater The amount of ticks to wait before execution. * @return The created {@link BukkitTask}. + * @deprecated Does not work with Folia. */ - BukkitTask runLater(@NotNull Runnable runnable, - long ticksLater); + @Deprecated(since = "6.53.0", forRemoval = true) + default BukkitTask runLater(@NotNull Runnable runnable, + long ticksLater) { + return runLater(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), (int) ticksLater, runnable); + } /** * Run the task after a specified tick delay. @@ -26,10 +32,12 @@ public interface Scheduler { * @param runnable The lambda to run. * @param ticksLater The amount of ticks to wait before execution. * @return The created {@link BukkitTask}. + * @deprecated Does not work with Folia. */ + @Deprecated(since = "6.53.0", forRemoval = true) default BukkitTask runLater(long ticksLater, @NotNull Runnable runnable) { - return runLater(runnable, ticksLater); + return runLater(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), (int) ticksLater, runnable); } /** @@ -39,10 +47,14 @@ public interface Scheduler { * @param delay The amount of ticks to wait before the first execution. * @param repeat The amount of ticks to wait between executions. * @return The created {@link BukkitTask}. + * @deprecated Does not work with Folia. */ - BukkitTask runTimer(@NotNull Runnable runnable, - long delay, - long repeat); + @Deprecated(since = "6.53.0", forRemoval = true) + default BukkitTask runTimer(@NotNull Runnable runnable, + long delay, + long repeat) { + return runTimer(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), (int) delay, (int) repeat, runnable); + } /** * Run the task repeatedly on a timer. @@ -53,11 +65,13 @@ public interface Scheduler { * @param delay The amount of ticks to wait before the first execution. * @param repeat The amount of ticks to wait between executions. * @return The created {@link BukkitTask}. + * @deprecated Does not work with Folia. */ + @Deprecated(since = "6.53.0", forRemoval = true) default BukkitTask runTimer(long delay, long repeat, @NotNull Runnable runnable) { - return runTimer(runnable, delay, repeat); + return runTimer(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), (int) delay, (int) repeat, runnable); } /** @@ -67,10 +81,14 @@ public interface Scheduler { * @param delay The amount of ticks to wait before the first execution. * @param repeat The amount of ticks to wait between executions. * @return The created {@link BukkitTask}. + * @deprecated Does not work with Folia. */ - BukkitTask runAsyncTimer(@NotNull Runnable runnable, - long delay, - long repeat); + @Deprecated(since = "6.53.0", forRemoval = true) + default BukkitTask runAsyncTimer(@NotNull Runnable runnable, + long delay, + long repeat) { + return runTimerAsync((int) delay, (int) repeat, runnable); + } /** * Run the task repeatedly and asynchronously on a timer. @@ -81,11 +99,13 @@ public interface Scheduler { * @param delay The amount of ticks to wait before the first execution. * @param repeat The amount of ticks to wait between executions. * @return The created {@link BukkitTask}. + * @deprecated Does not work with Folia. */ + @Deprecated(since = "6.53.0", forRemoval = true) default BukkitTask runAsyncTimer(long delay, long repeat, @NotNull Runnable runnable) { - return runAsyncTimer(runnable, delay, repeat); + return runTimerAsync((int) delay, (int) repeat, runnable); } /** @@ -93,28 +113,28 @@ public interface Scheduler { * * @param runnable The lambda to run. * @return The created {@link BukkitTask}. + * @deprecated Does not work with Folia. */ - BukkitTask run(@NotNull Runnable runnable); + @Deprecated(since = "6.53.0", forRemoval = true) + default BukkitTask run(@NotNull Runnable runnable) { + return run(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), runnable); + } /** - * Run the task asynchronously. - * - * @param runnable The lambda to run. - * @return The created {@link BukkitTask}. - */ - BukkitTask runAsync(@NotNull Runnable runnable); - - /** - * Schedule the task to be ran repeatedly on a timer. + * 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. */ - int syncRepeating(@NotNull Runnable runnable, - long delay, - long repeat); + @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. @@ -125,15 +145,73 @@ public interface Scheduler { * @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 syncRepeating(runnable, delay, repeat); + return runTimer(runnable, delay, repeat).getTaskId(); } /** * Cancel all running tasks from the linked {@link EcoPlugin}. */ void cancelAll(); + + /** + * Run a task asynchronously. + * + * @param task The lambda to run. + * @return The created {@link BukkitTask}. + */ + BukkitTask runAsync(@NotNull Runnable task); + + /** + * Run a task. + * + * @param location The location. + * @param task The task. + * @return The created {@link BukkitTask}. + */ + BukkitTask run(@NotNull Location location, + @NotNull Runnable task); + + /** + * Run a task after a delay. + * + * @param location The location. + * @param ticksLater The delay. + * @param task The task. + * @return The created {@link BukkitTask}. + */ + BukkitTask runLater(@NotNull Location location, + int ticksLater, + @NotNull Runnable task); + + /** + * Run a task on a timer. + * + * @param location The location. + * @param delay The delay. + * @param repeat The repeat delay. + * @param task The task. + * @return The created {@link BukkitTask}. + */ + BukkitTask runTimer(@NotNull Location location, + int delay, + int repeat, + @NotNull Runnable task); + + /** + * Run a task asynchronously on a timer. + * + * @param delay The delay. + * @param repeat The repeat delay. + * @param task The task. + * @return The created {@link BukkitTask}. + */ + BukkitTask runTimerAsync(int delay, + int repeat, + @NotNull Runnable task); } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/factory/EcoRunnableFactory.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/factory/EcoRunnableFactory.kt index 508ab91d..81d167fa 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/factory/EcoRunnableFactory.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/factory/EcoRunnableFactory.kt @@ -14,4 +14,4 @@ class EcoRunnableFactory(private val plugin: EcoPlugin) : RunnableFactory { } } } -} \ No newline at end of file +} diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoScheduler.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoScheduler.kt deleted file mode 100644 index a50168d9..00000000 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoScheduler.kt +++ /dev/null @@ -1,51 +0,0 @@ -package com.willfp.eco.internal.scheduling - -import com.willfp.eco.core.EcoPlugin -import com.willfp.eco.core.scheduling.Scheduler -import org.bukkit.Bukkit -import org.bukkit.scheduler.BukkitTask - -class EcoScheduler(private val plugin: EcoPlugin) : Scheduler { - override fun runLater( - runnable: Runnable, - ticksLater: Long - ): BukkitTask { - return Bukkit.getScheduler().runTaskLater(plugin, runnable, ticksLater) - } - - override fun runTimer( - runnable: Runnable, - delay: Long, - repeat: Long - ): BukkitTask { - return Bukkit.getScheduler().runTaskTimer(plugin, runnable, delay, repeat) - } - - override fun runAsyncTimer( - runnable: Runnable, - delay: Long, - repeat: Long - ): BukkitTask { - return Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, repeat) - } - - override fun run(runnable: Runnable): BukkitTask { - return Bukkit.getScheduler().runTask(plugin, runnable) - } - - override fun runAsync(runnable: Runnable): BukkitTask { - return Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable) - } - - override fun syncRepeating( - runnable: Runnable, - delay: Long, - repeat: Long - ): Int { - return Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, runnable, delay, repeat) - } - - override fun cancelAll() { - Bukkit.getScheduler().cancelTasks(plugin) - } -} \ No newline at end of file diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoSchedulerSpigot.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoSchedulerSpigot.kt new file mode 100644 index 00000000..d0c5cc9c --- /dev/null +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoSchedulerSpigot.kt @@ -0,0 +1,33 @@ +package com.willfp.eco.internal.scheduling + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.scheduling.Scheduler +import org.bukkit.Bukkit +import org.bukkit.Location +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 runTimer(location: Location, delay: Int, repeat: Int, task: Runnable): BukkitTask { + return Bukkit.getScheduler().runTaskTimer(plugin, task, delay.toLong(), repeat.toLong()) + } + + override fun run(location: Location, task: Runnable): BukkitTask { + return Bukkit.getScheduler().runTask(plugin, task) + } + + override fun runAsync(task: Runnable): BukkitTask { + return 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 cancelAll() { + Bukkit.getScheduler().cancelTasks(plugin) + } +} \ No newline at end of file diff --git a/eco-core/core-folia/build.gradle b/eco-core/core-folia/build.gradle new file mode 100644 index 00000000..6b48922c --- /dev/null +++ b/eco-core/core-folia/build.gradle @@ -0,0 +1,7 @@ +group 'com.willfp' +version rootProject.version + +dependencies { + //compileOnly 'dev.folia:folia-api:1.19.3-R0.1-SNAPSHOT' + compileOnly 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT' +} diff --git a/eco-core/core-folia/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoSchedulerFolia.kt b/eco-core/core-folia/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoSchedulerFolia.kt new file mode 100644 index 00000000..8d60c3ad --- /dev/null +++ b/eco-core/core-folia/src/main/kotlin/com/willfp/eco/internal/scheduling/EcoSchedulerFolia.kt @@ -0,0 +1,30 @@ +package com.willfp.eco.internal.scheduling + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.scheduling.Scheduler + +class EcoSchedulerFolia(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 runTimer(location: Location, delay: Int, repeat: Int, task: Runnable): BukkitTask { + return Bukkit.getScheduler().runTaskTimer(plugin, task, delay.toLong(), repeat.toLong()) + } + + override fun run(location: Location, task: Runnable): BukkitTask { + return Bukkit.getScheduler().runTask(plugin, task) + } + + override fun runAsync(task: Runnable): BukkitTask { + return 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 cancelAll() { + Bukkit.getScheduler().cancelTasks(plugin) + } +} diff --git a/eco-core/core-plugin/build.gradle b/eco-core/core-plugin/build.gradle index 87971af1..9f1d8246 100644 --- a/eco-core/core-plugin/build.gradle +++ b/eco-core/core-plugin/build.gradle @@ -4,10 +4,11 @@ version rootProject.version dependencies { compileOnly project(":eco-core:core-proxy") compileOnly project(":eco-core:core-backend") + compileOnly project(":eco-core:core-folia") // Libraries implementation 'com.github.WillFP:Crunch:1.1.3' - implementation 'mysql:mysql-connector-java:8.0.25' + implementation 'mysql:mysql-connector-java:8.0.28' implementation 'org.jetbrains.exposed:exposed-core:0.37.3' implementation 'org.jetbrains.exposed:exposed-dao:0.37.3' implementation 'org.jetbrains.exposed:exposed-jdbc:0.37.3' diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt index e0070e2c..fd8e1ae5 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt @@ -4,6 +4,7 @@ import com.willfp.eco.core.Eco import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.PluginLike import com.willfp.eco.core.PluginProps +import com.willfp.eco.core.Prerequisite import com.willfp.eco.core.command.CommandBase import com.willfp.eco.core.command.PluginCommandBase import com.willfp.eco.core.config.ConfigType @@ -36,7 +37,8 @@ import com.willfp.eco.internal.gui.slot.EcoSlotBuilder import com.willfp.eco.internal.integrations.PAPIExpansion import com.willfp.eco.internal.logging.EcoLogger import com.willfp.eco.internal.proxy.EcoProxyFactory -import com.willfp.eco.internal.scheduling.EcoScheduler +import com.willfp.eco.internal.scheduling.EcoSchedulerFolia +import com.willfp.eco.internal.scheduling.EcoSchedulerSpigot import com.willfp.eco.internal.spigot.data.DataYml import com.willfp.eco.internal.spigot.data.KeyRegistry import com.willfp.eco.internal.spigot.data.ProfileHandler @@ -87,7 +89,7 @@ class EcoImpl : EcoSpigotPlugin(), Eco { SafeInternalNamespacedKeyFactory() else FastInternalNamespacedKeyFactory() override fun createScheduler(plugin: EcoPlugin) = - EcoScheduler(plugin) + if (Prerequisite.HAS_FOLIA.isMet) EcoSchedulerFolia(plugin) else EcoSchedulerSpigot(plugin) override fun createEventManager(plugin: EcoPlugin) = EcoEventManager(plugin) diff --git a/settings.gradle.kts b/settings.gradle.kts index 3ae0770e..0b81ead5 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -19,4 +19,5 @@ include(":eco-core:core-nms:v1_19_R1") include(":eco-core:core-nms:v1_19_R2") include(":eco-core:core-proxy") include(":eco-core:core-plugin") -include(":eco-core:core-backend") \ No newline at end of file +include(":eco-core:core-backend") +include(":eco-core:core-folia")