.
This commit is contained in:
parent
6a725005f0
commit
b4c1e242c8
9 changed files with 79 additions and 7 deletions
|
|
@ -13,4 +13,6 @@ public interface LiteralNode<T, S extends CommandContext<T, ?>> {
|
|||
@Nullable CommandNode<T, S> getLiteral(@NotNull String name);
|
||||
|
||||
@NotNull Set<CommandNode<T, S>> getLiterals();
|
||||
|
||||
@Nullable String getPermission();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ import de.kentoj.kencommandapi.api.structure.argument.ArgumentType;
|
|||
import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
||||
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
|
||||
import de.kentoj.kencommandapi.impl.processing.ParsedArgumentImpl;
|
||||
import de.kentoj.kencommandapi.impl.structure.argument.CommandArgumentImpl;
|
||||
import de.kentoj.kencommandapi.impl.structure.node.CommandNodeImpl;
|
||||
import de.kentoj.kencommandapi.impl.structure.CommandArgumentImpl;
|
||||
import de.kentoj.kencommandapi.impl.structure.CommandNodeImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
|
@ -24,6 +24,8 @@ public abstract class AbstractKenCommandApi<T, S extends CommandContext<T, ?>> i
|
|||
|
||||
public abstract void sendErrorMessage(T t, CommandException exception);
|
||||
|
||||
public abstract boolean hasPermission(T t, String permission);
|
||||
|
||||
@Override
|
||||
public CommandNode<T, S> createNode(String... name) {
|
||||
return new CommandNodeImpl<>(name);
|
||||
|
|
@ -39,7 +41,7 @@ public abstract class AbstractKenCommandApi<T, S extends CommandContext<T, ?>> i
|
|||
var iter = Arrays.stream(args).iterator();
|
||||
|
||||
try {
|
||||
var executableLiteral = processLiterals(node, iter);
|
||||
var executableLiteral = processLiterals(sender, node, iter);
|
||||
var parsedArguments = parseArguments(executableLiteral, iter);
|
||||
assert executableLiteral.getExecutor() != null;
|
||||
executableLiteral.getExecutor().execute(createContext(sender, parsedArguments));
|
||||
|
|
@ -48,7 +50,7 @@ public abstract class AbstractKenCommandApi<T, S extends CommandContext<T, ?>> i
|
|||
}
|
||||
}
|
||||
|
||||
private CommandNode<T, S> processLiterals(CommandNode<T, S> commandNode, Iterator<String> iter) {
|
||||
private CommandNode<T, S> processLiterals(T sender, CommandNode<T, S> commandNode, Iterator<String> iter) {
|
||||
if (!iter.hasNext()) {
|
||||
// literals/arguments are optional if the node itself is executable
|
||||
if (commandNode.getExecutor() == null) throw new CommandException("no literal given but required");
|
||||
|
|
@ -64,7 +66,10 @@ public abstract class AbstractKenCommandApi<T, S extends CommandContext<T, ?>> i
|
|||
if (literal == null) {
|
||||
throw new CommandException("Invalid literal: " + arg);
|
||||
}
|
||||
return processLiterals(literal, iter);
|
||||
if (literal.getPermission() != null && !hasPermission(sender, literal.getPermission())) {
|
||||
throw new CommandException("No permission");
|
||||
}
|
||||
return processLiterals(sender, literal, iter);
|
||||
}
|
||||
|
||||
private Map<String, ParsedArgument<T, S, ?>> parseArguments(CommandNode<T, S> commandNode, Iterator<String> iter) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
package de.kentoj.kencommandapi.impl.processing;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class TabCompleter<T, S extends CommandContext<T, ?>> {
|
||||
|
||||
private List<String> tabComplete(CommandNode<T, S> node, String[] args) {
|
||||
var lastNode = node;
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
var tmp = node.getLiteral(args[i]);
|
||||
if (tmp == null) {
|
||||
break;
|
||||
}
|
||||
lastNode = tmp;
|
||||
i++;
|
||||
}
|
||||
|
||||
return node.getLiterals().stream().map(CommandNode::getNames).flatMap(Arrays::stream).toList();
|
||||
}
|
||||
|
||||
private List<String> getLastNode(CommandNode<T, S> node, String[] args) {
|
||||
if (node.get)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package de.kentoj.kencommandapi.impl.structure.argument;
|
||||
package de.kentoj.kencommandapi.impl.structure;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.ArgumentType;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package de.kentoj.kencommandapi.impl.structure.node;
|
||||
package de.kentoj.kencommandapi.impl.structure;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.CommandExecutor;
|
||||
|
|
@ -29,6 +29,9 @@ public class CommandNodeImpl<T, S extends CommandContext<T, ?>> implements Comma
|
|||
@Setter
|
||||
private String description;
|
||||
|
||||
@Getter
|
||||
private @Nullable String permission = null;
|
||||
|
||||
public CommandNodeImpl(String[] names) {
|
||||
if (names.length == 0) throw new IllegalArgumentException("at least one name is required");
|
||||
this.names = names;
|
||||
Loading…
Add table
Add a link
Reference in a new issue