Handle wrong param type and similar errors and improve things

- Improved help text
- Improved tests
This commit is contained in:
Norbi Peti 2023-07-28 00:56:13 +02:00
parent 79c1cc47f7
commit 401a54b078
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 37 additions and 15 deletions

View file

@ -106,7 +106,15 @@ abstract class Command2<TC : ICommand2<TP>, TP : Command2Sender>(
open fun handleCommand(sender: TP, commandline: String): Boolean {
val results = dispatcher.parse(commandline.removePrefix("/"), sender)
if (results.reader.canRead()) {
return false // Unknown command
if (results.context.nodes.isNotEmpty()) {
for ((node, ex) in results.exceptions) {
sender.sendMessage("${ChatColor.RED}${ex.message}")
executeHelpText(results.context.build(results.reader.string))
}
return true
} else {
return false // Unknown command
}
}
val executeCommand: () -> Unit = {
try {
@ -321,12 +329,17 @@ abstract class Command2<TC : ICommand2<TP>, TP : Command2Sender>(
* @param context The command context
* @return Vanilla command success level (0)
*/
private fun executeHelpText(context: CommandContext<TP>): Int {
private fun executeHelpText(context: CommandContext<TP>): Int { // TODO: Add usage string automatically (and dynamically?)
val node = context.nodes.lastOrNull()?.node ?: error("No nodes found when executing help text for ${context.input}!")
val helpText = node.subcommandDataNoOp()?.getHelpText(context.source) ?: error("No subcommand data found when executing help text for ${context.input}")
if (node.isCommand()) {
val subs = getSubcommands(node.coreCommandNoOp()!!).map { commandChar + it.data.fullPath }.sorted()
context.source.sendMessage(helpText + "${ChatColor.GOLD}---- Subcommands ----" + subs)
val messages = if (subs.isNotEmpty()) {
helpText + "${ChatColor.GOLD}---- Subcommands ----" + subs
} else {
helpText
}
context.source.sendMessage(messages)
}
return 0
}

View file

@ -48,7 +48,7 @@ class Command2MCTest {
NoArgTestCommand.register()
val errCmd = ErroringTestCommand
assertEquals("No sender parameter for method '${errCmd::class.java.getMethod("def")}'", assertFails { ErroringTestCommand.register() }.message)
assertEquals("No sender parameter for method '${errCmd::class.java.getMethod("def")}'", assertFails { errCmd.register() }.message)
MultiArgTestCommand.register()
TestNoMainCommand1.register()
@ -77,12 +77,12 @@ class Command2MCTest {
val user = ChromaGamerBase.getUser(UUID.randomUUID().toString(), TBMCPlayer::class.java)
user.playerName = "TestPlayer"
val sender = object : Command2MCSender(user, Channel.globalChat, user) {
private var messageReceived: String? = null
private var messageReceived: String = ""
private var allowMessageReceive = false
override fun sendMessage(message: String) {
if (allowMessageReceive) {
messageReceived = message
messageReceived += message + "\n"
} else {
error(message)
}
@ -92,11 +92,21 @@ class Command2MCTest {
sendMessage(message.joinToString("\n"))
}
fun withMessageReceive(action: () -> Unit): String? {
fun withMessageReceive(action: () -> Unit): String {
messageReceived = ""
allowMessageReceive = true
action()
allowMessageReceive = false
return messageReceived
return messageReceived.trim()
}
fun runCommand(command: String, obj: ITestCommand2MC, expected: String) {
assert(ButtonPlugin.command2MC.handleCommand(this, command)) { "Could not find command $command" }
assertEquals(expected, obj.testCommandReceived)
}
fun runCommandWithReceive(command: String): String {
return withMessageReceive { ButtonPlugin.command2MC.handleCommand(this, command) }
}
}
sender.runCommand("/test hmm", TestCommand, "hmm")
@ -119,27 +129,26 @@ class Command2MCTest {
"Used for testing\n" +
"§6---- Subcommands ----\n" +
"/test playerfail\n" +
"/test plugin", sender.withMessageReceive { ButtonPlugin.command2MC.handleCommand(sender, "/test") })
"/test plugin", sender.runCommandWithReceive("/test")
)
sender.runCommand("/some test cmd", TestNoMainCommand1, "TestPlayer")
sender.runCommand("/some another cmd", TestNoMainCommand2, "TestPlayer")
assertEquals("§6---- Subcommands ----\n" +
"/some another cmd\n" +
"/some test cmd", sender.withMessageReceive { ButtonPlugin.command2MC.handleCommand(sender, "/some") })
"/some test cmd", sender.runCommandWithReceive("/some")
)
sender.runCommand("/testparams 12 34 56 78", TestParamsCommand, "12 34 56.0 78.0 Player0")
assertEquals("§cExpected integer at position 11: ...estparams <--[HERE]", sender.runCommandWithReceive("/testparams asd 34 56 78"))
// TODO: Change test when usage help is added
}
private fun ICommand2MC.register() {
MainPlugin.instance.registerCommand(this)
}
private fun Command2MCSender.runCommand(command: String, obj: ITestCommand2MC, expected: String) {
assert(ButtonPlugin.command2MC.handleCommand(this, command)) { "Could not find command $command" }
assertEquals(expected, obj.testCommandReceived)
}
private fun runFailingCommand(sender: Command2MCSender, command: String) {
assert(!ButtonPlugin.command2MC.handleCommand(sender, command)) { "Could execute command $command that shouldn't work" }
}