brigadier: redirect

This commit is contained in:
Bixilon 2023-05-07 09:35:18 +02:00
parent 15f06e3980
commit cbf5d2a053
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 33 additions and 10 deletions

View File

@ -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.TargetParser
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.name.NameEntityTarget import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.name.NameEntityTarget
import de.bixilon.minosoft.commands.stack.CommandStack 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 import org.testng.annotations.Test
@Test(groups = ["command"]) @Test(groups = ["command"])
class MsgCommandIT { class MsgCommandIT {
private fun createNode(executor: (CommandStack) -> Unit): RootNode { private fun createNode(executor: (CommandStack) -> Unit): RootNode {
return RootNode().addChild( val msg = LiteralNode("msg").addChild(
LiteralNode("msg").addChild( ArgumentNode("targets", TargetParser()).addChild(
ArgumentNode("targets", TargetParser()).addChild( ArgumentNode("message", MessageParser, executor = executor)
ArgumentNode("message", MessageParser, executor = executor)
)
) )
)
return RootNode().addChild(
msg,
LiteralNode(name = "redirect", redirect = msg)
).unsafeCast() ).unsafeCast()
} }
@ -48,11 +51,30 @@ class MsgCommandIT {
fun validateStack() { fun validateStack() {
val node = createNode { val node = createNode {
assertNotNull(it["msg"]) assertEquals(it["msg"], "msg")
assertEquals(it["targets"], NameEntityTarget("Bixilon")) assertEquals(it["targets"], NameEntityTarget("Bixilon"))
assertEquals(it["message"], "hi there!") assertEquals(it["message"], "hi there!")
} }
node.execute("msg Bixilon 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!")
}
} }

View File

@ -25,6 +25,7 @@ abstract class CommandNode(
) { ) {
protected val children: MutableList<CommandNode> = mutableListOf() protected val children: MutableList<CommandNode> = mutableListOf()
open fun addChild(vararg node: CommandNode): CommandNode { open fun addChild(vararg node: CommandNode): CommandNode {
children += node children += node
return this return this
@ -42,7 +43,7 @@ abstract class CommandNode(
var childError: Throwable? = null var childError: Throwable? = null
var errorStack = -1 var errorStack = -1
for (child in children) { for (child in (redirect?.children ?: children)) {
reader.pointer = pointer reader.pointer = pointer
stack.reset(stackSize) stack.reset(stackSize)
try { try {
@ -73,7 +74,7 @@ abstract class CommandNode(
var errorStack = -1 var errorStack = -1
var parserSucceeds = 0 var parserSucceeds = 0
for (child in children) { for (child in (redirect?.children ?: children)) {
reader.pointer = pointer reader.pointer = pointer
stack.reset(stackSize) stack.reset(stackSize)
try { try {
@ -114,7 +115,7 @@ abstract class CommandNode(
protected fun checkForDeadEnd(reader: CommandReader) { protected fun checkForDeadEnd(reader: CommandReader) {
if (children.isEmpty()) { if ((redirect?.children ?: children).isEmpty()) {
if (reader.canPeek()) { if (reader.canPeek()) {
throw TrailingTextError(reader) throw TrailingTextError(reader)
} else { } else {