mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 10:25:06 -04:00
properly format command exception
This commit is contained in:
parent
8041df4d71
commit
3f47c3f5f3
@ -13,12 +13,16 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.commands.nodes
|
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.CommandExecutor
|
||||||
import de.bixilon.minosoft.commands.stack.CommandStack
|
import de.bixilon.minosoft.commands.stack.CommandStack
|
||||||
import de.bixilon.minosoft.commands.suggestion.Suggestion
|
import de.bixilon.minosoft.commands.suggestion.Suggestion
|
||||||
import de.bixilon.minosoft.commands.suggestion.types.SuggestionType
|
import de.bixilon.minosoft.commands.suggestion.types.SuggestionType
|
||||||
import de.bixilon.minosoft.commands.util.CommandReader
|
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.TextComponent
|
||||||
|
import de.bixilon.minosoft.data.text.formatting.TextFormattable
|
||||||
import de.bixilon.minosoft.data.text.formatting.color.ChatColors
|
import de.bixilon.minosoft.data.text.formatting.color.ChatColors
|
||||||
import de.bixilon.minosoft.terminal.commands.CommandException
|
import de.bixilon.minosoft.terminal.commands.CommandException
|
||||||
|
|
||||||
@ -36,7 +40,12 @@ abstract class ExecutableNode(
|
|||||||
try {
|
try {
|
||||||
executor?.invoke(stack)
|
executor?.invoke(stack)
|
||||||
} catch (exception: CommandException) {
|
} 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) {
|
} catch (exception: Throwable) {
|
||||||
exception.printStackTrace()
|
exception.printStackTrace()
|
||||||
}
|
}
|
||||||
|
@ -13,14 +13,7 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.commands.stack.print
|
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 {
|
interface PrintTarget {
|
||||||
|
|
||||||
fun print(message: Any)
|
fun print(message: Any)
|
||||||
fun error(message: Any) {
|
|
||||||
print(BaseComponent(TextComponent("[ERROR] ").bold().color(ChatColors.RED), message))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,7 @@ class NodeTextInputElement(
|
|||||||
try {
|
try {
|
||||||
node.execute(CommandReader(value), stack)
|
node.execute(CommandReader(value), stack)
|
||||||
} catch (exception: Throwable) {
|
} 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)
|
updateError(null)
|
||||||
}
|
}
|
||||||
|
@ -13,4 +13,14 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.terminal.commands
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -15,6 +15,7 @@ package de.bixilon.minosoft.terminal.commands.connection
|
|||||||
|
|
||||||
import de.bixilon.minosoft.commands.nodes.LiteralNode
|
import de.bixilon.minosoft.commands.nodes.LiteralNode
|
||||||
import de.bixilon.minosoft.commands.stack.CommandStack
|
import de.bixilon.minosoft.commands.stack.CommandStack
|
||||||
|
import de.bixilon.minosoft.terminal.commands.CommandException
|
||||||
|
|
||||||
object ActionCommand : ConnectionCommand {
|
object ActionCommand : ConnectionCommand {
|
||||||
override var node = LiteralNode("action", setOf("do"))
|
override var node = LiteralNode("action", setOf("do"))
|
||||||
@ -23,7 +24,7 @@ object ActionCommand : ConnectionCommand {
|
|||||||
|
|
||||||
private fun CommandStack.respawn() {
|
private fun CommandStack.respawn() {
|
||||||
if (connection.player.healthCondition.hp > 0.0f) {
|
if (connection.player.healthCondition.hp > 0.0f) {
|
||||||
return print.error("You are still alive!")
|
throw CommandException("You are still alive!")
|
||||||
}
|
}
|
||||||
print.print("Performing respawn...")
|
print.print("Performing respawn...")
|
||||||
connection.util.respawn()
|
connection.util.respawn()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user