Skip to content

Commit

Permalink
commands: Fix Mojang/brigadier#46 by copying command node
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobkmar committed Jun 24, 2022
1 parent 7edce6d commit dcd9632
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,25 @@ class LiteralCommandBuilder<Source : SharedSuggestionProvider>(

override fun toBrigadier(context: CommandBuildContext): List<LiteralCommandNode<Source>> {
return super.toBrigadier(context).let { mainNodes ->
if (aliases.isEmpty()) mainNodes else {
if (aliases.isEmpty() || mainNodes.size != 1) mainNodes else {
val mainNode = mainNodes.single()
mainNodes + aliases.map { alias ->
LiteralArgumentBuilder
.literal<Source>(alias)
.apply { mainNodes.singleOrNull()?.let { redirect(it) } }
.build()
if (mainNode.children.isNotEmpty() && mainNode.command == null) {
LiteralArgumentBuilder
.literal<Source>(alias)
.redirect(mainNode)
.build()
} else {
// we cannot use redirect here because of
// https://github.com/Mojang/brigadier/issues/46
// fix: create a new node here instead
LiteralCommandNode(alias, mainNode.command, mainNode.requirement, mainNode.redirect, mainNode.redirectModifier, mainNode.isFork)
.apply {
for (child in mainNode.children) {
addChild(child)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import net.silkmc.silk.core.text.sendText

val commandTestCommand = testCommand("commandtest") {
runs {
source.playerOrException.sendText("Top Level 'runs'")
source.playerOrException.sendText("top Level 'runs'")
}
argument<Float>("testarg") {
runs {
source.playerOrException.sendText("'runs' inside 'testarg' got the following argument: ${it()}")
}
}

literal("literal_test") {
literal("testliteral_base") {
alias("testliteral_alias")
runs {
source.playerOrException.sendText("'runs' inside 'testliteral'")
}
}
}
}

0 comments on commit dcd9632

Please sign in to comment.