From cbf5d2a053467bd7b254bb7e6bbf2d122fb6d1a5 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 7 May 2023 09:35:18 +0200 Subject: [PATCH] brigadier: redirect --- .../bixilon/minosoft/commands/MsgCommandIT.kt | 36 +++++++++++++++---- .../minosoft/commands/nodes/CommandNode.kt | 7 ++-- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/commands/MsgCommandIT.kt b/src/integration-test/kotlin/de/bixilon/minosoft/commands/MsgCommandIT.kt index 845c7f1c4..03dd52f01 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/commands/MsgCommandIT.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/commands/MsgCommandIT.kt @@ -21,19 +21,22 @@ import de.bixilon.minosoft.commands.parser.minecraft.message.MessageParser import de.bixilon.minosoft.commands.parser.minecraft.target.TargetParser import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.name.NameEntityTarget import de.bixilon.minosoft.commands.stack.CommandStack -import org.testng.Assert.* +import org.testng.Assert.assertEquals +import org.testng.Assert.assertTrue import org.testng.annotations.Test @Test(groups = ["command"]) class MsgCommandIT { private fun createNode(executor: (CommandStack) -> Unit): RootNode { - return RootNode().addChild( - LiteralNode("msg").addChild( - ArgumentNode("targets", TargetParser()).addChild( - ArgumentNode("message", MessageParser, executor = executor) - ) + val msg = LiteralNode("msg").addChild( + ArgumentNode("targets", TargetParser()).addChild( + ArgumentNode("message", MessageParser, executor = executor) ) + ) + return RootNode().addChild( + msg, + LiteralNode(name = "redirect", redirect = msg) ).unsafeCast() } @@ -48,11 +51,30 @@ class MsgCommandIT { fun validateStack() { val node = createNode { - assertNotNull(it["msg"]) + assertEquals(it["msg"], "msg") assertEquals(it["targets"], NameEntityTarget("Bixilon")) assertEquals(it["message"], "hi there!") } node.execute("msg Bixilon hi there!") } + + fun redirectExecution() { + var executed = false + val node = createNode { executed = true } + + node.execute("redirect Bixilon hi there!") + + assertTrue(executed) + } + + fun redirectStack() { + val node = createNode { + assertEquals(it["msg"], "redirect") + assertEquals(it["targets"], NameEntityTarget("Bixilon")) + assertEquals(it["message"], "hi there!") + } + + node.execute("redirect Bixilon hi there!") + } } diff --git a/src/main/java/de/bixilon/minosoft/commands/nodes/CommandNode.kt b/src/main/java/de/bixilon/minosoft/commands/nodes/CommandNode.kt index bb48f6ddb..053081cfa 100644 --- a/src/main/java/de/bixilon/minosoft/commands/nodes/CommandNode.kt +++ b/src/main/java/de/bixilon/minosoft/commands/nodes/CommandNode.kt @@ -25,6 +25,7 @@ abstract class CommandNode( ) { protected val children: MutableList = mutableListOf() + open fun addChild(vararg node: CommandNode): CommandNode { children += node return this @@ -42,7 +43,7 @@ abstract class CommandNode( var childError: Throwable? = null var errorStack = -1 - for (child in children) { + for (child in (redirect?.children ?: children)) { reader.pointer = pointer stack.reset(stackSize) try { @@ -73,7 +74,7 @@ abstract class CommandNode( var errorStack = -1 var parserSucceeds = 0 - for (child in children) { + for (child in (redirect?.children ?: children)) { reader.pointer = pointer stack.reset(stackSize) try { @@ -114,7 +115,7 @@ abstract class CommandNode( protected fun checkForDeadEnd(reader: CommandReader) { - if (children.isEmpty()) { + if ((redirect?.children ?: children).isEmpty()) { if (reader.canPeek()) { throw TrailingTextError(reader) } else {