mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 01:17:47 +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 java.util.function.Predicate;
|
||||||
|
|
||||||
import static net.minestom.server.command.builder.arguments.ArgumentType.Literal;
|
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 {
|
record GraphImpl(NodeImpl root) implements Graph {
|
||||||
static GraphImpl fromCommand(Command command) {
|
static GraphImpl fromCommand(Command command) {
|
||||||
@ -107,7 +108,7 @@ record GraphImpl(NodeImpl root) implements Graph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static ConversionNode fromCommand(Command command) {
|
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
|
// Syntaxes
|
||||||
for (CommandSyntax syntax : command.getSyntaxes()) {
|
for (CommandSyntax syntax : command.getSyntaxes()) {
|
||||||
ConversionNode syntaxNode = root;
|
ConversionNode syntaxNode = root;
|
||||||
@ -121,7 +122,7 @@ record GraphImpl(NodeImpl root) implements Graph {
|
|||||||
}
|
}
|
||||||
// Subcommands
|
// Subcommands
|
||||||
for (Command subcommand : command.getSubcommands()) {
|
for (Command subcommand : command.getSubcommands()) {
|
||||||
root.nextMap.put(Literal(subcommand.getName()), fromCommand(subcommand));
|
root.nextMap.put(commandToArgument(subcommand), fromCommand(subcommand));
|
||||||
}
|
}
|
||||||
return root;
|
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) {
|
static boolean compare(@NotNull Node first, Node second, @NotNull Comparator comparator) {
|
||||||
return switch (comparator) {
|
return switch (comparator) {
|
||||||
case TREE -> {
|
case TREE -> {
|
||||||
|
@ -169,6 +169,28 @@ public class CommandPacketTest {
|
|||||||
""", graph);
|
""", 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) {
|
static void assertPacketGraph(String expected, Graph... graphs) {
|
||||||
var packet = GraphConverter.createPacket(Graph.merge(graphs), null);
|
var packet = GraphConverter.createPacket(Graph.merge(graphs), null);
|
||||||
CommandTestUtils.assertPacket(packet, expected);
|
CommandTestUtils.assertPacket(packet, expected);
|
||||||
|
@ -101,6 +101,20 @@ public class GraphConversionTest {
|
|||||||
assertEqualsGraph(graph, main);
|
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) {
|
private static void assertEqualsGraph(Graph expected, Command command) {
|
||||||
final Graph actual = Graph.fromCommand(command);
|
final Graph actual = Graph.fromCommand(command);
|
||||||
assertTrue(expected.compare(actual, Graph.Comparator.TREE), () -> {
|
assertTrue(expected.compare(actual, Graph.Comparator.TREE), () -> {
|
||||||
|
Loading…
Reference in New Issue
Block a user