From d596992c0eafd8c5171f859aa49eca3dafe50669 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 17 Jul 2022 23:13:25 +0200 Subject: [PATCH] Include command aliases in graph (#1244) --- .../minestom/server/command/GraphImpl.java | 11 ++++++++-- .../server/command/CommandPacketTest.java | 22 +++++++++++++++++++ .../server/command/GraphConversionTest.java | 14 ++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/command/GraphImpl.java b/src/main/java/net/minestom/server/command/GraphImpl.java index 5cb464202..f30950bdd 100644 --- a/src/main/java/net/minestom/server/command/GraphImpl.java +++ b/src/main/java/net/minestom/server/command/GraphImpl.java @@ -11,6 +11,7 @@ import java.util.function.Consumer; import java.util.function.Predicate; import static net.minestom.server.command.builder.arguments.ArgumentType.Literal; +import static net.minestom.server.command.builder.arguments.ArgumentType.Word; record GraphImpl(NodeImpl root) implements Graph { static GraphImpl fromCommand(Command command) { @@ -107,7 +108,7 @@ record GraphImpl(NodeImpl root) implements Graph { } static ConversionNode fromCommand(Command command) { - ConversionNode root = new ConversionNode(Literal(command.getName()), ExecutionImpl.fromCommand(command)); + ConversionNode root = new ConversionNode(commandToArgument(command), ExecutionImpl.fromCommand(command)); // Syntaxes for (CommandSyntax syntax : command.getSyntaxes()) { ConversionNode syntaxNode = root; @@ -121,7 +122,7 @@ record GraphImpl(NodeImpl root) implements Graph { } // Subcommands for (Command subcommand : command.getSubcommands()) { - root.nextMap.put(Literal(subcommand.getName()), fromCommand(subcommand)); + root.nextMap.put(commandToArgument(subcommand), fromCommand(subcommand)); } return root; } @@ -136,6 +137,12 @@ record GraphImpl(NodeImpl root) implements Graph { } } + static Argument commandToArgument(Command command) { + final String[] aliases = command.getNames(); + if (aliases.length == 1) return Literal(aliases[0]); + return Word(command.getName()).from(command.getNames()); + } + static boolean compare(@NotNull Node first, Node second, @NotNull Comparator comparator) { return switch (comparator) { case TREE -> { diff --git a/src/test/java/net/minestom/server/command/CommandPacketTest.java b/src/test/java/net/minestom/server/command/CommandPacketTest.java index 18fdb74ba..f0aecb809 100644 --- a/src/test/java/net/minestom/server/command/CommandPacketTest.java +++ b/src/test/java/net/minestom/server/command/CommandPacketTest.java @@ -169,6 +169,28 @@ public class CommandPacketTest { """, graph); } + @Test + public void commandAliasWithoutArg() { + var graph = Graph.builder(ArgumentType.Word("foo").from("foo", "bar")) + .build(); + assertPacketGraph(""" + foo bar=% + 0->foo bar + """, graph); + } + + @Test + public void commandAliasWithArg() { + var graph = Graph.builder(ArgumentType.Word("foo").from("foo", "bar")) + .append(ArgumentType.Literal("l")) + .build(); + assertPacketGraph(""" + foo bar l=% + 0->foo bar + foo bar->l + """, graph); + } + static void assertPacketGraph(String expected, Graph... graphs) { var packet = GraphConverter.createPacket(Graph.merge(graphs), null); CommandTestUtils.assertPacket(packet, expected); diff --git a/src/test/java/net/minestom/server/command/GraphConversionTest.java b/src/test/java/net/minestom/server/command/GraphConversionTest.java index 0a8776f60..2bea24622 100644 --- a/src/test/java/net/minestom/server/command/GraphConversionTest.java +++ b/src/test/java/net/minestom/server/command/GraphConversionTest.java @@ -101,6 +101,20 @@ public class GraphConversionTest { assertEqualsGraph(graph, main); } + @Test + public void alias() { + final Command main = new Command("main", "alias"); + var graph = Graph.builder(Word("main").from("main", "alias")).build(); + assertEqualsGraph(graph, main); + } + + @Test + public void aliases() { + final Command main = new Command("main", "first", "second"); + var graph = Graph.builder(Word("main").from("main", "first", "second")).build(); + assertEqualsGraph(graph, main); + } + private static void assertEqualsGraph(Graph expected, Command command) { final Graph actual = Graph.fromCommand(command); assertTrue(expected.compare(actual, Graph.Comparator.TREE), () -> {