Skip to content

Commit

Permalink
Impl command dsl for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZoddiak committed Sep 27, 2023
1 parent 23b15cf commit 4395d54
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/src/main/kotlin/me/zodd/CommandManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CommandManager : DslArgument, DslContext {

operator fun invoke(initializer: CommandManager.() -> Unit): List<DslCommand> {
this.initializer()
RegistrationHelper.registerCommand(CommandBuilder.builtCommands)
return CommandBuilder.builtCommands
}

Expand Down
8 changes: 8 additions & 0 deletions api/src/main/kotlin/me/zodd/RegistrationHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.zodd
import io.leangen.geantyref.TypeToken
import org.spongepowered.api.Sponge
import org.spongepowered.api.command.Command
import org.spongepowered.api.command.Command.Parameterized
import org.spongepowered.api.event.Event
import org.spongepowered.api.event.EventListenerRegistration
import org.spongepowered.api.event.Order
Expand All @@ -24,6 +25,13 @@ object RegistrationHelper {
inline fun <reified T : Event> registerListener(noinline executor: T.() -> Unit) =
registerListener(typeToken<T>(), executor)

fun registerCommand(command: List<DslCommand>) {
val regToken = typeToken<RegisterCommandEvent<Parameterized>>()
registerListener(regToken) {
it.register(API.container, command)
}
}

//Allows easy creation of typeToken
inline fun <reified T : Event> typeToken() = object : TypeToken<T>() {}

Expand Down
3 changes: 3 additions & 0 deletions host/run/config/scripting-host/scripts/helloworld.plugin.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import me.zodd.*
import net.kyori.adventure.identity.Identity
import net.kyori.adventure.text.Component

//Scripts automatically execute during plugin construction
Logger.info("Hello World!")
Expand All @@ -8,6 +10,7 @@ ScriptCommandManager {
aliases += "baz"
permission = "plugin.command.$it"
executes {
this.sendMessage(Identity.nil(), Component.text("Hello World!"))
success()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import me.zodd.onServerStarting
*/

//Scripts can do multiple things
//noinspection UnresolvedReference
onServerStarted {
Logger.info("Multi-Event-Server-Started")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//org.invalid is not on classpath so script will error
//Despite this error other scripts will continue to load
import me.zodd.Logger
import me.zodd.onServerStarted
import org.invalid

onServerStarted {
Expand Down
1 change: 1 addition & 0 deletions script-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ tasks.jar {
}

tasks.shadowJar {
archiveClassifier.set("")
configurations.add(project.configurations.shadow.get())
}

0 comments on commit 4395d54

Please sign in to comment.