.
This commit is contained in:
parent
c8b7049f1d
commit
124e65a472
26 changed files with 123 additions and 116 deletions
|
|
@ -1,4 +1,4 @@
|
|||
As of v1.3.0, a command for getting/setting coins of a player could look like this:
|
||||
As of v2.0.1, a command for getting/setting coins of a player could look like this:
|
||||
```java
|
||||
public class CoinsCommand {
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ plugins {
|
|||
}
|
||||
|
||||
group = "de.kentoj"
|
||||
version = "1.3.0"
|
||||
version = "2.0.1"
|
||||
|
||||
fun scrowRepo(handler: RepositoryHandler) = handler.maven {
|
||||
name = "scrow"
|
||||
|
|
@ -38,7 +38,7 @@ dependencies {
|
|||
// https://mvnrepository.com/artifact/org.jetbrains/annotations
|
||||
implementation("org.jetbrains:annotations:26.0.2-1")
|
||||
|
||||
api("de.kentoj:kencommandapi-core:1.3.0")
|
||||
api("de.kentoj:kencommandapi-core:2.0.1")
|
||||
|
||||
api("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT")
|
||||
implementation("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT")
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
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;
|
||||
|
|
@ -8,8 +7,12 @@ import org.bukkit.entity.Player;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
public class BukkitCommandContext extends CommandContextImpl<CommandSender> {
|
||||
BukkitCommandContext(CommandSender sender, Map<String, ParsedArgument<CommandContext<CommandSender>, ?>> parsedArguments) {
|
||||
public class BukkitCommandContext extends CommandContextImpl<CommandSender, BukkitCommandContext> {
|
||||
|
||||
public BukkitCommandContext(
|
||||
CommandSender sender,
|
||||
Map<String, ParsedArgument<CommandSender, BukkitCommandContext, ?>> parsedArguments
|
||||
) {
|
||||
super(sender, parsedArguments);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package de.kentoj.kencommandapi;
|
||||
|
||||
import de.kentoj.kencommandapi.api.KenCommandApi;
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
|
@ -9,10 +8,13 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
public class BukkitKenCommand extends Command {
|
||||
|
||||
private final CommandNode<CommandContext<CommandSender>> rootNode;
|
||||
private final KenCommandApi<CommandSender, CommandContext<CommandSender>> commandManager;
|
||||
private final CommandNode<CommandSender, BukkitCommandContext> rootNode;
|
||||
private final KenCommandApi<CommandSender, BukkitCommandContext> commandManager;
|
||||
|
||||
protected BukkitKenCommand(KenCommandApi<CommandSender, CommandContext<CommandSender>> commandManager, CommandNode<CommandContext<CommandSender>> rootNode) {
|
||||
protected BukkitKenCommand(
|
||||
KenCommandApi<CommandSender, BukkitCommandContext> commandManager,
|
||||
CommandNode<CommandSender, BukkitCommandContext> rootNode
|
||||
) {
|
||||
super(rootNode.getNames()[0]);
|
||||
this.rootNode = rootNode;
|
||||
this.commandManager = commandManager;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package de.kentoj.kencommandapi;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
|
||||
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
|
||||
import de.kentoj.kencommandapi.impl.AbstractKenCommandApi;
|
||||
|
|
@ -11,14 +10,17 @@ import org.bukkit.command.SimpleCommandMap;
|
|||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
public class BukkitKenCommandApi extends AbstractKenCommandApi<CommandSender, CommandContext<CommandSender>> {
|
||||
public class BukkitKenCommandApi extends AbstractKenCommandApi<CommandSender, BukkitCommandContext> {
|
||||
|
||||
@Override
|
||||
public CommandContext<CommandSender> createContext(CommandSender sender, Map<String, ParsedArgument<CommandContext<CommandSender>, ?>> parsedArguments) {
|
||||
public BukkitCommandContext createContext(
|
||||
CommandSender sender,
|
||||
Map<String, ParsedArgument<CommandSender, BukkitCommandContext, ?>> parsedArguments
|
||||
) {
|
||||
return new BukkitCommandContext(sender, parsedArguments);
|
||||
}
|
||||
|
||||
public void register(CommandNode<CommandContext<CommandSender>> rootNode) {
|
||||
public void register(CommandNode<CommandSender, BukkitCommandContext> rootNode) {
|
||||
try {
|
||||
Field field = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
|
||||
field.setAccessible(true);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package de.kentoj.kencommandapi.type;
|
||||
|
||||
import de.kentoj.kencommandapi.BukkitCommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.ArgumentType;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.SuggestionProvider;
|
||||
|
|
@ -8,12 +9,13 @@ import lombok.Getter;
|
|||
import lombok.NoArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class OfflinePlayerArgumentType implements ArgumentType<OfflinePlayer> {
|
||||
public class OfflinePlayerArgumentType implements ArgumentType<CommandSender, BukkitCommandContext, OfflinePlayer> {
|
||||
|
||||
@Getter(lazy = true)
|
||||
private static final OfflinePlayerArgumentType instance = new OfflinePlayerArgumentType();
|
||||
|
|
@ -33,7 +35,7 @@ public class OfflinePlayerArgumentType implements ArgumentType<OfflinePlayer> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestionProvider<?> defaultSuggestionProvider() {
|
||||
return (SuggestionProvider<CommandContext<OfflinePlayer>>) context -> Set.of();
|
||||
public SuggestionProvider<CommandSender, BukkitCommandContext> defaultSuggestionProvider() {
|
||||
return context -> Set.of();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package de.kentoj.kencommandapi.type;
|
||||
|
||||
import de.kentoj.kencommandapi.BukkitCommandContext;
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.ArgumentType;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.SuggestionProvider;
|
||||
|
|
@ -7,12 +8,13 @@ import lombok.AccessLevel;
|
|||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class PlayerArgumentType implements ArgumentType<Player> {
|
||||
public class PlayerArgumentType implements ArgumentType<CommandSender, BukkitCommandContext, Player> {
|
||||
|
||||
@Getter(lazy = true)
|
||||
private static final PlayerArgumentType instance = new PlayerArgumentType();
|
||||
|
|
@ -25,7 +27,7 @@ public class PlayerArgumentType implements ArgumentType<Player> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestionProvider<?> defaultSuggestionProvider() {
|
||||
return (SuggestionProvider<CommandContext<Player>>) context -> Set.of();
|
||||
public SuggestionProvider<CommandSender, BukkitCommandContext> defaultSuggestionProvider() {
|
||||
return context -> Set.of();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ plugins {
|
|||
}
|
||||
|
||||
group = "de.kentoj"
|
||||
version = "1.3.0"
|
||||
version = "2.0.1"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package de.kentoj.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.AbstractKenCommandApi;
|
||||
|
|
@ -11,11 +10,23 @@ import java.util.Map;
|
|||
|
||||
public class Main {
|
||||
|
||||
private static class BasicCommandContext extends CommandContextImpl<PrintStream, BasicCommandContext> {
|
||||
public BasicCommandContext(PrintStream sender, Map<String, ParsedArgument<PrintStream, BasicCommandContext, ?>> parsedArguments) {
|
||||
super(sender, parsedArguments);
|
||||
}
|
||||
|
||||
public void respond(String msg) {
|
||||
getSender().println("|==================|");
|
||||
getSender().println(msg);
|
||||
getSender().println("|==================|");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
var api = new AbstractKenCommandApi<PrintStream, CommandContext<PrintStream>>() {
|
||||
var api = new AbstractKenCommandApi<PrintStream, BasicCommandContext>() {
|
||||
@Override
|
||||
public CommandContextImpl<PrintStream> createContext(PrintStream sender, Map<String, ParsedArgument<CommandContext<PrintStream>, ?>> parsedArguments) {
|
||||
return new CommandContextImpl<>(sender, parsedArguments);
|
||||
public BasicCommandContext createContext(PrintStream sender, Map<String, ParsedArgument<PrintStream, BasicCommandContext, ?>> parsedArguments) {
|
||||
return new BasicCommandContext(sender, parsedArguments);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -25,10 +36,10 @@ public class Main {
|
|||
addNode.setExecutor(ctx -> {
|
||||
int amount = ctx.getArg("amount");
|
||||
|
||||
ctx.getSender().println("adding " + amount + "$ to your balance");
|
||||
ctx.respond("adding " + amount + "$ to your balance");
|
||||
});
|
||||
|
||||
addNode.addArgument(api.createArgument("amount", IntegerArgumentType.getInstance()));
|
||||
addNode.addArgument(api.createArgument("amount", new IntegerArgumentType<>()));
|
||||
root.addLiteral(addNode);
|
||||
api.invoke(root, System.out, new String[]{"add", "100"});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ 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, S extends CommandContext<T>> {
|
||||
public interface KenCommandApi<T, S extends CommandContext<T, ?>> {
|
||||
|
||||
CommandNode<S> createNode(String... name);
|
||||
CommandNode<T, S> createNode(String... name);
|
||||
|
||||
<V> CommandArgument<S, V> createArgument(String id, ArgumentType<V> type);
|
||||
<V> CommandArgument<T, S, V> createArgument(String id, ArgumentType<T, S, V> type);
|
||||
|
||||
void invoke(CommandNode<S> node, T sender, String[] args);
|
||||
void invoke(CommandNode<T, S> node, T sender, String[] args);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package de.kentoj.kencommandapi.api.processing;
|
|||
|
||||
import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
||||
|
||||
public interface CommandContext<T> {
|
||||
public interface CommandContext<T, S extends CommandContext<T, ?>> {
|
||||
|
||||
T getSender();
|
||||
|
||||
|
|
@ -10,18 +10,18 @@ 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<CommandContext<T>, V> getParsedArgument(String name);
|
||||
<V> ParsedArgument<T, S, V> getParsedArgument(String name);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default <V> V getArg(String name) {
|
||||
return (V) getParsedArgument(name).getValue();
|
||||
}
|
||||
|
||||
default <V> V getArg(CommandArgument<CommandContext<T>, V> arg) {
|
||||
default <V> V getArg(CommandArgument<T, S, V> arg) {
|
||||
return getArg(arg.getId());
|
||||
}
|
||||
|
||||
default <V> ParsedArgument<CommandContext<T>, V> getParsedArgument(CommandArgument<CommandContext<T>, V> arg) {
|
||||
default <V> ParsedArgument<T, S, V> getParsedArgument(CommandArgument<T, S, V> arg) {
|
||||
return getParsedArgument(arg.getId());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package de.kentoj.kencommandapi.api.processing;
|
||||
|
||||
public interface CommandExecutor<S extends CommandContext<?>> {
|
||||
public interface CommandExecutor<T, S extends CommandContext<T, ?>> {
|
||||
|
||||
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<S extends CommandContext<?>, V> {
|
||||
public interface ParsedArgument<T, S extends CommandContext<T, ?>, V> {
|
||||
|
||||
V getValue();
|
||||
|
||||
CommandArgument<S, V> getArgument();
|
||||
CommandArgument<T, S, V> getArgument();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
package de.kentoj.kencommandapi.api.structure.argument;
|
||||
|
||||
public interface ArgumentType<V> {
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
|
||||
public interface ArgumentType<T, S extends CommandContext<T, ?>, V> {
|
||||
|
||||
V parseInput(String string);
|
||||
|
||||
SuggestionProvider<?> defaultSuggestionProvider();
|
||||
SuggestionProvider<T, S> defaultSuggestionProvider();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,20 +4,20 @@ import de.kentoj.kencommandapi.api.processing.CommandContext;
|
|||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface CommandArgument<S extends CommandContext<?>, V> {
|
||||
public interface CommandArgument<T, S extends CommandContext<T, ?>, V> {
|
||||
|
||||
String getId();
|
||||
|
||||
ArgumentType<V> getType();
|
||||
ArgumentType<T, S, V> getType();
|
||||
|
||||
void setDefaultValue(@Nullable V defaultValue);
|
||||
|
||||
@Nullable V getDefaultValue();
|
||||
|
||||
void setSuggestionProvider(@Nullable SuggestionProvider<S> suggestionProvider);
|
||||
void setSuggestionProvider(@Nullable SuggestionProvider<T, S> suggestionProvider);
|
||||
|
||||
/**
|
||||
* @return the explicitly set SuggestionProvider or the default suggestions for the type if unset/null
|
||||
*/
|
||||
@NotNull SuggestionProvider<S> getSuggestionProvider();
|
||||
@NotNull SuggestionProvider<T, S> getSuggestionProvider();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import de.kentoj.kencommandapi.api.processing.CommandContext;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
public interface SuggestionProvider<S extends CommandContext<?>> {
|
||||
public interface SuggestionProvider<T, S extends CommandContext<T, ?>> {
|
||||
|
||||
Set<String> suggest(S context);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,24 +2,13 @@ package de.kentoj.kencommandapi.api.structure.argument.suggestion;
|
|||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.SuggestionProvider;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.NONE)
|
||||
public class NoSuggestionProvider implements SuggestionProvider<CommandContext<Object>> {
|
||||
|
||||
@Getter(value = AccessLevel.PRIVATE, lazy = true)
|
||||
private static final SuggestionProvider<?> instance = new NoSuggestionProvider();
|
||||
|
||||
public static SuggestionProvider<?> create() {
|
||||
return getInstance();
|
||||
}
|
||||
public class NoSuggestionProvider<T, S extends CommandContext<T, ?>> implements SuggestionProvider<T, S> {
|
||||
|
||||
@Override
|
||||
public Set<String> suggest(CommandContext<Object> context) {
|
||||
public Set<String> suggest(S context) {
|
||||
return Set.of();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,11 @@
|
|||
package de.kentoj.kencommandapi.api.structure.argument.types;
|
||||
|
||||
import de.kentoj.kencommandapi.api.processing.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.ArgumentType;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.SuggestionProvider;
|
||||
import de.kentoj.kencommandapi.api.structure.argument.suggestion.NoSuggestionProvider;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class IntegerArgumentType implements ArgumentType<Integer> {
|
||||
|
||||
@Getter(lazy = true)
|
||||
private static final IntegerArgumentType instance = new IntegerArgumentType();
|
||||
public class IntegerArgumentType<T, S extends CommandContext<T, ?>> implements ArgumentType<T, S, Integer> {
|
||||
|
||||
@Override
|
||||
public Integer parseInput(String string) {
|
||||
|
|
@ -19,7 +13,7 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SuggestionProvider<?> defaultSuggestionProvider() {
|
||||
return NoSuggestionProvider.create();
|
||||
public SuggestionProvider<T, S> defaultSuggestionProvider() {
|
||||
return new NoSuggestionProvider<>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
public interface ArgumentNode<S extends CommandContext<?>> {
|
||||
public interface ArgumentNode<T, S extends CommandContext<T, ?>> {
|
||||
|
||||
<V> void addArgument(CommandArgument<S, V> argument);
|
||||
<V> void addArgument(CommandArgument<T, S, V> argument);
|
||||
|
||||
Set<CommandArgument<S, ?>> getArguments();
|
||||
Set<CommandArgument<T, S, ?>> getArguments();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,13 +8,13 @@ import org.jetbrains.annotations.Nullable;
|
|||
/**
|
||||
* @param <S> type of command context
|
||||
*/
|
||||
public interface CommandNode<S extends CommandContext<?>> extends ArgumentNode<S>, LiteralNode<S> {
|
||||
public interface CommandNode<T, S extends CommandContext<T, ?>> extends ArgumentNode<T, S>, LiteralNode<T, S> {
|
||||
|
||||
@NotNull String[] getNames();
|
||||
|
||||
void setExecutor(@Nullable CommandExecutor<S> executor);
|
||||
void setExecutor(@Nullable CommandExecutor<T, S> executor);
|
||||
|
||||
@Nullable CommandExecutor<S> getExecutor();
|
||||
@Nullable CommandExecutor<T, S> getExecutor();
|
||||
|
||||
void setDescription(@Nullable String description);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ import org.jetbrains.annotations.Nullable;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
public interface LiteralNode<S extends CommandContext<?>> {
|
||||
public interface LiteralNode<T, S extends CommandContext<T, ?>> {
|
||||
|
||||
void addLiteral(@NotNull CommandNode<S> commandNode);
|
||||
void addLiteral(@NotNull CommandNode<T, S> commandNode);
|
||||
|
||||
@Nullable CommandNode<S> getLiteral(@NotNull String name);
|
||||
@Nullable CommandNode<T, S> getLiteral(@NotNull String name);
|
||||
|
||||
@NotNull Set<CommandNode<S>> getLiterals();
|
||||
@NotNull Set<CommandNode<T, S>> getLiterals();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,30 +17,31 @@ import java.util.Iterator;
|
|||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public abstract class AbstractKenCommandApi<T, S extends CommandContext<T>> implements KenCommandApi<T, S> {
|
||||
public abstract class AbstractKenCommandApi<T, S extends CommandContext<T, ?>> implements KenCommandApi<T, S> {
|
||||
|
||||
public abstract S createContext(T sender, Map<String, ParsedArgument<S, ?>> parsedArguments);
|
||||
public abstract S createContext(T sender, Map<String, ParsedArgument<T, S, ?>> parsedArguments);
|
||||
|
||||
@Override
|
||||
public CommandNode<S> createNode(String... name) {
|
||||
public CommandNode<T, S> createNode(String... name) {
|
||||
return new CommandNodeImpl<>(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> CommandArgument<S, V> createArgument(String id, ArgumentType<V> type) {
|
||||
public <V> CommandArgument<T, S, V> createArgument(String id, ArgumentType<T, S, V> type) {
|
||||
return new CommandArgumentImpl<>(id, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(CommandNode<S> node, T sender, String[] args) {
|
||||
public void invoke(CommandNode<T, S> node, T sender, String[] args) {
|
||||
var iter = Arrays.stream(args).iterator();
|
||||
|
||||
var executableLiteral = processLiterals(node, iter);
|
||||
var parsedArguments = parseArguments(executableLiteral, iter);
|
||||
assert executableLiteral.getExecutor() != null;
|
||||
executableLiteral.getExecutor().execute(createContext(sender, parsedArguments));
|
||||
}
|
||||
|
||||
private CommandNode<S> processLiterals(CommandNode<S> commandNode, Iterator<String> iter) {
|
||||
private CommandNode<T, S> processLiterals(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 RuntimeException("no literal given but required");
|
||||
|
|
@ -59,11 +60,11 @@ public abstract class AbstractKenCommandApi<T, S extends CommandContext<T>> impl
|
|||
return processLiterals(literal, iter);
|
||||
}
|
||||
|
||||
private Map<String, ParsedArgument<S, ?>> parseArguments(CommandNode<S> commandNode, Iterator<String> iter) {
|
||||
Map<String, ParsedArgument<S, ?>> parsedArguments = new HashMap<>();
|
||||
private Map<String, ParsedArgument<T, S, ?>> parseArguments(CommandNode<T, S> commandNode, Iterator<String> iter) {
|
||||
Map<String, ParsedArgument<T, S, ?>> parsedArguments = new HashMap<>();
|
||||
for (var argument : commandNode.getArguments()) {
|
||||
@SuppressWarnings("unchecked")
|
||||
var arg = ((CommandArgument<S, Object>) argument);
|
||||
var arg = ((CommandArgument<T, S, Object>) argument);
|
||||
|
||||
if (!iter.hasNext()) {
|
||||
var defaultValue = arg.getDefaultValue();
|
||||
|
|
|
|||
|
|
@ -8,17 +8,17 @@ import lombok.RequiredArgsConstructor;
|
|||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CommandContextImpl<T> implements CommandContext<T> {
|
||||
public class CommandContextImpl<T, S extends CommandContext<T, ?>> implements CommandContext<T, S> {
|
||||
@Getter
|
||||
private final T sender;
|
||||
@Getter
|
||||
private final Map<String, ParsedArgument<CommandContext<T>, ?>> parsedArguments;
|
||||
private final Map<String, ParsedArgument<T, S, ?>> parsedArguments;
|
||||
|
||||
@Override
|
||||
public <V> ParsedArgument<CommandContext<T>, V> getParsedArgument(String name) {
|
||||
public <V> ParsedArgument<T, S, V> getParsedArgument(String name) {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
return (ParsedArgument<CommandContext<T>, V>) parsedArguments.get(name);
|
||||
return (ParsedArgument<T, S, V>) parsedArguments.get(name);
|
||||
} catch (ClassCastException e) {
|
||||
throw new RuntimeException("requested argument with wrong type", e);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ 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;
|
||||
import lombok.Value;
|
||||
|
||||
@Data
|
||||
public class ParsedArgumentImpl<S extends CommandContext<?>, V> implements ParsedArgument<S, V> {
|
||||
final V value;
|
||||
final CommandArgument<S, V> argument;
|
||||
@Value
|
||||
public class ParsedArgumentImpl<T, S extends CommandContext<T, ?>, V> implements ParsedArgument<T, S, V> {
|
||||
V value;
|
||||
CommandArgument<T, S, V> argument;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ import lombok.Setter;
|
|||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CommandArgumentImpl<S extends CommandContext<?>, V> implements CommandArgument<S, V> {
|
||||
public class CommandArgumentImpl<T, S extends CommandContext<T, ?>, V> implements CommandArgument<T, S, V> {
|
||||
|
||||
@Getter
|
||||
private final String id;
|
||||
@Getter
|
||||
private final ArgumentType<V> type;
|
||||
private final ArgumentType<T, S, V> type;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
|
|
@ -23,5 +23,5 @@ public class CommandArgumentImpl<S extends CommandContext<?>, V> implements Comm
|
|||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable SuggestionProvider<S> suggestionProvider;
|
||||
private @Nullable SuggestionProvider<T, S> suggestionProvider;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,31 +12,30 @@ import org.jetbrains.annotations.Nullable;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommandNodeImpl<S extends CommandContext<?>> implements CommandNode<S> {
|
||||
public class CommandNodeImpl<T, S extends CommandContext<T, ?>> implements CommandNode<T, S> {
|
||||
|
||||
@Getter
|
||||
private final String[] names;
|
||||
@Getter
|
||||
private final Set<CommandNode<T, S>> literals = new HashSet<>();
|
||||
@Getter
|
||||
private final Set<CommandArgument<T, S, ?>> arguments = new HashSet<>();
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable CommandExecutor<T, S> executor;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String description;
|
||||
|
||||
public CommandNodeImpl(String[] names) {
|
||||
if (names.length == 0) throw new IllegalArgumentException("at least one name is required");
|
||||
this.names = names;
|
||||
}
|
||||
|
||||
@Getter
|
||||
private final Set<CommandNode<S>> literals = new HashSet<>();
|
||||
@Getter
|
||||
private final Set<CommandArgument<S, ?>> arguments = new HashSet<>();
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable CommandExecutor<S> executor;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String description;
|
||||
|
||||
@Override
|
||||
public void addLiteral(@NotNull CommandNode<S> commandNode) {
|
||||
public void addLiteral(@NotNull CommandNode<T, S> commandNode) {
|
||||
for (String name : commandNode.getNames()) {
|
||||
if (getLiteral(name) != null) throw new IllegalArgumentException("literal with same name is already added");
|
||||
}
|
||||
|
|
@ -44,7 +43,7 @@ public class CommandNodeImpl<S extends CommandContext<?>> implements CommandNode
|
|||
}
|
||||
|
||||
@Override
|
||||
public CommandNode<S> getLiteral(@NotNull String name) {
|
||||
public CommandNode<T, S> getLiteral(@NotNull String name) {
|
||||
for (var literal : literals) {
|
||||
for (String literalName : literal.getNames()) {
|
||||
if (literalName.equalsIgnoreCase(name)) return literal;
|
||||
|
|
@ -54,7 +53,7 @@ public class CommandNodeImpl<S extends CommandContext<?>> implements CommandNode
|
|||
}
|
||||
|
||||
@Override
|
||||
public <V> void addArgument(CommandArgument<S, V> argument) {
|
||||
public <V> void addArgument(CommandArgument<T, S, V> argument) {
|
||||
if (arguments.stream()
|
||||
.anyMatch(arg -> arg.getId().equals(argument.getId()))
|
||||
) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue