From 045704a9b0bf21af7378877ce3aa9bf54739228f Mon Sep 17 00:00:00 2001 From: metabrixkt Date: Fri, 2 Aug 2024 22:50:04 +0500 Subject: [PATCH] fix(cloud-brigadier): Fix literal node aliases with Brigadier Fixes #5 --- .../node/LiteralBrigadierNodeFactory.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/cloud-brigadier/src/main/java/org/incendo/cloud/brigadier/node/LiteralBrigadierNodeFactory.java b/cloud-brigadier/src/main/java/org/incendo/cloud/brigadier/node/LiteralBrigadierNodeFactory.java index 00961126..bee45993 100644 --- a/cloud-brigadier/src/main/java/org/incendo/cloud/brigadier/node/LiteralBrigadierNodeFactory.java +++ b/cloud-brigadier/src/main/java/org/incendo/cloud/brigadier/node/LiteralBrigadierNodeFactory.java @@ -34,6 +34,7 @@ import io.leangen.geantyref.GenericTypeReflector; import io.leangen.geantyref.TypeToken; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Objects; @@ -102,7 +103,9 @@ public LiteralBrigadierNodeFactory( final LiteralCommandNode constructedRoot = literalArgumentBuilder.build(); for (final CommandNode child : cloudCommand.children()) { - constructedRoot.addChild(this.constructCommandNode(child, permissionChecker, executor).build()); + this.constructCommandNode(child, permissionChecker, executor).stream() + .map(ArgumentBuilder::build) + .forEach(constructedRoot::addChild); } return constructedRoot; } @@ -138,40 +141,46 @@ public LiteralBrigadierNodeFactory( (sender, permission) -> this.commandManager.testPermission(sender, permission).allowed()); } - private @NonNull ArgumentBuilder constructCommandNode( + private @NonNull List<@NonNull ArgumentBuilder> constructCommandNode( final @NonNull CommandNode root, final @NonNull BrigadierPermissionChecker permissionChecker, final com.mojang.brigadier.@NonNull Command executor ) { if (root.component().parser() instanceof AggregateParser) { final AggregateParser aggregateParser = (AggregateParser) root.component().parser(); - return this.constructAggregateNode( + return Arrays.asList(this.constructAggregateNode( aggregateParser, root, permissionChecker, executor - ); + )); } - final ArgumentBuilder argumentBuilder; + final List> argumentBuilders; if (root.component().type() == CommandComponent.ComponentType.LITERAL) { - argumentBuilder = this.createLiteralArgumentBuilder(root.component(), root, permissionChecker); + argumentBuilders = new ArrayList<>(); + argumentBuilders.add(this.createLiteralArgumentBuilder(root.component().name(), root, permissionChecker)); + for (final String alias : root.component().alternativeAliases()) { + argumentBuilders.add(this.createLiteralArgumentBuilder(alias, root, permissionChecker)); + } } else { - argumentBuilder = this.createVariableArgumentBuilder(root.component(), root, permissionChecker); + argumentBuilders = Arrays.asList(this.createVariableArgumentBuilder(root.component(), root, permissionChecker)); } - this.updateExecutes(argumentBuilder, root, executor); + argumentBuilders.forEach(argumentBuilder -> this.updateExecutes(argumentBuilder, root, executor)); for (final CommandNode node : root.children()) { - argumentBuilder.then(this.constructCommandNode(node, permissionChecker, executor)); + for (final ArgumentBuilder argumentBuilder : argumentBuilders) { + this.constructCommandNode(node, permissionChecker, executor).forEach(argumentBuilder::then); + } } - return argumentBuilder; + return argumentBuilders; } private @NonNull ArgumentBuilder createLiteralArgumentBuilder( - final @NonNull CommandComponent component, + final @NonNull String name, final @NonNull CommandNode root, final @NonNull BrigadierPermissionChecker permissionChecker ) { - return LiteralArgumentBuilder.literal(component.name()) + return LiteralArgumentBuilder.literal(name) .requires(this.requirement(root, permissionChecker)); } @@ -221,7 +230,7 @@ public LiteralBrigadierNodeFactory( // We now want to link up all subsequent components to the tail. final ArgumentBuilder tail = argumentBuilders.get(argumentBuilders.size() - 1); for (final CommandNode node : root.children()) { - tail.then(this.constructCommandNode(node, permissionChecker, executor)); + this.constructCommandNode(node, permissionChecker, executor).forEach(tail::then); } this.updateExecutes(tail, root, executor);