properly format command exception

This commit is contained in:
Moritz Zwerger 2023-07-30 14:56:40 +02:00
parent 8041df4d71
commit 3f47c3f5f3
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 24 additions and 11 deletions

View File

@ -13,12 +13,16 @@
package de.bixilon.minosoft.commands.nodes
import de.bixilon.kutil.cast.CastUtil.nullCast
import de.bixilon.minosoft.commands.stack.CommandExecutor
import de.bixilon.minosoft.commands.stack.CommandStack
import de.bixilon.minosoft.commands.suggestion.Suggestion
import de.bixilon.minosoft.commands.suggestion.types.SuggestionType
import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.data.text.BaseComponent
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.data.text.TextComponent
import de.bixilon.minosoft.data.text.formatting.TextFormattable
import de.bixilon.minosoft.data.text.formatting.color.ChatColors
import de.bixilon.minosoft.terminal.commands.CommandException
@ -36,7 +40,12 @@ abstract class ExecutableNode(
try {
executor?.invoke(stack)
} catch (exception: CommandException) {
stack.print.print(TextComponent(exception.message).color(ChatColors.RED))
val message = exception.nullCast<TextFormattable>()?.toText() ?: exception.message
if (message != null) {
val component = ChatComponent.of(message)
component.setFallbackColor(ChatColors.RED)
stack.print.print(BaseComponent(TextComponent("[ERROR] ").bold().color(ChatColors.RED), component))
}
} catch (exception: Throwable) {
exception.printStackTrace()
}

View File

@ -13,14 +13,7 @@
package de.bixilon.minosoft.commands.stack.print
import de.bixilon.minosoft.data.text.BaseComponent
import de.bixilon.minosoft.data.text.TextComponent
import de.bixilon.minosoft.data.text.formatting.color.ChatColors
interface PrintTarget {
fun print(message: Any)
fun error(message: Any) {
print(BaseComponent(TextComponent("[ERROR] ").bold().color(ChatColors.RED), message))
}
}

View File

@ -91,7 +91,7 @@ class NodeTextInputElement(
try {
node.execute(CommandReader(value), stack)
} catch (exception: Throwable) {
exception.message?.let { stack.print.error(TextComponent(it).color(ChatColors.RED)) }
exception.message?.let { stack.print.print(TextComponent(it).color(ChatColors.RED)) }
}
updateError(null)
}

View File

@ -13,4 +13,14 @@
package de.bixilon.minosoft.terminal.commands
class CommandException(message: String) : Exception(message)
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.data.text.formatting.TextFormattable
open class CommandException(text: Any) : Exception(), TextFormattable {
private val text = ChatComponent.of(text)
override val message: String get() = text.message
override fun toText(): Any {
return text
}
}

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.terminal.commands.connection
import de.bixilon.minosoft.commands.nodes.LiteralNode
import de.bixilon.minosoft.commands.stack.CommandStack
import de.bixilon.minosoft.terminal.commands.CommandException
object ActionCommand : ConnectionCommand {
override var node = LiteralNode("action", setOf("do"))
@ -23,7 +24,7 @@ object ActionCommand : ConnectionCommand {
private fun CommandStack.respawn() {
if (connection.player.healthCondition.hp > 0.0f) {
return print.error("You are still alive!")
throw CommandException("You are still alive!")
}
print.print("Performing respawn...")
connection.util.respawn()