Skip to content

Commit

Permalink
feat: add even rougher implementation of CommandContext
Browse files Browse the repository at this point in the history
  • Loading branch information
StillLutto committed Dec 2, 2024
1 parent cc5fca4 commit d868b18
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
import org.jetbrains.annotations.ApiStatus

@Suppress("UNCHECKED_CAST")
abstract class AbstractStellarCommand<T>(val name: String, var description: String = "", var usage: String = "") : SubCommandHandler() {

override fun getBase(): AbstractStellarCommand<*> = this

val aliases: MutableList<String> = mutableListOf()
val failureMessages: MutableList<Component> = mutableListOf()
val globalFailureMessages: MutableList<Component> = mutableListOf()
val failureExecutions: MutableList<CustomStellarExecution<*, Any>> = mutableListOf()
var hideDefaultFailureMessages: HideDefaultFailureMessages = HideDefaultFailureMessages(false, false)
private val _requirements: MutableList<StellarRequirement<*>> = mutableListOf()
@ApiStatus.Internal val aliases: MutableList<String> = mutableListOf()
@ApiStatus.Internal val failureMessages: MutableList<Component> = mutableListOf()
@ApiStatus.Internal val globalFailureMessages: MutableList<Component> = mutableListOf()
@ApiStatus.Internal val failureExecutions: MutableList<CustomStellarExecution<*, Any>> = mutableListOf()
@ApiStatus.Internal var hideDefaultFailureMessages: HideDefaultFailureMessages = HideDefaultFailureMessages(false, false)
@ApiStatus.Internal private val _requirements: MutableList<StellarRequirement<*>> = mutableListOf()
val requirements: List<StellarRequirement<*>>
get() {
if (this is CustomSubCommand<*>)
Expand All @@ -33,9 +34,9 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
)
return _requirements
}
val permissionRequirements: MutableList<PermissionStellarRequirement> = mutableListOf()
val executions: MutableList<StellarExecution<*>> = mutableListOf()
val runnables: MutableList<StellarRunnable<*>> = mutableListOf()
@ApiStatus.Internal val permissionRequirements: MutableList<PermissionStellarRequirement> = mutableListOf()
@ApiStatus.Internal val executions: MutableList<StellarExecution<*>> = mutableListOf()
@ApiStatus.Internal val runnables: MutableList<StellarRunnable<*>> = mutableListOf()

fun addAlias(name: String): T {
aliases.add(name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.undefined.stellar.data.arguments
package com.undefined.stellar.data.argument

import org.bukkit.Location
import org.bukkit.entity.LivingEntity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.undefined.stellar.data.argument

import java.util.*

typealias CommandNode = LinkedList<Any?>

class CommandContext(val arguments: CommandNode, val input: String) {
fun <T> getSubCommand(int: Int): T = arguments[int] as T
operator fun get(int: Int) = arguments[int]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.undefined.stellar.data.arguments
package com.undefined.stellar.data.argument

import kotlin.math.max
import kotlin.math.min
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.undefined.stellar.data.arguments
package com.undefined.stellar.data.argument

import org.bukkit.Particle

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.undefined.stellar.sub

import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.data.argument.CommandContext
import org.bukkit.command.CommandSender
import org.bukkit.plugin.java.JavaPlugin

abstract class AbstractStellarSubCommand<T>(val parent: AbstractStellarCommand<*>, name: String) : AbstractStellarCommand<AbstractStellarSubCommand<T>>(name) {
abstract class AbstractStellarSubCommand<T>(val parent: AbstractStellarCommand<*>, name: String) : AbstractStellarCommand<T>(name) {
override fun getBase(): AbstractStellarCommand<*> = parent.getBase()
override fun register(plugin: JavaPlugin) = parent.register(plugin)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.undefined.stellar.sub

import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.data.argument.CommandContext
import com.undefined.stellar.data.suggestion.StellarSuggestion
import com.undefined.stellar.data.execution.CustomStellarExecution
import com.undefined.stellar.data.execution.CustomStellarRunnable
import com.undefined.stellar.data.suggestion.Suggestion
import org.bukkit.command.CommandSender
import org.bukkit.plugin.java.JavaPlugin

abstract class BaseStellarSubCommand<T>(parent: AbstractStellarCommand<*>, name: String) : AbstractStellarSubCommand<T>(parent, name) {

val suggestions: MutableList<StellarSuggestion<*>> = mutableListOf()
val customExecutions: MutableList<CustomStellarExecution<*, Any?>> = mutableListOf()
val customRunnables: MutableList<CustomStellarRunnable<*, Any?>> = mutableListOf()
Expand Down Expand Up @@ -42,11 +45,6 @@ abstract class BaseStellarSubCommand<T>(parent: AbstractStellarCommand<*>, name:
return this as T
}

fun addSuggestions(suggestion: Suggestion): T {
suggestions.add(StellarSuggestion(CommandSender::class) { listOf(suggestion) })
return this as T
}

fun setSuggestions(vararg suggestion: Suggestion): T {
suggestions.clear()
suggestions.add(StellarSuggestion(CommandSender::class) { suggestion.toList() })
Expand Down Expand Up @@ -76,4 +74,12 @@ abstract class BaseStellarSubCommand<T>(parent: AbstractStellarCommand<*>, name:
return this as T
}

inline fun <reified C : CommandSender> addSubCommandExecution(noinline execution: C.(CommandContext) -> Unit): T {
customExecutions.add(CustomStellarExecution(C::class, execution) as CustomStellarExecution<*, Any?>)
return this as T
}

override fun getBase(): AbstractStellarCommand<*> = parent.getBase()
override fun register(plugin: JavaPlugin) = parent.register(plugin)

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ open class SubCommandHandler {
if (base is CustomSubCommand<*>) return _subCommands + base.getSubCommandsList()
return _subCommands
}
val optionalSubCommands: MutableList<BaseStellarSubCommand<*>> = mutableListOf()

fun addSubCommand(subCommand: AbstractStellarSubCommand<*>): AbstractStellarSubCommand<*> {
_subCommands.add(subCommand)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.undefined.stellar.sub.brigadier.entity

import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.data.arguments.Anchor
import com.undefined.stellar.data.argument.Anchor
import com.undefined.stellar.data.execution.CustomStellarExecution
import com.undefined.stellar.data.execution.CustomStellarRunnable
import com.undefined.stellar.sub.BaseStellarSubCommand
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.undefined.stellar.sub.brigadier.math

import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.data.arguments.Operation
import com.undefined.stellar.data.argument.Operation
import com.undefined.stellar.data.execution.CustomStellarExecution
import com.undefined.stellar.data.execution.CustomStellarRunnable
import com.undefined.stellar.sub.BaseStellarSubCommand
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.undefined.stellar.sub.brigadier.world

import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.data.arguments.ParticleData
import com.undefined.stellar.data.argument.ParticleData
import com.undefined.stellar.data.execution.CustomStellarExecution
import com.undefined.stellar.data.execution.CustomStellarRunnable
import com.undefined.stellar.sub.BaseStellarSubCommand
Expand Down
38 changes: 29 additions & 9 deletions server/src/main/kotlin/com/undefined/stellar/Main.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
package com.undefined.stellar

import com.undefined.stellar.data.argument.CommandContext
import com.undefined.stellar.data.suggestion.Suggestion
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
import java.util.LinkedList

class Main : JavaPlugin() {
typealias CommandNode = LinkedList<Any?>

companion object {
lateinit var INSTANCE: Main
}
class Main : JavaPlugin() {

override fun onEnable() {
INSTANCE = this

StellarCommand("test")
StellarCommand("test", description = "this is a description", "alias")
.addAlias("othertest")
.setDescription("This is a description")
.setUsageText("/test <string>")
.setUsageText("/test <other>")
.addStringSubCommand("test")
.addStringSubCommand("test")
.addSuggestion("Text", "Tooltip")
.addSuggestions(Suggestion("suggestion", "t"))
.alwaysRun<Player> {
sendMessage("This will always run no matter what")
true
}
.addExecution<Player> { sendMessage("Execution") }
.addSubCommandExecution<Player> { context ->
sendMessage(context.getSubCommand<String>(1))
}
.addFailureExecution<Player> { sendMessage("Failure!") }
.addFailureExecution<Player> { sendMessage("Incorrect!") }
.hideDefaultFailureMessages()
.register(this)

// val main = StellarCommand("test")
// main.addOnlinePlayersSubCommand("onlinePlayers")
// .addExecution<Player> { context ->
// context.input
// context.<String>get(1)
// context<String>[1]
// }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.context.ParsedArgument
import com.mojang.brigadier.context.StringRange
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import com.undefined.stellar.data.arguments.Anchor
import com.undefined.stellar.data.arguments.Operation
import com.undefined.stellar.data.arguments.ParticleData
import com.undefined.stellar.data.argument.Anchor
import com.undefined.stellar.data.argument.Operation
import com.undefined.stellar.data.argument.ParticleData
import com.undefined.stellar.exception.ServerTypeMismatchException
import com.undefined.stellar.exception.UnsupportedSubCommandException
import com.undefined.stellar.sub.AbstractStellarSubCommand
import com.undefined.stellar.sub.BaseStellarSubCommand
import com.undefined.stellar.sub.brigadier.entity.EntityAnchorSubCommand
import com.undefined.stellar.sub.brigadier.entity.EntityDisplayType
Expand All @@ -34,8 +34,6 @@ import com.undefined.stellar.sub.brigadier.text.ComponentSubCommand
import com.undefined.stellar.sub.brigadier.text.MessageSubCommand
import com.undefined.stellar.sub.brigadier.text.StyleSubCommand
import com.undefined.stellar.sub.brigadier.world.*
import com.undefined.stellar.sub.custom.CustomSubCommand
import com.undefined.stellar.sub.custom.CustomSubCommandInfo
import com.undefined.stellar.sub.custom.EnumSubCommand
import com.undefined.stellar.sub.custom.ListSubCommand
import net.kyori.adventure.text.format.Style
Expand Down Expand Up @@ -88,7 +86,7 @@ object ArgumentHelper {
when (subCommand) {
is ListSubCommand<*> -> RequiredArgumentBuilder.argument<CommandSourceStack, String>(subCommand.name, StringArgumentType.word()).suggestStringList { subCommand.getStringList() }
is EnumSubCommand<*> -> RequiredArgumentBuilder.argument<CommandSourceStack, String>(subCommand.name, StringArgumentType.word()).suggestStringList { subCommand.getStringList() }
else -> RequiredArgumentBuilder.argument(subCommand.name, getArgumentTypeFromBrigadierSubCommand(subCommand))
else -> RequiredArgumentBuilder.argument(subCommand.name, getArgumentTypeFromSubCommand(subCommand))
}

fun <T : BaseStellarSubCommand<*>> handleNativeSubCommandExecutors(subCommand: T, context: CommandContext<CommandSourceStack>) {
Expand All @@ -102,8 +100,9 @@ object ArgumentHelper {
for (execution in subCommand.customExecutions) enum?.let { execution.run(context.source.bukkitSender, enum) }
}
else -> {
val argument = getArgumentFromBrigadierSubCommand(context, subCommand)
for (execution in subCommand.customExecutions) execution.run(context.source.bukkitSender, argument ?: break)
val stellarContext = CommandContextAdapter(context).getStellarCommandContext()
// val argument = getParsedArgumentFromBrigadier(context, subCommand)
for (execution in subCommand.customExecutions) execution.run(context.source.bukkitSender, stellarContext)
}
}
}
Expand All @@ -117,18 +116,22 @@ object ArgumentHelper {
}
is EnumSubCommand<*> -> {
val enum = subCommand.parse(StringArgumentType.getString(context, subCommand.name))
for (runnable in subCommand.customRunnables) enum?.let { runnable.run(context.source.bukkitSender, enum) }
for (runnable in subCommand.customRunnables)
enum?.let { runnable.run(context.source.bukkitSender, enum) }
}
else -> {
val argument = getArgumentFromBrigadierSubCommand(context, subCommand) ?: return true
subCommand.customRunnables.forEach { if (!it.run(context.source.bukkitSender, argument)) return false }
// val argument = getParsedArgumentFromBrigadier(context, subCommand) ?: return true
val stellarContext = CommandContextAdapter(context).getStellarCommandContext()
for (runnable in subCommand.customRunnables)
if (!runnable.run(context.source.bukkitSender, stellarContext)) return false
}
}
return true
}

fun <T : BaseStellarSubCommand<*>> getArgumentTypeFromBrigadierSubCommand(subCommand: T): ArgumentType<*> =
fun <T : AbstractStellarSubCommand<*>> getArgumentTypeFromSubCommand(subCommand: T): ArgumentType<*> =
when (subCommand) {
is ListSubCommand<*> -> StringArgumentType.string()
is StringSubCommand -> subCommand.type.brigadier()
is IntegerSubCommand -> IntegerArgumentType.integer(subCommand.min, subCommand.max)
is LongSubCommand -> LongArgumentType.longArg(subCommand.min, subCommand.max)
Expand Down Expand Up @@ -178,7 +181,7 @@ object ArgumentHelper {
else -> throw UnsupportedSubCommandException()
}

private fun <T : BaseStellarSubCommand<*>> getArgumentFromBrigadierSubCommand(context: CommandContext<CommandSourceStack>, subCommand: T): Any? {
fun <T : AbstractStellarSubCommand<*>> getParsedArgumentFromSubCommand(context: CommandContext<CommandSourceStack>, subCommand: T): Any? {
return when (subCommand) {
is StringSubCommand -> StringArgumentType.getString(context, subCommand.name)
is IntegerSubCommand -> IntegerArgumentType.getInteger(context, subCommand.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package com.undefined.stellar.v1_20_6

import com.mojang.brigadier.Command
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.Message
import com.mojang.brigadier.builder.ArgumentBuilder
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.mojang.brigadier.builder.RequiredArgumentBuilder
import com.mojang.brigadier.context.CommandContext
import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.StellarCommands
import com.undefined.stellar.data.help.CustomCommandHelpTopic
import com.undefined.stellar.data.suggestion.Suggestion
import com.undefined.stellar.exception.UnsupportedSubCommandException
import com.undefined.stellar.sub.AbstractStellarSubCommand
import com.undefined.stellar.sub.BaseStellarSubCommand
Expand All @@ -21,13 +19,7 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.minecraft.commands.CommandSourceStack
import net.minecraft.server.MinecraftServer
import org.bukkit.Bukkit
import org.bukkit.Server
import org.bukkit.command.CommandMap
import org.bukkit.command.CommandSender
import org.bukkit.craftbukkit.CraftServer
import org.bukkit.craftbukkit.help.CustomHelpTopic
import org.bukkit.help.GenericCommandHelpTopic
import org.bukkit.help.HelpTopic
import java.util.SortedMap

object BrigadierCommandRegistrar {
Expand Down Expand Up @@ -56,7 +48,7 @@ object BrigadierCommandRegistrar {
return baseCommand.hasGlobalHiddenDefaultFailureMessages()
}

private fun getSubCommands(
fun getSubCommands(
baseCommand: AbstractStellarCommand<*>,
context: CommandContext<CommandSourceStack>,
currentIndex: Int = 1,
Expand Down Expand Up @@ -111,11 +103,9 @@ object BrigadierCommandRegistrar {

private fun BaseStellarSubCommand<*>.handleExecutions(context: CommandContext<CommandSourceStack>) {
for (subCommand in this.getBase().subCommands) if (subCommand is BaseStellarSubCommand && !handleSubCommandRunnables(subCommand, context)) return
when (this) {
is StellarSubCommand -> for (execution in executions) { execution.run(context.source.bukkitSender) }
is BaseStellarSubCommand -> ArgumentHelper.handleNativeSubCommandExecutors(this, context)
else -> throw UnsupportedSubCommandException()
}

if (this is StellarSubCommand) for (execution in executions) execution.run(context.source.bukkitSender)
else ArgumentHelper.handleNativeSubCommandExecutors(this, context)
}

private fun ArgumentBuilder<CommandSourceStack, *>.handleCommand(stellarCommand: AbstractStellarCommand<*>) {
Expand All @@ -135,7 +125,7 @@ object BrigadierCommandRegistrar {
private fun ArgumentBuilder<CommandSourceStack, *>.handleSubCommands(stellarCommand: AbstractStellarCommand<*>) {
for (subCommand in stellarCommand.subCommands) {
if (subCommand is CustomSubCommand<*>) {
val type = ArgumentHelper.getArgumentTypeFromBrigadierSubCommand(subCommand.type)
val type = ArgumentHelper.getArgumentTypeFromSubCommand(subCommand.type)
val argument: RequiredArgumentBuilder<CommandSourceStack, *> = RequiredArgumentBuilder.argument(subCommand.name, type)
argument.requires { subCommand.requirement() }
argument.suggests { context, builder ->
Expand Down Expand Up @@ -183,6 +173,6 @@ object BrigadierCommandRegistrar {
return ArgumentHelper.handleNativeSubCommandRunnables(subCommand, context)
}

private fun commandDispatcher(): CommandDispatcher<CommandSourceStack> = (Bukkit.getServer() as CraftServer).server.functions.dispatcher
private fun commandDispatcher(): CommandDispatcher<CommandSourceStack> = MinecraftServer.getServer().functions.dispatcher

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.undefined.stellar.v1_20_6

import com.mojang.brigadier.context.CommandContext
import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.StellarCommands
import com.undefined.stellar.data.argument.CommandNode
import com.undefined.stellar.sub.BaseStellarSubCommand
import com.undefined.stellar.sub.custom.CustomSubCommand
import net.minecraft.commands.CommandSourceStack

class CommandContextAdapter(private val context: CommandContext<CommandSourceStack>) {
fun getStellarCommandContext(): com.undefined.stellar.data.argument.CommandContext {
// val arguments: CommandNode = CommandNode()
// arguments.addAll(context.nodes.map { ArgumentHelper.getParsedArgumentFromBrigadier(context, BrigadierCommandRegistrar.) })
println(context.input.startsWith('/'))
val input = context.input.removePrefix("/")
val baseCommand: AbstractStellarCommand<*> = StellarCommands.getStellarCommand(input.substring(input.indexOf('/') + 1, input.indexOf(' ')))!!
val arguments: CommandNode = CommandNode()
arguments.addAll(BrigadierCommandRegistrar.getSubCommands(baseCommand, context).map {
if (it is CustomSubCommand) return@map it.parse(context.source.bukkitSender, input)
ArgumentHelper.getParsedArgumentFromSubCommand(context, it as? BaseStellarSubCommand ?: return@map null)
})
return com.undefined.stellar.data.argument.CommandContext(
arguments,
input
)
}
}

0 comments on commit d868b18

Please sign in to comment.