mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-21 15:41:38 +01:00
Rename node executor
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
c885ca1d7c
commit
07a0fb129c
@ -38,12 +38,12 @@ sealed interface Graph permits GraphImpl {
|
||||
sealed interface Node permits GraphImpl.NodeImpl {
|
||||
@NotNull Argument<?> argument();
|
||||
|
||||
@UnknownNullability Executor executor();
|
||||
@UnknownNullability Execution execution();
|
||||
|
||||
@NotNull List<@NotNull Node> next();
|
||||
}
|
||||
|
||||
sealed interface Executor extends Predicate<CommandSender> permits GraphImpl.ExecutorImpl {
|
||||
sealed interface Execution extends Predicate<CommandSender> permits GraphImpl.ExecutionImpl {
|
||||
// TODO execute the node
|
||||
}
|
||||
|
||||
|
@ -32,9 +32,9 @@ final class GraphConverter {
|
||||
private static int[] append(Graph.Node graphNode, List<DeclareCommandsPacket.Node> to,
|
||||
List<Consumer<Integer>> rootRedirect, AtomicInteger id, @Nullable AtomicInteger redirect,
|
||||
List<Runnable> redirectSetters, @Nullable Player player) {
|
||||
final Graph.Executor executor = graphNode.executor();
|
||||
if (player != null && executor != null) {
|
||||
if (!executor.test(player)) return new int[0];
|
||||
final Graph.Execution execution = graphNode.execution();
|
||||
if (player != null && execution != null) {
|
||||
if (!execution.test(player)) return new int[0];
|
||||
}
|
||||
|
||||
final Argument<?> argument = graphNode.argument();
|
||||
|
@ -57,7 +57,7 @@ record GraphImpl(NodeImpl root) implements Graph {
|
||||
}
|
||||
}
|
||||
|
||||
record NodeImpl(Argument<?> argument, ExecutorImpl executor, List<Graph.Node> next) implements Graph.Node {
|
||||
record NodeImpl(Argument<?> argument, ExecutionImpl execution, List<Graph.Node> next) implements Graph.Node {
|
||||
static NodeImpl fromBuilder(BuilderImpl builder) {
|
||||
final List<BuilderImpl> children = builder.children;
|
||||
Node[] nodes = new NodeImpl[children.size()];
|
||||
@ -74,47 +74,47 @@ record GraphImpl(NodeImpl root) implements Graph {
|
||||
}
|
||||
}
|
||||
|
||||
record ExecutorImpl(Predicate<CommandSender> predicate) implements Graph.Executor {
|
||||
record ExecutionImpl(Predicate<CommandSender> predicate) implements Execution {
|
||||
@Override
|
||||
public boolean test(CommandSender commandSender) {
|
||||
return predicate.test(commandSender);
|
||||
}
|
||||
|
||||
static ExecutorImpl fromCommand(Command command) {
|
||||
static ExecutionImpl fromCommand(Command command) {
|
||||
final CommandCondition condition = command.getCondition();
|
||||
if (condition == null) return null;
|
||||
return new ExecutorImpl(commandSender -> condition.canUse(commandSender, null));
|
||||
return new ExecutionImpl(commandSender -> condition.canUse(commandSender, null));
|
||||
}
|
||||
|
||||
static ExecutorImpl fromSyntax(CommandSyntax syntax) {
|
||||
static ExecutionImpl fromSyntax(CommandSyntax syntax) {
|
||||
final CommandCondition condition = syntax.getCommandCondition();
|
||||
if (condition == null) return null;
|
||||
return new ExecutorImpl(commandSender -> condition.canUse(commandSender, null));
|
||||
return new ExecutionImpl(commandSender -> condition.canUse(commandSender, null));
|
||||
}
|
||||
}
|
||||
|
||||
private record ConversionNode(Argument<?> argument, ExecutorImpl executor,
|
||||
private record ConversionNode(Argument<?> argument, ExecutionImpl execution,
|
||||
Map<Argument<?>, ConversionNode> nextMap) {
|
||||
ConversionNode(Argument<?> argument, ExecutorImpl executor) {
|
||||
this(argument, executor, new LinkedHashMap<>());
|
||||
ConversionNode(Argument<?> argument, ExecutionImpl execution) {
|
||||
this(argument, execution, new LinkedHashMap<>());
|
||||
}
|
||||
|
||||
private NodeImpl toNode() {
|
||||
Node[] nodes = new NodeImpl[nextMap.size()];
|
||||
int i = 0;
|
||||
for (var entry : nextMap.values()) nodes[i++] = entry.toNode();
|
||||
return new NodeImpl(argument, executor, List.of(nodes));
|
||||
return new NodeImpl(argument, execution, List.of(nodes));
|
||||
}
|
||||
|
||||
static ConversionNode fromCommand(Command command) {
|
||||
ConversionNode root = new ConversionNode(Literal(command.getName()), ExecutorImpl.fromCommand(command));
|
||||
ConversionNode root = new ConversionNode(Literal(command.getName()), ExecutionImpl.fromCommand(command));
|
||||
// Syntaxes
|
||||
for (CommandSyntax syntax : command.getSyntaxes()) {
|
||||
ConversionNode syntaxNode = root;
|
||||
for (Argument<?> arg : syntax.getArguments()) {
|
||||
boolean last = arg == syntax.getArguments()[syntax.getArguments().length - 1];
|
||||
syntaxNode = syntaxNode.nextMap.computeIfAbsent(arg, argument -> {
|
||||
var ex = last ? ExecutorImpl.fromSyntax(syntax) : null;
|
||||
var ex = last ? ExecutionImpl.fromSyntax(syntax) : null;
|
||||
return new ConversionNode(argument, ex);
|
||||
});
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class GraphConversionExecutorTest {
|
||||
public void empty() {
|
||||
final Command foo = new Command("foo");
|
||||
var graph = Graph.fromCommand(foo);
|
||||
assertNull(graph.root().executor());
|
||||
assertNull(graph.root().execution());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -22,17 +22,17 @@ public class GraphConversionExecutorTest {
|
||||
{
|
||||
foo.setCondition((sender, commandString) -> true);
|
||||
var graph = Graph.fromCommand(foo);
|
||||
var executor = graph.root().executor();
|
||||
assertNotNull(executor);
|
||||
assertTrue(executor.test(null));
|
||||
var execution = graph.root().execution();
|
||||
assertNotNull(execution);
|
||||
assertTrue(execution.test(null));
|
||||
}
|
||||
// Constant false
|
||||
{
|
||||
foo.setCondition((sender, commandString) -> false);
|
||||
var graph = Graph.fromCommand(foo);
|
||||
var executor = graph.root().executor();
|
||||
assertNotNull(executor);
|
||||
assertFalse(executor.test(null));
|
||||
var execution = graph.root().execution();
|
||||
assertNotNull(execution);
|
||||
assertFalse(execution.test(null));
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,7 +43,7 @@ public class GraphConversionExecutorTest {
|
||||
|
||||
var graph = Graph.fromCommand(foo);
|
||||
assertEquals(1, graph.root().next().size());
|
||||
assertNull(graph.root().next().get(0).executor());
|
||||
assertNull(graph.root().next().get(0).execution());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -54,9 +54,9 @@ public class GraphConversionExecutorTest {
|
||||
|
||||
var graph = Graph.fromCommand(foo);
|
||||
assertEquals(1, graph.root().next().size());
|
||||
var executor = graph.root().next().get(0).executor();
|
||||
assertNotNull(executor);
|
||||
assertTrue(executor.test(null));
|
||||
var execution = graph.root().next().get(0).execution();
|
||||
assertNotNull(execution);
|
||||
assertTrue(execution.test(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -67,9 +67,9 @@ public class GraphConversionExecutorTest {
|
||||
|
||||
var graph = Graph.fromCommand(foo);
|
||||
assertEquals(1, graph.root().next().size());
|
||||
var executor = graph.root().next().get(0).executor();
|
||||
assertNotNull(executor);
|
||||
assertFalse(executor.test(null));
|
||||
var execution = graph.root().next().get(0).execution();
|
||||
assertNotNull(execution);
|
||||
assertFalse(execution.test(null));
|
||||
}
|
||||
|
||||
private static void dummyExecutor(CommandSender sender, CommandContext context) {
|
||||
|
Loading…
Reference in New Issue
Block a user