@@ -23,6 +23,8 @@ import org.bukkit.entity.Entity
2323import org.bukkit.entity.Player
2424import org.bukkit.plugin.java.JavaPlugin
2525
26+ typealias CoroutineScopeProvider = () -> CoroutineScope
27+
2628/* *
2729 * Registers an executor that can be run by any [CommandSender] and executes it inside a coroutine.
2830 *
@@ -33,10 +35,10 @@ import org.bukkit.plugin.java.JavaPlugin
3335 * @param executor The suspending executor logic.
3436 */
3537inline fun BukkitExecutable <* >.anyExecutorSuspend (
36- scope : CoroutineScope = extractCallingPluginScopeOrThrow(),
38+ noinline scope : CoroutineScopeProvider = extractCallingPluginScopeOrThrow(),
3739 crossinline executor : suspend CoroutineScope .(CommandSender , CommandArguments ) -> Unit
3840) = anyExecutor { sender, arguments ->
39- scope.launchCommandExecutor(sender) {
41+ scope() .launchCommandExecutor(sender) {
4042 executor(sender, arguments)
4143 }
4244}
@@ -48,10 +50,10 @@ inline fun BukkitExecutable<*>.anyExecutorSuspend(
4850 * @param executor The suspending executor logic.
4951 */
5052inline fun BukkitExecutable <* >.playerExecutorSuspend (
51- scope : CoroutineScope = extractCallingPluginScopeOrThrow(),
53+ noinline scope : CoroutineScopeProvider = extractCallingPluginScopeOrThrow(),
5254 crossinline executor : suspend CoroutineScope .(Player , CommandArguments ) -> Unit
5355) = playerExecutor { sender, arguments ->
54- scope.launchCommandExecutor(sender) {
56+ scope() .launchCommandExecutor(sender) {
5557 executor(sender, arguments)
5658 }
5759}
@@ -63,10 +65,10 @@ inline fun BukkitExecutable<*>.playerExecutorSuspend(
6365 * @param executor The suspending executor logic.
6466 */
6567inline fun BukkitExecutable <* >.entityExecutorSuspend (
66- scope : CoroutineScope = extractCallingPluginScopeOrThrow(),
68+ noinline scope : CoroutineScopeProvider = extractCallingPluginScopeOrThrow(),
6769 crossinline executor : suspend CoroutineScope .(Entity , CommandArguments ) -> Unit
6870) = entityExecutor { sender, arguments ->
69- scope.launchCommandExecutor(sender) {
71+ scope() .launchCommandExecutor(sender) {
7072 executor(sender, arguments)
7173 }
7274}
@@ -78,10 +80,10 @@ inline fun BukkitExecutable<*>.entityExecutorSuspend(
7880 * @param executor The suspending executor logic.
7981 */
8082inline fun BukkitExecutable <* >.consoleExecutorSuspend (
81- scope : CoroutineScope = extractCallingPluginScopeOrThrow(),
83+ noinline scope : CoroutineScopeProvider = extractCallingPluginScopeOrThrow(),
8284 crossinline executor : suspend CoroutineScope .(ConsoleCommandSender , CommandArguments ) -> Unit
8385) = consoleExecutor { sender, arguments ->
84- scope.launchCommandExecutor(sender) {
86+ scope() .launchCommandExecutor(sender) {
8587 executor(sender, arguments)
8688 }
8789}
@@ -93,10 +95,10 @@ inline fun BukkitExecutable<*>.consoleExecutorSuspend(
9395 * @param executor The suspending executor logic.
9496 */
9597inline fun BukkitExecutable <* >.commandBlockExecutorSuspend (
96- scope : CoroutineScope = extractCallingPluginScopeOrThrow(),
98+ noinline scope : CoroutineScopeProvider = extractCallingPluginScopeOrThrow(),
9799 crossinline executor : suspend CoroutineScope .(BlockCommandSender , CommandArguments ) -> Unit
98100) = commandBlockExecutor { sender, arguments ->
99- scope.launchCommandExecutor(sender) {
101+ scope() .launchCommandExecutor(sender) {
100102 executor(sender, arguments)
101103 }
102104}
@@ -108,10 +110,10 @@ inline fun BukkitExecutable<*>.commandBlockExecutorSuspend(
108110 * @param executor The suspending executor logic.
109111 */
110112inline fun BukkitExecutable <* >.proxyExecutorSuspend (
111- scope : CoroutineScope = extractCallingPluginScopeOrThrow(),
113+ noinline scope : CoroutineScopeProvider = extractCallingPluginScopeOrThrow(),
112114 crossinline executor : suspend CoroutineScope .(ProxiedCommandSender , CommandArguments ) -> Unit
113115) = proxyExecutor { sender, arguments ->
114- scope.launchCommandExecutor(sender) {
116+ scope() .launchCommandExecutor(sender) {
115117 executor(sender, arguments)
116118 }
117119}
@@ -123,10 +125,10 @@ inline fun BukkitExecutable<*>.proxyExecutorSuspend(
123125 * @param executor The suspending executor logic.
124126 */
125127inline fun BukkitExecutable <* >.nativeExecutorSuspend (
126- scope : CoroutineScope = extractCallingPluginScopeOrThrow(),
128+ noinline scope : CoroutineScopeProvider = extractCallingPluginScopeOrThrow(),
127129 crossinline executor : suspend CoroutineScope .(NativeProxyCommandSender , CommandArguments ) -> Unit
128130) = nativeExecutor { sender, arguments ->
129- scope.launchCommandExecutor(sender) {
131+ scope() .launchCommandExecutor(sender) {
130132 executor(sender, arguments)
131133 }
132134}
@@ -138,10 +140,10 @@ inline fun BukkitExecutable<*>.nativeExecutorSuspend(
138140 * @param executor The suspending executor logic.
139141 */
140142inline fun BukkitExecutable <* >.remoteConsoleExecutorSuspend (
141- scope : CoroutineScope = extractCallingPluginScopeOrThrow(),
143+ noinline scope : CoroutineScopeProvider = extractCallingPluginScopeOrThrow(),
142144 crossinline executor : suspend CoroutineScope .(RemoteConsoleCommandSender , CommandArguments ) -> Unit
143145) = remoteConsoleExecutor { sender, arguments ->
144- scope.launchCommandExecutor(sender) {
146+ scope() .launchCommandExecutor(sender) {
145147 executor(sender, arguments)
146148 }
147149}
@@ -222,7 +224,7 @@ internal fun CommandSender.sendSyntaxMessageOrRethrow(message: String?, t: Throw
222224@Suppress(" NOTHING_TO_INLINE" )
223225@PublishedApi
224226@InternalSurfApi
225- internal inline fun Any.extractCallingPluginScopeOrThrow (): CoroutineScope {
227+ internal inline fun Any.extractCallingPluginScopeOrThrow (): CoroutineScopeProvider {
226228 val commandApiPlugin = JavaPlugin .getProvidingPlugin(CommandAPI ::class .java)
227229 val callerClass = StackWalker .getInstance(StackWalker .Option .RETAIN_CLASS_REFERENCE )
228230 .walk { frames ->
@@ -243,5 +245,5 @@ internal inline fun Any.extractCallingPluginScopeOrThrow(): CoroutineScope {
243245 " Provide a CoroutineScope explicitly or use a SuspendingPlugin."
244246 }
245247
246- return plugin.scope
248+ return { plugin.scope }
247249}
0 commit comments