Handle wrong param type and similar errors and improve things
- Improved help text - Improved tests
This commit is contained in:
parent
79c1cc47f7
commit
401a54b078
2 changed files with 37 additions and 15 deletions
|
@ -106,8 +106,16 @@ abstract class Command2<TC : ICommand2<TP>, TP : Command2Sender>(
|
||||||
open fun handleCommand(sender: TP, commandline: String): Boolean {
|
open fun handleCommand(sender: TP, commandline: String): Boolean {
|
||||||
val results = dispatcher.parse(commandline.removePrefix("/"), sender)
|
val results = dispatcher.parse(commandline.removePrefix("/"), sender)
|
||||||
if (results.reader.canRead()) {
|
if (results.reader.canRead()) {
|
||||||
|
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
|
return false // Unknown command
|
||||||
}
|
}
|
||||||
|
}
|
||||||
val executeCommand: () -> Unit = {
|
val executeCommand: () -> Unit = {
|
||||||
try {
|
try {
|
||||||
dispatcher.execute(results)
|
dispatcher.execute(results)
|
||||||
|
@ -321,12 +329,17 @@ abstract class Command2<TC : ICommand2<TP>, TP : Command2Sender>(
|
||||||
* @param context The command context
|
* @param context The command context
|
||||||
* @return Vanilla command success level (0)
|
* @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 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}")
|
val helpText = node.subcommandDataNoOp()?.getHelpText(context.source) ?: error("No subcommand data found when executing help text for ${context.input}")
|
||||||
if (node.isCommand()) {
|
if (node.isCommand()) {
|
||||||
val subs = getSubcommands(node.coreCommandNoOp()!!).map { commandChar + it.data.fullPath }.sorted()
|
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
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ class Command2MCTest {
|
||||||
|
|
||||||
NoArgTestCommand.register()
|
NoArgTestCommand.register()
|
||||||
val errCmd = ErroringTestCommand
|
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()
|
MultiArgTestCommand.register()
|
||||||
|
|
||||||
TestNoMainCommand1.register()
|
TestNoMainCommand1.register()
|
||||||
|
@ -77,12 +77,12 @@ class Command2MCTest {
|
||||||
val user = ChromaGamerBase.getUser(UUID.randomUUID().toString(), TBMCPlayer::class.java)
|
val user = ChromaGamerBase.getUser(UUID.randomUUID().toString(), TBMCPlayer::class.java)
|
||||||
user.playerName = "TestPlayer"
|
user.playerName = "TestPlayer"
|
||||||
val sender = object : Command2MCSender(user, Channel.globalChat, user) {
|
val sender = object : Command2MCSender(user, Channel.globalChat, user) {
|
||||||
private var messageReceived: String? = null
|
private var messageReceived: String = ""
|
||||||
private var allowMessageReceive = false
|
private var allowMessageReceive = false
|
||||||
|
|
||||||
override fun sendMessage(message: String) {
|
override fun sendMessage(message: String) {
|
||||||
if (allowMessageReceive) {
|
if (allowMessageReceive) {
|
||||||
messageReceived = message
|
messageReceived += message + "\n"
|
||||||
} else {
|
} else {
|
||||||
error(message)
|
error(message)
|
||||||
}
|
}
|
||||||
|
@ -92,11 +92,21 @@ class Command2MCTest {
|
||||||
sendMessage(message.joinToString("\n"))
|
sendMessage(message.joinToString("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun withMessageReceive(action: () -> Unit): String? {
|
fun withMessageReceive(action: () -> Unit): String {
|
||||||
|
messageReceived = ""
|
||||||
allowMessageReceive = true
|
allowMessageReceive = true
|
||||||
action()
|
action()
|
||||||
allowMessageReceive = false
|
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")
|
sender.runCommand("/test hmm", TestCommand, "hmm")
|
||||||
|
@ -119,27 +129,26 @@ class Command2MCTest {
|
||||||
"Used for testing\n" +
|
"Used for testing\n" +
|
||||||
"§6---- Subcommands ----\n" +
|
"§6---- Subcommands ----\n" +
|
||||||
"/test playerfail\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 test cmd", TestNoMainCommand1, "TestPlayer")
|
||||||
sender.runCommand("/some another cmd", TestNoMainCommand2, "TestPlayer")
|
sender.runCommand("/some another cmd", TestNoMainCommand2, "TestPlayer")
|
||||||
|
|
||||||
assertEquals("§6---- Subcommands ----\n" +
|
assertEquals("§6---- Subcommands ----\n" +
|
||||||
"/some another cmd\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")
|
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() {
|
private fun ICommand2MC.register() {
|
||||||
MainPlugin.instance.registerCommand(this)
|
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) {
|
private fun runFailingCommand(sender: Command2MCSender, command: String) {
|
||||||
assert(!ButtonPlugin.command2MC.handleCommand(sender, command)) { "Could execute command $command that shouldn't work" }
|
assert(!ButtonPlugin.command2MC.handleCommand(sender, command)) { "Could execute command $command that shouldn't work" }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue