first commit
This commit is contained in:
parent
72da8e063b
commit
76784dba7e
20 changed files with 588 additions and 0 deletions
10
src/main/java/de/kencommand/api/parsing/ParsedArgument.java
Normal file
10
src/main/java/de/kencommand/api/parsing/ParsedArgument.java
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package de.kencommand.api.parsing;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.literal.CommandArgument;
|
||||
|
||||
public interface ParsedArgument<T> {
|
||||
|
||||
T getValue();
|
||||
|
||||
CommandArgument<T> getArgument();
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
*/
|
||||
<T> ParsedArgument<T> getArgData(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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package de.kencommand.api.processing;
|
||||
|
||||
public interface CommandExecutor<S> {
|
||||
|
||||
void execute(S ctx);
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
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();
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package de.kencommand.api.structure.argument;
|
||||
|
||||
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> {
|
||||
|
||||
String getId();
|
||||
|
||||
ArgumentType<V> getType();
|
||||
|
||||
void setDefaultValue(@Nullable T defaultValue);
|
||||
|
||||
@Nullable T getDefaultValue();
|
||||
|
||||
void setSuggestionProvider(@Nullable SuggestionProvider<T, S> suggestionProvider);
|
||||
|
||||
/**
|
||||
* @return the explicitly set SuggestionProvider or the default suggestions for the type if unset/null
|
||||
*/
|
||||
@NotNull SuggestionProvider<T, S> getSuggestionProvider();
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package de.kencommand.api.structure.argument;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface SuggestionProvider<T, S extends CommandContext<T>> {
|
||||
|
||||
Set<String> suggest(S context);
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
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>> {
|
||||
|
||||
<V> void addArgument(CommandArgument<T, S, V> argument);
|
||||
|
||||
Set<CommandArgument<T, S, Object>> getArguments();
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
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> {
|
||||
|
||||
@NotNull String[] getNames();
|
||||
|
||||
void setExecutor(@Nullable CommandExecutor<? extends S> executor);
|
||||
|
||||
@Nullable CommandExecutor<? extends S> getExecutor();
|
||||
|
||||
void setDescription(@Nullable String description);
|
||||
|
||||
@Nullable String getDescription();
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package de.kencommand.api.structure.node;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface LiteralNode<T, S extends CommandContext<T>> {
|
||||
|
||||
void addLiteral(@NotNull CommandNode<? extends T, ? extends S> commandNode);
|
||||
|
||||
CommandNode<? extends T, ? extends S> getLiteral(@NotNull String name);
|
||||
|
||||
@NotNull Set<CommandNode<? extends T, ? extends S>> getLiterals();
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package de.kencommand.impl.structure.argument;
|
||||
|
||||
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.argument.SuggestionProvider;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
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;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable T defaultValue;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private @Nullable SuggestionProvider<T, S> suggestionProvider;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package de.kencommand.impl.structure.node;
|
||||
|
||||
import de.kencommand.api.processing.CommandContext;
|
||||
import de.kencommand.api.structure.node.CommandNode;
|
||||
import de.kencommand.api.structure.node.LiteralNode;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class LiteralNodeImpl<T, S extends CommandContext<T>> implements LiteralNode<T, S> {
|
||||
|
||||
@Getter
|
||||
private final Set<CommandNode<? extends T, ? extends S>> literals = new HashSet<>();
|
||||
|
||||
@Override
|
||||
public void addLiteral(@NotNull CommandNode<? extends T, ? extends S> commandNode) {
|
||||
for (String name : commandNode.getNames()) {
|
||||
if (getLiteral(name) != null) throw new IllegalArgumentException("literal with same name is already added");
|
||||
}
|
||||
literals.add(commandNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandNode<? extends T, ? extends S> getLiteral(@NotNull String name) {
|
||||
for (var literal : literals) {
|
||||
for (String literalName : literal.getNames()) {
|
||||
if (literalName.equalsIgnoreCase(name)) return literal;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue