Skip to content

Commit 248650c

Browse files
committed
feat: improve plugin scope extraction in SuspendCommandExecutors for better command handling
1 parent a52b658 commit 248650c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

surf-api-bukkit/surf-api-bukkit-api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/command/executors/SuspendCommandExecutors.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.github.shynixn.mccoroutine.folia.SuspendingPlugin
66
import com.github.shynixn.mccoroutine.folia.scope
77
import com.mojang.brigadier.exceptions.CommandSyntaxException
88
import dev.jorel.commandapi.BukkitExecutable
9+
import dev.jorel.commandapi.CommandAPI
910
import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException
1011
import dev.jorel.commandapi.executors.CommandArguments
1112
import dev.jorel.commandapi.kotlindsl.*
@@ -222,7 +223,21 @@ internal fun CommandSender.sendSyntaxMessageOrRethrow(message: String?, t: Throw
222223
@PublishedApi
223224
@InternalSurfApi
224225
internal inline fun Any.extractCallingPluginScopeOrThrow(): CoroutineScope {
225-
val plugin = JavaPlugin.getProvidingPlugin(javaClass)
226+
val commandApiPlugin = JavaPlugin.getProvidingPlugin(CommandAPI::class.java)
227+
val callerClass = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
228+
.walk { frames ->
229+
frames
230+
.map { it.declaringClass }
231+
.filter { declaringClass ->
232+
val plugin =
233+
runCatching { JavaPlugin.getProvidingPlugin(declaringClass) }.getOrNull()
234+
plugin != null && plugin != commandApiPlugin
235+
}
236+
.findFirst()
237+
.orElseThrow { IllegalStateException("No calling plugin frame found (non-CommandAPI).") }
238+
}
239+
240+
val plugin = JavaPlugin.getProvidingPlugin(callerClass)
226241
require(plugin is SuspendingPlugin) {
227242
"Failed to extract CoroutineScope: plugin '${plugin.name}' does not implement SuspendingPlugin. " +
228243
"Provide a CoroutineScope explicitly or use a SuspendingPlugin."

0 commit comments

Comments
 (0)