Skip to content

Commit cb49a51

Browse files
committed
Use Plugin instead of JavaPlugin
Refactor code to use the `Plugin` interface instead of `JavaPlugin` class for better flexibility and broader compatibility. Additionally, update dependency versions and adjust tests accordingly to ensure consistency with the new changes.
1 parent 8d41868 commit cb49a51

File tree

8 files changed

+35
-41
lines changed

8 files changed

+35
-41
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ kotlin-test-junit5 = { group = "org.jetbrains.kotlin", name = "kotlin-test-junit
1111
kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
1212
kotlinx-coroutines-reactive = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-reactive", version.ref = "kotlinx-coroutines" }
1313
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
14-
mockbukkit = { group = "com.github.seeseemelk", name = "MockBukkit-v1.21", version = "3.124.0" }
14+
mockbukkit = { group = "com.github.seeseemelk", name = "MockBukkit-v1.21", version = "3.127.1" }
1515
reflections = { group = "org.reflections", name = "reflections", version = "0.10.2" }
1616
guice = { group = "com.google.inject", name = "guice", version = "7.0.0" }
1717
kotest = { group = "io.kotest", name = "kotest-assertions-core-jvm", version = "5.9.1" }

src/main/kotlin/dev/newspicel/coroutine/CoroutineJavaPluginExt.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import kotlinx.coroutines.Job
77
import kotlinx.coroutines.isActive
88
import kotlinx.coroutines.launch
99
import dev.newspicel.utils.pluginLazy
10+
import org.bukkit.plugin.Plugin
1011
import org.bukkit.plugin.java.JavaPlugin
1112
import kotlin.coroutines.CoroutineContext
1213
import kotlin.coroutines.resume
1314
import kotlin.coroutines.suspendCoroutine
1415

15-
fun JavaPlugin.launch(
16+
fun Plugin.launch(
1617
context: CoroutineContext = syncDispatcher,
1718
start: CoroutineStart = CoroutineStart.DEFAULT,
1819
block: suspend CoroutineScope.() -> Unit,
@@ -24,16 +25,16 @@ fun JavaPlugin.launch(
2425
return coroutineScope.launch(context, start, block)
2526
}
2627

27-
suspend fun JavaPlugin.launchAndAwait(
28+
suspend fun Plugin.launchAndAwait(
2829
context: CoroutineContext = syncDispatcher,
2930
start: CoroutineStart = CoroutineStart.DEFAULT,
3031
block: suspend CoroutineScope.() -> Unit,
3132
) {
3233
launch(context, start, block).join()
3334
}
3435

35-
val JavaPlugin.asyncDispatcher: CoroutineDispatcher by pluginLazy { MinecraftAsyncCoroutineDispatcher(this) }
36+
val Plugin.asyncDispatcher: CoroutineDispatcher by pluginLazy { MinecraftAsyncCoroutineDispatcher(this) }
3637

37-
val JavaPlugin.syncDispatcher: CoroutineDispatcher by pluginLazy { MinecraftCoroutineDispatcher(this) }
38+
val Plugin.syncDispatcher: CoroutineDispatcher by pluginLazy { MinecraftCoroutineDispatcher(this) }
3839

39-
val JavaPlugin.coroutineScope: CoroutineScope by pluginLazy { CoroutineScopeJavaPluginHelper.createScope(this, this.syncDispatcher) }
40+
val Plugin.coroutineScope: CoroutineScope by pluginLazy { CoroutineScopeJavaPluginHelper.createScope(this, this.syncDispatcher) }

src/main/kotlin/dev/newspicel/coroutine/CoroutineScopeJavaPluginHelper.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
package dev.newspicel.coroutine
22

3-
import kotlinx.coroutines.CoroutineDispatcher
4-
import kotlinx.coroutines.CoroutineExceptionHandler
5-
import kotlinx.coroutines.CoroutineScope
6-
import kotlinx.coroutines.SupervisorJob
7-
import kotlinx.coroutines.cancel
8-
import kotlinx.coroutines.cancelChildren
9-
import kotlinx.coroutines.plus
3+
import kotlinx.coroutines.*
104
import org.bukkit.event.EventHandler
115
import org.bukkit.event.Listener
126
import org.bukkit.event.server.PluginDisableEvent
13-
import org.bukkit.plugin.java.JavaPlugin
7+
import org.bukkit.plugin.Plugin
148
import java.util.logging.Level
159

1610
internal object CoroutineScopeJavaPluginHelper {
1711

18-
fun createScope(plugin: JavaPlugin, syncDispatcher: CoroutineDispatcher): CoroutineScope {
12+
fun createScope(plugin: Plugin, syncDispatcher: CoroutineDispatcher): CoroutineScope {
1913
val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
2014
plugin.logger.log(Level.SEVERE, "Caught exception in coroutine context $coroutineContext", throwable)
2115
}

src/main/kotlin/dev/newspicel/coroutine/MinecraftAsyncCoroutineDispatcher.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package dev.newspicel.coroutine
33
import kotlinx.coroutines.CoroutineDispatcher
44
import kotlinx.coroutines.Runnable
55
import kotlinx.coroutines.runBlocking
6-
import org.bukkit.plugin.java.JavaPlugin
6+
import org.bukkit.plugin.Plugin
77
import kotlin.coroutines.CoroutineContext
88

99
class MinecraftAsyncCoroutineDispatcher(
10-
private val plugin: JavaPlugin,
10+
private val plugin: Plugin,
1111
) : CoroutineDispatcher() {
1212

1313
override fun isDispatchNeeded(context: CoroutineContext): Boolean {

src/main/kotlin/dev/newspicel/coroutine/MinecraftCoroutineDispatcher.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package dev.newspicel.coroutine
22

33
import kotlinx.coroutines.CoroutineDispatcher
44
import kotlinx.coroutines.runBlocking
5-
import org.bukkit.plugin.java.JavaPlugin
5+
import org.bukkit.plugin.Plugin
66
import kotlin.coroutines.CoroutineContext
77

88
class MinecraftCoroutineDispatcher(
9-
private val plugin: JavaPlugin,
9+
private val plugin: Plugin,
1010
) : CoroutineDispatcher() {
1111

1212
override fun isDispatchNeeded(context: CoroutineContext): Boolean {

src/main/kotlin/dev/newspicel/scheduler/SchedulerExt.kt

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@ package dev.newspicel.scheduler
44

55
import dev.newspicel.coroutine.asyncDispatcher
66
import dev.newspicel.coroutine.launch
7-
import org.bukkit.plugin.java.JavaPlugin
8-
import org.bukkit.scheduler.BukkitTask
7+
import org.bukkit.plugin.Plugin
98

10-
private fun suspendToRunnable(plugin: JavaPlugin, block: suspend () -> Unit) = Runnable { plugin.launch { block() } }
11-
private fun suspendToAsynchronouslyRunnable(plugin: JavaPlugin, block: suspend () -> Unit) = Runnable { plugin.launch(plugin.asyncDispatcher) { block() } }
9+
private fun suspendToRunnable(plugin: Plugin, block: suspend () -> Unit) = Runnable { plugin.launch { block() } }
10+
private fun suspendToAsynchronouslyRunnable(plugin: Plugin, block: suspend () -> Unit) = Runnable { plugin.launch(plugin.asyncDispatcher) { block() } }
1211

13-
fun runTask(plugin: JavaPlugin, block: () -> Unit) = plugin.server.scheduler.runTask(plugin, block)
12+
fun runTask(plugin: Plugin, block: () -> Unit) = plugin.server.scheduler.runTask(plugin, block)
1413

15-
fun runSuspendTask(plugin: JavaPlugin, block: suspend () -> Unit) = plugin.launch { block() }
14+
fun runSuspendTask(plugin: Plugin, block: suspend () -> Unit) = plugin.launch { block() }
1615

17-
fun runTaskAsynchronously(plugin: JavaPlugin, block: () -> Unit) = plugin.server.scheduler.runTaskAsynchronously(plugin, block)
16+
fun runTaskAsynchronously(plugin: Plugin, block: () -> Unit) = plugin.server.scheduler.runTaskAsynchronously(plugin, block)
1817

19-
fun runSuspendTaskAsynchronously(plugin: JavaPlugin, block: suspend () -> Unit) = plugin.launch(context = plugin.asyncDispatcher) { block() }
18+
fun runSuspendTaskAsynchronously(plugin: Plugin, block: suspend () -> Unit) = plugin.launch(context = plugin.asyncDispatcher) { block() }
2019

21-
fun runTaskLater(plugin: JavaPlugin, delay: Long, block: () -> Unit) = plugin.server.scheduler.runTaskLater(plugin, block, delay)
20+
fun runTaskLater(plugin: Plugin, delay: Long, block: () -> Unit) = plugin.server.scheduler.runTaskLater(plugin, block, delay)
2221

23-
fun runSuspendTaskLater(plugin: JavaPlugin, delay: Long, block: suspend () -> Unit) = plugin.server.scheduler.runTaskLater(plugin, suspendToRunnable(plugin, block), delay - 1L)
22+
fun runSuspendTaskLater(plugin: Plugin, delay: Long, block: suspend () -> Unit) = plugin.server.scheduler.runTaskLater(plugin, suspendToRunnable(plugin, block), delay - 1L)
2423

25-
fun runTaskLaterAsynchronously(plugin: JavaPlugin, delay: Long, block: () -> Unit) = plugin.server.scheduler.runTaskLaterAsynchronously(plugin, block, delay)
24+
fun runTaskLaterAsynchronously(plugin: Plugin, delay: Long, block: () -> Unit) = plugin.server.scheduler.runTaskLaterAsynchronously(plugin, block, delay)
2625

27-
fun runSuspendTaskLaterAsynchronously(plugin: JavaPlugin, delay: Long, block: suspend () -> Unit) = plugin.server.scheduler.runTaskLaterAsynchronously(plugin, suspendToAsynchronouslyRunnable(plugin, block), delay - 1L)
26+
fun runSuspendTaskLaterAsynchronously(plugin: Plugin, delay: Long, block: suspend () -> Unit) = plugin.server.scheduler.runTaskLaterAsynchronously(plugin, suspendToAsynchronouslyRunnable(plugin, block), delay - 1L)
2827

29-
fun runTaskTimer(plugin: JavaPlugin, delay: Long, period: Long, block: () -> Unit) = plugin.server.scheduler.runTaskTimer(plugin, block, delay, period)
28+
fun runTaskTimer(plugin: Plugin, delay: Long, period: Long, block: () -> Unit) = plugin.server.scheduler.runTaskTimer(plugin, block, delay, period)
3029

31-
fun runSuspendTaskTimer(plugin: JavaPlugin, delay: Long, period: Long, block: suspend () -> Unit) = plugin.server.scheduler.runTaskTimer(plugin, suspendToRunnable(plugin, block), delay, period - 1L)
30+
fun runSuspendTaskTimer(plugin: Plugin, delay: Long, period: Long, block: suspend () -> Unit) = plugin.server.scheduler.runTaskTimer(plugin, suspendToRunnable(plugin, block), delay, period - 1L)
3231

33-
fun runTaskTimerAsynchronously(plugin: JavaPlugin, delay: Long, period: Long, block: () -> Unit) = plugin.server.scheduler.runTaskTimerAsynchronously(plugin, block, delay, period)
32+
fun runTaskTimerAsynchronously(plugin: Plugin, delay: Long, period: Long, block: () -> Unit) = plugin.server.scheduler.runTaskTimerAsynchronously(plugin, block, delay, period)
3433

35-
fun runSuspendTaskTimerAsynchronously(plugin: JavaPlugin, delay: Long, period: Long, block: suspend () -> Unit) = plugin.server.scheduler.runTaskTimerAsynchronously(plugin, suspendToAsynchronouslyRunnable(plugin, block), delay - 1L, period)
34+
fun runSuspendTaskTimerAsynchronously(plugin: Plugin, delay: Long, period: Long, block: suspend () -> Unit) = plugin.server.scheduler.runTaskTimerAsynchronously(plugin, suspendToAsynchronouslyRunnable(plugin, block), delay - 1L, period)

src/main/kotlin/dev/newspicel/utils/ContextLazy.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package dev.newspicel.utils
22

3-
import org.bukkit.plugin.java.JavaPlugin
3+
import org.bukkit.plugin.Plugin
44
import kotlin.properties.ReadOnlyProperty
55
import kotlin.reflect.KProperty
6+
67
fun <P, T> contextLazy(initializer: P.() -> T): ReadOnlyProperty<P, T> = UnsafeContextLazy(initializer)
78

8-
fun <T> pluginLazy(initializer: JavaPlugin.() -> T): ReadOnlyProperty<JavaPlugin, T> = UnsafeContextLazy(initializer)
9+
fun <T> pluginLazy(initializer: Plugin.() -> T): ReadOnlyProperty<Plugin, T> = UnsafeContextLazy(initializer)
910

1011
private class UnsafeContextLazy<P, out T>(private val initializer: P.() -> T) : ReadOnlyProperty<P, T> {
1112
private var value: T? = null

src/test/kotlin/dev/newspicel/di/commands/CommandTests.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ package dev.newspicel.di.commands
22

33
import be.seeseemelk.mockbukkit.MockBukkit
44
import be.seeseemelk.mockbukkit.ServerMock
5+
import dev.newspicel.di.TestPlugin
6+
import io.kotest.matchers.ints.shouldBeInRange
57
import io.kotest.matchers.shouldBe
68
import kotlinx.coroutines.delay
79
import kotlinx.coroutines.runBlocking
8-
import dev.newspicel.di.TestPlugin
9-
import io.kotest.matchers.ints.shouldBeInRange
1010
import org.junit.jupiter.api.AfterEach
1111
import org.junit.jupiter.api.BeforeEach
1212
import org.junit.jupiter.api.Test
1313
import org.junit.jupiter.api.TestInstance
14-
import java.lang.NullPointerException
1514

1615
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
1716
open class CommandTests {
@@ -58,7 +57,7 @@ open class CommandTests {
5857
server.executePlayer("test2").assertSucceeded()
5958
}
6059
delay(4000)
61-
ctc?.i?.shouldBeInRange(19..20)
60+
ctc?.i?.shouldBeInRange(18..20)
6261
}
6362

6463
@Test

0 commit comments

Comments
 (0)