.
This commit is contained in:
parent
7bfbe48729
commit
0078822da1
18 changed files with 106 additions and 61 deletions
|
|
@ -0,0 +1,19 @@
|
|||
package de.kentoj.kencommandapi;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
|
||||
import de.kentoj.kencommandapi.impl.processing.CommandContextImpl;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class BukkitCommandContext extends CommandContextImpl<CommandSender> {
|
||||
BukkitCommandContext(CommandSender sender, Map<String, ParsedArgument<CommandContext<CommandSender>, ?>> parsedArguments) {
|
||||
super(sender, parsedArguments);
|
||||
}
|
||||
|
||||
public Player getPlayerSender() {
|
||||
return ((Player) getSender());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,16 @@
|
|||
package de.kentoj.kencommandapi;
|
||||
|
||||
import de.kentoj.kencommandapi.api.KenCommandApi;
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
|
||||
import de.kentoj.kencommandapi.impl.AbstractKenCommandApi;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class BukkitKenCommandApi extends KenCommandApiImpl<> {
|
||||
import java.util.Map;
|
||||
|
||||
private final KenCommandApi<>
|
||||
public class BukkitKenCommandApi extends AbstractKenCommandApi<CommandSender, CommandContext<CommandSender>> {
|
||||
|
||||
@Override
|
||||
public CommandContext<CommandSender> createContext(CommandSender sender, Map<String, ParsedArgument<CommandContext<CommandSender>, ?>> parsedArguments) {
|
||||
return new BukkitCommandContext(sender, parsedArguments);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,23 @@
|
|||
package de.kentoj.kencommandapi;
|
||||
|
||||
import de.kentoj.kencommandapi.api.KenCommandApi;
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.types.IntegerArgumentType;
|
||||
import de.kentoj.kencommandapi.impl.KenCommandApiImpl;
|
||||
import de.kentoj.kencommandapi.impl.AbstractKenCommandApi;
|
||||
import de.kentoj.kencommandapi.impl.processing.CommandContextImpl;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
KenCommandApi<PrintStream> api = new KenCommandApiImpl<>();
|
||||
var api = new AbstractKenCommandApi<PrintStream, CommandContext<PrintStream>>() {
|
||||
@Override
|
||||
public CommandContextImpl<PrintStream> createContext(PrintStream sender, Map<String, ParsedArgument<CommandContext<PrintStream>, ?>> parsedArguments) {
|
||||
return new CommandContextImpl<>(sender, parsedArguments);
|
||||
}
|
||||
};
|
||||
|
||||
var root = api.createNode("coins");
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
package de.kentoj.kencommandapi.api;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.ArgumentType;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
||||
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
|
||||
|
||||
public interface KenCommandApi<T> {
|
||||
public interface KenCommandApi<T, S extends CommandContext<T>> {
|
||||
|
||||
CommandNode<T> createNode(String... name);
|
||||
CommandNode<S> createNode(String... name);
|
||||
|
||||
<V> CommandArgument<T, V> createArgument(String id, ArgumentType<V> type);
|
||||
<V> CommandArgument<S, V> createArgument(String id, ArgumentType<V> type);
|
||||
|
||||
void invoke(CommandNode<T> node, T sender, String[] args);
|
||||
void invoke(CommandNode<S> node, T sender, String[] args);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ public interface CommandContext<T> {
|
|||
* @return Object that is parsed from the raw argument
|
||||
* @throws RuntimeException if no value given and no default value supplied
|
||||
*/
|
||||
<V> ParsedArgument<T, V> getParsedArgument(String name);
|
||||
<V> ParsedArgument<CommandContext<T>, V> getParsedArgument(String name);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default <V> V getArg(String name) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package de.kentoj.kencommandapi.api.processing;
|
||||
|
||||
public interface CommandExecutor<S> {
|
||||
public interface CommandExecutor<S extends CommandContext<?>> {
|
||||
|
||||
void execute(CommandContext<T> ctx);
|
||||
void execute(S ctx);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ package de.kentoj.kencommandapi.api.processing;
|
|||
|
||||
import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
||||
|
||||
public interface ParsedArgument<T, V> {
|
||||
public interface ParsedArgument<S extends CommandContext<?>, V> {
|
||||
|
||||
V getValue();
|
||||
|
||||
CommandArgument<T, V> getArgument();
|
||||
CommandArgument<S, V> getArgument();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,23 @@
|
|||
package de.kentoj.kencommandapi.api.structure.argument;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface CommandArgument<T, V> {
|
||||
public interface CommandArgument<S extends CommandContext<?>, V> {
|
||||
|
||||
String getId();
|
||||
|
||||
ArgumentType<V> getType();
|
||||
|
||||
void setDefaultValue(@Nullable T defaultValue);
|
||||
void setDefaultValue(@Nullable V defaultValue);
|
||||
|
||||
@Nullable T getDefaultValue();
|
||||
@Nullable V getDefaultValue();
|
||||
|
||||
void setSuggestionProvider(@Nullable SuggestionProvider<T> suggestionProvider);
|
||||
void setSuggestionProvider(@Nullable SuggestionProvider<S> suggestionProvider);
|
||||
|
||||
/**
|
||||
* @return the explicitly set SuggestionProvider or the default suggestions for the type if unset/null
|
||||
*/
|
||||
@NotNull SuggestionProvider<T> getSuggestionProvider();
|
||||
@NotNull SuggestionProvider<S> getSuggestionProvider();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import de.kentoj.kencommandapi.api.processing.CommandContext;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
public interface SuggestionProvider<T> {
|
||||
public interface SuggestionProvider<S extends CommandContext<?>> {
|
||||
|
||||
Set<String> suggest(CommandContext<T> context);
|
||||
Set<String> suggest(S context);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@ import lombok.NoArgsConstructor;
|
|||
import java.util.Set;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.NONE)
|
||||
public class NoSuggestionProvider implements SuggestionProvider<Object> {
|
||||
public class NoSuggestionProvider implements SuggestionProvider<CommandContext<Object>> {
|
||||
|
||||
@Getter(value = AccessLevel.PRIVATE, lazy = true)
|
||||
private static final SuggestionProvider<Object> instance = new NoSuggestionProvider();
|
||||
private static final SuggestionProvider<?> instance = new NoSuggestionProvider();
|
||||
|
||||
public static SuggestionProvider<Object> create() {
|
||||
public static SuggestionProvider<?> create() {
|
||||
return getInstance();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
package de.kentoj.kencommandapi.api.structure.node;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface ArgumentNode<T> {
|
||||
public interface ArgumentNode<S extends CommandContext<?>> {
|
||||
|
||||
<V> void addArgument(CommandArgument<T, V> argument);
|
||||
<V> void addArgument(CommandArgument<S, V> argument);
|
||||
|
||||
Set<CommandArgument<T, ?>> getArguments();
|
||||
Set<CommandArgument<S, ?>> getArguments();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,20 @@
|
|||
package de.kentoj.kencommandapi.api.structure.node;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.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> extends ArgumentNode<T>, LiteralNode<T> {
|
||||
public interface CommandNode<S extends CommandContext<?>> extends ArgumentNode<S>, LiteralNode<S> {
|
||||
|
||||
@NotNull String[] getNames();
|
||||
|
||||
void setExecutor(@Nullable CommandExecutor<T> executor);
|
||||
void setExecutor(@Nullable CommandExecutor<S> executor);
|
||||
|
||||
@Nullable CommandExecutor<T> getExecutor();
|
||||
@Nullable CommandExecutor<S> getExecutor();
|
||||
|
||||
void setDescription(@Nullable String description);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,16 @@
|
|||
package de.kentoj.kencommandapi.api.structure.node;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface LiteralNode<T> {
|
||||
public interface LiteralNode<S extends CommandContext<?>> {
|
||||
|
||||
void addLiteral(@NotNull CommandNode<T> commandNode);
|
||||
void addLiteral(@NotNull CommandNode<S> commandNode);
|
||||
|
||||
@Nullable CommandNode<T> getLiteral(@NotNull String name);
|
||||
@Nullable CommandNode<S> getLiteral(@NotNull String name);
|
||||
|
||||
@NotNull Set<CommandNode<T>> getLiterals();
|
||||
@NotNull Set<CommandNode<S>> getLiterals();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
package de.kentoj.kencommandapi.impl;
|
||||
|
||||
import de.kentoj.kencommandapi.api.KenCommandApi;
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
|
||||
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.CommandContextImpl;
|
||||
import de.kentoj.kencommandapi.impl.processing.ParsedArgumentImpl;
|
||||
import de.kentoj.kencommandapi.impl.structure.argument.CommandArgumentImpl;
|
||||
import de.kentoj.kencommandapi.impl.structure.node.CommandNodeImpl;
|
||||
|
|
@ -17,28 +17,30 @@ import java.util.Iterator;
|
|||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class KenCommandApiImpl<T> implements KenCommandApi<T> {
|
||||
public abstract class AbstractKenCommandApi<T, S extends CommandContext<T>> implements KenCommandApi<T, S> {
|
||||
|
||||
public abstract S createContext(T sender, Map<String, ParsedArgument<S, ?>> parsedArguments);
|
||||
|
||||
@Override
|
||||
public CommandNode<T> createNode(String... name) {
|
||||
public CommandNode<S> createNode(String... name) {
|
||||
return new CommandNodeImpl<>(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> CommandArgument<T, V> createArgument(String id, ArgumentType<V> type) {
|
||||
public <V> CommandArgument<S, V> createArgument(String id, ArgumentType<V> type) {
|
||||
return new CommandArgumentImpl<>(id, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(CommandNode<T> node, T sender, String[] args) {
|
||||
public void invoke(CommandNode<S> node, T sender, String[] args) {
|
||||
var iter = Arrays.stream(args).iterator();
|
||||
|
||||
var executableLiteral = processLiterals(node, iter);
|
||||
var parsedArguments = parseArguments(executableLiteral, iter);
|
||||
executableLiteral.getExecutor().execute(new CommandContextImpl<>(sender, parsedArguments));
|
||||
executableLiteral.getExecutor().execute(createContext(sender, parsedArguments));
|
||||
}
|
||||
|
||||
private CommandNode<T> processLiterals(CommandNode<T> commandNode, Iterator<String> iter) {
|
||||
private CommandNode<S> processLiterals(CommandNode<S> 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");
|
||||
|
|
@ -57,11 +59,11 @@ public class KenCommandApiImpl<T> implements KenCommandApi<T> {
|
|||
return processLiterals(literal, iter);
|
||||
}
|
||||
|
||||
private Map<String, ParsedArgument<T, ?>> parseArguments(CommandNode<T> commandNode, Iterator<String> iter) {
|
||||
Map<String, ParsedArgument<T, ?>> parsedArguments = new HashMap<>();
|
||||
private Map<String, ParsedArgument<S, ?>> parseArguments(CommandNode<S> commandNode, Iterator<String> iter) {
|
||||
Map<String, ParsedArgument<S, ?>> parsedArguments = new HashMap<>();
|
||||
for (var argument : commandNode.getArguments()) {
|
||||
@SuppressWarnings("unchecked")
|
||||
var arg = ((CommandArgument<T, Object>) argument);
|
||||
var arg = ((CommandArgument<S, Object>) argument);
|
||||
|
||||
if (!iter.hasNext()) {
|
||||
var defaultValue = arg.getDefaultValue();
|
||||
|
|
@ -12,13 +12,13 @@ public class CommandContextImpl<T> implements CommandContext<T> {
|
|||
@Getter
|
||||
private final T sender;
|
||||
@Getter
|
||||
private final Map<String, ParsedArgument<T, ?>> parsedArguments;
|
||||
private final Map<String, ParsedArgument<CommandContext<T>, ?>> parsedArguments;
|
||||
|
||||
@Override
|
||||
public <V> ParsedArgument<T, V> getParsedArgument(String name) {
|
||||
public <V> ParsedArgument<CommandContext<T>, V> getParsedArgument(String name) {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
return (ParsedArgument<T, V>) parsedArguments.get(name);
|
||||
return (ParsedArgument<CommandContext<T>, V>) parsedArguments.get(name);
|
||||
} catch (ClassCastException e) {
|
||||
throw new RuntimeException("requested argument with wrong type", e);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
package de.kentoj.kencommandapi.impl.processing;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ParsedArgumentImpl<T, V> implements ParsedArgument<T, V> {
|
||||
public class ParsedArgumentImpl<S extends CommandContext<?>, V> implements ParsedArgument<S, V> {
|
||||
final V value;
|
||||
final CommandArgument<T, V> argument;
|
||||
final CommandArgument<S, V> argument;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package de.kentoj.kencommandapi.impl.structure.argument;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.ArgumentType;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.SuggestionProvider;
|
||||
|
|
@ -9,7 +10,7 @@ import lombok.Setter;
|
|||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CommandArgumentImpl<T, V> implements CommandArgument<T, V> {
|
||||
public class CommandArgumentImpl<S extends CommandContext<?>, V> implements CommandArgument<S, V> {
|
||||
|
||||
@Getter
|
||||
private final String id;
|
||||
|
|
@ -18,9 +19,9 @@ public class CommandArgumentImpl<T, V> implements CommandArgument<T, V> {
|
|||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable T defaultValue;
|
||||
private @Nullable V defaultValue;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable SuggestionProvider<T> suggestionProvider;
|
||||
private @Nullable SuggestionProvider<S> suggestionProvider;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package de.kentoj.kencommandapi.impl.structure.node;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.CommandExecutor;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
||||
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
|
||||
|
|
@ -11,7 +12,7 @@ import org.jetbrains.annotations.Nullable;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommandNodeImpl<T> implements CommandNode<T> {
|
||||
public class CommandNodeImpl<S extends CommandContext<?>> implements CommandNode<S> {
|
||||
|
||||
@Getter
|
||||
private final String[] names;
|
||||
|
|
@ -22,20 +23,20 @@ public class CommandNodeImpl<T> implements CommandNode<T> {
|
|||
}
|
||||
|
||||
@Getter
|
||||
private final Set<CommandNode<T>> literals = new HashSet<>();
|
||||
private final Set<CommandNode<S>> literals = new HashSet<>();
|
||||
@Getter
|
||||
private final Set<CommandArgument<T, ?>> arguments = new HashSet<>();
|
||||
private final Set<CommandArgument<S, ?>> arguments = new HashSet<>();
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable CommandExecutor<T> executor;
|
||||
private @Nullable CommandExecutor<S> executor;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String description;
|
||||
|
||||
@Override
|
||||
public void addLiteral(@NotNull CommandNode<T> commandNode) {
|
||||
public void addLiteral(@NotNull CommandNode<S> commandNode) {
|
||||
for (String name : commandNode.getNames()) {
|
||||
if (getLiteral(name) != null) throw new IllegalArgumentException("literal with same name is already added");
|
||||
}
|
||||
|
|
@ -43,7 +44,7 @@ public class CommandNodeImpl<T> implements CommandNode<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public CommandNode<T> getLiteral(@NotNull String name) {
|
||||
public CommandNode<S> getLiteral(@NotNull String name) {
|
||||
for (var literal : literals) {
|
||||
for (String literalName : literal.getNames()) {
|
||||
if (literalName.equalsIgnoreCase(name)) return literal;
|
||||
|
|
@ -53,7 +54,7 @@ public class CommandNodeImpl<T> implements CommandNode<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public <V> void addArgument(CommandArgument<T, V> argument) {
|
||||
public <V> void addArgument(CommandArgument<S, V> argument) {
|
||||
if (arguments.stream()
|
||||
.anyMatch(arg -> arg.getId().equals(argument.getId()))
|
||||
) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue