Skip to content

Commit

Permalink
fix(command-context): arguments not being ordered correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
StillLutto committed Dec 10, 2024
1 parent 300b5dc commit e9c1843
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
12 changes: 10 additions & 2 deletions server/src/main/kotlin/com/undefined/stellar/Main.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.undefined.stellar

import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin

class Main : JavaPlugin() {

override fun onEnable() {
StellarCommand("test", "othertest")
.addFailureMessages("")
StellarCommand("test")
.addLocationArgument("location")
.addStringArgument("test")
.addExecution<Player> {
// sender.sendMessage(getArgument<Location>(0).toVector().toString())
// sender.sendMessage(getArgument<String>(3))
sender.sendMessage(arguments.toList().withIndex().joinToString(", ") { "${it.index}: ${it.value.first}" })
}
.register(this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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.argument.AbstractStellarArgument
import com.undefined.stellar.argument.LiteralStellarArgument
import com.undefined.stellar.argument.types.custom.CustomArgument
import com.undefined.stellar.data.argument.CommandNode
Expand All @@ -21,22 +22,21 @@ import org.bukkit.command.CommandSender

object CommandContextAdapter {

@Suppress("UNCHECKED_CAST")
fun getStellarCommandContext(context: CommandContext<CommandSourceStack>): com.undefined.stellar.data.argument.CommandContext<CommandSender> {
val input = context.input.removePrefix("/")
val baseCommand: AbstractStellarCommand<*> = StellarCommands.getStellarCommand(context.nodes[0].node.name)!!
val arguments = BrigadierCommandHelper.getArguments(baseCommand, context)
if (arguments.filter { it !is LiteralStellarArgument }.groupingBy { it.name }.eachCount().any { it.value > 1 }) throw DuplicateArgumentNameException()
val parsedArguments: CommandNode = CommandNode()
parsedArguments.putAll(
val parsedArguments: CommandNode =
BrigadierCommandHelper.getArguments(baseCommand, context)
.associate { argument ->
.associate<AbstractStellarArgument<*>, String, (com.undefined.stellar.data.argument.CommandContext<CommandSender>) -> Any?> { argument ->
if (argument is CustomArgument) return@associate Pair(argument.name) { argument.parse(it) }
if (argument is LiteralStellarArgument) return@associate Pair(argument.name) { throw LiteralArgumentMismatchException() }
Pair(argument.name) {
ArgumentHelper.getParsedArgument(context, argument)
}
}
)
} as CommandNode
return com.undefined.stellar.data.argument.CommandContext(
parsedArguments,
context.source.bukkitSender,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.undefined.stellar.v1_21_3
import com.mojang.brigadier.context.CommandContext
import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.StellarCommands
import com.undefined.stellar.argument.AbstractStellarArgument
import com.undefined.stellar.argument.LiteralStellarArgument
import com.undefined.stellar.argument.types.custom.CustomArgument
import com.undefined.stellar.data.argument.CommandNode
Expand All @@ -21,22 +22,21 @@ import org.bukkit.command.CommandSender

object CommandContextAdapter {

@Suppress("UNCHECKED_CAST")
fun getStellarCommandContext(context: CommandContext<CommandSourceStack>): com.undefined.stellar.data.argument.CommandContext<CommandSender> {
val input = context.input.removePrefix("/")
val baseCommand: AbstractStellarCommand<*> = StellarCommands.getStellarCommand(context.nodes[0].node.name)!!
val arguments = BrigadierCommandHelper.getArguments(baseCommand, context)
if (arguments.filter { it !is LiteralStellarArgument }.groupingBy { it.name }.eachCount().any { it.value > 1 }) throw DuplicateArgumentNameException()
val parsedArguments: CommandNode = CommandNode()
parsedArguments.putAll(
val parsedArguments: CommandNode =
BrigadierCommandHelper.getArguments(baseCommand, context)
.associate { argument ->
.associate<AbstractStellarArgument<*>, String, (com.undefined.stellar.data.argument.CommandContext<CommandSender>) -> Any?> { argument ->
if (argument is CustomArgument) return@associate Pair(argument.name) { argument.parse(it) }
if (argument is LiteralStellarArgument) return@associate Pair(argument.name) { throw LiteralArgumentMismatchException() }
Pair(argument.name) {
ArgumentHelper.getParsedArgument(context, argument)
}
}
)
} as CommandNode
return com.undefined.stellar.data.argument.CommandContext(
parsedArguments,
context.source.bukkitSender,
Expand Down

0 comments on commit e9c1843

Please sign in to comment.