mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-07 00:48:28 +01:00
Include command aliases in graph (#1244)
This commit is contained in:
parent
07a0fb129c
commit
d596992c0e
@ -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<String> 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 -> {
|
||||
|
@ -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);
|
||||
|
@ -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), () -> {
|
||||
|
Loading…
Reference in New Issue
Block a user