Skip to content

Commit

Permalink
Update command dsl
Browse files Browse the repository at this point in the history
bump ver
  • Loading branch information
DrZoddiak committed Sep 27, 2023
1 parent 4395d54 commit f094479
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 67 deletions.
7 changes: 4 additions & 3 deletions api/src/main/kotlin/me/zodd/Managers.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package me.zodd

import me.zodd.dsl.command.CommandManager
import org.apache.logging.log4j.Logger
import org.spongepowered.api.Server
import org.spongepowered.api.Sponge
import org.spongepowered.api.command.manager.CommandManager
import org.spongepowered.api.event.EventManager
import org.spongepowered.api.plugin.PluginManager
import org.spongepowered.api.scheduler.Scheduler
import org.spongepowered.api.service.ServiceProvider
import org.spongepowered.plugin.PluginContainer
import org.spongepowered.api.command.manager.CommandManager as SpongeCommandManager

inline val SpongeServer: Server get() = Sponge.server()
inline val SpongePluginManager: PluginManager get() = Sponge.pluginManager()
inline val SpongeCommandManager: CommandManager get() = Sponge.server().commandManager()
inline val SpongeCommandManager: SpongeCommandManager get() = Sponge.server().commandManager()
inline val SpongeEventManager: EventManager get() = Sponge.eventManager()
inline val SpongeServerServiceManager: ServiceProvider.ServerScoped get() = Sponge.server().serviceProvider()
inline val SpongeGameServiceManager: ServiceProvider.GameScoped get() = Sponge.serviceProvider()
inline val Scheduler: Scheduler get() = Sponge.server().scheduler()
inline val SpongeAsyncScheduler: Scheduler get() = Sponge.asyncScheduler()
inline val Container: PluginContainer get() = API.container
inline val Logger: Logger get() = API.logger
inline val ScriptCommandManager: me.zodd.CommandManager get() = CommandManager()
inline val ScriptCommandManager: CommandManager get() = CommandManager
1 change: 1 addition & 0 deletions api/src/main/kotlin/me/zodd/RegistrationHelper.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.zodd

import io.leangen.geantyref.TypeToken
import me.zodd.dsl.command.DslCommand
import org.spongepowered.api.Sponge
import org.spongepowered.api.command.Command
import org.spongepowered.api.command.Command.Parameterized
Expand Down
3 changes: 3 additions & 0 deletions api/src/main/kotlin/me/zodd/dsl/command/CommandArgument.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package me.zodd.dsl.command

interface CommandArgument : CommandParameter, CommandFlag
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
package me.zodd
package me.zodd.dsl.command

import me.zodd.annotations.ScriptDsl
import net.kyori.adventure.text.Component
import org.spongepowered.api.command.Command
import org.spongepowered.api.command.CommandCause
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.parameter.CommandContext
import org.spongepowered.api.command.parameter.Parameter
import org.spongepowered.api.command.parameter.managed.Flag
import org.spongepowered.api.command.parameter.CommandContext as SpongeContext
import java.util.function.Predicate

@ScriptDsl
class CommandManager : DslArgument, DslContext {

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

fun command(name: String, initializer: CommandBuilder.(name: String) -> Unit): DslCommand {
val builder = CommandBuilder()
builder.aliases += name
builder.initializer(name)
return builder.buildCommand()
}
}

@ScriptDsl
class CommandBuilder : DslArgument, DslContext {
class CommandBuilder : CommandArgument, CommandContext {

companion object {
internal val builtCommands = mutableListOf<DslCommand>()
Expand All @@ -40,7 +23,7 @@ class CommandBuilder : DslArgument, DslContext {
var terminal: Boolean = false
var executionRequirement: Predicate<CommandCause> = Predicate { true }

private var commandExecutor: CommandContext.() -> CommandResult =
private var commandExecutor: SpongeContext.() -> CommandResult =
{ error(Component.text("Command executor not registered")) }

private var parameters = mutableListOf<Parameter>()
Expand All @@ -64,7 +47,7 @@ class CommandBuilder : DslArgument, DslContext {
return command
}

fun executes(exec: CommandContext.() -> CommandResult) {
fun executes(exec: SpongeContext.() -> CommandResult) {
commandExecutor = exec
}

Expand All @@ -88,45 +71,4 @@ class CommandBuilder : DslArgument, DslContext {

return command
}
}


sealed interface DslContext {
infix fun <T> CommandContext.requireOne(param: Parameter.Value<T>): T = this.requireOne(param)

fun CommandContext.success(): CommandResult = CommandResult.success()

infix fun CommandContext.error(errorMessage: Component): CommandResult = CommandResult.error(errorMessage)

infix fun CommandContext.hasFlag(flag: Flag) = this.hasFlag(flag)
}

sealed interface DslFlag {
infix fun String.buildFlag(permission: String): Flag {
return asFlag(permission).build()
}

infix fun String.asFlag(permission: String): Flag.Builder {
return Flag.builder().aliases(this.split(",")).setPermission(permission)
}
}

sealed interface DslParameter {
infix fun <T> String.withType(type: Parameter.Value.Builder<T>): Parameter.Value<T> {
return type.key(this).build()
}

infix fun <T> Parameter.Value.Builder<T>.keyedWith(key: String): Parameter.Value<T> {
return this.key(key).build()
}
}

interface DslArgument : DslParameter, DslFlag

data class DslCommand(
val aliases: List<String>,
val command: Command.Parameterized,
) {
val baseAlias = aliases[0]
val remainingAliases = aliases.filterNot { it.contentEquals(baseAlias) }.toTypedArray()
}
17 changes: 17 additions & 0 deletions api/src/main/kotlin/me/zodd/dsl/command/CommandContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.zodd.dsl.command

import net.kyori.adventure.text.Component
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.parameter.Parameter
import org.spongepowered.api.command.parameter.managed.Flag
import org.spongepowered.api.command.parameter.CommandContext as SpongeContext

sealed interface CommandContext {
infix fun <T> SpongeContext.requireOne(param: Parameter.Value<T>): T = this.requireOne(param)

fun SpongeContext.success(): CommandResult = CommandResult.success()

infix fun SpongeContext.error(errorMessage: Component): CommandResult = CommandResult.error(errorMessage)

infix fun SpongeContext.hasFlag(flag: Flag) = this.hasFlag(flag)
}
13 changes: 13 additions & 0 deletions api/src/main/kotlin/me/zodd/dsl/command/CommandFlag.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.zodd.dsl.command

import org.spongepowered.api.command.parameter.managed.Flag

sealed interface CommandFlag {
infix fun String.buildFlag(permission: String): Flag {
return asFlag(permission).build()
}

infix fun String.asFlag(permission: String): Flag.Builder {
return Flag.builder().aliases(this.split(",")).setPermission(permission)
}
}
21 changes: 21 additions & 0 deletions api/src/main/kotlin/me/zodd/dsl/command/CommandManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.zodd.dsl.command

import me.zodd.RegistrationHelper
import me.zodd.annotations.ScriptDsl

@ScriptDsl
object CommandManager : CommandArgument, CommandContext {

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

fun command(name: String, initializer: CommandBuilder.(name: String) -> Unit): DslCommand {
val builder = CommandBuilder()
builder.aliases += name
builder.initializer(name)
return builder.buildCommand()
}
}
13 changes: 13 additions & 0 deletions api/src/main/kotlin/me/zodd/dsl/command/CommandParameter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.zodd.dsl.command

import org.spongepowered.api.command.parameter.Parameter

sealed interface CommandParameter {
infix fun <T> String.withType(type: Parameter.Value.Builder<T>): Parameter.Value<T> {
return type.key(this).build()
}

infix fun <T> Parameter.Value.Builder<T>.keyedWith(key: String): Parameter.Value<T> {
return this.key(key).build()
}
}
11 changes: 11 additions & 0 deletions api/src/main/kotlin/me/zodd/dsl/command/DslCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.zodd.dsl.command

import org.spongepowered.api.command.Command

data class DslCommand(
val aliases: List<String>,
val command: Command.Parameterized,
) {
val baseAlias = aliases[0]
val remainingAliases = aliases.filterNot { it.contentEquals(baseAlias) }.toTypedArray()
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/common.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "me.zodd"
version = "0.1.0"
version = "0.1.1"

repositories {
mavenCentral()
Expand Down

0 comments on commit f094479

Please sign in to comment.