.
This commit is contained in:
parent
678762f797
commit
fc431bf8df
17 changed files with 160 additions and 114 deletions
|
|
@ -1,22 +0,0 @@
|
|||
package de.kencommand;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import de.kencommand.api.structure.argument.ArgumentType;
|
||||
import de.kencommand.api.structure.argument.CommandArgument;
|
||||
import de.kencommand.api.structure.node.CommandNode;
|
||||
import de.kencommand.impl.structure.argument.CommandArgumentImpl;
|
||||
import de.kencommand.impl.structure.node.CommandNodeImpl;
|
||||
|
||||
public class KenCommandAPI {
|
||||
|
||||
private KenCommandAPI() {
|
||||
}
|
||||
|
||||
public static <T, S extends CommandContext<T>> CommandNode<T, S> createCommandNode(String[] names) {
|
||||
return new CommandNodeImpl<T, S>(names);
|
||||
}
|
||||
|
||||
public static <T, S extends CommandContext<T>, V> CommandArgument<T, S, V> createArgument(String id, ArgumentType<V> type) {
|
||||
return new CommandArgumentImpl<>(id, type);
|
||||
}
|
||||
}
|
||||
17
src/main/java/de/kencommand/api/KenCommandApi.java
Normal file
17
src/main/java/de/kencommand/api/KenCommandApi.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package de.kencommand.api;
|
||||
|
||||
import de.kencommand.api.processing.CommandExecutor;
|
||||
import de.kencommand.api.processing.ParsedArgument;
|
||||
import de.kencommand.api.structure.node.CommandNode;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public interface KenCommandApi<T> {
|
||||
|
||||
void invoke(CommandNode<T> node, T sender, String[] args);
|
||||
|
||||
CommandExecutor<T> processLiterals(CommandNode<T> node, Iterator<String> argsIterator);
|
||||
|
||||
Map<String, ParsedArgument<T, ?>> parseArguments(CommandNode<T> node, Iterator<String> argsIterator);
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
package de.kencommand.api.parsing;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import de.kencommand.api.structure.argument.CommandArgument;
|
||||
|
||||
public interface ParsedArgument<T, S extends CommandContext<T>, V> {
|
||||
|
||||
V getValue();
|
||||
|
||||
CommandArgument<T, S, V> getArgument();
|
||||
}
|
||||
|
|
@ -1,34 +1,17 @@
|
|||
package de.kencommand.api.processing;
|
||||
|
||||
import de.kencommand.api.parsing.ParsedArgument;
|
||||
import de.kentoj.scrow.bukkit.command.exception.CommandInvocationException;
|
||||
import de.kentoj.scrow.bukkit.command.literal.CommandArgument;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public interface CommandContext<T> {
|
||||
|
||||
T getSender();
|
||||
|
||||
/**
|
||||
* @return Object that is parsed from the raw argument
|
||||
* @throws CommandInvocationException if no value given and no default value supplied
|
||||
* @throws RuntimeException if no value given and no default value supplied
|
||||
*/
|
||||
<T> ParsedArgument<T> getArgData(String name);
|
||||
<V> ParsedArgument<T, V> getParsedArgument(String name);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default <T> T getArgData(CommandArgument<T> arg) {
|
||||
return (T) getArgData(arg.getName());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default <T> T getArg(String name) {
|
||||
return (T) getArgData(name).getValue();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default <T> T getArg(CommandArgument<T> arg) {
|
||||
return (T) getArgData(arg.getName()).getValue();
|
||||
default <V> V getArg(String name) {
|
||||
return (V) getParsedArgument(name).getValue();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package de.kencommand.api.processing;
|
||||
|
||||
public interface CommandExecutor<S> {
|
||||
public interface CommandExecutor<T> {
|
||||
|
||||
void execute(S ctx);
|
||||
void execute(CommandContext<T> ctx);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
package de.kencommand.api.processing;
|
||||
|
||||
import de.kencommand.api.structure.argument.CommandArgument;
|
||||
|
||||
public interface ParsedArgument<T, V> {
|
||||
|
||||
V getValue();
|
||||
|
||||
CommandArgument<T, V> getArgument();
|
||||
}
|
||||
|
|
@ -1,10 +1,8 @@
|
|||
package de.kencommand.api.structure.argument;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
|
||||
public interface ArgumentType<V> {
|
||||
|
||||
V parseInput(String string);
|
||||
|
||||
SuggestionProvider<Object, CommandContext<Object>> defaultSuggestionProvider();
|
||||
SuggestionProvider<?> defaultSuggestionProvider();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import de.kencommand.api.processing.CommandContext;
|
|||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface CommandArgument<T, S extends CommandContext<T>, V> {
|
||||
public interface CommandArgument<T, V> {
|
||||
|
||||
String getId();
|
||||
|
||||
|
|
@ -14,10 +14,10 @@ public interface CommandArgument<T, S extends CommandContext<T>, V> {
|
|||
|
||||
@Nullable T getDefaultValue();
|
||||
|
||||
void setSuggestionProvider(@Nullable SuggestionProvider<T, S> suggestionProvider);
|
||||
void setSuggestionProvider(@Nullable SuggestionProvider<T> suggestionProvider);
|
||||
|
||||
/**
|
||||
* @return the explicitly set SuggestionProvider or the default suggestions for the type if unset/null
|
||||
*/
|
||||
@NotNull SuggestionProvider<T, S> getSuggestionProvider();
|
||||
@NotNull SuggestionProvider<T> getSuggestionProvider();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import de.kencommand.api.processing.CommandContext;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
public interface SuggestionProvider<T, S extends CommandContext<T>> {
|
||||
public interface SuggestionProvider<T> {
|
||||
|
||||
Set<String> suggest(S context);
|
||||
Set<String> suggest(CommandContext<T> context);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
package de.kencommand.api.structure.node;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import de.kencommand.api.structure.argument.CommandArgument;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface ArgumentNode<T, S extends CommandContext<T>> {
|
||||
public interface ArgumentNode<T> {
|
||||
|
||||
<V> void addArgument(CommandArgument<T, S, V> argument);
|
||||
<V> void addArgument(CommandArgument<T, V> argument);
|
||||
|
||||
Set<CommandArgument<T, S, ?>> getArguments();
|
||||
Set<CommandArgument<T, ?>> getArguments();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,19 @@
|
|||
package de.kencommand.api.structure.node;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import de.kencommand.api.processing.CommandExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @param <T> type of command sender
|
||||
* @param <S> type of command context
|
||||
*/
|
||||
public interface CommandNode<T, S extends CommandContext<T>> extends LiteralNode<T, S>, ArgumentNode<T, S> {
|
||||
public interface CommandNode<T> extends ArgumentNode<T>, LiteralNode<T> {
|
||||
|
||||
@NotNull String[] getNames();
|
||||
|
||||
void setExecutor(@Nullable CommandExecutor<? extends S> executor);
|
||||
void setExecutor(@Nullable CommandExecutor<T> executor);
|
||||
|
||||
@Nullable CommandExecutor<? extends S> getExecutor();
|
||||
@Nullable CommandExecutor<T> getExecutor();
|
||||
|
||||
void setDescription(@Nullable String description);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
package de.kencommand.api.structure.node;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface LiteralNode<T, S extends CommandContext<T>> {
|
||||
public interface LiteralNode<T> {
|
||||
|
||||
void addLiteral(@NotNull CommandNode<? extends T, ? extends S> commandNode);
|
||||
void addLiteral(@NotNull CommandNode<T> commandNode);
|
||||
|
||||
CommandNode<T, S> getLiteral(@NotNull String name);
|
||||
@Nullable CommandNode<T> getLiteral(@NotNull String name);
|
||||
|
||||
@NotNull Set<CommandNode<? extends T, ? extends S>> getLiterals();
|
||||
@NotNull Set<CommandNode<T>> getLiterals();
|
||||
}
|
||||
|
|
|
|||
66
src/main/java/de/kencommand/impl/KenCommandApiImpl.java
Normal file
66
src/main/java/de/kencommand/impl/KenCommandApiImpl.java
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package de.kencommand.impl;
|
||||
|
||||
import de.kencommand.api.KenCommandApi;
|
||||
import de.kencommand.api.processing.CommandExecutor;
|
||||
import de.kencommand.api.processing.ParsedArgument;
|
||||
import de.kencommand.api.structure.argument.CommandArgument;
|
||||
import de.kencommand.api.structure.node.CommandNode;
|
||||
import de.kencommand.impl.processing.CommandContextImpl;
|
||||
import de.kencommand.impl.processing.ParsedArgumentImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class KenCommandApiImpl<T> implements KenCommandApi<T> {
|
||||
|
||||
@Override
|
||||
public void invoke(CommandNode<T> node, T sender, String[] args) {
|
||||
var iter = Arrays.stream(args).iterator();
|
||||
|
||||
var executor = processLiterals(node, iter);
|
||||
var parsedArguments = parseArguments(node, iter);
|
||||
executor.execute(new CommandContextImpl<>(sender, parsedArguments));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandExecutor<T> processLiterals(CommandNode<T> commandNode, Iterator<String> iter) {
|
||||
if (!iter.hasNext()) {
|
||||
// literals/arguments are optional if the node itself is executable
|
||||
if (commandNode.getExecutor() == null) throw new RuntimeException("no literal given but required");
|
||||
// TODO parse arguments for executor ig, or rather do that in a separate method
|
||||
return commandNode.getExecutor();
|
||||
}
|
||||
|
||||
final var arg = iter.next();
|
||||
// TODO rethink
|
||||
final var literal = commandNode.getLiteral(arg);
|
||||
if (literal == null) {
|
||||
throw new RuntimeException("Invalid literal: " + arg);
|
||||
}
|
||||
return processLiterals(literal, iter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, ParsedArgument<T, ?>> parseArguments(CommandNode<T> commandNode, Iterator<String> iter) {
|
||||
Map<String, ParsedArgument<T, ?>> parsedArguments = new HashMap<>();
|
||||
for (CommandArgument<T, ?> argument : commandNode.getArguments()) {
|
||||
@SuppressWarnings("unchecked")
|
||||
var arg = ((CommandArgument<T, Object>) argument);
|
||||
|
||||
if (!iter.hasNext()) {
|
||||
var defaultValue = arg.getDefaultValue();
|
||||
if (defaultValue == null) throw new RuntimeException("no argument given but required");
|
||||
parsedArguments.put(arg.getId(), new ParsedArgumentImpl<>(defaultValue, arg));
|
||||
continue;
|
||||
}
|
||||
|
||||
var parsedArg = arg.getType().parseInput(iter.next());
|
||||
parsedArguments.put(arg.getId(), new ParsedArgumentImpl<>(parsedArg, arg));
|
||||
}
|
||||
return parsedArguments;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
package de.kencommand.impl.parsing;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import de.kencommand.api.processing.CommandExecutor;
|
||||
import de.kencommand.api.structure.node.CommandNode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CommandNodeParser<T, S extends CommandContext<T>> {
|
||||
|
||||
public CommandExecutor<? extends S> parse(CommandNode<T, S> commandNode, String[] args) {
|
||||
var iter = Arrays.stream(args).iterator();
|
||||
|
||||
while (true) {
|
||||
if (!iter.hasNext()) {
|
||||
// literals/arguments are optional if the node itself is executable
|
||||
if (commandNode.getExecutor() == null) throw new RuntimeException("no literal given but required");
|
||||
// TODO parse arguments for executor ig, or rather do that in a separate method
|
||||
return commandNode.getExecutor();
|
||||
}
|
||||
|
||||
final var arg = iter.next();
|
||||
// TODO rethink
|
||||
return parse(commandNode.getLiteral(arg), args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package de.kencommand.impl.processing;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import de.kencommand.api.processing.ParsedArgument;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CommandContextImpl<T> implements CommandContext<T> {
|
||||
@Getter
|
||||
private final T sender;
|
||||
@Getter
|
||||
private final Map<String, ParsedArgument<T, ?>> parsedArguments;
|
||||
|
||||
@Override
|
||||
public <V> ParsedArgument<T, V> getParsedArgument(String name) {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
return (ParsedArgument<T, V>) parsedArguments.get(name);
|
||||
} catch (ClassCastException e) {
|
||||
throw new RuntimeException("requested argument with wrong type", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package de.kencommand.impl.processing;
|
||||
|
||||
import de.kencommand.api.processing.ParsedArgument;
|
||||
import de.kencommand.api.structure.argument.CommandArgument;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ParsedArgumentImpl<T, V> implements ParsedArgument<T, V> {
|
||||
final V value;
|
||||
final CommandArgument<T, V> argument;
|
||||
}
|
||||
|
|
@ -14,26 +14,26 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CommandNodeImpl<T, S extends CommandContext<T>> implements CommandNode<T, S> {
|
||||
public class CommandNodeImpl<T> implements CommandNode<T> {
|
||||
|
||||
@Getter
|
||||
private final String[] names;
|
||||
|
||||
@Getter
|
||||
private final Set<CommandNode<? extends T, ? extends S>> literals = new HashSet<>();
|
||||
private final Set<CommandNode<T>> literals = new HashSet<>();
|
||||
@Getter
|
||||
private final Set<CommandArgument<T, S, ?>> arguments = new HashSet<>();
|
||||
private final Set<CommandArgument<T, ?>> arguments = new HashSet<>();
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable CommandExecutor<? extends S> executor;
|
||||
private @Nullable CommandExecutor<T> executor;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String description;
|
||||
|
||||
@Override
|
||||
public void addLiteral(@NotNull CommandNode<? extends T, ? extends S> commandNode) {
|
||||
public void addLiteral(@NotNull CommandNode<T> commandNode) {
|
||||
for (String name : commandNode.getNames()) {
|
||||
if (getLiteral(name) != null) throw new IllegalArgumentException("literal with same name is already added");
|
||||
}
|
||||
|
|
@ -41,7 +41,7 @@ public class CommandNodeImpl<T, S extends CommandContext<T>> implements CommandN
|
|||
}
|
||||
|
||||
@Override
|
||||
public CommandNode<? extends T, ? extends S> getLiteral(@NotNull String name) {
|
||||
public CommandNode<T> getLiteral(@NotNull String name) {
|
||||
for (var literal : literals) {
|
||||
for (String literalName : literal.getNames()) {
|
||||
if (literalName.equalsIgnoreCase(name)) return literal;
|
||||
|
|
@ -51,7 +51,7 @@ public class CommandNodeImpl<T, S extends CommandContext<T>> implements CommandN
|
|||
}
|
||||
|
||||
@Override
|
||||
public <V> void addArgument(CommandArgument<T, S, V> argument) {
|
||||
public <V> void addArgument(CommandArgument<T, V> argument) {
|
||||
if (arguments.stream()
|
||||
.anyMatch(arg -> arg.getId().equals(argument.getId()))
|
||||
) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue