mirror of https://github.com/Minestom/Minestom.git
Simplify Node, move conversions to separate class
This commit is contained in:
parent
543df701d5
commit
01d14588e6
|
@ -159,6 +159,6 @@ public final class CommandManager {
|
|||
public @NotNull DeclareCommandsPacket createDeclareCommandsPacket(@NotNull Player player) {
|
||||
final NodeGraph nodeGraph = GraphBuilder.forPlayer(this.dispatcher.getCommands(), player);
|
||||
System.out.println(nodeGraph.exportGarphvizDot()); //TODO remove before merging
|
||||
return nodeGraph.createPacket();
|
||||
return GraphConverter.createPacket(nodeGraph);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package net.minestom.server.command;
|
||||
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.arguments.ArgumentLiteral;
|
||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
|
||||
final class GraphConverter {
|
||||
private GraphConverter() {
|
||||
//no instance
|
||||
}
|
||||
|
||||
public static DeclareCommandsPacket.Node getPacketNode(Node node) {
|
||||
final DeclareCommandsPacket.Node packetNode = new DeclareCommandsPacket.Node();
|
||||
packetNode.children = node.children().toIntArray();
|
||||
final Argument<?> arg = node.arg();
|
||||
DeclareCommandsPacket.NodeType type = arg == null ? DeclareCommandsPacket.NodeType.ROOT :
|
||||
arg instanceof ArgumentLiteral ? DeclareCommandsPacket.NodeType.LITERAL :
|
||||
DeclareCommandsPacket.NodeType.ARGUMENT;
|
||||
packetNode.flags = DeclareCommandsPacket.getFlag(type, node.executable(), node.redirectTarget() != null,
|
||||
type == DeclareCommandsPacket.NodeType.ARGUMENT && arg.hasSuggestion());
|
||||
packetNode.name = arg == null ? null : arg.getId();
|
||||
if (node.redirectTarget() != null) {
|
||||
packetNode.redirectedNode = node.redirectTarget().get();
|
||||
}
|
||||
if (type == DeclareCommandsPacket.NodeType.ARGUMENT) {
|
||||
packetNode.properties = arg.nodeProperties();
|
||||
packetNode.parser = arg.parser();
|
||||
if (arg.hasSuggestion()) {
|
||||
//noinspection ConstantConditions
|
||||
packetNode.suggestionsType = arg.suggestionType().getIdentifier();
|
||||
}
|
||||
}
|
||||
return packetNode;
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
public static DeclareCommandsPacket createPacket(NodeGraph graph) {
|
||||
return new DeclareCommandsPacket(graph.nodes().stream().map(GraphConverter::getPacketNode).toList(), graph.root().id());
|
||||
}
|
||||
}
|
|
@ -4,31 +4,27 @@ import it.unimi.dsi.fastutil.ints.IntArrayList;
|
|||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.arguments.ArgumentLiteral;
|
||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket.NodeType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
record Node(int id, IntList children, NodeType type, String name, boolean executable, Argument<?> arg,
|
||||
AtomicInteger redirectTarget) {
|
||||
record Node(int id, IntList children, boolean executable, Argument<?> arg, AtomicInteger redirectTarget) {
|
||||
|
||||
public static Node root(int id) {
|
||||
return new Node(id, new IntArrayList(), NodeType.ROOT, null, false, null, null);
|
||||
return new Node(id, new IntArrayList(), false, null, null);
|
||||
}
|
||||
|
||||
public static Node literal(int id, String name, boolean executable, @Nullable AtomicInteger redirectTarget) {
|
||||
return literal(id, name, executable, new ArgumentLiteral(name), redirectTarget);
|
||||
return literal(id, executable, new ArgumentLiteral(name), redirectTarget);
|
||||
}
|
||||
|
||||
public static Node literal(int id, String name, boolean executable, @NotNull Argument<?> backingArg, @Nullable AtomicInteger redirectTarget) {
|
||||
return new Node(id, new IntArrayList(), NodeType.LITERAL, name, executable, backingArg, redirectTarget);
|
||||
public static Node literal(int id, boolean executable, @NotNull Argument<?> backingArg, @Nullable AtomicInteger redirectTarget) {
|
||||
return new Node(id, new IntArrayList(), executable, backingArg, redirectTarget);
|
||||
}
|
||||
|
||||
public static Node argument(int id, Argument<?> argument, boolean executable, @Nullable AtomicInteger redirectTarget) {
|
||||
return new Node(id, new IntArrayList(), argument instanceof ArgumentLiteral ? NodeType.LITERAL :
|
||||
NodeType.ARGUMENT, argument.getId(), executable, argument, redirectTarget);
|
||||
return new Node(id, new IntArrayList(), executable, argument, redirectTarget);
|
||||
}
|
||||
|
||||
public void addChildren(Node ...nodes) {
|
||||
|
@ -42,26 +38,7 @@ record Node(int id, IntList children, NodeType type, String name, boolean execut
|
|||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return type == NodeType.ROOT;
|
||||
return arg == null;
|
||||
}
|
||||
|
||||
public DeclareCommandsPacket.Node getPacketNode() {
|
||||
final DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node();
|
||||
node.children = children.toIntArray();
|
||||
node.flags = DeclareCommandsPacket.getFlag(type, executable, redirectTarget != null,
|
||||
type == NodeType.ARGUMENT && arg.hasSuggestion());
|
||||
node.name = name;
|
||||
if (redirectTarget != null) {
|
||||
node.redirectedNode = redirectTarget.get();
|
||||
}
|
||||
if (type == NodeType.ARGUMENT) {
|
||||
node.properties = arg.nodeProperties();
|
||||
node.parser = arg.parser();
|
||||
if (arg.hasSuggestion()) {
|
||||
//noinspection ConstantConditions
|
||||
node.suggestionsType = arg.suggestionType().getIdentifier();
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package net.minestom.server.command;
|
||||
|
||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -23,11 +21,6 @@ record NodeGraph(List<Node> nodes, Node root) {
|
|||
return resolveId(node.redirectTarget().get());
|
||||
}
|
||||
|
||||
@Contract("-> new")
|
||||
public DeclareCommandsPacket createPacket() {
|
||||
return new DeclareCommandsPacket(nodes.stream().map(Node::getPacketNode).toList(), root.id());
|
||||
}
|
||||
|
||||
public String exportGarphvizDot() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
final char statementSeparator = ';';
|
||||
|
@ -65,6 +58,6 @@ record NodeGraph(List<Node> nodes, Node root) {
|
|||
}
|
||||
|
||||
private static String graphvizName(Node node) {
|
||||
return '"' + (node.isRoot() ? "root" : node.name()) + '"';
|
||||
return '"' + (node.isRoot() ? "root" : node.arg().getId()) + '"';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ public class NodeGraphTest {
|
|||
final Command foo = new Command("foo");
|
||||
foo.addSyntax(NodeGraphTest::dummyExecutor, ArgumentType.Integer("bar"));
|
||||
|
||||
final DeclareCommandsPacket packet = GraphBuilder.forServer(Set.of(foo)).createPacket();
|
||||
final DeclareCommandsPacket packet = GraphConverter.createPacket(GraphBuilder.forServer(Set.of(foo)));
|
||||
assertEquals(3, packet.nodes().size());
|
||||
final DeclareCommandsPacket.Node root = packet.nodes().get(packet.rootIndex());
|
||||
assertNotNull(root);
|
||||
|
|
Loading…
Reference in New Issue