Skip to content

Commit b11eb47

Browse files
committed
fix: improve Folia 1.21.11 compatibility
1 parent 0b0f7ec commit b11eb47

5 files changed

Lines changed: 92 additions & 16 deletions

File tree

build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ tasks.withType<JavaCompile> {
5050
options.encoding = "UTF-8"
5151
}
5252

53+
tasks.processResources {
54+
filesMatching(listOf("plugin.yml", "paper-plugin.yml")) {
55+
expand("version" to project.version)
56+
}
57+
}
58+
5359
tasks.withType<DokkaTask>().configureEach {
5460
moduleName.set("MatrixLib API")
5561
dokkaSourceSets.configureEach {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=com.y54895.matrixlib
2-
version=1.5.0
2+
version=1.5.1
33
kotlin.incremental=true
44
kotlin.incremental.java=true
55
kotlin.caching.enabled=true

src/main/kotlin/com/y54895/matrixlib/MatrixLib.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ object MatrixLib : Plugin() {
6060
}
6161

6262
override fun onDisable() {
63+
MatrixPluginUpdates.shutdown()
6364
MatrixConsoleVisuals.renderShutdown(
6465
branding = branding,
6566
details = listOf(

src/main/kotlin/com/y54895/matrixlib/api/compat/CompatApi.kt

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.bukkit.material.Hopper as LegacyHopper
1515
import org.bukkit.plugin.Plugin
1616
import java.lang.reflect.Method
1717
import java.util.Locale
18+
import java.util.concurrent.CompletableFuture
1819
import java.util.concurrent.ConcurrentHashMap
1920
import java.util.function.Consumer
2021

@@ -52,6 +53,10 @@ object ServerCompat {
5253
val implementationClass = server.javaClass.name
5354
val serverName = runCatching { server.name }.getOrElse { Bukkit.getName() }
5455
val versionText = Bukkit.getVersion()
56+
val hasFoliaSchedulers =
57+
serverHasMethod(server, "getGlobalRegionScheduler") &&
58+
serverHasMethod(server, "getRegionScheduler") &&
59+
serverHasMethod(server, "getAsyncScheduler")
5560
val composite = listOf(serverName, versionText, implementationClass)
5661
.joinToString(" ")
5762
.lowercase(Locale.ROOT)
@@ -61,7 +66,9 @@ object ServerCompat {
6166
}
6267

6368
when {
64-
classExists("io.papermc.paper.threadedregions.RegionizedServer") || composite.contains("folia") -> {
69+
classExists("io.papermc.paper.threadedregions.RegionizedServer") ||
70+
composite.contains("folia") ||
71+
hasFoliaSchedulers -> {
6572
ServerRuntime(ServerFamily.FOLIA, "Folia", serverName, versionText, implementationClass)
6673
}
6774
classExists("io.papermc.paper.configuration.Configuration") || composite.contains("paper") ||
@@ -120,6 +127,12 @@ object ServerCompat {
120127
Class.forName(className)
121128
}.isSuccess
122129
}
130+
131+
private fun serverHasMethod(server: Any, name: String): Boolean {
132+
return runCatching {
133+
server.javaClass.getMethod(name)
134+
}.isSuccess
135+
}
123136
}
124137

125138
object BukkitCompat {
@@ -441,11 +454,11 @@ object BukkitCompat {
441454
object SchedulerBridge {
442455

443456
fun runLater(plugin: Plugin, delayTicks: Long, task: Runnable) {
444-
Bukkit.getScheduler().runTaskLater(plugin, task, delayTicks)
457+
FoliaUtil.runLater(plugin, delayTicks, task)
445458
}
446459

447460
fun runAsync(plugin: Plugin, task: Runnable) {
448-
Bukkit.getScheduler().runTaskAsynchronously(plugin, task)
461+
FoliaUtil.runAsync(plugin, task)
449462
}
450463

451464
fun runPlayerTaskLater(plugin: Plugin, player: Player, delayTicks: Long, task: Runnable) {
@@ -512,6 +525,22 @@ object FoliaUtil {
512525
}
513526
}
514527

528+
private val asyncScheduler by lazy {
529+
if (!isFolia) {
530+
null
531+
} else {
532+
runCatching {
533+
Bukkit.getServer().javaClass.getMethod("getAsyncScheduler").invoke(Bukkit.getServer())
534+
}.getOrNull()
535+
}
536+
}
537+
538+
private val asyncRunNowMethod by lazy {
539+
asyncScheduler?.javaClass?.methods?.firstOrNull {
540+
it.name == "runNow" && it.parameterCount == 2
541+
}
542+
}
543+
515544
private val regionScheduler by lazy {
516545
if (!isFolia) {
517546
null
@@ -589,6 +618,32 @@ object FoliaUtil {
589618
return TaskHandle { task.cancel() }
590619
}
591620

621+
fun runAsync(plugin: Plugin, action: Runnable): TaskHandle {
622+
if (!isFolia) {
623+
val task = Bukkit.getScheduler().runTaskAsynchronously(plugin, action)
624+
return TaskHandle { task.cancel() }
625+
}
626+
627+
var scheduledTask: Any? = null
628+
val consumer = Consumer<Any?> { task ->
629+
if (scheduledTask == null && task != null) {
630+
scheduledTask = task
631+
}
632+
action.run()
633+
}
634+
635+
val returnedTask = invokeAsync(asyncRunNowMethod, plugin, consumer)
636+
if (returnedTask != null) {
637+
scheduledTask = returnedTask
638+
}
639+
if (scheduledTask != null) {
640+
return TaskHandle { cancelScheduledTask(scheduledTask) }
641+
}
642+
643+
val future = CompletableFuture.runAsync(action)
644+
return TaskHandle { future.cancel(true) }
645+
}
646+
592647
fun runAtLocation(plugin: Plugin, location: Location, action: Runnable) {
593648
if (!isFolia) {
594649
action.run()
@@ -653,6 +708,14 @@ object FoliaUtil {
653708
}.getOrNull()
654709
}
655710

711+
private fun invokeAsync(method: Method?, vararg args: Any?): Any? {
712+
val scheduler = asyncScheduler ?: return null
713+
val targetMethod = method ?: return null
714+
return runCatching {
715+
targetMethod.invoke(scheduler, *args)
716+
}.getOrNull()
717+
}
718+
656719
private fun cancelScheduledTask(task: Any?) {
657720
val scheduledTask = task ?: return
658721
val cancelMethod = scheduledTaskCancelMethods.computeIfAbsent(scheduledTask.javaClass) { taskClass ->

src/main/kotlin/com/y54895/matrixlib/api/update/MatrixPluginUpdates.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ object MatrixPluginUpdates {
117117
private var bootstrapped = false
118118

119119
@Volatile
120-
private var repeatingTaskId = -1
120+
private var repeatingTask: FoliaUtil.TaskHandle? = null
121121

122122
lateinit var settings: MatrixUpdateSettings
123123
private set
@@ -263,6 +263,15 @@ object MatrixPluginUpdates {
263263
return releaseNotesPreview(candidate.release.body)
264264
}
265265

266+
fun shutdown() {
267+
repeatingTask?.cancel()
268+
repeatingTask = null
269+
registrations.clear()
270+
candidates.clear()
271+
notifiedOps.clear()
272+
bootstrapped = false
273+
}
274+
266275
private fun checkAsync(managed: MatrixManagedPlugin, sender: CommandSender? = null) {
267276
if (!settings.enabled) {
268277
return
@@ -442,21 +451,18 @@ object MatrixPluginUpdates {
442451
if (settings.checkIntervalMinutes <= 0L) {
443452
return
444453
}
445-
val periodTicks = settings.checkIntervalMinutes * 60L * 20L
446-
repeatingTaskId = FoliaUtil.runRepeating(
454+
val periodMinutes = settings.checkIntervalMinutes
455+
repeatingTask?.cancel()
456+
repeatingTask = FoliaUtil.runRepeating(
447457
BukkitPlugin.getInstance(),
448-
periodTicks,
449-
periodTicks,
458+
periodMinutes * 60L * 20L,
459+
periodMinutes * 60L * 20L,
450460
Runnable { checkAllAsync() }
451-
).hashCode()
461+
)
452462
}
453463

454464
private fun runAsync(block: () -> Unit) {
455-
if (FoliaUtil.isFolia) {
456-
FoliaUtil.runLater(BukkitPlugin.getInstance(), 1L, Runnable(block))
457-
} else {
458-
Bukkit.getScheduler().runTaskAsynchronously(BukkitPlugin.getInstance(), Runnable(block))
459-
}
465+
FoliaUtil.runAsync(BukkitPlugin.getInstance(), Runnable(block))
460466
}
461467

462468
private fun loadSettings(): MatrixUpdateSettings {
@@ -549,4 +555,4 @@ private fun CommandSender.sendUpdaterMessage(text: String) {
549555

550556
fun CommandSender.sendMatrixUpdaterMessage(text: String) {
551557
sendUpdaterMessage(text)
552-
}
558+
}

0 commit comments

Comments
 (0)