diff options
Diffstat (limited to 'dicore3/command/src/main/java/io/dico/dicore/command')
77 files changed, 5574 insertions, 5407 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/ChildCommandAddress.java b/dicore3/command/src/main/java/io/dico/dicore/command/ChildCommandAddress.java index 9a26f61..72bc303 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/ChildCommandAddress.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/ChildCommandAddress.java @@ -1,120 +1,120 @@ -package io.dico.dicore.command; - -import io.dico.dicore.command.predef.DefaultGroupCommand; -import io.dico.dicore.command.predef.HelpCommand; - -import java.util.*; - -public class ChildCommandAddress extends ModifiableCommandAddress { - ModifiableCommandAddress parent; - final List<String> namesModifiable = new ArrayList<>(4); - List<String> names = namesModifiable; - Command command; - boolean isCommandTrailing; - - public ChildCommandAddress() { - } - - public ChildCommandAddress(Command command) { - this.command = command; - } - - public ChildCommandAddress(Command command, String name, String... aliases) { - this(command); - addNameAndAliases(name, aliases); - } - - public static ChildCommandAddress newPlaceHolderCommand(String name, String... aliases) { - ChildCommandAddress rv = new ChildCommandAddress(); - rv.setupAsPlaceholder(name, aliases); - return rv; - } - - public void setupAsPlaceholder(String name, String... aliases) { - if (!hasCommand()) { - setCommand(DefaultGroupCommand.getInstance()); - } - - addNameAndAliases(name, aliases); - HelpCommand.registerAsChild(this); - } - - @Override - public boolean isRoot() { - return false; - } - - @Override - public ModifiableCommandAddress getParent() { - return parent; - } - - @Override - public Command getCommand() { - return command; - } - - @Override - public void setCommand(Command command) { - if (hasUserDeclaredCommand()) { - throw new IllegalStateException("Command is already set at address \"" + getAddress() + "\""); - } - this.command = command; - } - - @Override - public List<String> getNames() { - return names; - } - - public void addNameAndAliases(String name, String... aliases) { - names.add(name); - names.addAll(Arrays.asList(aliases)); - } - - @Override - public String getMainKey() { - return namesModifiable.isEmpty() ? null : namesModifiable.get(0); - } - - @Override - public String getAddress() { - ICommandAddress address = this; - int depth = getDepth(); - String[] keys = new String[depth]; - for (int i = depth - 1; i >= 0; i--) { - keys[i] = address.getMainKey(); - address = address.getParent(); - } - return String.join(" ", keys); - } - - public void finalizeNames() { - if (names == namesModifiable) { - names = Collections.unmodifiableList(namesModifiable); - } - } - - Iterator<String> modifiableNamesIterator() { - return namesModifiable.iterator(); - } - - void setParent(ModifiableCommandAddress parent) { - finalizeNames(); - this.parent = parent; - } - - @Override - public boolean isCommandTrailing() { - return isCommandTrailing; - } - - @Override - public void setCommandTrailing(boolean trailing) { - if (hasChildren()) { - throw new IllegalStateException("Address already has children, this property can't be modified"); - } - isCommandTrailing = trailing; - } - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.command.predef.DefaultGroupCommand;
+import io.dico.dicore.command.predef.HelpCommand;
+
+import java.util.*;
+
+public class ChildCommandAddress extends ModifiableCommandAddress {
+ ModifiableCommandAddress parent;
+ final List<String> namesModifiable = new ArrayList<>(4);
+ List<String> names = namesModifiable;
+ Command command;
+ boolean isCommandTrailing;
+
+ public ChildCommandAddress() {
+ }
+
+ public ChildCommandAddress(Command command) {
+ this.command = command;
+ }
+
+ public ChildCommandAddress(Command command, String name, String... aliases) {
+ this(command);
+ addNameAndAliases(name, aliases);
+ }
+
+ public static ChildCommandAddress newPlaceHolderCommand(String name, String... aliases) {
+ ChildCommandAddress rv = new ChildCommandAddress();
+ rv.setupAsPlaceholder(name, aliases);
+ return rv;
+ }
+
+ public void setupAsPlaceholder(String name, String... aliases) {
+ if (!hasCommand()) {
+ setCommand(DefaultGroupCommand.getInstance());
+ }
+
+ addNameAndAliases(name, aliases);
+ HelpCommand.registerAsChild(this);
+ }
+
+ @Override
+ public boolean isRoot() {
+ return false;
+ }
+
+ @Override
+ public ModifiableCommandAddress getParent() {
+ return parent;
+ }
+
+ @Override
+ public Command getCommand() {
+ return command;
+ }
+
+ @Override
+ public void setCommand(Command command) {
+ if (hasUserDeclaredCommand()) {
+ throw new IllegalStateException("Command is already set at address \"" + getAddress() + "\"");
+ }
+ this.command = command;
+ }
+
+ @Override
+ public List<String> getNames() {
+ return names;
+ }
+
+ public void addNameAndAliases(String name, String... aliases) {
+ names.add(name);
+ names.addAll(Arrays.asList(aliases));
+ }
+
+ @Override
+ public String getMainKey() {
+ return namesModifiable.isEmpty() ? null : namesModifiable.get(0);
+ }
+
+ @Override
+ public String getAddress() {
+ ICommandAddress address = this;
+ int depth = getDepth();
+ String[] keys = new String[depth];
+ for (int i = depth - 1; i >= 0; i--) {
+ keys[i] = address.getMainKey();
+ address = address.getParent();
+ }
+ return String.join(" ", keys);
+ }
+
+ public void finalizeNames() {
+ if (names == namesModifiable) {
+ names = Collections.unmodifiableList(namesModifiable);
+ }
+ }
+
+ Iterator<String> modifiableNamesIterator() {
+ return namesModifiable.iterator();
+ }
+
+ void setParent(ModifiableCommandAddress parent) {
+ finalizeNames();
+ this.parent = parent;
+ }
+
+ @Override
+ public boolean isCommandTrailing() {
+ return isCommandTrailing;
+ }
+
+ @Override
+ public void setCommandTrailing(boolean trailing) {
+ if (hasChildren()) {
+ throw new IllegalStateException("Address already has children, this property can't be modified");
+ }
+ isCommandTrailing = trailing;
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/Command.java b/dicore3/command/src/main/java/io/dico/dicore/command/Command.java index 894e74e..97f4b78 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/Command.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/Command.java @@ -1,158 +1,158 @@ -package io.dico.dicore.command; - -import io.dico.dicore.command.IContextFilter.Priority; -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.parameter.IArgumentPreProcessor; -import io.dico.dicore.command.parameter.Parameter; -import io.dico.dicore.command.parameter.ParameterList; -import io.dico.dicore.command.parameter.type.ParameterType; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; - -public abstract class Command { - private static final String[] EMPTY_DESCRIPTION = new String[0]; - private final ParameterList parameterList = new ParameterList(); - private final List<IContextFilter> contextFilters = new ArrayList<>(3); - private String[] description = EMPTY_DESCRIPTION; - private String shortDescription; - - public Command addParameter(Parameter<?, ?> parameter) { - parameterList.addParameter(parameter); - return this; - } - - public <TType> Command addParameter(String name, String description, ParameterType<TType, Void> type) { - return addParameter(new Parameter<>(name, description, type, null, false, null)); - } - - public <TType, TParamInfo> Command addParameter(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo) { - return addParameter(new Parameter<>(name, description, type, paramInfo, false, null)); - } - - public <TType> Command addFlag(String name, String description, ParameterType<TType, Void> type) { - return addParameter(new Parameter<>('-' + name, description, type, null, true, null)); - } - - public <TType, TParamInfo> Command addFlag(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo) { - return addParameter(new Parameter<>('-' + name, description, type, paramInfo, true, null)); - } - - public <TType> Command addAuthorizedFlag(String name, String description, ParameterType<TType, Void> type, String permission) { - return addParameter(new Parameter<>('-' + name, description, type, null, true, permission)); - } - - public <TType, TParamInfo> Command addAuthorizedFlag(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo, String permission) { - return addParameter(new Parameter<>('-' + name, description, type, paramInfo, true, permission)); - } - - public Command requiredParameters(int requiredParameters) { - parameterList.setRequiredCount(requiredParameters); - return this; - } - - public Command repeatFinalParameter() { - parameterList.setRepeatFinalParameter(true); - return this; - } - - public Command setDescription(String... description) { - this.description = Objects.requireNonNull(description); - return this; - } - - public Command setShortDescription(String shortDescription) { - this.shortDescription = shortDescription; - return this; - } - - /* - public Command preprocessArguments(IArgumentPreProcessor processor) { - parameterList.setArgumentPreProcessor(processor); - return this; - }*/ - - public final ParameterList getParameterList() { - return parameterList; - } - - public final String[] getDescription() { - return description.length == 0 ? description : description.clone(); - } - - public String getShortDescription() { - return shortDescription; - } - - /** - * ---- CONTEXT FILTERS ---- - * Filter the contexts. For example, if the sender must be a player but it's the console, - * throw a CommandException describing the problem. - */ - private transient int postParameterFilterCount = 0; - - public Command addContextFilter(IContextFilter contextFilter) { - Objects.requireNonNull(contextFilter); - if (!contextFilters.contains(contextFilter)) { - contextFilters.add(contextFilter); - contextFilters.sort(null); - - if (contextFilter.getPriority().compareTo(Priority.POST_PARAMETERS) >= 0) { - postParameterFilterCount++; - } - } - return this; - } - - public List<IContextFilter> getContextFilters() { - return Collections.unmodifiableList(contextFilters); - } - - public Command removeContextFilter(IContextFilter contextFilter) { - boolean ret = contextFilters.remove(contextFilter); - if (ret) { - if (contextFilter.getPriority().compareTo(Priority.POST_PARAMETERS) >= 0) { - postParameterFilterCount--; - } - } - return this; - } - - // ---- CONTROL FLOW IN COMMAND TREES ---- - - public boolean isVisibleTo(CommandSender sender) { - return true; - } - - public boolean takePrecedenceOverSubcommand(String subCommand, ArgumentBuffer buffer) { - return false; - } - - // ---- EXECUTION ---- - - public void initializeAndFilterContext(ExecutionContext context) throws CommandException { - int i, n; - for (i = 0, n = contextFilters.size() - postParameterFilterCount; i < n; i++) { - contextFilters.get(i).filterContext(context); - } - - context.parse(parameterList); - - if (!context.isTabComplete()) { - for (n = contextFilters.size(); i < n; i++) { - contextFilters.get(i).filterContext(context); - } - } - } - - public abstract String execute(CommandSender sender, ExecutionContext context) throws CommandException; - - public List<String> tabComplete(CommandSender sender, ExecutionContext context, Location location) { - return context.getSuggestedCompletions(location); - } - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.command.IContextFilter.Priority;
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.parameter.IArgumentPreProcessor;
+import io.dico.dicore.command.parameter.Parameter;
+import io.dico.dicore.command.parameter.ParameterList;
+import io.dico.dicore.command.parameter.type.ParameterType;
+import org.bukkit.Location;
+import org.bukkit.command.CommandSender;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+public abstract class Command {
+ private static final String[] EMPTY_DESCRIPTION = new String[0];
+ private final ParameterList parameterList = new ParameterList();
+ private final List<IContextFilter> contextFilters = new ArrayList<>(3);
+ private String[] description = EMPTY_DESCRIPTION;
+ private String shortDescription;
+
+ public Command addParameter(Parameter<?, ?> parameter) {
+ parameterList.addParameter(parameter);
+ return this;
+ }
+
+ public <TType> Command addParameter(String name, String description, ParameterType<TType, Void> type) {
+ return addParameter(new Parameter<>(name, description, type, null, false, null));
+ }
+
+ public <TType, TParamInfo> Command addParameter(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo) {
+ return addParameter(new Parameter<>(name, description, type, paramInfo, false, null));
+ }
+
+ public <TType> Command addFlag(String name, String description, ParameterType<TType, Void> type) {
+ return addParameter(new Parameter<>('-' + name, description, type, null, true, null));
+ }
+
+ public <TType, TParamInfo> Command addFlag(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo) {
+ return addParameter(new Parameter<>('-' + name, description, type, paramInfo, true, null));
+ }
+
+ public <TType> Command addAuthorizedFlag(String name, String description, ParameterType<TType, Void> type, String permission) {
+ return addParameter(new Parameter<>('-' + name, description, type, null, true, permission));
+ }
+
+ public <TType, TParamInfo> Command addAuthorizedFlag(String name, String description, ParameterType<TType, TParamInfo> type, TParamInfo paramInfo, String permission) {
+ return addParameter(new Parameter<>('-' + name, description, type, paramInfo, true, permission));
+ }
+
+ public Command requiredParameters(int requiredParameters) {
+ parameterList.setRequiredCount(requiredParameters);
+ return this;
+ }
+
+ public Command repeatFinalParameter() {
+ parameterList.setRepeatFinalParameter(true);
+ return this;
+ }
+
+ public Command setDescription(String... description) {
+ this.description = Objects.requireNonNull(description);
+ return this;
+ }
+
+ public Command setShortDescription(String shortDescription) {
+ this.shortDescription = shortDescription;
+ return this;
+ }
+
+ /*
+ public Command preprocessArguments(IArgumentPreProcessor processor) {
+ parameterList.setArgumentPreProcessor(processor);
+ return this;
+ }*/
+
+ public final ParameterList getParameterList() {
+ return parameterList;
+ }
+
+ public final String[] getDescription() {
+ return description.length == 0 ? description : description.clone();
+ }
+
+ public String getShortDescription() {
+ return shortDescription;
+ }
+
+ /**
+ * ---- CONTEXT FILTERS ----
+ * Filter the contexts. For example, if the sender must be a player but it's the console,
+ * throw a CommandException describing the problem.
+ */
+ private transient int postParameterFilterCount = 0;
+
+ public Command addContextFilter(IContextFilter contextFilter) {
+ Objects.requireNonNull(contextFilter);
+ if (!contextFilters.contains(contextFilter)) {
+ contextFilters.add(contextFilter);
+ contextFilters.sort(null);
+
+ if (contextFilter.getPriority().compareTo(Priority.POST_PARAMETERS) >= 0) {
+ postParameterFilterCount++;
+ }
+ }
+ return this;
+ }
+
+ public List<IContextFilter> getContextFilters() {
+ return Collections.unmodifiableList(contextFilters);
+ }
+
+ public Command removeContextFilter(IContextFilter contextFilter) {
+ boolean ret = contextFilters.remove(contextFilter);
+ if (ret) {
+ if (contextFilter.getPriority().compareTo(Priority.POST_PARAMETERS) >= 0) {
+ postParameterFilterCount--;
+ }
+ }
+ return this;
+ }
+
+ // ---- CONTROL FLOW IN COMMAND TREES ----
+
+ public boolean isVisibleTo(CommandSender sender) {
+ return true;
+ }
+
+ public boolean takePrecedenceOverSubcommand(String subCommand, ArgumentBuffer buffer) {
+ return false;
+ }
+
+ // ---- EXECUTION ----
+
+ public void initializeAndFilterContext(ExecutionContext context) throws CommandException {
+ int i, n;
+ for (i = 0, n = contextFilters.size() - postParameterFilterCount; i < n; i++) {
+ contextFilters.get(i).filterContext(context);
+ }
+
+ context.parse(parameterList);
+
+ if (!context.isTabComplete()) {
+ for (n = contextFilters.size(); i < n; i++) {
+ contextFilters.get(i).filterContext(context);
+ }
+ }
+ }
+
+ public abstract String execute(CommandSender sender, ExecutionContext context) throws CommandException;
+
+ public List<String> tabComplete(CommandSender sender, ExecutionContext context, Location location) {
+ return context.getSuggestedCompletions(location);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/CommandBuilder.java b/dicore3/command/src/main/java/io/dico/dicore/command/CommandBuilder.java index e527f27..118dd0f 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/CommandBuilder.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/CommandBuilder.java @@ -1,432 +1,432 @@ -package io.dico.dicore.command; - -import io.dico.dicore.command.chat.IChatHandler; -import io.dico.dicore.command.parameter.type.IParameterTypeSelector; -import io.dico.dicore.command.parameter.type.MapBasedParameterTypeSelector; -import io.dico.dicore.command.parameter.type.ParameterType; -import io.dico.dicore.command.predef.HelpCommand; -import io.dico.dicore.command.predef.PredefinedCommand; -import io.dico.dicore.command.predef.SyntaxCommand; -import io.dico.dicore.command.registration.reflect.ReflectiveRegistration; - -import java.util.HashSet; -import java.util.Objects; -import java.util.function.Consumer; - -/** - * Mimic of WorldEdit's CommandGraph - */ -public final class CommandBuilder { - private final RootCommandAddress root; - private ModifiableCommandAddress cur; - private IParameterTypeSelector selector; - - /** - * Instantiate a new CommandBuilder with a new command root system - * Commands registered to this command builder might interfere with - * commands registered to other commands builders or by other plugins. - */ - public CommandBuilder() { - this(new RootCommandAddress()); - } - - /** - * Instantiate a new CommandBuilder with a specified root address. - * If the root address is identical to that of another command builder, - * they will modify the same tree. - * - * @param root the root address - */ - public CommandBuilder(RootCommandAddress root) { - this.root = Objects.requireNonNull(root); - this.cur = root; - this.selector = new MapBasedParameterTypeSelector(true); - } - - /** - * Add a sub command at the current address - * The current address can be inspected using {@link #getAddress()} - * - * @param name the name of the command - * @param command the command executor - * @param aliases any aliases - * @return this - */ - public CommandBuilder addSubCommand(String name, Command command, String... aliases) { - ChildCommandAddress address = new ChildCommandAddress(command); - address.addNameAndAliases(name, aliases); - return addSubCommand(address); - } - - /** - * Add a subcommand as an address at the current address - * The result of this call is the same as - * {@code addSubCommand(address.getMainKey(), address.getCommand(), address.getNames().sublist(1).toArray(new String[0]))} - * - * @param address the address - * @return this - * @throws IllegalArgumentException if {@code address.isRoot()} - */ - public CommandBuilder addSubCommand(ICommandAddress address) { - cur.addChild(address); - return this; - } - - /** - * Search the given class for any (static) methods using command annotations - * The class gets a localized parameter type selector if it defines parameter types. - * Any commands found are registered as sub commands to the current address. - * - * @param clazz the clazz - * @return this - * @throws IllegalArgumentException if an exception occurs while parsing the methods of this class - * @see #registerCommands(Class, Object) - */ - public CommandBuilder registerCommands(Class<?> clazz) { - return registerCommands(clazz, null); - } - - /** - * Search the given object's class for methods using command annotations. - * If the object is null, only static methods are checked. Otherwise, instance methods are also checked. - * The class gets a localized parameter type selector if it defines parameter types. - * Any commands found are registered as sub commands to the current address. - * - * @param object the object - * @return this - * @throws IllegalArgumentException if an exception occurs while parsing the methods of this class - * @see #registerCommands(Class, Object) - */ - public CommandBuilder registerCommands(Object object) { - return registerCommands(object.getClass(), object); - } - - /** - * Search the given class for methods using command annotations. - * The class gets a localized parameter type selector if it defines parameter types. - * Any commands found are registered as sub commands to the current address. - * The instance is used to invoke non-static methods. - * - * @param clazz the class - * @param instance the instance, null if only static methods - * @return this - * @throws IllegalArgumentException if instance is not null and it's not an instance of the class - * @throws IllegalArgumentException if another exception occurs while parsing the methods of this class - */ - public CommandBuilder registerCommands(Class<?> clazz, Object instance) { - try { - ReflectiveRegistration.parseCommandGroup(cur, selector, clazz, instance); - return this; - } catch (Exception ex) { - throw new IllegalArgumentException(ex); - } - } - - /** - * register the {@link HelpCommand} as a sub command at the current address - * - * @return this - */ - public CommandBuilder registerHelpCommand() { - HelpCommand.registerAsChild(cur); - return this; - } - - /** - * register the {@link SyntaxCommand} as a sub command a the current address - * - * @return this - */ - public CommandBuilder registerSyntaxCommand() { - SyntaxCommand.registerAsChild(cur); - return this; - } - - /** - * Generate the predefined commands. - * These are presets. - * Examples include {@code help} and {@code syntax}. - * <p> - * Predefined commands can be registered through {@link PredefinedCommand#registerPredefinedCommandGenerator(String, Consumer)} - * - * @param commands the commands - * @return this - */ - public CommandBuilder generatePredefinedCommands(String... commands) { - ReflectiveRegistration.generateCommands(cur, commands); - return this; - } - - /** - * Unregister any childs present at the given keys. - * <p> - * This method can be used to remove unwanted keys, that might have been added - * outside of your control. For example, because you didn't want all commands - * registered by {@link #registerCommands(Class, Object)}, or because you didn't - * want the help command registered by {@link #group(String, String...)} - * - * @param removeAliases true if any aliases of the children present at the keys should be removed - * @param keys a varargs array containing the keys - * @return this - * @throws IllegalArgumentException if keys array is empty - */ - public CommandBuilder unregisterCommands(boolean removeAliases, String... keys) { - cur.removeChildren(removeAliases, keys); - return this; - } - - /** - * Jump to the sub-address with the given name as main key. - * If an address with the exact name as main key exists, - * that address becomes the current address. - * <p> - * Otherwise, a new addresses is registered with the name and aliases. - * New addresses registered by this command have a HelpCommand added by default. - * <p> - * After this call, any registered commands are registered as a sub command - * to the new address. To restore the previous state, a call to {@link #parent()} - * should be made. - * <p> - * If the address is the target of a command, it will provide information about its sub commands - * using the HelpCommand. - * - * @param name the main key - * @param aliases the aliases - * @return this - */ - public CommandBuilder group(String name, String... aliases) { - ChildCommandAddress address = cur.getChild(name); - if (address == null || !name.equals(address.getMainKey())) { - address = new ChildCommandAddress(); - address.setupAsPlaceholder(name, aliases); - cur.addChild(address); - } - cur = address; - return this; - } - - /** - * Similar to {@link #group(String, String[])} but this will force overwrite any present group, - * using the address passed. The address MUST be an instance of {@link ChildCommandAddress}. - * - * <p>The address must not have a parent or any keys</p> - * - * @param address the address object to use - * @param name the main key - * @param aliases any aliases - * @return this - * @throws IllegalArgumentException if any of the requirements set out above aren't met - */ - public CommandBuilder group(ICommandAddress address, String name, String... aliases) { - if (address.hasParent() || address.getMainKey() != null || !(address instanceof ChildCommandAddress)) { - throw new IllegalArgumentException(); - } - - ChildCommandAddress asChild = (ChildCommandAddress) address; - asChild.setupAsPlaceholder(name, aliases); - cur.addChild(address); - cur = asChild; - return this; - } - - /** - * Sets the description of a group created by {@link #group(String, String...)} - * Can be called subsequently to making a call to {@link #group(String, String...)} - * - * @param shortDescription a short description - * @param description the lines of a full description. - * @return this - * @throws IllegalStateException if the current group has no command - */ - public CommandBuilder setGroupDescription(String shortDescription, String... description) { - Command command = cur.getCommand(); - if (command == null) throw new IllegalStateException(); - cur.setCommand(command - .setShortDescription(shortDescription) - .setDescription(description)); - return this; - } - - /** - * Add a context filter to the command of the current group - * @return this - * @throws IllegalStateException if the current group has no command - */ - public CommandBuilder addContextFilter(IContextFilter contextFilter) { - Command command = cur.getCommand(); - if (command == null) throw new IllegalStateException(); - cur.setCommand(command - .addContextFilter(contextFilter)); - return this; - } - - /** - * Add a required permission to the command of the current group - * @return this - * @throws IllegalStateException if the current group has no command - */ - public CommandBuilder addPermission(String permission) { - return addContextFilter(IContextFilter.permission(permission)); - } - - /** - * Add a required permission to the command of the current group, which can be inherited - * @return this - * @throws IllegalStateException if the current group has no command - */ - public CommandBuilder addInheritablePermission(String permission) { - return addContextFilter(IContextFilter.inheritablePermission(permission)); - } - - /** - * Jump up a level in the address - * - * @return this - * @throws IllegalStateException if the address is empty - * // has a depth of 0 // is at level 0 - */ - public CommandBuilder parent() { - if (cur.hasParent()) { - cur = cur.getParent(); - return this; - } - throw new IllegalStateException("No parent exists at this address"); - } - - /** - * Jump to the root (empty) address, - * such that a subsequent call to {@link #parent()} - * will throw a {@link IllegalStateException} - * - * @return this - */ - public CommandBuilder root() { - cur = root; - return this; - } - - /** - * Get the current address, as a space-separated string - * - * @return the current address - */ - public String getAddress() { - return cur.getAddress(); - } - - /** - * Get the depth of the current address. - * This is equivalent to {@code getAddress().split(" ").length}. - * If the address is empty, the depth is 0. - * - * @return the depth - */ - public int getDepth() { - return cur.getDepth(); - } - - /** - * Set the command at the current group. The command is set - * a level higher than it would be if this were a call to {@link #addSubCommand(String, Command, String...)} - * <p> - * If a call to {@link #setGroupDescription(String, String...)} was made at the same address before, - * the description is copied to the given executor. - * - * @param command the executor - * @return this - * @throws IllegalArgumentException if the command at the address is present and declared by the user, - * in other words, it's not a {@link PredefinedCommand} - */ - public CommandBuilder setCommand(Command command) { - Command current = cur.getCommand(); - if (current instanceof HelpCommand && current != HelpCommand.INSTANCE) { - command.setShortDescription(current.getShortDescription()); - command.setDescription(current.getDescription()); - } - - cur.setCommand(command); - return this; - } - - /** - * Configure the chat handler at this address. The chat handler - * is used for all children down the tree if they don't explicitly have - * their own chat handler configured. If this isn't configured, - * {@code ChatHandlers.defaultChat()} is used. - * - * @param chatHandler the chat handler - * @return this - */ - public CommandBuilder setChatHandler(IChatHandler chatHandler) { - cur.setChatHandler(chatHandler); - return this; - } - - /** - * Add the parameter type to this builder's selector. - * - * @param type the type - * @param <T> the return type of the parameter type - * @return this - */ - public <T> CommandBuilder addParameterType(ParameterType<T, Void> type) { - selector.addType(false, type); - return this; - } - - /** - * Add the parameter type to this builder's selector. - * - * @param infolessAlias whether to also register the type with an infoless alias. - * this increases the priority assigned to the type if no info object is present. - * @param type the type - * @param <T> the return type of the parameter type - * @param <C> the parameter config type (info object) - * @return this - */ - - public <T, C> CommandBuilder addParameterType(boolean infolessAlias, ParameterType<T, C> type) { - selector.addType(infolessAlias, type); - return this; - } - - /** - * Get the dispatcher for the root address. - * The dispatcher should be used to finally register all commands, - * after they are all declared. - * - * @return the dispatcher - */ - public ICommandDispatcher getDispatcher() { - return root; - } - - /** - * Print debugging information about the current addresses and commands in this builder - * A StackTraceElement indicating where this was called from is also included - * - * @return this - */ - public CommandBuilder printDebugInformation() { - String address = cur == root ? "<root>" : cur.getAddress(); - StackTraceElement caller = getCallsite(); - - StringBuilder message = new StringBuilder("### CommandBuilder dump ###"); - message.append("\nCalled from ").append(caller); - message.append("\nPosition: ").append(address); - cur.appendDebugInformation(message, "", new HashSet<>()); - - System.out.println(message); - return this; - } - - private static StackTraceElement getCallsite() { - // [0] Thread.currentThread() - // [1] CommandBuilder.getCallsite() - // [2] Calling method - // [3] Method calling the calling method - StackTraceElement[] trace = Thread.currentThread().getStackTrace(); - return trace.length > 3 ? trace[3] : null; - } - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.command.chat.IChatHandler;
+import io.dico.dicore.command.parameter.type.IParameterTypeSelector;
+import io.dico.dicore.command.parameter.type.MapBasedParameterTypeSelector;
+import io.dico.dicore.command.parameter.type.ParameterType;
+import io.dico.dicore.command.predef.HelpCommand;
+import io.dico.dicore.command.predef.PredefinedCommand;
+import io.dico.dicore.command.predef.SyntaxCommand;
+import io.dico.dicore.command.registration.reflect.ReflectiveRegistration;
+
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.function.Consumer;
+
+/**
+ * Mimic of WorldEdit's CommandGraph
+ */
+public final class CommandBuilder {
+ private final RootCommandAddress root;
+ private ModifiableCommandAddress cur;
+ private IParameterTypeSelector selector;
+
+ /**
+ * Instantiate a new CommandBuilder with a new command root system
+ * Commands registered to this command builder might interfere with
+ * commands registered to other commands builders or by other plugins.
+ */
+ public CommandBuilder() {
+ this(new RootCommandAddress());
+ }
+
+ /**
+ * Instantiate a new CommandBuilder with a specified root address.
+ * If the root address is identical to that of another command builder,
+ * they will modify the same tree.
+ *
+ * @param root the root address
+ */
+ public CommandBuilder(RootCommandAddress root) {
+ this.root = Objects.requireNonNull(root);
+ this.cur = root;
+ this.selector = new MapBasedParameterTypeSelector(true);
+ }
+
+ /**
+ * Add a sub command at the current address
+ * The current address can be inspected using {@link #getAddress()}
+ *
+ * @param name the name of the command
+ * @param command the command executor
+ * @param aliases any aliases
+ * @return this
+ */
+ public CommandBuilder addSubCommand(String name, Command command, String... aliases) {
+ ChildCommandAddress address = new ChildCommandAddress(command);
+ address.addNameAndAliases(name, aliases);
+ return addSubCommand(address);
+ }
+
+ /**
+ * Add a subcommand as an address at the current address
+ * The result of this call is the same as
+ * {@code addSubCommand(address.getMainKey(), address.getCommand(), address.getNames().sublist(1).toArray(new String[0]))}
+ *
+ * @param address the address
+ * @return this
+ * @throws IllegalArgumentException if {@code address.isRoot()}
+ */
+ public CommandBuilder addSubCommand(ICommandAddress address) {
+ cur.addChild(address);
+ return this;
+ }
+
+ /**
+ * Search the given class for any (static) methods using command annotations
+ * The class gets a localized parameter type selector if it defines parameter types.
+ * Any commands found are registered as sub commands to the current address.
+ *
+ * @param clazz the clazz
+ * @return this
+ * @throws IllegalArgumentException if an exception occurs while parsing the methods of this class
+ * @see #registerCommands(Class, Object)
+ */
+ public CommandBuilder registerCommands(Class<?> clazz) {
+ return registerCommands(clazz, null);
+ }
+
+ /**
+ * Search the given object's class for methods using command annotations.
+ * If the object is null, only static methods are checked. Otherwise, instance methods are also checked.
+ * The class gets a localized parameter type selector if it defines parameter types.
+ * Any commands found are registered as sub commands to the current address.
+ *
+ * @param object the object
+ * @return this
+ * @throws IllegalArgumentException if an exception occurs while parsing the methods of this class
+ * @see #registerCommands(Class, Object)
+ */
+ public CommandBuilder registerCommands(Object object) {
+ return registerCommands(object.getClass(), object);
+ }
+
+ /**
+ * Search the given class for methods using command annotations.
+ * The class gets a localized parameter type selector if it defines parameter types.
+ * Any commands found are registered as sub commands to the current address.
+ * The instance is used to invoke non-static methods.
+ *
+ * @param clazz the class
+ * @param instance the instance, null if only static methods
+ * @return this
+ * @throws IllegalArgumentException if instance is not null and it's not an instance of the class
+ * @throws IllegalArgumentException if another exception occurs while parsing the methods of this class
+ */
+ public CommandBuilder registerCommands(Class<?> clazz, Object instance) {
+ try {
+ ReflectiveRegistration.parseCommandGroup(cur, selector, clazz, instance);
+ return this;
+ } catch (Exception ex) {
+ throw new IllegalArgumentException(ex);
+ }
+ }
+
+ /**
+ * register the {@link HelpCommand} as a sub command at the current address
+ *
+ * @return this
+ */
+ public CommandBuilder registerHelpCommand() {
+ HelpCommand.registerAsChild(cur);
+ return this;
+ }
+
+ /**
+ * register the {@link SyntaxCommand} as a sub command a the current address
+ *
+ * @return this
+ */
+ public CommandBuilder registerSyntaxCommand() {
+ SyntaxCommand.registerAsChild(cur);
+ return this;
+ }
+
+ /**
+ * Generate the predefined commands.
+ * These are presets.
+ * Examples include {@code help} and {@code syntax}.
+ * <p>
+ * Predefined commands can be registered through {@link PredefinedCommand#registerPredefinedCommandGenerator(String, Consumer)}
+ *
+ * @param commands the commands
+ * @return this
+ */
+ public CommandBuilder generatePredefinedCommands(String... commands) {
+ ReflectiveRegistration.generateCommands(cur, commands);
+ return this;
+ }
+
+ /**
+ * Unregister any childs present at the given keys.
+ * <p>
+ * This method can be used to remove unwanted keys, that might have been added
+ * outside of your control. For example, because you didn't want all commands
+ * registered by {@link #registerCommands(Class, Object)}, or because you didn't
+ * want the help command registered by {@link #group(String, String...)}
+ *
+ * @param removeAliases true if any aliases of the children present at the keys should be removed
+ * @param keys a varargs array containing the keys
+ * @return this
+ * @throws IllegalArgumentException if keys array is empty
+ */
+ public CommandBuilder unregisterCommands(boolean removeAliases, String... keys) {
+ cur.removeChildren(removeAliases, keys);
+ return this;
+ }
+
+ /**
+ * Jump to the sub-address with the given name as main key.
+ * If an address with the exact name as main key exists,
+ * that address becomes the current address.
+ * <p>
+ * Otherwise, a new addresses is registered with the name and aliases.
+ * New addresses registered by this command have a HelpCommand added by default.
+ * <p>
+ * After this call, any registered commands are registered as a sub command
+ * to the new address. To restore the previous state, a call to {@link #parent()}
+ * should be made.
+ * <p>
+ * If the address is the target of a command, it will provide information about its sub commands
+ * using the HelpCommand.
+ *
+ * @param name the main key
+ * @param aliases the aliases
+ * @return this
+ */
+ public CommandBuilder group(String name, String... aliases) {
+ ChildCommandAddress address = cur.getChild(name);
+ if (address == null || !name.equals(address.getMainKey())) {
+ address = new ChildCommandAddress();
+ address.setupAsPlaceholder(name, aliases);
+ cur.addChild(address);
+ }
+ cur = address;
+ return this;
+ }
+
+ /**
+ * Similar to {@link #group(String, String[])} but this will force overwrite any present group,
+ * using the address passed. The address MUST be an instance of {@link ChildCommandAddress}.
+ *
+ * <p>The address must not have a parent or any keys</p>
+ *
+ * @param address the address object to use
+ * @param name the main key
+ * @param aliases any aliases
+ * @return this
+ * @throws IllegalArgumentException if any of the requirements set out above aren't met
+ */
+ public CommandBuilder group(ICommandAddress address, String name, String... aliases) {
+ if (address.hasParent() || address.getMainKey() != null || !(address instanceof ChildCommandAddress)) {
+ throw new IllegalArgumentException();
+ }
+
+ ChildCommandAddress asChild = (ChildCommandAddress) address;
+ asChild.setupAsPlaceholder(name, aliases);
+ cur.addChild(address);
+ cur = asChild;
+ return this;
+ }
+
+ /**
+ * Sets the description of a group created by {@link #group(String, String...)}
+ * Can be called subsequently to making a call to {@link #group(String, String...)}
+ *
+ * @param shortDescription a short description
+ * @param description the lines of a full description.
+ * @return this
+ * @throws IllegalStateException if the current group has no command
+ */
+ public CommandBuilder setGroupDescription(String shortDescription, String... description) {
+ Command command = cur.getCommand();
+ if (command == null) throw new IllegalStateException();
+ cur.setCommand(command
+ .setShortDescription(shortDescription)
+ .setDescription(description));
+ return this;
+ }
+
+ /**
+ * Add a context filter to the command of the current group
+ * @return this
+ * @throws IllegalStateException if the current group has no command
+ */
+ public CommandBuilder addContextFilter(IContextFilter contextFilter) {
+ Command command = cur.getCommand();
+ if (command == null) throw new IllegalStateException();
+ cur.setCommand(command
+ .addContextFilter(contextFilter));
+ return this;
+ }
+
+ /**
+ * Add a required permission to the command of the current group
+ * @return this
+ * @throws IllegalStateException if the current group has no command
+ */
+ public CommandBuilder addPermission(String permission) {
+ return addContextFilter(IContextFilter.permission(permission));
+ }
+
+ /**
+ * Add a required permission to the command of the current group, which can be inherited
+ * @return this
+ * @throws IllegalStateException if the current group has no command
+ */
+ public CommandBuilder addInheritablePermission(String permission) {
+ return addContextFilter(IContextFilter.inheritablePermission(permission));
+ }
+
+ /**
+ * Jump up a level in the address
+ *
+ * @return this
+ * @throws IllegalStateException if the address is empty
+ * // has a depth of 0 // is at level 0
+ */
+ public CommandBuilder parent() {
+ if (cur.hasParent()) {
+ cur = cur.getParent();
+ return this;
+ }
+ throw new IllegalStateException("No parent exists at this address");
+ }
+
+ /**
+ * Jump to the root (empty) address,
+ * such that a subsequent call to {@link #parent()}
+ * will throw a {@link IllegalStateException}
+ *
+ * @return this
+ */
+ public CommandBuilder root() {
+ cur = root;
+ return this;
+ }
+
+ /**
+ * Get the current address, as a space-separated string
+ *
+ * @return the current address
+ */
+ public String getAddress() {
+ return cur.getAddress();
+ }
+
+ /**
+ * Get the depth of the current address.
+ * This is equivalent to {@code getAddress().split(" ").length}.
+ * If the address is empty, the depth is 0.
+ *
+ * @return the depth
+ */
+ public int getDepth() {
+ return cur.getDepth();
+ }
+
+ /**
+ * Set the command at the current group. The command is set
+ * a level higher than it would be if this were a call to {@link #addSubCommand(String, Command, String...)}
+ * <p>
+ * If a call to {@link #setGroupDescription(String, String...)} was made at the same address before,
+ * the description is copied to the given executor.
+ *
+ * @param command the executor
+ * @return this
+ * @throws IllegalArgumentException if the command at the address is present and declared by the user,
+ * in other words, it's not a {@link PredefinedCommand}
+ */
+ public CommandBuilder setCommand(Command command) {
+ Command current = cur.getCommand();
+ if (current instanceof HelpCommand && current != HelpCommand.INSTANCE) {
+ command.setShortDescription(current.getShortDescription());
+ command.setDescription(current.getDescription());
+ }
+
+ cur.setCommand(command);
+ return this;
+ }
+
+ /**
+ * Configure the chat handler at this address. The chat handler
+ * is used for all children down the tree if they don't explicitly have
+ * their own chat handler configured. If this isn't configured,
+ * {@code ChatHandlers.defaultChat()} is used.
+ *
+ * @param chatHandler the chat handler
+ * @return this
+ */
+ public CommandBuilder setChatHandler(IChatHandler chatHandler) {
+ cur.setChatHandler(chatHandler);
+ return this;
+ }
+
+ /**
+ * Add the parameter type to this builder's selector.
+ *
+ * @param type the type
+ * @param <T> the return type of the parameter type
+ * @return this
+ */
+ public <T> CommandBuilder addParameterType(ParameterType<T, Void> type) {
+ selector.addType(false, type);
+ return this;
+ }
+
+ /**
+ * Add the parameter type to this builder's selector.
+ *
+ * @param infolessAlias whether to also register the type with an infoless alias.
+ * this increases the priority assigned to the type if no info object is present.
+ * @param type the type
+ * @param <T> the return type of the parameter type
+ * @param <C> the parameter config type (info object)
+ * @return this
+ */
+
+ public <T, C> CommandBuilder addParameterType(boolean infolessAlias, ParameterType<T, C> type) {
+ selector.addType(infolessAlias, type);
+ return this;
+ }
+
+ /**
+ * Get the dispatcher for the root address.
+ * The dispatcher should be used to finally register all commands,
+ * after they are all declared.
+ *
+ * @return the dispatcher
+ */
+ public ICommandDispatcher getDispatcher() {
+ return root;
+ }
+
+ /**
+ * Print debugging information about the current addresses and commands in this builder
+ * A StackTraceElement indicating where this was called from is also included
+ *
+ * @return this
+ */
+ public CommandBuilder printDebugInformation() {
+ String address = cur == root ? "<root>" : cur.getAddress();
+ StackTraceElement caller = getCallsite();
+
+ StringBuilder message = new StringBuilder("### CommandBuilder dump ###");
+ message.append("\nCalled from ").append(caller);
+ message.append("\nPosition: ").append(address);
+ cur.appendDebugInformation(message, "", new HashSet<>());
+
+ System.out.println(message);
+ return this;
+ }
+
+ private static StackTraceElement getCallsite() {
+ // [0] Thread.currentThread()
+ // [1] CommandBuilder.getCallsite()
+ // [2] Calling method
+ // [3] Method calling the calling method
+ StackTraceElement[] trace = Thread.currentThread().getStackTrace();
+ return trace.length > 3 ? trace[3] : null;
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/CommandException.java b/dicore3/command/src/main/java/io/dico/dicore/command/CommandException.java index b859952..adf66de 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/CommandException.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/CommandException.java @@ -1,28 +1,28 @@ -package io.dico.dicore.command; - -public class CommandException extends Exception { - - public CommandException() { - } - - public CommandException(String message) { - super(message); - } - - public CommandException(String message, Throwable cause) { - super(message, cause); - } - - public CommandException(Throwable cause) { - super(cause); - } - - public static CommandException missingArgument(String parameterName) { - return new CommandException("Missing argument for " + parameterName); - } - - public static CommandException invalidArgument(String parameterName, String syntaxHelp) { - return new CommandException("Invalid input for " + parameterName + ", should be " + syntaxHelp); - } - -} +package io.dico.dicore.command;
+
+public class CommandException extends Exception {
+
+ public CommandException() {
+ }
+
+ public CommandException(String message) {
+ super(message);
+ }
+
+ public CommandException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public CommandException(Throwable cause) {
+ super(cause);
+ }
+
+ public static CommandException missingArgument(String parameterName) {
+ return new CommandException("Missing argument for " + parameterName);
+ }
+
+ public static CommandException invalidArgument(String parameterName, String syntaxHelp) {
+ return new CommandException("Invalid input for " + parameterName + ", should be " + syntaxHelp);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/EMessageType.java b/dicore3/command/src/main/java/io/dico/dicore/command/EMessageType.java index fba8780..0b02459 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/EMessageType.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/EMessageType.java @@ -1,19 +1,19 @@ -package io.dico.dicore.command; - -public enum EMessageType { - GOOD_NEWS, - BAD_NEWS, - NEUTRAL, - INFORMATIVE, - WARNING, - INSTRUCTION, - EXCEPTION, - RESULT, - CUSTOM, - - DESCRIPTION, - SYNTAX, - HIGHLIGHT, - SUBCOMMAND, - NUMBER, -} +package io.dico.dicore.command;
+
+public enum EMessageType {
+ GOOD_NEWS,
+ BAD_NEWS,
+ NEUTRAL,
+ INFORMATIVE,
+ WARNING,
+ INSTRUCTION,
+ EXCEPTION,
+ RESULT,
+ CUSTOM,
+
+ DESCRIPTION,
+ SYNTAX,
+ HIGHLIGHT,
+ SUBCOMMAND,
+ NUMBER,
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/EOverridePolicy.java b/dicore3/command/src/main/java/io/dico/dicore/command/EOverridePolicy.java index 83b0151..980477a 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/EOverridePolicy.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/EOverridePolicy.java @@ -1,12 +1,12 @@ -package io.dico.dicore.command; - -/** - * Override policies for registering to the command map - */ -public enum EOverridePolicy { - OVERRIDE_ALL, - MAIN_KEY_ONLY, - MAIN_AND_FALLBACK, - FALLBACK_ONLY, - OVERRIDE_NONE -} +package io.dico.dicore.command;
+
+/**
+ * Override policies for registering to the command map
+ */
+public enum EOverridePolicy {
+ OVERRIDE_ALL,
+ MAIN_KEY_ONLY,
+ MAIN_AND_FALLBACK,
+ FALLBACK_ONLY,
+ OVERRIDE_NONE
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java b/dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java index a329f40..af42650 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java @@ -1,385 +1,385 @@ -package io.dico.dicore.command; - -import io.dico.dicore.Formatting; -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.parameter.ContextParser; -import io.dico.dicore.command.parameter.Parameter; -import io.dico.dicore.command.parameter.ParameterList; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; - -import java.util.*; - -/** - * The context of execution. - * <p> - * This class is responsible for the control flow of parameter parsing, as well as caching and providing the parsed parameter values. - * It is also responsible for keeping track of the parameter to complete in the case of a tab completion. - */ -public class ExecutionContext { - // Sender of the command - private final CommandSender sender; - // Address while parsing parameters with ContextParser - private ICommandAddress address; - // Command to execute - private Command command; - // if this flag is set, this execution is only for completion purposes. - private boolean tabComplete; - - private final ArgumentBuffer buffer; - // private ArgumentBuffer processedBuffer; - - // caches the buffer's cursor before parsing. This is needed to provide the original input of the player. - private int cursorStart; - - // when the context starts parsing parameters, this flag is set, and any subsequent calls to #parseParameters() throw an IllegalStateException. - //private boolean attemptedToParse; - - - // The parsed parameter values, mapped by parameter name. - // This also includes default values. All parameters from the parameter list are present if parsing was successful. - private Map<String, Object> parameterValueMap = new HashMap<>(); - // this set contains the names of the parameters that were present in the command, and not given a default value. - private Set<String> parsedParameters = new HashSet<>(); - - - // these fields store information required to provide completions. - // the parameter to complete is the parameter that threw an exception when it was parsing. - // the exception's message was discarded because it is a completion. - private Parameter<?, ?> parameterToComplete; - // this is the cursor that the ArgumentBuffer is reset to when suggested completions are requested. - private int parameterToCompleteCursor = -1; - - // if this flag is set, any messages sent through the sendMessage methods are discarded. - private boolean muted; - - public ExecutionContext(CommandSender sender, ArgumentBuffer buffer, boolean tabComplete) { - this.sender = Objects.requireNonNull(sender); - this.buffer = Objects.requireNonNull(buffer); - this.muted = tabComplete; - this.tabComplete = tabComplete; - - // If its tab completing, keep the empty element that might be at the end of the buffer - // due to a space at the end of the command. - // This allows the parser to correctly identify the parameter to be completed in this case. - if (!tabComplete) { - buffer.dropTrailingEmptyElements(); - } - } - - /** - * Construct an execution context that is ready to parse the parameter values. - * - * @param sender the sender - * @param address the address - * @param command the command - * @param buffer the arguments - * @param tabComplete true if this execution is a tab-completion - */ - public ExecutionContext(CommandSender sender, ICommandAddress address, Command command, ArgumentBuffer buffer, boolean tabComplete) { - this(sender, buffer, tabComplete); - setAddress(address); - setCommand(command); - } - - /** - * Sender of the command - * - * @return the sender of the command - */ - public CommandSender getSender() { - return sender; - } - - /** - * @return the buffer of arguments - */ - public ArgumentBuffer getBuffer() { - return buffer; - } - - /** - * Command's address - * - * @return the command's address - */ - public ICommandAddress getAddress() { - return address; - } - - /** - * Set the address - * - * @param address the new address - */ - public void setAddress(ICommandAddress address) { - this.address = address; - } - - /** - * The command - * - * @return the command - */ - public Command getCommand() { - return command; - } - - /** - * Set the command - * - * @param command the new command - */ - public void setCommand(Command command) { - this.command = command; - } - - /** - * @return true if this context is for a tab completion. - */ - public boolean isTabComplete() { - return tabComplete; - } - - /** - * @return true if this context is muted. - */ - public boolean isMuted() { - return muted; - } - - /** - * Parse parameters from the given parameter list, - * adding their values to the cache of this context. - * - * @param parameterList the parameterList - * @throws CommandException if the arguments are not valid - */ - public void parse(ParameterList parameterList) throws CommandException { - cursorStart = buffer.getCursor(); - - ContextParser parser = new ContextParser(this, parameterList, parameterValueMap, parsedParameters); - - try { - parser.parse(); - } finally { - if (tabComplete) { - parameterToComplete = parser.getCompletionTarget(); - parameterToCompleteCursor = parser.getCompletionCursor(); - } - } - - } - - /** - * The command's parameter definition. - * - * @return the parameter list - */ - @Deprecated - public ParameterList getParameterList() { - return null;//command.getParameterList(); - } - - /** - * Get the buffer as it was before preprocessing the arguments. - * - * @return the original buffer - */ - @Deprecated - public ArgumentBuffer getOriginalBuffer() { - return buffer; - } - - /** - * The arguments - * - * @return the argument buffer - */ - @Deprecated - public ArgumentBuffer getProcessedBuffer() { - return buffer; - } - - /** - * The cursor start, in other words, the buffer's cursor before parameters were parsed. - * - * @return the cursor start - */ - public int getCursorStart() { - return cursorStart; - } - - /** - * The original arguments. - * - * @return original arguments. - */ - public String[] getOriginal() { - return buffer.getArrayFromIndex(cursorStart); - } - - /** - * The path used to access this address. - * - * @return the path used to access this address. - */ - public String[] getRoute() { - return Arrays.copyOf(buffer.toArray(), address.getDepth()); - } - - public Formatting getFormat(EMessageType type) { - return address.getChatHandler().getChatFormatForType(type); - } - - /** - * The full command as cached by the buffer. Might be incomplete depending on how it was dispatched. - * - * @return the full command - */ - public String getRawInput() { - return buffer.getRawInput(); - } - - /** - * Get the value of the parameter with the given name - * - * @param name the parameter's name - * @param <T> expected type - * @return the parsed value or the default value - */ - @SuppressWarnings("unchecked") - public <T> T get(String name) { - if (!parameterValueMap.containsKey(name)) { - throw new IllegalArgumentException(); - } - - try { - return (T) parameterValueMap.get(name); - } catch (ClassCastException ex) { - throw new IllegalArgumentException("Invalid type parameter requested for parameter " + name, ex); - } - } - - /** - * Get the value of the flag with the given name - * - * @param flag the flag's name, without preceding "-" - * @param <T> expected type - * @return the parsed value or the default value - */ - public <T> T getFlag(String flag) { - return get("-" + flag); - } - - @SuppressWarnings("unchecked") - @Deprecated - public <T> T get(int index) { - return null;//get(getParameterList().getIndexedParameterName(index)); - } - - /** - * Checks if the parameter by the name was provided in the command's arguments. - * - * @param name the parameter name - * @return true if it was provided - */ - public boolean isProvided(String name) { - return parsedParameters.contains(name); - } - - /** - * Checks if the parameter by the index was provided in the command's arguments. - * - * @param index the parameter index - * @return true if it was provided - */ - @Deprecated - public boolean isProvided(int index) { - return false;//isProvided(getParameterList().getIndexedParameterName(index)); - } - - /** - * The parameter to complete. - * This parameter is requested suggestions - * - * @return the parameter to complete. - */ - public Parameter<?, ?> getParameterToComplete() { - return parameterToComplete; - } - - /** - * Get suggested completions. - * - * @param location The location as passed to {link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}, or null if requested in another way. - * @return completions. - */ - public List<String> getSuggestedCompletions(Location location) { - if (parameterToComplete != null) { - return parameterToComplete.complete(this, location, buffer.getUnaffectingCopy().setCursor(parameterToCompleteCursor)); - } - - List<String> result = new ArrayList<>(); - for (String name : parameterValueMap.keySet()) { - if (name.startsWith("-") && !parsedParameters.contains(name)) { - result.add(name); - } - } - return result; - } - - /* - Chat handling - */ - - public void sendMessage(String message) { - sendMessage(true, message); - } - - public void sendMessage(EMessageType messageType, String message) { - sendMessage(messageType, true, message); - } - - public void sendMessage(boolean translateColours, String message) { - sendMessage(EMessageType.NEUTRAL, translateColours, message); - } - - public void sendMessage(EMessageType messageType, boolean translateColours, String message) { - if (!muted) { - if (translateColours) { - message = Formatting.translateChars('&', message); - } - address.getChatHandler().sendMessage(this, messageType, message); - } - } - - public void sendMessage(String messageFormat, Object... args) { - sendMessage(true, messageFormat, args); - } - - public void sendMessage(EMessageType messageType, String messageFormat, Object... args) { - sendMessage(messageType, true, messageFormat, args); - } - - public void sendMessage(boolean translateColours, String messageFormat, Object... args) { - sendMessage(EMessageType.NEUTRAL, translateColours, messageFormat, args); - } - - public void sendMessage(EMessageType messageType, boolean translateColours, String messageFormat, Object... args) { - sendMessage(messageType, translateColours, String.format(messageFormat, args)); - } - - public void sendHelpMessage(int page) { - if (!muted) { - address.getChatHandler().sendHelpMessage(sender, this, address, page); - } - } - - public void sendSyntaxMessage() { - if (!muted) { - address.getChatHandler().sendSyntaxMessage(sender, this, address); - } - } - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.Formatting;
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.parameter.ContextParser;
+import io.dico.dicore.command.parameter.Parameter;
+import io.dico.dicore.command.parameter.ParameterList;
+import org.bukkit.Location;
+import org.bukkit.command.CommandSender;
+
+import java.util.*;
+
+/**
+ * The context of execution.
+ * <p>
+ * This class is responsible for the control flow of parameter parsing, as well as caching and providing the parsed parameter values.
+ * It is also responsible for keeping track of the parameter to complete in the case of a tab completion.
+ */
+public class ExecutionContext {
+ // Sender of the command
+ private final CommandSender sender;
+ // Address while parsing parameters with ContextParser
+ private ICommandAddress address;
+ // Command to execute
+ private Command command;
+ // if this flag is set, this execution is only for completion purposes.
+ private boolean tabComplete;
+
+ private final ArgumentBuffer buffer;
+ // private ArgumentBuffer processedBuffer;
+
+ // caches the buffer's cursor before parsing. This is needed to provide the original input of the player.
+ private int cursorStart;
+
+ // when the context starts parsing parameters, this flag is set, and any subsequent calls to #parseParameters() throw an IllegalStateException.
+ //private boolean attemptedToParse;
+
+
+ // The parsed parameter values, mapped by parameter name.
+ // This also includes default values. All parameters from the parameter list are present if parsing was successful.
+ private Map<String, Object> parameterValueMap = new HashMap<>();
+ // this set contains the names of the parameters that were present in the command, and not given a default value.
+ private Set<String> parsedParameters = new HashSet<>();
+
+
+ // these fields store information required to provide completions.
+ // the parameter to complete is the parameter that threw an exception when it was parsing.
+ // the exception's message was discarded because it is a completion.
+ private Parameter<?, ?> parameterToComplete;
+ // this is the cursor that the ArgumentBuffer is reset to when suggested completions are requested.
+ private int parameterToCompleteCursor = -1;
+
+ // if this flag is set, any messages sent through the sendMessage methods are discarded.
+ private boolean muted;
+
+ public ExecutionContext(CommandSender sender, ArgumentBuffer buffer, boolean tabComplete) {
+ this.sender = Objects.requireNonNull(sender);
+ this.buffer = Objects.requireNonNull(buffer);
+ this.muted = tabComplete;
+ this.tabComplete = tabComplete;
+
+ // If its tab completing, keep the empty element that might be at the end of the buffer
+ // due to a space at the end of the command.
+ // This allows the parser to correctly identify the parameter to be completed in this case.
+ if (!tabComplete) {
+ buffer.dropTrailingEmptyElements();
+ }
+ }
+
+ /**
+ * Construct an execution context that is ready to parse the parameter values.
+ *
+ * @param sender the sender
+ * @param address the address
+ * @param command the command
+ * @param buffer the arguments
+ * @param tabComplete true if this execution is a tab-completion
+ */
+ public ExecutionContext(CommandSender sender, ICommandAddress address, Command command, ArgumentBuffer buffer, boolean tabComplete) {
+ this(sender, buffer, tabComplete);
+ setAddress(address);
+ setCommand(command);
+ }
+
+ /**
+ * Sender of the command
+ *
+ * @return the sender of the command
+ */
+ public CommandSender getSender() {
+ return sender;
+ }
+
+ /**
+ * @return the buffer of arguments
+ */
+ public ArgumentBuffer getBuffer() {
+ return buffer;
+ }
+
+ /**
+ * Command's address
+ *
+ * @return the command's address
+ */
+ public ICommandAddress getAddress() {
+ return address;
+ }
+
+ /**
+ * Set the address
+ *
+ * @param address the new address
+ */
+ public void setAddress(ICommandAddress address) {
+ this.address = address;
+ }
+
+ /**
+ * The command
+ *
+ * @return the command
+ */
+ public Command getCommand() {
+ return command;
+ }
+
+ /**
+ * Set the command
+ *
+ * @param command the new command
+ */
+ public void setCommand(Command command) {
+ this.command = command;
+ }
+
+ /**
+ * @return true if this context is for a tab completion.
+ */
+ public boolean isTabComplete() {
+ return tabComplete;
+ }
+
+ /**
+ * @return true if this context is muted.
+ */
+ public boolean isMuted() {
+ return muted;
+ }
+
+ /**
+ * Parse parameters from the given parameter list,
+ * adding their values to the cache of this context.
+ *
+ * @param parameterList the parameterList
+ * @throws CommandException if the arguments are not valid
+ */
+ public void parse(ParameterList parameterList) throws CommandException {
+ cursorStart = buffer.getCursor();
+
+ ContextParser parser = new ContextParser(this, parameterList, parameterValueMap, parsedParameters);
+
+ try {
+ parser.parse();
+ } finally {
+ if (tabComplete) {
+ parameterToComplete = parser.getCompletionTarget();
+ parameterToCompleteCursor = parser.getCompletionCursor();
+ }
+ }
+
+ }
+
+ /**
+ * The command's parameter definition.
+ *
+ * @return the parameter list
+ */
+ @Deprecated
+ public ParameterList getParameterList() {
+ return null;//command.getParameterList();
+ }
+
+ /**
+ * Get the buffer as it was before preprocessing the arguments.
+ *
+ * @return the original buffer
+ */
+ @Deprecated
+ public ArgumentBuffer getOriginalBuffer() {
+ return buffer;
+ }
+
+ /**
+ * The arguments
+ *
+ * @return the argument buffer
+ */
+ @Deprecated
+ public ArgumentBuffer getProcessedBuffer() {
+ return buffer;
+ }
+
+ /**
+ * The cursor start, in other words, the buffer's cursor before parameters were parsed.
+ *
+ * @return the cursor start
+ */
+ public int getCursorStart() {
+ return cursorStart;
+ }
+
+ /**
+ * The original arguments.
+ *
+ * @return original arguments.
+ */
+ public String[] getOriginal() {
+ return buffer.getArrayFromIndex(cursorStart);
+ }
+
+ /**
+ * The path used to access this address.
+ *
+ * @return the path used to access this address.
+ */
+ public String[] getRoute() {
+ return Arrays.copyOf(buffer.toArray(), address.getDepth());
+ }
+
+ public Formatting getFormat(EMessageType type) {
+ return address.getChatHandler().getChatFormatForType(type);
+ }
+
+ /**
+ * The full command as cached by the buffer. Might be incomplete depending on how it was dispatched.
+ *
+ * @return the full command
+ */
+ public String getRawInput() {
+ return buffer.getRawInput();
+ }
+
+ /**
+ * Get the value of the parameter with the given name
+ *
+ * @param name the parameter's name
+ * @param <T> expected type
+ * @return the parsed value or the default value
+ */
+ @SuppressWarnings("unchecked")
+ public <T> T get(String name) {
+ if (!parameterValueMap.containsKey(name)) {
+ throw new IllegalArgumentException();
+ }
+
+ try {
+ return (T) parameterValueMap.get(name);
+ } catch (ClassCastException ex) {
+ throw new IllegalArgumentException("Invalid type parameter requested for parameter " + name, ex);
+ }
+ }
+
+ /**
+ * Get the value of the flag with the given name
+ *
+ * @param flag the flag's name, without preceding "-"
+ * @param <T> expected type
+ * @return the parsed value or the default value
+ */
+ public <T> T getFlag(String flag) {
+ return get("-" + flag);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Deprecated
+ public <T> T get(int index) {
+ return null;//get(getParameterList().getIndexedParameterName(index));
+ }
+
+ /**
+ * Checks if the parameter by the name was provided in the command's arguments.
+ *
+ * @param name the parameter name
+ * @return true if it was provided
+ */
+ public boolean isProvided(String name) {
+ return parsedParameters.contains(name);
+ }
+
+ /**
+ * Checks if the parameter by the index was provided in the command's arguments.
+ *
+ * @param index the parameter index
+ * @return true if it was provided
+ */
+ @Deprecated
+ public boolean isProvided(int index) {
+ return false;//isProvided(getParameterList().getIndexedParameterName(index));
+ }
+
+ /**
+ * The parameter to complete.
+ * This parameter is requested suggestions
+ *
+ * @return the parameter to complete.
+ */
+ public Parameter<?, ?> getParameterToComplete() {
+ return parameterToComplete;
+ }
+
+ /**
+ * Get suggested completions.
+ *
+ * @param location The location as passed to {link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}, or null if requested in another way.
+ * @return completions.
+ */
+ public List<String> getSuggestedCompletions(Location location) {
+ if (parameterToComplete != null) {
+ return parameterToComplete.complete(this, location, buffer.getUnaffectingCopy().setCursor(parameterToCompleteCursor));
+ }
+
+ List<String> result = new ArrayList<>();
+ for (String name : parameterValueMap.keySet()) {
+ if (name.startsWith("-") && !parsedParameters.contains(name)) {
+ result.add(name);
+ }
+ }
+ return result;
+ }
+
+ /*
+ Chat handling
+ */
+
+ public void sendMessage(String message) {
+ sendMessage(true, message);
+ }
+
+ public void sendMessage(EMessageType messageType, String message) {
+ sendMessage(messageType, true, message);
+ }
+
+ public void sendMessage(boolean translateColours, String message) {
+ sendMessage(EMessageType.NEUTRAL, translateColours, message);
+ }
+
+ public void sendMessage(EMessageType messageType, boolean translateColours, String message) {
+ if (!muted) {
+ if (translateColours) {
+ message = Formatting.translateChars('&', message);
+ }
+ address.getChatHandler().sendMessage(this, messageType, message);
+ }
+ }
+
+ public void sendMessage(String messageFormat, Object... args) {
+ sendMessage(true, messageFormat, args);
+ }
+
+ public void sendMessage(EMessageType messageType, String messageFormat, Object... args) {
+ sendMessage(messageType, true, messageFormat, args);
+ }
+
+ public void sendMessage(boolean translateColours, String messageFormat, Object... args) {
+ sendMessage(EMessageType.NEUTRAL, translateColours, messageFormat, args);
+ }
+
+ public void sendMessage(EMessageType messageType, boolean translateColours, String messageFormat, Object... args) {
+ sendMessage(messageType, translateColours, String.format(messageFormat, args));
+ }
+
+ public void sendHelpMessage(int page) {
+ if (!muted) {
+ address.getChatHandler().sendHelpMessage(sender, this, address, page);
+ }
+ }
+
+ public void sendSyntaxMessage() {
+ if (!muted) {
+ address.getChatHandler().sendSyntaxMessage(sender, this, address);
+ }
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/ExtendedCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/ExtendedCommand.java index 47c2aca..eb93b30 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/ExtendedCommand.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/ExtendedCommand.java @@ -1,64 +1,64 @@ -package io.dico.dicore.command; - -import io.dico.dicore.command.parameter.IArgumentPreProcessor; -import io.dico.dicore.command.parameter.Parameter; -import io.dico.dicore.command.parameter.type.ParameterType; - -@SuppressWarnings("unchecked") -public abstract class ExtendedCommand<T extends ExtendedCommand<T>> extends Command { - protected boolean modifiable; - - public ExtendedCommand() { - this(true); - } - - public ExtendedCommand(boolean modifiable) { - this.modifiable = modifiable; - } - - protected T newModifiableInstance() { - return (T) this; - } - - @Override - public T addParameter(Parameter<?, ?> parameter) { - return modifiable ? (T) super.addParameter(parameter) : newModifiableInstance().addParameter(parameter); - } - - @Override - public T addContextFilter(IContextFilter contextFilter) { - return modifiable ? (T) super.addContextFilter(contextFilter) : newModifiableInstance().addContextFilter(contextFilter); - } - - @Override - public T removeContextFilter(IContextFilter contextFilter) { - return modifiable ? (T) super.removeContextFilter(contextFilter) : newModifiableInstance().removeContextFilter(contextFilter); - } - - @Override - public T requiredParameters(int requiredParameters) { - return modifiable ? (T) super.requiredParameters(requiredParameters) : newModifiableInstance().requiredParameters(requiredParameters); - } - - @Override - public T repeatFinalParameter() { - return modifiable ? (T) super.repeatFinalParameter() : newModifiableInstance().repeatFinalParameter(); - } - - @Override - public T setDescription(String... description) { - return modifiable ? (T) super.setDescription(description) : newModifiableInstance().setDescription(description); - } - - @Override - public T setShortDescription(String shortDescription) { - return modifiable ? (T) super.setShortDescription(shortDescription) : newModifiableInstance().setShortDescription(shortDescription); - } - - /* - @Override - public T preprocessArguments(IArgumentPreProcessor processor) { - return modifiable ? (T) super.preprocessArguments(processor) : newModifiableInstance().preprocessArguments(processor); - }*/ - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.command.parameter.IArgumentPreProcessor;
+import io.dico.dicore.command.parameter.Parameter;
+import io.dico.dicore.command.parameter.type.ParameterType;
+
+@SuppressWarnings("unchecked")
+public abstract class ExtendedCommand<T extends ExtendedCommand<T>> extends Command {
+ protected boolean modifiable;
+
+ public ExtendedCommand() {
+ this(true);
+ }
+
+ public ExtendedCommand(boolean modifiable) {
+ this.modifiable = modifiable;
+ }
+
+ protected T newModifiableInstance() {
+ return (T) this;
+ }
+
+ @Override
+ public T addParameter(Parameter<?, ?> parameter) {
+ return modifiable ? (T) super.addParameter(parameter) : newModifiableInstance().addParameter(parameter);
+ }
+
+ @Override
+ public T addContextFilter(IContextFilter contextFilter) {
+ return modifiable ? (T) super.addContextFilter(contextFilter) : newModifiableInstance().addContextFilter(contextFilter);
+ }
+
+ @Override
+ public T removeContextFilter(IContextFilter contextFilter) {
+ return modifiable ? (T) super.removeContextFilter(contextFilter) : newModifiableInstance().removeContextFilter(contextFilter);
+ }
+
+ @Override
+ public T requiredParameters(int requiredParameters) {
+ return modifiable ? (T) super.requiredParameters(requiredParameters) : newModifiableInstance().requiredParameters(requiredParameters);
+ }
+
+ @Override
+ public T repeatFinalParameter() {
+ return modifiable ? (T) super.repeatFinalParameter() : newModifiableInstance().repeatFinalParameter();
+ }
+
+ @Override
+ public T setDescription(String... description) {
+ return modifiable ? (T) super.setDescription(description) : newModifiableInstance().setDescription(description);
+ }
+
+ @Override
+ public T setShortDescription(String shortDescription) {
+ return modifiable ? (T) super.setShortDescription(shortDescription) : newModifiableInstance().setShortDescription(shortDescription);
+ }
+
+ /*
+ @Override
+ public T preprocessArguments(IArgumentPreProcessor processor) {
+ return modifiable ? (T) super.preprocessArguments(processor) : newModifiableInstance().preprocessArguments(processor);
+ }*/
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/ICommandAddress.java b/dicore3/command/src/main/java/io/dico/dicore/command/ICommandAddress.java index 158b1f0..f900139 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/ICommandAddress.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/ICommandAddress.java @@ -1,205 +1,205 @@ -package io.dico.dicore.command; - -import io.dico.dicore.command.chat.IChatHandler; -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.parameter.ParameterList; -import io.dico.dicore.command.predef.PredefinedCommand; -import org.bukkit.command.CommandSender; - -import java.util.Collection; -import java.util.List; -import java.util.Map; - -/** - * Interface for an address of a command. - * <p> - * The address holds what the name and aliases of a command are. - * The address also (optionally) holds a reference to a {@link Command} - * <p> - * One instance of {@link Command} can be held by multiple addresses, - * because the address decides what the command's name and aliases are. - * <p> - * The address holds children by key in a map. This map's keys include aliases for its children. - * This creates a tree of addresses. If a command is dispatches, the tree is traversed untill a command is found - * and no children deeper down match the command (there are exceptions to the later as defined by - * {@link Command#takePrecedenceOverSubcommand(String, ArgumentBuffer)} - * and {@link Command#isVisibleTo(CommandSender)} - */ -public interface ICommandAddress { - - /** - * @return true if this address has a parent. - */ - boolean hasParent(); - - /** - * Get the parent of this address - * - * @return the parent of this address, or null if none exists. - */ - ICommandAddress getParent(); - - /** - * @return true if this address has a command. - */ - boolean hasCommand(); - - /** - * @return true if this address has a command that is not an instance of {@link PredefinedCommand} - */ - boolean hasUserDeclaredCommand(); - - /** - * @return Get the command of this address, or null if none exists. - */ - Command getCommand(); - - /** - * @return true if this address is an instance of {@link RootCommandAddress} - */ - boolean isRoot(); - - /** - * @return the root address of the tree which this address resides in. - */ - ICommandAddress getRoot(); - - /** - * A list of the names of this address, at the current level. - * The first entry is the main key, the subsequent ones are aliases. - * <p> - * Untill an address is assigned a parent, this list is mutable. - * <p> - * If {@link #isRoot()}, this returns an immutable, empty list. - * - * @return the list of names. - */ - List<String> getNames(); - - /** - * A list of the aliases of this address. That is, {@link #getNames()} - * without the first entry. - * - * @return a list of aliases - */ - List<String> getAliases(); - - /** - * @return The first element of {@link #getNames()} - */ - String getMainKey(); - - /** - * Get the address of this command. - * That is, the main keys of all commands leading up to this address, and this address itself, separated by a space. - * In other words, the command without the / that is required to target the command at this address. - * - * @return the address of this command. - */ - String getAddress(); - - /** - * Get the amount of addresses that separate this address from the root of the tree, + 1. - * The root of the tree has a depth of 0. Each subsequent child has its depth incremented by 1. - * - * @return The depth of this address - */ - int getDepth(); - - /** - * @return true if the depth of this address is larger than the argument. - */ - boolean isDepthLargerThan(int depth); - - /** - * @return true if this address has any children. - */ - boolean hasChildren(); - - /** - * @return total number of children, not considering any aliases - */ - int getNumberOfRealChildren(); - - /** - * Get an unmodifiable view of all main keys of the children of this address. - * - * @return the main keys - */ - Collection<String> getChildrenMainKeys(); - - /** - * Get an unmodifiable view of the children of this address. - * Values might be duplicated for aliases. - * - * <p> - * To iterate children without duplicates, you can do something like this: - * <pre>{@code - * for (String key : address.getChildrenMainKeys()) { - * ICommandAddress child = address.getChild(key); - * // do stuff with child - * } - * }</pre> - * </p> - * - * @return the children of this address. - */ - Map<String, ? extends ICommandAddress> getChildren(); - - /** - * Query for a child at the given key. - * - * @param key the key. The name or alias of a command. - * @return the child, or null if it's not found - */ - ICommandAddress getChild(String key); - - /** - * Query for a child using the given buffer, with the given context for reference. - * Can be used to override behaviour of the address tree. - * <p> - * The default implementation is as follows: - * <pre>{@code - * return buffer.hasNext() ? getChild(buffer.next()) : null; - * }</pre> - * - * @param context context of a command being executed - * @param buffer the buffer. The name or alias of a command. - * @return the child, or null if it's not found, altered freely by the implementation - */ - ICommandAddress getChild(ExecutionContext context, ArgumentBuffer buffer) throws CommandException; - - /** - * Get the command dispatcher for this tree - * - * @return the command dispatcher - */ - ICommandDispatcher getDispatcherForTree(); - - /** - * @return The desired chathandler for use by commands at this address and any sub-addresses, if they define no explicit chat handler. - */ - IChatHandler getChatHandler(); - - /** - * Returns if the command attached to this address should be treated as trailing. - * A trailing command is executed whenever the address is scanned for children. - * Its parameters are parsed and added to the context. - * - * @return true if the command attached to this address should be treated as trailing. - */ - boolean isCommandTrailing(); - - static ICommandAddress newChild() { - return new ChildCommandAddress(); - } - - static ICommandAddress newChild(Command command) { - return new ChildCommandAddress(command); - } - - static ICommandAddress newRoot() { - return new RootCommandAddress(); - } - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.command.chat.IChatHandler;
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.parameter.ParameterList;
+import io.dico.dicore.command.predef.PredefinedCommand;
+import org.bukkit.command.CommandSender;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Interface for an address of a command.
+ * <p>
+ * The address holds what the name and aliases of a command are.
+ * The address also (optionally) holds a reference to a {@link Command}
+ * <p>
+ * One instance of {@link Command} can be held by multiple addresses,
+ * because the address decides what the command's name and aliases are.
+ * <p>
+ * The address holds children by key in a map. This map's keys include aliases for its children.
+ * This creates a tree of addresses. If a command is dispatches, the tree is traversed untill a command is found
+ * and no children deeper down match the command (there are exceptions to the later as defined by
+ * {@link Command#takePrecedenceOverSubcommand(String, ArgumentBuffer)}
+ * and {@link Command#isVisibleTo(CommandSender)}
+ */
+public interface ICommandAddress {
+
+ /**
+ * @return true if this address has a parent.
+ */
+ boolean hasParent();
+
+ /**
+ * Get the parent of this address
+ *
+ * @return the parent of this address, or null if none exists.
+ */
+ ICommandAddress getParent();
+
+ /**
+ * @return true if this address has a command.
+ */
+ boolean hasCommand();
+
+ /**
+ * @return true if this address has a command that is not an instance of {@link PredefinedCommand}
+ */
+ boolean hasUserDeclaredCommand();
+
+ /**
+ * @return Get the command of this address, or null if none exists.
+ */
+ Command getCommand();
+
+ /**
+ * @return true if this address is an instance of {@link RootCommandAddress}
+ */
+ boolean isRoot();
+
+ /**
+ * @return the root address of the tree which this address resides in.
+ */
+ ICommandAddress getRoot();
+
+ /**
+ * A list of the names of this address, at the current level.
+ * The first entry is the main key, the subsequent ones are aliases.
+ * <p>
+ * Untill an address is assigned a parent, this list is mutable.
+ * <p>
+ * If {@link #isRoot()}, this returns an immutable, empty list.
+ *
+ * @return the list of names.
+ */
+ List<String> getNames();
+
+ /**
+ * A list of the aliases of this address. That is, {@link #getNames()}
+ * without the first entry.
+ *
+ * @return a list of aliases
+ */
+ List<String> getAliases();
+
+ /**
+ * @return The first element of {@link #getNames()}
+ */
+ String getMainKey();
+
+ /**
+ * Get the address of this command.
+ * That is, the main keys of all commands leading up to this address, and this address itself, separated by a space.
+ * In other words, the command without the / that is required to target the command at this address.
+ *
+ * @return the address of this command.
+ */
+ String getAddress();
+
+ /**
+ * Get the amount of addresses that separate this address from the root of the tree, + 1.
+ * The root of the tree has a depth of 0. Each subsequent child has its depth incremented by 1.
+ *
+ * @return The depth of this address
+ */
+ int getDepth();
+
+ /**
+ * @return true if the depth of this address is larger than the argument.
+ */
+ boolean isDepthLargerThan(int depth);
+
+ /**
+ * @return true if this address has any children.
+ */
+ boolean hasChildren();
+
+ /**
+ * @return total number of children, not considering any aliases
+ */
+ int getNumberOfRealChildren();
+
+ /**
+ * Get an unmodifiable view of all main keys of the children of this address.
+ *
+ * @return the main keys
+ */
+ Collection<String> getChildrenMainKeys();
+
+ /**
+ * Get an unmodifiable view of the children of this address.
+ * Values might be duplicated for aliases.
+ *
+ * <p>
+ * To iterate children without duplicates, you can do something like this:
+ * <pre>{@code
+ * for (String key : address.getChildrenMainKeys()) {
+ * ICommandAddress child = address.getChild(key);
+ * // do stuff with child
+ * }
+ * }</pre>
+ * </p>
+ *
+ * @return the children of this address.
+ */
+ Map<String, ? extends ICommandAddress> getChildren();
+
+ /**
+ * Query for a child at the given key.
+ *
+ * @param key the key. The name or alias of a command.
+ * @return the child, or null if it's not found
+ */
+ ICommandAddress getChild(String key);
+
+ /**
+ * Query for a child using the given buffer, with the given context for reference.
+ * Can be used to override behaviour of the address tree.
+ * <p>
+ * The default implementation is as follows:
+ * <pre>{@code
+ * return buffer.hasNext() ? getChild(buffer.next()) : null;
+ * }</pre>
+ *
+ * @param context context of a command being executed
+ * @param buffer the buffer. The name or alias of a command.
+ * @return the child, or null if it's not found, altered freely by the implementation
+ */
+ ICommandAddress getChild(ExecutionContext context, ArgumentBuffer buffer) throws CommandException;
+
+ /**
+ * Get the command dispatcher for this tree
+ *
+ * @return the command dispatcher
+ */
+ ICommandDispatcher getDispatcherForTree();
+
+ /**
+ * @return The desired chathandler for use by commands at this address and any sub-addresses, if they define no explicit chat handler.
+ */
+ IChatHandler getChatHandler();
+
+ /**
+ * Returns if the command attached to this address should be treated as trailing.
+ * A trailing command is executed whenever the address is scanned for children.
+ * Its parameters are parsed and added to the context.
+ *
+ * @return true if the command attached to this address should be treated as trailing.
+ */
+ boolean isCommandTrailing();
+
+ static ICommandAddress newChild() {
+ return new ChildCommandAddress();
+ }
+
+ static ICommandAddress newChild(Command command) {
+ return new ChildCommandAddress(command);
+ }
+
+ static ICommandAddress newRoot() {
+ return new RootCommandAddress();
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/ICommandDispatcher.java b/dicore3/command/src/main/java/io/dico/dicore/command/ICommandDispatcher.java index 055171d..4f3ffc6 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/ICommandDispatcher.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/ICommandDispatcher.java @@ -1,146 +1,146 @@ -package io.dico.dicore.command; - -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.registration.CommandMap; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; - -import java.util.List; -import java.util.Map; - -public interface ICommandDispatcher { - - /** - * Get a potentially indirect child of the root of this dispatcher - * - * @param buffer the argument buffer with the subsequent keys to traverse. Any keys beyond the first that isn't found are ignored. - * @return the child, or this same instance of no child is found. - */ - ICommandAddress getDeepChild(ArgumentBuffer buffer); - - /** - * Similar to {@link #getDeepChild(ArgumentBuffer)}, - * but this method incorporates checks on the command of traversed children: - * {@link Command#isVisibleTo(CommandSender)} - * and {@link Command#takePrecedenceOverSubcommand(String, ArgumentBuffer)} - * <p> - * The target of a command is never null, however, the same instance might be returned, and the returned address might not hold a command. - * - * @param sender the sender of the command - * @param buffer the command itself as a buffer. - * @return the address that is the target of the command. - */ - @Deprecated - ICommandAddress getCommandTarget(CommandSender sender, ArgumentBuffer buffer); - - /** - * Similar to {@link #getDeepChild(ArgumentBuffer)}, - * but this method incorporates checks on the command of traversed children: - * {@link Command#isVisibleTo(CommandSender)} - * and {@link Command#takePrecedenceOverSubcommand(String, ArgumentBuffer)} - * <p> - * The target of a command is never null, however, the same instance might be returned, and the returned address might not hold a command. - * - * @param context the context of the command. The context must not have its address set. - * @param buffer the command itself as a buffer. - * @return the address that is the target of the command. - */ - ICommandAddress getCommandTarget(ExecutionContext context, ArgumentBuffer buffer) throws CommandException; - - /** - * dispatch the command - * - * @param sender the sender - * @param command the command - * @return true if a command has executed - */ - boolean dispatchCommand(CommandSender sender, String[] command); - - /** - * dispatch the command - * - * @param sender the sender - * @param usedLabel the label (word after the /) - * @param args the arguments - * @return true if a command has executed - */ - boolean dispatchCommand(CommandSender sender, String usedLabel, String[] args); - - /** - * dispatch the command - * - * @param sender the sender - * @param buffer the command - * @return true if a command has executed - */ - boolean dispatchCommand(CommandSender sender, ArgumentBuffer buffer); - - /** - * suggest tab completions - * - * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * @param args the arguments as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * args must be sanitized such that it contains no empty elements, particularly at the last index. - * @return tab completions - */ - List<String> getTabCompletions(CommandSender sender, Location location, String[] args); - - /** - * suggest tab completions - * - * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * @param usedLabel the label as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * @param args the arguments as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * @return tab completions - */ - List<String> getTabCompletions(CommandSender sender, String usedLabel, Location location, String[] args); - - /** - * suggest tab completions - * - * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)} - * @param buffer the arguments as a buffer - * @return tab completions - */ - List<String> getTabCompletions(CommandSender sender, Location location, ArgumentBuffer buffer); - - /** - * Register this dispatcher's commands to the command map - * - * @throws UnsupportedOperationException if this dispatcher is not the root of the tree - */ - default void registerToCommandMap() { - registerToCommandMap(null, CommandMap.getCommandMap(), EOverridePolicy.OVERRIDE_ALL); - } - - /** - * Register this dispatcher's commands to the command map - * - * @param fallbackPrefix the fallback prefix to use, null if none - * @param overridePolicy the override policy - * @throws UnsupportedOperationException if this dispatcher is not the root of the tree - */ - default void registerToCommandMap(String fallbackPrefix, EOverridePolicy overridePolicy) { - registerToCommandMap(fallbackPrefix, CommandMap.getCommandMap(), overridePolicy); - } - - /** - * Register this dispatcher's commands to the command map - * - * @param fallbackPrefix the fallback prefix to use, null if none - * @param map the command map - * @param overridePolicy the override policy - * @throws UnsupportedOperationException if this dispatcher is not the root of the tree - */ - void registerToCommandMap(String fallbackPrefix, Map<String, org.bukkit.command.Command> map, EOverridePolicy overridePolicy); - - default void unregisterFromCommandMap() { - unregisterFromCommandMap(CommandMap.getCommandMap()); - } - - void unregisterFromCommandMap(Map<String, org.bukkit.command.Command> map); - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.registration.CommandMap;
+import org.bukkit.Location;
+import org.bukkit.command.CommandSender;
+
+import java.util.List;
+import java.util.Map;
+
+public interface ICommandDispatcher {
+
+ /**
+ * Get a potentially indirect child of the root of this dispatcher
+ *
+ * @param buffer the argument buffer with the subsequent keys to traverse. Any keys beyond the first that isn't found are ignored.
+ * @return the child, or this same instance of no child is found.
+ */
+ ICommandAddress getDeepChild(ArgumentBuffer buffer);
+
+ /**
+ * Similar to {@link #getDeepChild(ArgumentBuffer)},
+ * but this method incorporates checks on the command of traversed children:
+ * {@link Command#isVisibleTo(CommandSender)}
+ * and {@link Command#takePrecedenceOverSubcommand(String, ArgumentBuffer)}
+ * <p>
+ * The target of a command is never null, however, the same instance might be returned, and the returned address might not hold a command.
+ *
+ * @param sender the sender of the command
+ * @param buffer the command itself as a buffer.
+ * @return the address that is the target of the command.
+ */
+ @Deprecated
+ ICommandAddress getCommandTarget(CommandSender sender, ArgumentBuffer buffer);
+
+ /**
+ * Similar to {@link #getDeepChild(ArgumentBuffer)},
+ * but this method incorporates checks on the command of traversed children:
+ * {@link Command#isVisibleTo(CommandSender)}
+ * and {@link Command#takePrecedenceOverSubcommand(String, ArgumentBuffer)}
+ * <p>
+ * The target of a command is never null, however, the same instance might be returned, and the returned address might not hold a command.
+ *
+ * @param context the context of the command. The context must not have its address set.
+ * @param buffer the command itself as a buffer.
+ * @return the address that is the target of the command.
+ */
+ ICommandAddress getCommandTarget(ExecutionContext context, ArgumentBuffer buffer) throws CommandException;
+
+ /**
+ * dispatch the command
+ *
+ * @param sender the sender
+ * @param command the command
+ * @return true if a command has executed
+ */
+ boolean dispatchCommand(CommandSender sender, String[] command);
+
+ /**
+ * dispatch the command
+ *
+ * @param sender the sender
+ * @param usedLabel the label (word after the /)
+ * @param args the arguments
+ * @return true if a command has executed
+ */
+ boolean dispatchCommand(CommandSender sender, String usedLabel, String[] args);
+
+ /**
+ * dispatch the command
+ *
+ * @param sender the sender
+ * @param buffer the command
+ * @return true if a command has executed
+ */
+ boolean dispatchCommand(CommandSender sender, ArgumentBuffer buffer);
+
+ /**
+ * suggest tab completions
+ *
+ * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * @param args the arguments as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * args must be sanitized such that it contains no empty elements, particularly at the last index.
+ * @return tab completions
+ */
+ List<String> getTabCompletions(CommandSender sender, Location location, String[] args);
+
+ /**
+ * suggest tab completions
+ *
+ * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * @param usedLabel the label as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * @param args the arguments as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * @return tab completions
+ */
+ List<String> getTabCompletions(CommandSender sender, String usedLabel, Location location, String[] args);
+
+ /**
+ * suggest tab completions
+ *
+ * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
+ * @param buffer the arguments as a buffer
+ * @return tab completions
+ */
+ List<String> getTabCompletions(CommandSender sender, Location location, ArgumentBuffer buffer);
+
+ /**
+ * Register this dispatcher's commands to the command map
+ *
+ * @throws UnsupportedOperationException if this dispatcher is not the root of the tree
+ */
+ default void registerToCommandMap() {
+ registerToCommandMap(null, CommandMap.getCommandMap(), EOverridePolicy.OVERRIDE_ALL);
+ }
+
+ /**
+ * Register this dispatcher's commands to the command map
+ *
+ * @param fallbackPrefix the fallback prefix to use, null if none
+ * @param overridePolicy the override policy
+ * @throws UnsupportedOperationException if this dispatcher is not the root of the tree
+ */
+ default void registerToCommandMap(String fallbackPrefix, EOverridePolicy overridePolicy) {
+ registerToCommandMap(fallbackPrefix, CommandMap.getCommandMap(), overridePolicy);
+ }
+
+ /**
+ * Register this dispatcher's commands to the command map
+ *
+ * @param fallbackPrefix the fallback prefix to use, null if none
+ * @param map the command map
+ * @param overridePolicy the override policy
+ * @throws UnsupportedOperationException if this dispatcher is not the root of the tree
+ */
+ void registerToCommandMap(String fallbackPrefix, Map<String, org.bukkit.command.Command> map, EOverridePolicy overridePolicy);
+
+ default void unregisterFromCommandMap() {
+ unregisterFromCommandMap(CommandMap.getCommandMap());
+ }
+
+ void unregisterFromCommandMap(Map<String, org.bukkit.command.Command> map);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/IContextFilter.java b/dicore3/command/src/main/java/io/dico/dicore/command/IContextFilter.java index 5c05e27..bf34873 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/IContextFilter.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/IContextFilter.java @@ -1,201 +1,201 @@ -package io.dico.dicore.command; - -import io.dico.dicore.exceptions.checkedfunctions.CheckedConsumer; -import io.dico.dicore.exceptions.checkedfunctions.CheckedRunnable; -import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.NotNull; - -public interface IContextFilter extends Comparable<IContextFilter> { - - /** - * Filter the given context by this filter's criteria. - * If the context does not match the criteria, an exception is thrown describing the problem. - * - * @param context the context to match - * @throws CommandException if it doesn't match - */ - void filterContext(ExecutionContext context) throws CommandException; - - /** - * Filter an execution context for a direct or indirect sub command of the command that registered this filter. - * - * @param subContext the context for the execution - * @param path the path traversed from the command that registered this filter to the executed command - */ - default void filterSubContext(ExecutionContext subContext, String... path) throws CommandException { - filterContext(subContext); - } - - /** - * Get the priority of this context filter. - * The priorities determine the order in which a command's context filters are executed. - * - * @return the priority - */ - Priority getPriority(); - - default boolean allowsContext(ExecutionContext context) { - try { - filterContext(context); - return true; - } catch (CommandException ex) { - return false; - } - } - - /** - * Used to sort filters in execution order. That is, filters are ordered by {@link #getPriority()} - * - * @param o compared filter - * @return comparison value - */ - @Override - default int compareTo(@NotNull IContextFilter o) { - return getPriority().compareTo(o.getPriority()); - } - - /* - default boolean isInheritable() { - return false; - } - - default IContextFilter inherit(String... components) { - if (!isInheritable()) { - throw new IllegalStateException("This IContextFilter cannot be inherited"); - } - - return this; - }*/ - - /** - * IContextFilter priorities. Executes from top to bottom. - */ - enum Priority { - /** - * This priority should have checks on the sender type. - * Any filters on this priority are tested before permissions are. - * This is the highest priority. - */ - VERY_EARLY, // sender type check - - /** - * This priority is specific to permissions. - */ - PERMISSION, - - /** - * Early priority. Post permissions, pre parameter-parsing. - */ - EARLY, - - /** - * Normal priority. Post permissions, pre parameter-parsing. - */ - NORMAL, - - /** - * Late priority. Post permissions, pre parameter-parsing. - */ - LATE, - - /** - * Very late priority. Post permissions, pre parameter-parsing. - */ - VERY_LATE, - - /** - * Post parameters priority. Post permissions, post parameter-parsing. - * This is the lowest priority. - */ - POST_PARAMETERS; - - private IContextFilter inheritor; - - /** - * Get the context filter that inherits context filters from the parent of the same priority. - * If this filter is also present at the parent, it will do the same for the parent's parent, and so on. - * - * @return the inheritor - */ - public IContextFilter getInheritor() { - if (inheritor == null) { - inheritor = InheritingContextFilter.inheritingPriority(this); - } - return inheritor; - } - - } - - /** - * Ensures that only {@link org.bukkit.entity.Player} type senders can execute the command. - */ - IContextFilter PLAYER_ONLY = filterSender(Priority.VERY_EARLY, Validate::isPlayer); - - /** - * Ensures that only {@link org.bukkit.command.ConsoleCommandSender} type senders can execute the command. - */ - IContextFilter CONSOLE_ONLY = filterSender(Priority.VERY_EARLY, Validate::isConsole); - - /** - * This filter is not working as intended. - * <p> - * There is supposed to be a permission filter that takes a base, and appends the command's address to the base, and checks that permission. - */ - IContextFilter INHERIT_PERMISSIONS = Priority.PERMISSION.getInheritor(); - - static IContextFilter fromCheckedRunnable(Priority priority, CheckedRunnable<? extends CommandException> runnable) { - return new IContextFilter() { - @Override - public void filterContext(ExecutionContext context) throws CommandException { - runnable.checkedRun(); - } - - @Override - public Priority getPriority() { - return priority; - } - }; - } - - static IContextFilter filterSender(Priority priority, CheckedConsumer<? super CommandSender, ? extends CommandException> consumer) { - return new IContextFilter() { - @Override - public void filterContext(ExecutionContext context) throws CommandException { - consumer.checkedAccept(context.getSender()); - } - - @Override - public Priority getPriority() { - return priority; - } - }; - } - - static IContextFilter permission(String permission) { - return new PermissionContextFilter(permission); - } - - static IContextFilter permission(String permission, String failMessage) { - return new PermissionContextFilter(permission, failMessage); - } - - static IContextFilter inheritablePermission(String permission) { - return new PermissionContextFilter(permission, true); - } - - /** - * Produce an inheritable permission context filter. - * A permission component is an element in {@code permission.split("\\.")} - * - * @param permission The permission that is required for the command that this is directly assigned to - * @param componentInsertionIndex the index where any sub-components are inserted. -1 for "at the end". - * @param failMessage the message to send if the permission is not met - * @return the context filter - * @throws IllegalArgumentException if componentInsertionIndex is out of range - */ - static IContextFilter inheritablePermission(String permission, int componentInsertionIndex, String failMessage) { - return new PermissionContextFilter(permission, componentInsertionIndex, failMessage); - } - -} - +package io.dico.dicore.command;
+
+import io.dico.dicore.exceptions.checkedfunctions.CheckedConsumer;
+import io.dico.dicore.exceptions.checkedfunctions.CheckedRunnable;
+import org.bukkit.command.CommandSender;
+import org.jetbrains.annotations.NotNull;
+
+public interface IContextFilter extends Comparable<IContextFilter> {
+
+ /**
+ * Filter the given context by this filter's criteria.
+ * If the context does not match the criteria, an exception is thrown describing the problem.
+ *
+ * @param context the context to match
+ * @throws CommandException if it doesn't match
+ */
+ void filterContext(ExecutionContext context) throws CommandException;
+
+ /**
+ * Filter an execution context for a direct or indirect sub command of the command that registered this filter.
+ *
+ * @param subContext the context for the execution
+ * @param path the path traversed from the command that registered this filter to the executed command
+ */
+ default void filterSubContext(ExecutionContext subContext, String... path) throws CommandException {
+ filterContext(subContext);
+ }
+
+ /**
+ * Get the priority of this context filter.
+ * The priorities determine the order in which a command's context filters are executed.
+ *
+ * @return the priority
+ */
+ Priority getPriority();
+
+ default boolean allowsContext(ExecutionContext context) {
+ try {
+ filterContext(context);
+ return true;
+ } catch (CommandException ex) {
+ return false;
+ }
+ }
+
+ /**
+ * Used to sort filters in execution order. That is, filters are ordered by {@link #getPriority()}
+ *
+ * @param o compared filter
+ * @return comparison value
+ */
+ @Override
+ default int compareTo(@NotNull IContextFilter o) {
+ return getPriority().compareTo(o.getPriority());
+ }
+
+ /*
+ default boolean isInheritable() {
+ return false;
+ }
+
+ default IContextFilter inherit(String... components) {
+ if (!isInheritable()) {
+ throw new IllegalStateException("This IContextFilter cannot be inherited");
+ }
+
+ return this;
+ }*/
+
+ /**
+ * IContextFilter priorities. Executes from top to bottom.
+ */
+ enum Priority {
+ /**
+ * This priority should have checks on the sender type.
+ * Any filters on this priority are tested before permissions are.
+ * This is the highest priority.
+ */
+ VERY_EARLY, // sender type check
+
+ /**
+ * This priority is specific to permissions.
+ */
+ PERMISSION,
+
+ /**
+ * Early priority. Post permissions, pre parameter-parsing.
+ */
+ EARLY,
+
+ /**
+ * Normal priority. Post permissions, pre parameter-parsing.
+ */
+ NORMAL,
+
+ /**
+ * Late priority. Post permissions, pre parameter-parsing.
+ */
+ LATE,
+
+ /**
+ * Very late priority. Post permissions, pre parameter-parsing.
+ */
+ VERY_LATE,
+
+ /**
+ * Post parameters priority. Post permissions, post parameter-parsing.
+ * This is the lowest priority.
+ */
+ POST_PARAMETERS;
+
+ private IContextFilter inheritor;
+
+ /**
+ * Get the context filter that inherits context filters from the parent of the same priority.
+ * If this filter is also present at the parent, it will do the same for the parent's parent, and so on.
+ *
+ * @return the inheritor
+ */
+ public IContextFilter getInheritor() {
+ if (inheritor == null) {
+ inheritor = InheritingContextFilter.inheritingPriority(this);
+ }
+ return inheritor;
+ }
+
+ }
+
+ /**
+ * Ensures that only {@link org.bukkit.entity.Player} type senders can execute the command.
+ */
+ IContextFilter PLAYER_ONLY = filterSender(Priority.VERY_EARLY, Validate::isPlayer);
+
+ /**
+ * Ensures that only {@link org.bukkit.command.ConsoleCommandSender} type senders can execute the command.
+ */
+ IContextFilter CONSOLE_ONLY = filterSender(Priority.VERY_EARLY, Validate::isConsole);
+
+ /**
+ * This filter is not working as intended.
+ * <p>
+ * There is supposed to be a permission filter that takes a base, and appends the command's address to the base, and checks that permission.
+ */
+ IContextFilter INHERIT_PERMISSIONS = Priority.PERMISSION.getInheritor();
+
+ static IContextFilter fromCheckedRunnable(Priority priority, CheckedRunnable<? extends CommandException> runnable) {
+ return new IContextFilter() {
+ @Override
+ public void filterContext(ExecutionContext context) throws CommandException {
+ runnable.checkedRun();
+ }
+
+ @Override
+ public Priority getPriority() {
+ return priority;
+ }
+ };
+ }
+
+ static IContextFilter filterSender(Priority priority, CheckedConsumer<? super CommandSender, ? extends CommandException> consumer) {
+ return new IContextFilter() {
+ @Override
+ public void filterContext(ExecutionContext context) throws CommandException {
+ consumer.checkedAccept(context.getSender());
+ }
+
+ @Override
+ public Priority getPriority() {
+ return priority;
+ }
+ };
+ }
+
+ static IContextFilter permission(String permission) {
+ return new PermissionContextFilter(permission);
+ }
+
+ static IContextFilter permission(String permission, String failMessage) {
+ return new PermissionContextFilter(permission, failMessage);
+ }
+
+ static IContextFilter inheritablePermission(String permission) {
+ return new PermissionContextFilter(permission, true);
+ }
+
+ /**
+ * Produce an inheritable permission context filter.
+ * A permission component is an element in {@code permission.split("\\.")}
+ *
+ * @param permission The permission that is required for the command that this is directly assigned to
+ * @param componentInsertionIndex the index where any sub-components are inserted. -1 for "at the end".
+ * @param failMessage the message to send if the permission is not met
+ * @return the context filter
+ * @throws IllegalArgumentException if componentInsertionIndex is out of range
+ */
+ static IContextFilter inheritablePermission(String permission, int componentInsertionIndex, String failMessage) {
+ return new PermissionContextFilter(permission, componentInsertionIndex, failMessage);
+ }
+
+}
+
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/InheritingContextFilter.java b/dicore3/command/src/main/java/io/dico/dicore/command/InheritingContextFilter.java index 0b4875b..58cd7a6 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/InheritingContextFilter.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/InheritingContextFilter.java @@ -1,64 +1,64 @@ -package io.dico.dicore.command; - -import java.util.List; - -public abstract class InheritingContextFilter implements IContextFilter { - private static final String[] emptyStringArray = new String[0]; - - private static String[] addParent(String[] path, String parent) { - String[] out = new String[path.length + 1]; - System.arraycopy(path, 0, out, 1, path.length); - out[0] = parent; - return out; - } - - protected abstract boolean isInherited(IContextFilter filter); - - @Override - public void filterContext(ExecutionContext context) throws CommandException { - ICommandAddress address = context.getAddress(); - - String[] traversedPath = emptyStringArray; - do { - traversedPath = addParent(traversedPath, address.getMainKey()); - address = address.getParent(); - - if (address != null && address.hasCommand()) { - boolean doBreak = true; - - Command command = address.getCommand(); - List<IContextFilter> contextFilterList = command.getContextFilters(); - for (IContextFilter filter : contextFilterList) { - if (isInherited(filter)) { - if (filter == this) { - // do the same for next parent - // this method is necessary to keep traversedPath information - doBreak = false; - } else { - filter.filterSubContext(context, traversedPath); - } - } - } - - if (doBreak) { - break; - } - } - } while (address != null); - } - - static InheritingContextFilter inheritingPriority(Priority priority) { - return new InheritingContextFilter() { - @Override - protected boolean isInherited(IContextFilter filter) { - return filter.getPriority() == priority; - } - - @Override - public Priority getPriority() { - return priority; - } - }; - } - -} +package io.dico.dicore.command;
+
+import java.util.List;
+
+public abstract class InheritingContextFilter implements IContextFilter {
+ private static final String[] emptyStringArray = new String[0];
+
+ private static String[] addParent(String[] path, String parent) {
+ String[] out = new String[path.length + 1];
+ System.arraycopy(path, 0, out, 1, path.length);
+ out[0] = parent;
+ return out;
+ }
+
+ protected abstract boolean isInherited(IContextFilter filter);
+
+ @Override
+ public void filterContext(ExecutionContext context) throws CommandException {
+ ICommandAddress address = context.getAddress();
+
+ String[] traversedPath = emptyStringArray;
+ do {
+ traversedPath = addParent(traversedPath, address.getMainKey());
+ address = address.getParent();
+
+ if (address != null && address.hasCommand()) {
+ boolean doBreak = true;
+
+ Command command = address.getCommand();
+ List<IContextFilter> contextFilterList = command.getContextFilters();
+ for (IContextFilter filter : contextFilterList) {
+ if (isInherited(filter)) {
+ if (filter == this) {
+ // do the same for next parent
+ // this method is necessary to keep traversedPath information
+ doBreak = false;
+ } else {
+ filter.filterSubContext(context, traversedPath);
+ }
+ }
+ }
+
+ if (doBreak) {
+ break;
+ }
+ }
+ } while (address != null);
+ }
+
+ static InheritingContextFilter inheritingPriority(Priority priority) {
+ return new InheritingContextFilter() {
+ @Override
+ protected boolean isInherited(IContextFilter filter) {
+ return filter.getPriority() == priority;
+ }
+
+ @Override
+ public Priority getPriority() {
+ return priority;
+ }
+ };
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/LambdaCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/LambdaCommand.java index 71b5ca4..5f55122 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/LambdaCommand.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/LambdaCommand.java @@ -1,35 +1,35 @@ -package io.dico.dicore.command; - -import io.dico.dicore.exceptions.checkedfunctions.CheckedBiFunction; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; - -import java.util.List; -import java.util.Objects; -import java.util.function.BiFunction; - -public class LambdaCommand extends ExtendedCommand<LambdaCommand> { - private CheckedBiFunction<CommandSender, ExecutionContext, String, CommandException> executor; - private BiFunction<CommandSender, ExecutionContext, List<String>> completer; - - public LambdaCommand executor(CheckedBiFunction<CommandSender, ExecutionContext, String, CommandException> executor) { - this.executor = Objects.requireNonNull(executor); - return this; - } - - public LambdaCommand completer(BiFunction<CommandSender, ExecutionContext, List<String>> completer) { - this.completer = Objects.requireNonNull(completer); - return this; - } - - @Override - public String execute(CommandSender sender, ExecutionContext context) throws CommandException { - return executor.checkedApply(sender, context); - } - - @Override - public List<String> tabComplete(CommandSender sender, ExecutionContext context, Location location) { - return completer == null ? super.tabComplete(sender, context, location) : completer.apply(sender, context); - } - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.exceptions.checkedfunctions.CheckedBiFunction;
+import org.bukkit.Location;
+import org.bukkit.command.CommandSender;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.function.BiFunction;
+
+public class LambdaCommand extends ExtendedCommand<LambdaCommand> {
+ private CheckedBiFunction<CommandSender, ExecutionContext, String, CommandException> executor;
+ private BiFunction<CommandSender, ExecutionContext, List<String>> completer;
+
+ public LambdaCommand executor(CheckedBiFunction<CommandSender, ExecutionContext, String, CommandException> executor) {
+ this.executor = Objects.requireNonNull(executor);
+ return this;
+ }
+
+ public LambdaCommand completer(BiFunction<CommandSender, ExecutionContext, List<String>> completer) {
+ this.completer = Objects.requireNonNull(completer);
+ return this;
+ }
+
+ @Override
+ public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
+ return executor.checkedApply(sender, context);
+ }
+
+ @Override
+ public List<String> tabComplete(CommandSender sender, ExecutionContext context, Location location) {
+ return completer == null ? super.tabComplete(sender, context, location) : completer.apply(sender, context);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/ModifiableCommandAddress.java b/dicore3/command/src/main/java/io/dico/dicore/command/ModifiableCommandAddress.java index 0cfd755..70e3e03 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/ModifiableCommandAddress.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/ModifiableCommandAddress.java @@ -1,312 +1,312 @@ -package io.dico.dicore.command; - -import io.dico.dicore.command.chat.ChatHandlers; -import io.dico.dicore.command.chat.IChatHandler; -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.parameter.ParameterList; -import io.dico.dicore.command.predef.DefaultGroupCommand; -import io.dico.dicore.command.predef.HelpCommand; -import io.dico.dicore.command.predef.PredefinedCommand; - -import java.util.*; - -public abstract class ModifiableCommandAddress implements ICommandAddress { - Map<String, ChildCommandAddress> children; - Collection<String> childrenMainKeys = Collections.emptyList(); - - // the chat handler as configured by the programmer - IChatHandler chatHandler; - // cache for the algorithm that finds the first chat handler going up the tree - transient IChatHandler cachedChatHandlerFallback; - - ModifiableCommandAddress helpChild; - - public ModifiableCommandAddress() { - this.children = new LinkedHashMap<>(4); - } - - @Override - public boolean hasParent() { - return getParent() != null; - } - - @Override - public boolean hasCommand() { - return getCommand() != null; - } - - @Override - public boolean hasUserDeclaredCommand() { - Command command = getCommand(); - return command != null && !(command instanceof PredefinedCommand); - } - - @Override - public Command getCommand() { - return null; - } - - @Override - public boolean isRoot() { - return false; - } - - @Override - public List<String> getNames() { - return null; - } - - @Override - public List<String> getAliases() { - List<String> names = getNames(); - if (names == null) { - return null; - } - if (names.isEmpty()) { - return Collections.emptyList(); - } - return names.subList(1, names.size()); - } - - @Override - public String getMainKey() { - return null; - } - - public void setCommand(Command command) { - throw new UnsupportedOperationException(); - } - - @Override - public abstract ModifiableCommandAddress getParent(); - - @Override - public RootCommandAddress getRoot() { - ModifiableCommandAddress out = this; - while (out.hasParent()) { - out = out.getParent(); - } - return out.isRoot() ? (RootCommandAddress) out : null; - } - - @Override - public int getDepth() { - int depth = 0; - ICommandAddress address = this; - while (address.hasParent()) { - address = address.getParent(); - depth++; - } - return depth; - } - - @Override - public boolean isDepthLargerThan(int value) { - int depth = 0; - ICommandAddress address = this; - do { - if (depth > value) { - return true; - } - - address = address.getParent(); - depth++; - } while (address != null); - return false; - } - - @Override - public boolean hasChildren() { - return !children.isEmpty(); - } - - @Override - public int getNumberOfRealChildren() { - return childrenMainKeys.size(); - } - - @Override - public Collection<String> getChildrenMainKeys() { - return Collections.unmodifiableCollection(childrenMainKeys); - } - - @Override - public Map<String, ? extends ModifiableCommandAddress> getChildren() { - return Collections.unmodifiableMap(children); - } - - @Override - public ChildCommandAddress getChild(String key) { - return children.get(key); - } - - @Override - public ChildCommandAddress getChild(ExecutionContext context, ArgumentBuffer buffer) throws CommandException { - return buffer.hasNext() ? getChild(buffer.next()) : null; - } - - public void addChild(ICommandAddress child) { - if (!(child instanceof ChildCommandAddress)) { - throw new IllegalArgumentException("Argument must be a ChildCommandAddress"); - } - - ChildCommandAddress mChild = (ChildCommandAddress) child; - if (mChild.parent != null) { - throw new IllegalArgumentException("Argument already has a parent"); - } - - if (mChild.names.isEmpty()) { - throw new IllegalArgumentException("Argument must have names"); - } - - Iterator<String> names = mChild.modifiableNamesIterator(); - String mainKey = names.next(); - - if (!childrenMainKeys.contains(mainKey)) { - if (!(childrenMainKeys instanceof ArrayList)) { - childrenMainKeys = new ArrayList<>(); - } - childrenMainKeys.add(mainKey); - } - - children.put(mainKey, mChild); - - while (names.hasNext()) { - String name = names.next(); - if (children.putIfAbsent(name, mChild) != null) { - names.remove(); - } - } - - mChild.setParent(this); - - if (mChild.hasCommand() && mChild.getCommand() instanceof HelpCommand) { - helpChild = mChild; - } - } - - public void removeChildren(boolean removeAliases, String... keys) { - if (keys.length == 0) { - throw new IllegalArgumentException("keys is empty"); - } - - for (String key : keys) { - ChildCommandAddress keyTarget = getChild(key); - if (keyTarget == null) { - continue; - } - - if (removeAliases) { - Iterator<String> iterator = keyTarget.namesModifiable.iterator(); - boolean first = true; - while (iterator.hasNext()) { - String alias = iterator.next(); - ChildCommandAddress aliasTarget = getChild(key); - if (aliasTarget == keyTarget) { - if (first) { - childrenMainKeys.remove(alias); - } - children.remove(alias); - } - iterator.remove(); - first = false; - } - - } else { - if (key.equals(keyTarget.getMainKey())) { - childrenMainKeys.remove(key); - } - - children.remove(key); - keyTarget.namesModifiable.remove(key); - } - } - } - - public boolean hasHelpCommand() { - return helpChild != null; - } - - public ModifiableCommandAddress getHelpCommand() { - return helpChild; - } - - @Override - public IChatHandler getChatHandler() { - if (cachedChatHandlerFallback == null) { - if (chatHandler != null) { - cachedChatHandlerFallback = chatHandler; - } else if (!hasParent()) { - cachedChatHandlerFallback = ChatHandlers.defaultChat(); - } else { - cachedChatHandlerFallback = getParent().getChatHandler(); - } - } - return cachedChatHandlerFallback; - } - - public void setChatHandler(IChatHandler chatHandler) { - this.chatHandler = chatHandler; - resetChatHandlerCache(new HashSet<>()); - } - - void resetChatHandlerCache(Set<ModifiableCommandAddress> dejaVu) { - if (dejaVu.add(this)) { - cachedChatHandlerFallback = chatHandler; - for (ChildCommandAddress address : children.values()) { - if (address.chatHandler == null) { - address.resetChatHandlerCache(dejaVu); - } - } - } - } - - @Override - public ICommandDispatcher getDispatcherForTree() { - return getRoot(); - } - - @Override - public boolean isCommandTrailing() { - return false; - } - - public void setCommandTrailing(boolean trailing) { - throw new UnsupportedOperationException(); - } - - void appendDebugInformation(StringBuilder target, String linePrefix, Set<ICommandAddress> seen) { - target.append('\n').append(linePrefix); - if (!seen.add(this)) { - target.append("<duplicate of address '").append(getAddress()).append("'>"); - return; - } - - if (this instanceof ChildCommandAddress) { - List<String> namesModifiable = ((ChildCommandAddress) this).namesModifiable; - if (namesModifiable.isEmpty()) { - target.append("<no key>"); - } else { - Iterator<String> keys = namesModifiable.iterator(); - target.append(keys.next()).append(' '); - if (keys.hasNext()) { - target.append('(').append(keys.next()); - while (keys.hasNext()) { - target.append(" ,").append(keys.next()); - } - target.append(") "); - } - } - } else { - target.append("<root> "); - } - - String commandClass = hasCommand() ? getCommand().getClass().getCanonicalName() : "<no command>"; - target.append(commandClass); - - for (ChildCommandAddress child : new HashSet<>(children.values())) { - child.appendDebugInformation(target, linePrefix + " ", seen); - } - } - -} +package io.dico.dicore.command;
+
+import io.dico.dicore.command.chat.ChatHandlers;
+import io.dico.dicore.command.chat.IChatHandler;
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.parameter.ParameterList;
+import io.dico.dicore.command.predef.DefaultGroupCommand;
+import io.dico.dicore.command.predef.HelpCommand;
+import io.dico.dicore.command.predef.PredefinedCommand;
+
+import java.util.*;
+
+public abstract class ModifiableCommandAddress implements ICommandAddress {
+ Map<String, ChildCommandAddress> children;
+ Collection<String> childrenMainKeys = Collections.emptyList();
+
+ // the chat handler as configured by the programmer
+ IChatHandler chatHandler;
+ // cache for the algorithm that finds the first chat handler going up the tree
+ transient IChatHandler cachedChatHandlerFallback;
+
+ ModifiableCommandAddress helpChild;
+
+ public ModifiableCommandAddress() {
+ this.children = new LinkedHashMap<>(4);
+ }
+
+ @Override
+ public boolean hasParent() {
+ return getParent() != null;
+ }
+
+ @Override
+ public boolean hasCommand() {
+ return getCommand() != null;
+ }
+
+ @Override
+ public boolean hasUserDeclaredCommand() {
+ Command command = getCommand();
+ return command != null && !(command instanceof PredefinedCommand);
+ }
+
+ @Override
+ public Command getCommand() {
+ return null;
+ }
+
+ @Override
+ public boolean isRoot() {
+ return false;
+ }
+
+ @Override
+ public List<String> getNames() {
+ return null;
+ }
+
+ @Override
+ public List<String> getAliases() {
+ List<String> names = getNames();
+ if (names == null) {
+ return null;
+ }
+ if (names.isEmpty()) {
+ return Collections.emptyList();
+ }
+ return names.subList(1, names.size());
+ }
+
+ @Override
+ public String getMainKey() {
+ return null;
+ }
+
+ public void setCommand(Command command) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public abstract ModifiableCommandAddress getParent();
+
+ @Override
+ public RootCommandAddress getRoot() {
+ ModifiableCommandAddress out = this;
+ while (out.hasParent()) {
+ out = out.getParent();
+ }
+ return out.isRoot() ? (RootCommandAddress) out : null;
+ }
+
+ @Override
+ public int getDepth() {
+ int depth = 0;
+ ICommandAddress address = this;
+ while (address.hasParent()) {
+ address = address.getParent();
+ depth++;
+ }
+ return depth;
+ }
+
+ @Override
+ public boolean isDepthLargerThan(int value) {
+ int depth = 0;
+ ICommandAddress address = this;
+ do {
+ if (depth > value) {
+ return true;
+ }
+
+ address = address.getParent();
+ depth++;
+ } while (address != null);
+ return false;
+ }
+
+ @Override
+ public boolean hasChildren() {
+ return !children.isEmpty();
+ }
+
+ @Override
+ public int getNumberOfRealChildren() {
+ return childrenMainKeys.size();
+ }
+
+ @Override
+ public Collection<String> getChildrenMainKeys() {
+ return Collections.unmodifiableCollection(childrenMainKeys);
+ }
+
+ @Override
+ public Map<String, ? extends ModifiableCommandAddress> getChildren() {
+ return Collections.unmodifiableMap(children);
+ }
+
+ @Override
+ public ChildCommandAddress getChild(String key) {
+ return children.get(key);
+ }
+
+ @Override
+ public ChildCommandAddress getChild(ExecutionContext context, ArgumentBuffer buffer) throws CommandException {
+ return buffer.hasNext() ? getChild(buffer.next()) : null;
+ }
+
+ public void addChild(ICommandAddress child) {
+ if (!(child instanceof ChildCommandAddress)) {
+ throw new IllegalArgumentException("Argument must be a ChildCommandAddress");
+ }
+
+ ChildCommandAddress mChild = (ChildCommandAddress) child;
+ if (mChild.parent != null) {
+ throw new IllegalArgumentException("Argument already has a parent");
+ }
+
+ if (mChild.names.isEmpty()) {
+ throw new IllegalArgumentException("Argument must have names");
+ }
+
+ Iterator<String> names = mChild.modifiableNamesIterator();
+ String mainKey = names.next();
+
+ if (!childrenMainKeys.contains(mainKey)) {
+ if (!(childrenMainKeys instanceof ArrayList)) {
+ childrenMainKeys = new ArrayList<>();
+ }
+ childrenMainKeys.add(mainKey);
+ }
+
+ children.put(mainKey, mChild);
+
+ while (names.hasNext()) {
+ String name = names.next();
+ if (children.putIfAbsent(name, mChild) != null) {
+ names.remove();
+ }
+ }
+
+ mChild.setParent(this);
+
+ if (mChild.hasCommand() && mChild.getCommand() instanceof HelpCommand) {
+ helpChild = mChild;
+ }
+ }
+
+ public void removeChildren(boolean removeAliases, String... keys) {
+ if (keys.length == 0) {
+ throw new IllegalArgumentException("keys is empty");
+ }
+
+ for (String key : keys) {
+ ChildCommandAddress keyTarget = getChild(key);
+ if (keyTarget == null) {
+ continue;
+ }
+
+ if (removeAliases) {
+ Iterator<String> iterator = keyTarget.namesModifiable.iterator();
+ boolean first = true;
+ while (iterator.hasNext()) {
+ String alias = iterator.next();
+ ChildCommandAddress aliasTarget = getChild(key);
+ if (aliasTarget == keyTarget) {
+ if (first) {
+ childrenMainKeys.remove(alias);
+ }
+ children.remove(alias);
+ }
+ iterator.remove();
+ first = false;
+ }
+
+ } else {
+ if (key.equals(keyTarget.getMainKey())) {
+ childrenMainKeys.remove(key);
+ }
+
+ children.remove(key);
+ keyTarget.namesModifiable.remove(key);
+ }
+ }
+ }
+
+ public boolean hasHelpCommand() {
+ return helpChild != null;
+ }
+
+ public ModifiableCommandAddress getHelpCommand() {
+ return helpChild;
+ }
+
+ @Override
+ public IChatHandler getChatHandler() {
+ if (cachedChatHandlerFallback == null) {
+ if (chatHandler != null) {
+ cachedChatHandlerFallback = chatHandler;
+ } else if (!hasParent()) {
+ cachedChatHandlerFallback = ChatHandlers.defaultChat();
+ } else {
+ cachedChatHandlerFallback = getParent().getChatHandler();
+ }
+ }
+ return cachedChatHandlerFallback;
+ }
+
+ public void setChatHandler(IChatHandler chatHandler) {
+ this.chatHandler = chatHandler;
+ resetChatHandlerCache(new HashSet<>());
+ }
+
+ void resetChatHandlerCache(Set<ModifiableCommandAddress> dejaVu) {
+ if (dejaVu.add(this)) {
+ cachedChatHandlerFallback = chatHandler;
+ for (ChildCommandAddress address : children.values()) {
+ if (address.chatHandler == null) {
+ address.resetChatHandlerCache(dejaVu);
+ }
+ }
+ }
+ }
+
+ @Override
+ public ICommandDispatcher getDispatcherForTree() {
+ return getRoot();
+ }
+
+ @Override
+ public boolean isCommandTrailing() {
+ return false;
+ }
+
+ public void setCommandTrailing(boolean trailing) {
+ throw new UnsupportedOperationException();
+ }
+
+ void appendDebugInformation(StringBuilder target, String linePrefix, Set<ICommandAddress> seen) {
+ target.append('\n').append(linePrefix);
+ if (!seen.add(this)) {
+ target.append("<duplicate of address '").append(getAddress()).append("'>");
+ return;
+ }
+
+ if (this instanceof ChildCommandAddress) {
+ List<String> namesModifiable = ((ChildCommandAddress) this).namesModifiable;
+ if (namesModifiable.isEmpty()) {
+ target.append("<no key>");
+ } else {
+ Iterator<String> keys = namesModifiable.iterator();
+ target.append(keys.next()).append(' ');
+ if (keys.hasNext()) {
+ target.append('(').append(keys.next());
+ while (keys.hasNext()) {
+ target.append(" ,").append(keys.next());
+ }
+ target.append(") ");
+ }
+ }
+ } else {
+ target.append("<root> ");
+ }
+
+ String commandClass = hasCommand() ? getCommand().getClass().getCanonicalName() : "<no command>";
+ target.append(commandClass);
+
+ for (ChildCommandAddress child : new HashSet<>(children.values())) {
+ child.appendDebugInformation(target, linePrefix + " ", seen);
+ }
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/PermissionContextFilter.java b/dicore3/command/src/main/java/io/dico/dicore/command/PermissionContextFilter.java index 75b2035..c996356 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/PermissionContextFilter.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/PermissionContextFilter.java @@ -1,136 +1,136 @@ -package io.dico.dicore.command; - -import java.util.List; -import java.util.Objects; - -public class PermissionContextFilter implements IContextFilter { - private String permission; - private String[] permissionComponents; - private int componentInsertionIndex; - private String failMessage; - - public PermissionContextFilter(String permission) { - this.permission = Objects.requireNonNull(permission); - } - - public PermissionContextFilter(String permission, String failMessage) { - this(permission); - this.failMessage = failMessage; - } - - public PermissionContextFilter(String permission, boolean inheritable) { - this(permission, null, inheritable); - } - - public PermissionContextFilter(String permission, String failMessage, boolean inheritable) { - this(permission, failMessage); - if (inheritable) { - setupInheritability(-1); - } - } - - public PermissionContextFilter(String permission, int componentInsertionIndex, String failMessage) { - this(permission, failMessage); - setupInheritability(componentInsertionIndex); - } - - private void setupInheritability(int componentInsertionIndex) { - this.permissionComponents = permission.split("\\."); - this.componentInsertionIndex = componentInsertionIndex < 0 ? permissionComponents.length : componentInsertionIndex; - if (componentInsertionIndex > permissionComponents.length) throw new IllegalArgumentException(); - } - - private void doFilter(ExecutionContext context, String permission) throws CommandException { - if (failMessage != null) { - Validate.isAuthorized(context.getSender(), permission, failMessage); - } else { - Validate.isAuthorized(context.getSender(), permission); - } - } - - @Override - public void filterContext(ExecutionContext context) throws CommandException { - doFilter(context, permission); - } - - public String getInheritedPermission(String[] components) { - int insertedAmount = components.length; - String[] currentComponents = permissionComponents; - int currentAmount = currentComponents.length; - String[] targetArray = new String[currentAmount + insertedAmount]; - - int insertionIndex; - //int newInsertionIndex; - if (componentInsertionIndex == -1) { - insertionIndex = currentAmount; - //newInsertionIndex = -1; - } else { - insertionIndex = componentInsertionIndex; - //newInsertionIndex = insertionIndex + insertedAmount; - } - - // copy the current components up to insertionIndex - System.arraycopy(currentComponents, 0, targetArray, 0, insertionIndex); - // copy the new components into the array at insertionIndex - System.arraycopy(components, 0, targetArray, insertionIndex, insertedAmount); - // copy the current components from insertionIndex + inserted amount - System.arraycopy(currentComponents, insertionIndex, targetArray, insertionIndex + insertedAmount, currentAmount - insertionIndex); - - return String.join(".", targetArray); - } - - @Override - public void filterSubContext(ExecutionContext subContext, String... path) throws CommandException { - if (isInheritable()) { - doFilter(subContext, getInheritedPermission(path)); - } - } - - @Override - public Priority getPriority() { - return Priority.PERMISSION; - } - - public boolean isInheritable() { - return permissionComponents != null; - } - - public String getPermission() { - return permission; - } - - public int getComponentInsertionIndex() { - return componentInsertionIndex; - } - - public String getFailMessage() { - return failMessage; - } - - /* - private fun getPermissionsOf(address: ICommandAddress) = getPermissionsOf(address, emptyArray(), mutableListOf()) - - private fun getPermissionsOf(address: ICommandAddress, path: Array<String>, result: MutableList<String>): List<String> { - val command = address.command ?: return result - - var inherited = false - for (filter in command.contextFilters) { - when (filter) { - is PermissionContextFilter -> { - if (path.isEmpty()) result.add(filter.permission) - else if (filter.isInheritable) result.add(filter.getInheritedPermission(path)) - } - is InheritingContextFilter -> { - if (filter.priority == PERMISSION && address.hasParent() && !inherited) { - inherited = true - getPermissionsOf(address.parent, arrayOf(address.mainKey, *path), result) - } - } - } - } - - return result - } - */ - -} +package io.dico.dicore.command;
+
+import java.util.List;
+import java.util.Objects;
+
+public class PermissionContextFilter implements IContextFilter {
+ private String permission;
+ private String[] permissionComponents;
+ private int componentInsertionIndex;
+ private String failMessage;
+
+ public PermissionContextFilter(String permission) {
+ this.permission = Objects.requireNonNull(permission);
+ }
+
+ public PermissionContextFilter(String permission, String failMessage) {
+ this(permission);
+ this.failMessage = failMessage;
+ }
+
+ public PermissionContextFilter(String permission, boolean inheritable) {
+ this(permission, null, inheritable);
+ }
+
+ public PermissionContextFilter(String permission, String failMessage, boolean inheritable) {
+ this(permission, failMessage);
+ if (inheritable) {
+ setupInheritability(-1);
+ }
+ }
+
+ public PermissionContextFilter(String permission, int componentInsertionIndex, String failMessage) {
+ this(permission, failMessage);
+ setupInheritability(componentInsertionIndex);
+ }
+
+ private void setupInheritability(int componentInsertionIndex) {
+ this.permissionComponents = permission.split("\\.");
+ this.componentInsertionIndex = componentInsertionIndex < 0 ? permissionComponents.length : componentInsertionIndex;
+ if (componentInsertionIndex > permissionComponents.length) throw new IllegalArgumentException();
+ }
+
+ private void doFilter(ExecutionContext context, String permission) throws CommandException {
+ if (failMessage != null) {
+ Validate.isAuthorized(context.getSender(), permission, failMessage);
+ } else {
+ Validate.isAuthorized(context.getSender(), permission);
+ }
+ }
+
+ @Override
+ public void filterContext(ExecutionContext context) throws CommandException {
+ doFilter(context, permission);
+ }
+
+ public String getInheritedPermission(String[] components) {
+ int insertedAmount = components.length;
+ String[] currentComponents = permissionComponents;
+ int currentAmount = currentComponents.length;
+ String[] targetArray = new String[currentAmount + insertedAmount];
+
+ int insertionIndex;
+ //int newInsertionIndex;
+ if (componentInsertionIndex == -1) {
+ insertionIndex = currentAmount;
+ //newInsertionIndex = -1;
+ } else {
+ insertionIndex = componentInsertionIndex;
+ //newInsertionIndex = insertionIndex + insertedAmount;
+ }
+
+ // copy the current components up to insertionIndex
+ System.arraycopy(currentComponents, 0, targetArray, 0, insertionIndex);
+ // copy the new components into the array at insertionIndex
+ System.arraycopy(components, 0, targetArray, insertionIndex, insertedAmount);
+ // copy the current components from insertionIndex + inserted amount
+ System.arraycopy(currentComponents, insertionIndex, targetArray, insertionIndex + insertedAmount, currentAmount - insertionIndex);
+
+ return String.join(".", targetArray);
+ }
+
+ @Override
+ public void filterSubContext(ExecutionContext subContext, String... path) throws CommandException {
+ if (isInheritable()) {
+ doFilter(subContext, getInheritedPermission(path));
+ }
+ }
+
+ @Override
+ public Priority getPriority() {
+ return Priority.PERMISSION;
+ }
+
+ public boolean isInheritable() {
+ return permissionComponents != null;
+ }
+
+ public String getPermission() {
+ return permission;
+ }
+
+ public int getComponentInsertionIndex() {
+ return componentInsertionIndex;
+ }
+
+ public String getFailMessage() {
+ return failMessage;
+ }
+
+ /*
+ private fun getPermissionsOf(address: ICommandAddress) = getPermissionsOf(address, emptyArray(), mutableListOf())
+
+ private fun getPermissionsOf(address: ICommandAddress, path: Array<String>, result: MutableList<String>): List<String> {
+ val command = address.command ?: return result
+
+ var inherited = false
+ for (filter in command.contextFilters) {
+ when (filter) {
+ is PermissionContextFilter -> {
+ if (path.isEmpty()) result.add(filter.permission)
+ else if (filter.isInheritable) result.add(filter.getInheritedPermission(path))
+ }
+ is InheritingContextFilter -> {
+ if (filter.priority == PERMISSION && address.hasParent() && !inherited) {
+ inherited = true
+ getPermissionsOf(address.parent, arrayOf(address.mainKey, *path), result)
+ }
+ }
+ }
+ }
+
+ return result
+ }
+ */
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java b/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java index 44f0540..a17cfb0 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/RootCommandAddress.java @@ -76,7 +76,7 @@ public class RootCommandAddress extends ModifiableCommandAddress implements ICom } - private static void debugChildren(ModifiableCommandAddress address) { + public static void debugChildren(ModifiableCommandAddress address) { Collection<String> keys = address.getChildrenMainKeys(); for (String key : keys) { ChildCommandAddress child = address.getChild(key); @@ -233,18 +233,19 @@ public class RootCommandAddress extends ModifiableCommandAddress implements ICom @Override public List<String> getTabCompletions(CommandSender sender, Location location, ArgumentBuffer buffer) { ExecutionContext context = new ExecutionContext(sender, buffer, true); + long start = System.currentTimeMillis(); try { ICommandAddress target = getCommandTarget(context, buffer); - List<String> out; - if (target.hasCommand()) { + List<String> out = Collections.emptyList(); + /*if (target.hasCommand()) { context.setCommand(target.getCommand()); target.getCommand().initializeAndFilterContext(context); out = target.getCommand().tabComplete(sender, context, location); } else { out = Collections.emptyList(); - } + }*/ int cursor = buffer.getCursor(); String input; @@ -269,6 +270,11 @@ public class RootCommandAddress extends ModifiableCommandAddress implements ICom } catch (CommandException ex) { return Collections.emptyList(); + } finally { + long duration = System.currentTimeMillis() - start; + if (duration > 2) { + System.out.println(String.format("Complete took %.3f seconds", duration / 1000.0)); + } } } diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/Validate.java b/dicore3/command/src/main/java/io/dico/dicore/command/Validate.java index 596ad08..3b1f7d4 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/Validate.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/Validate.java @@ -1,52 +1,52 @@ -package io.dico.dicore.command; - -import org.bukkit.command.CommandSender; -import org.bukkit.command.ConsoleCommandSender; -import org.bukkit.entity.Player; - -import java.util.Optional; - -public class Validate { - - private Validate() { - - } - - //@Contract("false, _ -> fail") - public static void isTrue(boolean expression, String failMessage) throws CommandException { - if (!expression) { - throw new CommandException(failMessage); - } - } - - //@Contract("null, _ -> fail") - public static void notNull(Object obj, String failMessage) throws CommandException { - Validate.isTrue(obj != null, failMessage); - } - - public static void isAuthorized(CommandSender sender, String permission, String failMessage) throws CommandException { - Validate.isTrue(sender.hasPermission(permission), failMessage); - } - - public static void isAuthorized(CommandSender sender, String permission) throws CommandException { - Validate.isAuthorized(sender, permission, "You do not have permission to use that command"); - } - - //@Contract("null -> fail") - public static void isPlayer(CommandSender sender) throws CommandException { - isTrue(sender instanceof Player, "That command can only be used by players"); - } - - //@Contract("null -> fail") - public static void isConsole(CommandSender sender) throws CommandException { - isTrue(sender instanceof ConsoleCommandSender, "That command can only be used by the console"); - } - - public static <T> T returnIfPresent(Optional<T> maybe, String failMessage) throws CommandException { - if (!maybe.isPresent()) { - throw new CommandException(failMessage); - } - return maybe.get(); - } - -} +package io.dico.dicore.command;
+
+import org.bukkit.command.CommandSender;
+import org.bukkit.command.ConsoleCommandSender;
+import org.bukkit.entity.Player;
+
+import java.util.Optional;
+
+public class Validate {
+
+ private Validate() {
+
+ }
+
+ //@Contract("false, _ -> fail")
+ public static void isTrue(boolean expression, String failMessage) throws CommandException {
+ if (!expression) {
+ throw new CommandException(failMessage);
+ }
+ }
+
+ //@Contract("null, _ -> fail")
+ public static void notNull(Object obj, String failMessage) throws CommandException {
+ Validate.isTrue(obj != null, failMessage);
+ }
+
+ public static void isAuthorized(CommandSender sender, String permission, String failMessage) throws CommandException {
+ Validate.isTrue(sender.hasPermission(permission), failMessage);
+ }
+
+ public static void isAuthorized(CommandSender sender, String permission) throws CommandException {
+ Validate.isAuthorized(sender, permission, "You do not have permission to use that command");
+ }
+
+ //@Contract("null -> fail")
+ public static void isPlayer(CommandSender sender) throws CommandException {
+ isTrue(sender instanceof Player, "That command can only be used by players");
+ }
+
+ //@Contract("null -> fail")
+ public static void isConsole(CommandSender sender) throws CommandException {
+ isTrue(sender instanceof ConsoleCommandSender, "That command can only be used by the console");
+ }
+
+ public static <T> T returnIfPresent(Optional<T> maybe, String failMessage) throws CommandException {
+ if (!maybe.isPresent()) {
+ throw new CommandException(failMessage);
+ }
+ return maybe.get();
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/BigRange.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/BigRange.java index 467ba4b..b0277c6 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/BigRange.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/BigRange.java @@ -1,52 +1,52 @@ -package io.dico.dicore.command.annotation; - -import io.dico.dicore.command.parameter.type.ParameterConfig; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.PARAMETER) -public @interface BigRange { - Class<?> MEMORY_CLASS = Memory.class; - ParameterConfig<BigRange, Memory> CONFIG = ParameterConfig.getMemoryClassFromField(BigRange.class); - Memory DEFAULT = new Memory("MIN", "MAX", "0"); - - String min() default "MIN"; - - String max() default "MAX"; - - String defaultValue() default "0"; - - class Memory { - private final String min; - private final String max; - private final String defaultValue; - - public Memory(BigRange range) { - this(range.min(), range.max(), range.defaultValue()); - } - - public Memory(String min, String max, String defaultValue) { - this.min = min; - this.max = max; - this.defaultValue = defaultValue; - } - - public String min() { - return min; - } - - public String max() { - return max; - } - - public String defaultValue() { - return defaultValue; - } - - } - -} +package io.dico.dicore.command.annotation;
+
+import io.dico.dicore.command.parameter.type.ParameterConfig;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.PARAMETER)
+public @interface BigRange {
+ Class<?> MEMORY_CLASS = Memory.class;
+ ParameterConfig<BigRange, Memory> CONFIG = ParameterConfig.getMemoryClassFromField(BigRange.class);
+ Memory DEFAULT = new Memory("MIN", "MAX", "0");
+
+ String min() default "MIN";
+
+ String max() default "MAX";
+
+ String defaultValue() default "0";
+
+ class Memory {
+ private final String min;
+ private final String max;
+ private final String defaultValue;
+
+ public Memory(BigRange range) {
+ this(range.min(), range.max(), range.defaultValue());
+ }
+
+ public Memory(String min, String max, String defaultValue) {
+ this.min = min;
+ this.max = max;
+ this.defaultValue = defaultValue;
+ }
+
+ public String min() {
+ return min;
+ }
+
+ public String max() {
+ return max;
+ }
+
+ public String defaultValue() {
+ return defaultValue;
+ }
+
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Cmd.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Cmd.java index 109490a..dfad0bb 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Cmd.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Cmd.java @@ -1,16 +1,16 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface Cmd { - - String value(); - - String[] aliases() default {}; - -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface Cmd {
+
+ String value();
+
+ String[] aliases() default {};
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/CmdParamType.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/CmdParamType.java index ea51e44..3eb2d53 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/CmdParamType.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/CmdParamType.java @@ -1,27 +1,27 @@ -package io.dico.dicore.command.annotation; - -import io.dico.dicore.command.parameter.type.IParameterTypeSelector; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation to mark methods that register a parameter type to the localized selector for use in reflective commands. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface CmdParamType { - - /** - * If this flag is set, the type is registered without its annotation type. - * As a result, the {@link IParameterTypeSelector} is more likely to select it (faster). - * This is irrelevant if there is no annotation type or param config. - * - * @return true if this parameter type should be registered without its annotation type too - */ - boolean infolessAlias() default false; - -} - +package io.dico.dicore.command.annotation;
+
+import io.dico.dicore.command.parameter.type.IParameterTypeSelector;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation to mark methods that register a parameter type to the localized selector for use in reflective commands.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface CmdParamType {
+
+ /**
+ * If this flag is set, the type is registered without its annotation type.
+ * As a result, the {@link IParameterTypeSelector} is more likely to select it (faster).
+ * This is irrelevant if there is no annotation type or param config.
+ *
+ * @return true if this parameter type should be registered without its annotation type too
+ */
+ boolean infolessAlias() default false;
+
+}
+
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/CommandAnnotationUtils.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/CommandAnnotationUtils.java index 868884c..7983997 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/CommandAnnotationUtils.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/CommandAnnotationUtils.java @@ -1,35 +1,35 @@ -package io.dico.dicore.command.annotation; - -public class CommandAnnotationUtils { - - /** - * Get the short description from a {@link Desc} annotation. - * If {@link Desc#shortVersion()} is given, returns that. - * Otherwise, returns the first element of {@link Desc#value()} - * If neither is available, returns null. - * - * @param desc the annotation - * @return the short description - */ - public static String getShortDescription(Desc desc) { - String descString; - if (desc == null) { - descString = null; - } else if (!desc.shortVersion().isEmpty()) { - descString = desc.shortVersion(); - } else if (desc.value().length > 0) { - descString = desc.value()[0]; - if (desc.value().length > 1) { - //System.out.println("[Command Warning] Multiline descriptions not supported here. Keep it short for: " + targetIdentifier); - } - if (descString != null && descString.isEmpty()) { - descString = null; - } - } else { - descString = null; - } - - return descString; - } - -} +package io.dico.dicore.command.annotation;
+
+public class CommandAnnotationUtils {
+
+ /**
+ * Get the short description from a {@link Desc} annotation.
+ * If {@link Desc#shortVersion()} is given, returns that.
+ * Otherwise, returns the first element of {@link Desc#value()}
+ * If neither is available, returns null.
+ *
+ * @param desc the annotation
+ * @return the short description
+ */
+ public static String getShortDescription(Desc desc) {
+ String descString;
+ if (desc == null) {
+ descString = null;
+ } else if (!desc.shortVersion().isEmpty()) {
+ descString = desc.shortVersion();
+ } else if (desc.value().length > 0) {
+ descString = desc.value()[0];
+ if (desc.value().length > 1) {
+ //System.out.println("[Command Warning] Multiline descriptions not supported here. Keep it short for: " + targetIdentifier);
+ }
+ if (descString != null && descString.isEmpty()) {
+ descString = null;
+ }
+ } else {
+ descString = null;
+ }
+
+ return descString;
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Desc.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Desc.java index 0011fb8..ab3e555 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Desc.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Desc.java @@ -1,27 +1,27 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.RUNTIME) -public @interface Desc { - - /** - * Multiline description if {@link #shortVersion} is set. - * Otherwise, this should be an array with one element (aka, you don't have to add array brackets). - * - * @return the multiline description. - * @see CommandAnnotationUtils#getShortDescription(Desc) - */ - String[] value(); - - /** - * Short description, use if {@link #value} is multi-line. - * To get a short description from a {@link Desc}, you should use {@link CommandAnnotationUtils#getShortDescription(Desc)} - * - * @return short description - * @see CommandAnnotationUtils#getShortDescription(Desc) - */ - String shortVersion() default ""; - -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Desc {
+
+ /**
+ * Multiline description if {@link #shortVersion} is set.
+ * Otherwise, this should be an array with one element (aka, you don't have to add array brackets).
+ *
+ * @return the multiline description.
+ * @see CommandAnnotationUtils#getShortDescription(Desc)
+ */
+ String[] value();
+
+ /**
+ * Short description, use if {@link #value} is multi-line.
+ * To get a short description from a {@link Desc}, you should use {@link CommandAnnotationUtils#getShortDescription(Desc)}
+ *
+ * @return short description
+ * @see CommandAnnotationUtils#getShortDescription(Desc)
+ */
+ String shortVersion() default "";
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Flag.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Flag.java index 31a47dd..edc50ef 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Flag.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Flag.java @@ -1,16 +1,16 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.PARAMETER) -public @interface Flag { - - String value() default ""; - - String permission() default ""; - -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.PARAMETER)
+public @interface Flag {
+
+ String value() default "";
+
+ String permission() default "";
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/GenerateCommands.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/GenerateCommands.java index 9b7164d..eeff351 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/GenerateCommands.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/GenerateCommands.java @@ -1,14 +1,14 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface GenerateCommands { - - String[] value() default {"help"}; - -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface GenerateCommands {
+
+ String[] value() default {"help"};
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/GroupMatchedCommands.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/GroupMatchedCommands.java index 53e3e9e..de89e00 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/GroupMatchedCommands.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/GroupMatchedCommands.java @@ -1,68 +1,68 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation to define sub-groups of the group registered reflectively from all methods in a class. - * <p> - * Commands are selected for grouping by matching their method's names to a regular expression. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -public @interface GroupMatchedCommands { - - @Retention(RetentionPolicy.RUNTIME) - @interface GroupEntry { - - /** - * Regular expression to match method names for this group - * Must be non-empty - * - * @return the regular expression - */ - String regex(); - - /** - * The name or main key of the sub-group or address - * Must be non-empty - * - * @return the group name - */ - String group(); - - /** - * The aliases for the sub-group - * - * @return the group aliases - */ - String[] groupAliases() default {}; - - /** - * Generated (predefined) commands for the sub-group - */ - String[] generatedCommands() default {}; - - /** - * @see Desc - */ - String[] description() default {}; - - /** - * @see Desc - */ - String shortDescription() default ""; - } - - /** - * The defined groups. - * If a method name matches the regex of multiple groups, - * groups are prioritized by the order in which they appear in this array. - * - * @return the defined groups - */ - GroupEntry[] value(); - -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation to define sub-groups of the group registered reflectively from all methods in a class.
+ * <p>
+ * Commands are selected for grouping by matching their method's names to a regular expression.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface GroupMatchedCommands {
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @interface GroupEntry {
+
+ /**
+ * Regular expression to match method names for this group
+ * Must be non-empty
+ *
+ * @return the regular expression
+ */
+ String regex();
+
+ /**
+ * The name or main key of the sub-group or address
+ * Must be non-empty
+ *
+ * @return the group name
+ */
+ String group();
+
+ /**
+ * The aliases for the sub-group
+ *
+ * @return the group aliases
+ */
+ String[] groupAliases() default {};
+
+ /**
+ * Generated (predefined) commands for the sub-group
+ */
+ String[] generatedCommands() default {};
+
+ /**
+ * @see Desc
+ */
+ String[] description() default {};
+
+ /**
+ * @see Desc
+ */
+ String shortDescription() default "";
+ }
+
+ /**
+ * The defined groups.
+ * If a method name matches the regex of multiple groups,
+ * groups are prioritized by the order in which they appear in this array.
+ *
+ * @return the defined groups
+ */
+ GroupEntry[] value();
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/NamedArg.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/NamedArg.java index fa42e6b..d84358b 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/NamedArg.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/NamedArg.java @@ -1,14 +1,14 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.PARAMETER) -public @interface NamedArg { - - String value(); - -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.PARAMETER)
+public @interface NamedArg {
+
+ String value();
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/PreprocessArgs.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/PreprocessArgs.java index 57f53bd..32b0723 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/PreprocessArgs.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/PreprocessArgs.java @@ -1,17 +1,17 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -@Deprecated -public @interface PreprocessArgs { - - String tokens() default "\"\""; - - char escapeChar() default '\\'; - -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+@Deprecated
+public @interface PreprocessArgs {
+
+ String tokens() default "\"\"";
+
+ char escapeChar() default '\\';
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Range.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Range.java index 3fd4160..98aec22 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Range.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Range.java @@ -1,67 +1,67 @@ -package io.dico.dicore.command.annotation; - -import io.dico.dicore.command.CommandException; -import io.dico.dicore.command.Validate; -import io.dico.dicore.command.parameter.type.ParameterConfig; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.PARAMETER) -public @interface Range { - Class<?> MEMORY_CLASS = Memory.class; - ParameterConfig<Range, Memory> CONFIG = ParameterConfig.getMemoryClassFromField(Range.class); - Memory DEFAULT = new Memory(-Double.MAX_VALUE, Double.MAX_VALUE, 0); - - double min() default -Double.MAX_VALUE; - - double max() default Double.MAX_VALUE; - - double defaultValue() default 0; - - class Memory { - private final double min; - private final double max; - private final double defaultValue; - - public Memory(Range range) { - this(range.min(), range.max(), range.defaultValue()); - } - - public Memory(double min, double max, double defaultValue) { - this.min = min; - this.max = max; - this.defaultValue = defaultValue; - } - - public double min() { - return min; - } - - public double max() { - return max; - } - - public double defaultValue() { - return defaultValue; - } - - public void validate(Number x, String failMessage) throws CommandException { - Validate.isTrue(valid(x), failMessage); - } - - public boolean valid(Number x) { - double d = x.doubleValue(); - return min <= d && d <= max; - } - - public boolean isDefault() { - return this == DEFAULT || (min == DEFAULT.min && max == DEFAULT.max && defaultValue == DEFAULT.defaultValue); - } - - } - -} +package io.dico.dicore.command.annotation;
+
+import io.dico.dicore.command.CommandException;
+import io.dico.dicore.command.Validate;
+import io.dico.dicore.command.parameter.type.ParameterConfig;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.PARAMETER)
+public @interface Range {
+ Class<?> MEMORY_CLASS = Memory.class;
+ ParameterConfig<Range, Memory> CONFIG = ParameterConfig.getMemoryClassFromField(Range.class);
+ Memory DEFAULT = new Memory(-Double.MAX_VALUE, Double.MAX_VALUE, 0);
+
+ double min() default -Double.MAX_VALUE;
+
+ double max() default Double.MAX_VALUE;
+
+ double defaultValue() default 0;
+
+ class Memory {
+ private final double min;
+ private final double max;
+ private final double defaultValue;
+
+ public Memory(Range range) {
+ this(range.min(), range.max(), range.defaultValue());
+ }
+
+ public Memory(double min, double max, double defaultValue) {
+ this.min = min;
+ this.max = max;
+ this.defaultValue = defaultValue;
+ }
+
+ public double min() {
+ return min;
+ }
+
+ public double max() {
+ return max;
+ }
+
+ public double defaultValue() {
+ return defaultValue;
+ }
+
+ public void validate(Number x, String failMessage) throws CommandException {
+ Validate.isTrue(valid(x), failMessage);
+ }
+
+ public boolean valid(Number x) {
+ double d = x.doubleValue();
+ return min <= d && d <= max;
+ }
+
+ public boolean isDefault() {
+ return this == DEFAULT || (min == DEFAULT.min && max == DEFAULT.max && defaultValue == DEFAULT.defaultValue);
+ }
+
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequireConsole.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequireConsole.java index 362f05c..3510ca8 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequireConsole.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequireConsole.java @@ -1,11 +1,11 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface RequireConsole { -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface RequireConsole {
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequireParameters.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequireParameters.java index 02f5548..68d5706 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequireParameters.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequireParameters.java @@ -1,14 +1,14 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface RequireParameters { - - int value(); - -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface RequireParameters {
+
+ int value();
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequirePermissions.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequirePermissions.java index d2ba782..13d7167 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequirePermissions.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequirePermissions.java @@ -1,33 +1,33 @@ -package io.dico.dicore.command.annotation; - -import io.dico.dicore.command.IContextFilter; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * If this annotation is not present, inheriting permissions is default. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface RequirePermissions { - - /** - * Any permissions that must be present on the sender - * - * @return an array of permission nodes - */ - String[] value(); - - /** - * Whether permissions should (also) be inherited from the parent. - * This uses {@link IContextFilter#INHERIT_PERMISSIONS} - * This is true by default. - * - * @return true if permissions should be inherited. - */ - boolean inherit() default true; - -} +package io.dico.dicore.command.annotation;
+
+import io.dico.dicore.command.IContextFilter;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * If this annotation is not present, inheriting permissions is default.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface RequirePermissions {
+
+ /**
+ * Any permissions that must be present on the sender
+ *
+ * @return an array of permission nodes
+ */
+ String[] value();
+
+ /**
+ * Whether permissions should (also) be inherited from the parent.
+ * This uses {@link IContextFilter#INHERIT_PERMISSIONS}
+ * This is true by default.
+ *
+ * @return true if permissions should be inherited.
+ */
+ boolean inherit() default true;
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequirePlayer.java b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequirePlayer.java index 2165e05..3f447f9 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequirePlayer.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/annotation/RequirePlayer.java @@ -1,11 +1,11 @@ -package io.dico.dicore.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface RequirePlayer { -} +package io.dico.dicore.command.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface RequirePlayer {
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatHandler.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatHandler.java index c0d23ee..ac2a76d 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatHandler.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatHandler.java @@ -1,94 +1,94 @@ -package io.dico.dicore.command.chat; - -import io.dico.dicore.Formatting; -import io.dico.dicore.command.EMessageType; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.chat.help.HelpPages; -import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.NotNull; - -public class AbstractChatHandler implements IChatHandler { - private @NotNull HelpPages helpPages; - - public AbstractChatHandler(@NotNull HelpPages helpPages) { - this.helpPages = helpPages; - } - - public AbstractChatHandler() { - this(HelpPages.newDefaultHelpPages()); - } - - @NotNull - public HelpPages getHelpPages() { - return helpPages; - } - - public void setHelpPages(@NotNull HelpPages helpPages) { - this.helpPages = helpPages; - } - - @Override - public Formatting getChatFormatForType(EMessageType type) { - switch (type) { - case EXCEPTION: - case BAD_NEWS: - return Formatting.RED; - case INSTRUCTION: - case NEUTRAL: - return Formatting.GRAY; - case CUSTOM: - return Formatting.WHITE; - case INFORMATIVE: - return Formatting.AQUA; - case RESULT: - default: - case GOOD_NEWS: - return Formatting.GREEN; - case WARNING: - return Formatting.YELLOW; - - case DESCRIPTION: - return Formatting.GREEN; - case SYNTAX: - return Formatting.AQUA; - case HIGHLIGHT: - return Formatting.RED; - case SUBCOMMAND: - return Formatting.GRAY; - case NUMBER: - return Formatting.YELLOW; - } - } - - @Override - public String getMessagePrefixForType(EMessageType type) { - return ""; - } - - protected String createMessage(EMessageType type, String message) { - if (message == null || message.isEmpty()) return null; - return getMessagePrefixForType(type) + getChatFormatForType(type) + message; - } - - @Override - public String createMessage(ExecutionContext context, EMessageType type, String message) { - return createMessage(type, message); - } - - @Override - public String createMessage(CommandSender sender, EMessageType type, String message) { - return createMessage(type, message); - } - - @Override - public String createHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) { - return helpPages.getHelpPage(sender, context, address, page); - } - - @Override - public String createSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) { - return helpPages.getSyntax(sender, context, address); - } - -} +package io.dico.dicore.command.chat;
+
+import io.dico.dicore.Formatting;
+import io.dico.dicore.command.EMessageType;
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.chat.help.HelpPages;
+import org.bukkit.command.CommandSender;
+import org.jetbrains.annotations.NotNull;
+
+public class AbstractChatHandler implements IChatHandler {
+ private @NotNull HelpPages helpPages;
+
+ public AbstractChatHandler(@NotNull HelpPages helpPages) {
+ this.helpPages = helpPages;
+ }
+
+ public AbstractChatHandler() {
+ this(HelpPages.newDefaultHelpPages());
+ }
+
+ @NotNull
+ public HelpPages getHelpPages() {
+ return helpPages;
+ }
+
+ public void setHelpPages(@NotNull HelpPages helpPages) {
+ this.helpPages = helpPages;
+ }
+
+ @Override
+ public Formatting getChatFormatForType(EMessageType type) {
+ switch (type) {
+ case EXCEPTION:
+ case BAD_NEWS:
+ return Formatting.RED;
+ case INSTRUCTION:
+ case NEUTRAL:
+ return Formatting.GRAY;
+ case CUSTOM:
+ return Formatting.WHITE;
+ case INFORMATIVE:
+ return Formatting.AQUA;
+ case RESULT:
+ default:
+ case GOOD_NEWS:
+ return Formatting.GREEN;
+ case WARNING:
+ return Formatting.YELLOW;
+
+ case DESCRIPTION:
+ return Formatting.GREEN;
+ case SYNTAX:
+ return Formatting.AQUA;
+ case HIGHLIGHT:
+ return Formatting.RED;
+ case SUBCOMMAND:
+ return Formatting.GRAY;
+ case NUMBER:
+ return Formatting.YELLOW;
+ }
+ }
+
+ @Override
+ public String getMessagePrefixForType(EMessageType type) {
+ return "";
+ }
+
+ protected String createMessage(EMessageType type, String message) {
+ if (message == null || message.isEmpty()) return null;
+ return getMessagePrefixForType(type) + getChatFormatForType(type) + message;
+ }
+
+ @Override
+ public String createMessage(ExecutionContext context, EMessageType type, String message) {
+ return createMessage(type, message);
+ }
+
+ @Override
+ public String createMessage(CommandSender sender, EMessageType type, String message) {
+ return createMessage(type, message);
+ }
+
+ @Override
+ public String createHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) {
+ return helpPages.getHelpPage(sender, context, address, page);
+ }
+
+ @Override
+ public String createSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) {
+ return helpPages.getSyntax(sender, context, address);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/ChatHandlers.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/ChatHandlers.java index 232d5cf..cebdb45 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/ChatHandlers.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/ChatHandlers.java @@ -1,20 +1,20 @@ -package io.dico.dicore.command.chat; - -/** - * Static factory methods for {@link IChatHandler} - */ -public class ChatHandlers { - private static final IChatHandler defaultChat; - - private ChatHandlers() { - - } - - public static IChatHandler defaultChat() { - return defaultChat; - } - - static { - defaultChat = new AbstractChatHandler(); - } -} +package io.dico.dicore.command.chat;
+
+/**
+ * Static factory methods for {@link IChatHandler}
+ */
+public class ChatHandlers {
+ private static final IChatHandler defaultChat;
+
+ private ChatHandlers() {
+
+ }
+
+ public static IChatHandler defaultChat() {
+ return defaultChat;
+ }
+
+ static {
+ defaultChat = new AbstractChatHandler();
+ }
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/IChatHandler.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/IChatHandler.java index 98283ef..0dd6002 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/IChatHandler.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/IChatHandler.java @@ -1,60 +1,60 @@ -package io.dico.dicore.command.chat; - -import io.dico.dicore.Formatting; -import io.dico.dicore.command.CommandException; -import io.dico.dicore.command.EMessageType; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import org.bukkit.command.CommandSender; - -//TODO add methods to send JSON messages -public interface IChatHandler { - - default void sendMessage(ExecutionContext context, EMessageType type, String message) { - message = createMessage(context, type, message); - if (message != null) { - context.getSender().sendMessage(message); - } - } - - default void sendMessage(CommandSender sender, EMessageType type, String message) { - message = createMessage(sender, type, message); - if (message != null) { - sender.sendMessage(message); - } - } - - default void handleCommandException(CommandSender sender, ExecutionContext context, CommandException exception) { - sendMessage(context, EMessageType.EXCEPTION, exception.getMessage()); - } - - default void handleException(CommandSender sender, ExecutionContext context, Throwable exception) { - if (exception instanceof CommandException) { - handleCommandException(sender, context, (CommandException) exception); - } else { - sendMessage(sender, EMessageType.EXCEPTION, "An internal error occurred whilst executing this command"); - exception.printStackTrace(); - } - } - - default void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) { - sender.sendMessage(createHelpMessage(sender, context, address, page)); - } - - default void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) { - sender.sendMessage(createSyntaxMessage(sender, context, address)); - } - - Formatting getChatFormatForType(EMessageType type); - - String getMessagePrefixForType(EMessageType type); - - String createMessage(ExecutionContext context, EMessageType type, String message); - - String createMessage(CommandSender sender, EMessageType type, String message); - - String createHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page); - - String createSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address); - -} +package io.dico.dicore.command.chat;
+
+import io.dico.dicore.Formatting;
+import io.dico.dicore.command.CommandException;
+import io.dico.dicore.command.EMessageType;
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import org.bukkit.command.CommandSender;
+
+//TODO add methods to send JSON messages
+public interface IChatHandler {
+
+ default void sendMessage(ExecutionContext context, EMessageType type, String message) {
+ message = createMessage(context, type, message);
+ if (message != null) {
+ context.getSender().sendMessage(message);
+ }
+ }
+
+ default void sendMessage(CommandSender sender, EMessageType type, String message) {
+ message = createMessage(sender, type, message);
+ if (message != null) {
+ sender.sendMessage(message);
+ }
+ }
+
+ default void handleCommandException(CommandSender sender, ExecutionContext context, CommandException exception) {
+ sendMessage(context, EMessageType.EXCEPTION, exception.getMessage());
+ }
+
+ default void handleException(CommandSender sender, ExecutionContext context, Throwable exception) {
+ if (exception instanceof CommandException) {
+ handleCommandException(sender, context, (CommandException) exception);
+ } else {
+ sendMessage(sender, EMessageType.EXCEPTION, "An internal error occurred whilst executing this command");
+ exception.printStackTrace();
+ }
+ }
+
+ default void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) {
+ sender.sendMessage(createHelpMessage(sender, context, address, page));
+ }
+
+ default void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) {
+ sender.sendMessage(createSyntaxMessage(sender, context, address));
+ }
+
+ Formatting getChatFormatForType(EMessageType type);
+
+ String getMessagePrefixForType(EMessageType type);
+
+ String createMessage(ExecutionContext context, EMessageType type, String message);
+
+ String createMessage(CommandSender sender, EMessageType type, String message);
+
+ String createHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page);
+
+ String createSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpPages.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpPages.java index 4382d93..616e85a 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpPages.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpPages.java @@ -1,92 +1,92 @@ -package io.dico.dicore.command.chat.help; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.chat.help.defaults.*; -import org.bukkit.permissions.Permissible; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class HelpPages { - private @NotNull IPageBuilder pageBuilder; - private @NotNull IPageLayout pageLayout; - private int pageLength; - private @NotNull List<IHelpTopic> helpTopics; - private @NotNull IHelpTopic syntaxTopic; - - public HelpPages(@NotNull IPageBuilder pageBuilder, @NotNull IPageLayout pageLayout, int pageLength, @NotNull IHelpTopic syntaxTopic, @NotNull List<IHelpTopic> helpTopics) { - this.pageBuilder = pageBuilder; - this.pageLayout = pageLayout; - this.pageLength = pageLength; - this.syntaxTopic = syntaxTopic; - this.helpTopics = helpTopics; - } - - public HelpPages(IPageBuilder pageBuilder, IPageLayout pageLayout, int pageLength, IHelpTopic syntaxTopic, IHelpTopic... helpTopics) { - this(pageBuilder, pageLayout, pageLength, syntaxTopic, new ArrayList<>(Arrays.asList(helpTopics))); - } - - @SuppressWarnings("RedundantArrayCreation") - public static HelpPages newDefaultHelpPages() { - IHelpTopic syntaxTopic = new SyntaxHelpTopic(); - return new HelpPages(new DefaultPageBuilder(), new DefaultPageLayout(), 12, - syntaxTopic, new IHelpTopic[]{new DescriptionHelpTopic(), syntaxTopic, new SubcommandsHelpTopic()}); - } - - public @NotNull IPageBuilder getPageBuilder() { - return pageBuilder; - } - - public void setPageBuilder(@NotNull IPageBuilder pageBuilder) { - this.pageBuilder = pageBuilder; - } - - public @NotNull IPageLayout getPageLayout() { - return pageLayout; - } - - public void setPageLayout(@NotNull IPageLayout pageLayout) { - this.pageLayout = pageLayout; - } - - public int getPageLength() { - return pageLength; - } - - public void setPageLength(int pageLength) { - this.pageLength = pageLength; - } - - public @NotNull IHelpTopic getSyntaxTopic() { - return syntaxTopic; - } - - public void setSyntaxTopic(@NotNull IHelpTopic syntaxTopic) { - this.syntaxTopic = syntaxTopic; - } - - @NotNull - public List<IHelpTopic> getHelpTopics() { - return helpTopics; - } - - public void setHelpTopics(@NotNull List<IHelpTopic> helpTopics) { - this.helpTopics = helpTopics; - } - - public @NotNull String getHelpPage(Permissible viewer, ExecutionContext context, ICommandAddress address, int page) { - return pageBuilder.getPage(helpTopics, pageLayout, address, viewer, context, page, pageLength); - } - - public @NotNull String getSyntax(Permissible viewer, ExecutionContext context, ICommandAddress address) { - List<IHelpComponent> components = syntaxTopic.getComponents(address, viewer, context, false); - if (components.isEmpty()) { - return getHelpPage(viewer, context, address, 1); - } - return DefaultPageBuilder.combine(components); - } - -} +package io.dico.dicore.command.chat.help;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.chat.help.defaults.*;
+import org.bukkit.permissions.Permissible;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class HelpPages {
+ private @NotNull IPageBuilder pageBuilder;
+ private @NotNull IPageLayout pageLayout;
+ private int pageLength;
+ private @NotNull List<IHelpTopic> helpTopics;
+ private @NotNull IHelpTopic syntaxTopic;
+
+ public HelpPages(@NotNull IPageBuilder pageBuilder, @NotNull IPageLayout pageLayout, int pageLength, @NotNull IHelpTopic syntaxTopic, @NotNull List<IHelpTopic> helpTopics) {
+ this.pageBuilder = pageBuilder;
+ this.pageLayout = pageLayout;
+ this.pageLength = pageLength;
+ this.syntaxTopic = syntaxTopic;
+ this.helpTopics = helpTopics;
+ }
+
+ public HelpPages(IPageBuilder pageBuilder, IPageLayout pageLayout, int pageLength, IHelpTopic syntaxTopic, IHelpTopic... helpTopics) {
+ this(pageBuilder, pageLayout, pageLength, syntaxTopic, new ArrayList<>(Arrays.asList(helpTopics)));
+ }
+
+ @SuppressWarnings("RedundantArrayCreation")
+ public static HelpPages newDefaultHelpPages() {
+ IHelpTopic syntaxTopic = new SyntaxHelpTopic();
+ return new HelpPages(new DefaultPageBuilder(), new DefaultPageLayout(), 12,
+ syntaxTopic, new IHelpTopic[]{new DescriptionHelpTopic(), syntaxTopic, new SubcommandsHelpTopic()});
+ }
+
+ public @NotNull IPageBuilder getPageBuilder() {
+ return pageBuilder;
+ }
+
+ public void setPageBuilder(@NotNull IPageBuilder pageBuilder) {
+ this.pageBuilder = pageBuilder;
+ }
+
+ public @NotNull IPageLayout getPageLayout() {
+ return pageLayout;
+ }
+
+ public void setPageLayout(@NotNull IPageLayout pageLayout) {
+ this.pageLayout = pageLayout;
+ }
+
+ public int getPageLength() {
+ return pageLength;
+ }
+
+ public void setPageLength(int pageLength) {
+ this.pageLength = pageLength;
+ }
+
+ public @NotNull IHelpTopic getSyntaxTopic() {
+ return syntaxTopic;
+ }
+
+ public void setSyntaxTopic(@NotNull IHelpTopic syntaxTopic) {
+ this.syntaxTopic = syntaxTopic;
+ }
+
+ @NotNull
+ public List<IHelpTopic> getHelpTopics() {
+ return helpTopics;
+ }
+
+ public void setHelpTopics(@NotNull List<IHelpTopic> helpTopics) {
+ this.helpTopics = helpTopics;
+ }
+
+ public @NotNull String getHelpPage(Permissible viewer, ExecutionContext context, ICommandAddress address, int page) {
+ return pageBuilder.getPage(helpTopics, pageLayout, address, viewer, context, page, pageLength);
+ }
+
+ public @NotNull String getSyntax(Permissible viewer, ExecutionContext context, ICommandAddress address) {
+ List<IHelpComponent> components = syntaxTopic.getComponents(address, viewer, context, false);
+ if (components.isEmpty()) {
+ return getHelpPage(viewer, context, address, 1);
+ }
+ return DefaultPageBuilder.combine(components);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpTopicModifier.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpTopicModifier.java index 7aeb304..b295a35 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpTopicModifier.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/HelpTopicModifier.java @@ -1,24 +1,24 @@ -package io.dico.dicore.command.chat.help; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import org.bukkit.permissions.Permissible; - -import java.util.List; -import java.util.Objects; - -public abstract class HelpTopicModifier implements IHelpTopic { - private final IHelpTopic delegate; - - public HelpTopicModifier(IHelpTopic delegate) { - this.delegate = Objects.requireNonNull(delegate); - } - - @Override - public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) { - return modify(delegate.getComponents(target, viewer, context, true), target, viewer, context); - } - - protected abstract List<IHelpComponent> modify(List<IHelpComponent> components, ICommandAddress target, Permissible viewer, ExecutionContext context); - -} +package io.dico.dicore.command.chat.help;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import org.bukkit.permissions.Permissible;
+
+import java.util.List;
+import java.util.Objects;
+
+public abstract class HelpTopicModifier implements IHelpTopic {
+ private final IHelpTopic delegate;
+
+ public HelpTopicModifier(IHelpTopic delegate) {
+ this.delegate = Objects.requireNonNull(delegate);
+ }
+
+ @Override
+ public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) {
+ return modify(delegate.getComponents(target, viewer, context, true), target, viewer, context);
+ }
+
+ protected abstract List<IHelpComponent> modify(List<IHelpComponent> components, ICommandAddress target, Permissible viewer, ExecutionContext context);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IHelpComponent.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IHelpComponent.java index e1867b5..446c7d7 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IHelpComponent.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IHelpComponent.java @@ -1,9 +1,9 @@ -package io.dico.dicore.command.chat.help; - -public interface IHelpComponent { - - int lineCount(); - - void appendTo(StringBuilder sb); - -} +package io.dico.dicore.command.chat.help;
+
+public interface IHelpComponent {
+
+ int lineCount();
+
+ void appendTo(StringBuilder sb);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IHelpTopic.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IHelpTopic.java index 0b3fba2..0f1b5c9 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IHelpTopic.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IHelpTopic.java @@ -1,23 +1,23 @@ -package io.dico.dicore.command.chat.help; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import org.bukkit.permissions.Permissible; - -import java.util.List; - -public interface IHelpTopic { - - /** - * Get the components of this help topic - * - * @param target The address of the command to provide help about - * @param viewer The permissible that the page will be shown to (null -> choose a default set). - * @param context Context of the command execution - * @param isForPage A boolean indicating if the components are to be used in a page (for help) - * @return a mutable list of components to include in the help pages - */ - List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage); - - -} +package io.dico.dicore.command.chat.help;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import org.bukkit.permissions.Permissible;
+
+import java.util.List;
+
+public interface IHelpTopic {
+
+ /**
+ * Get the components of this help topic
+ *
+ * @param target The address of the command to provide help about
+ * @param viewer The permissible that the page will be shown to (null -> choose a default set).
+ * @param context Context of the command execution
+ * @param isForPage A boolean indicating if the components are to be used in a page (for help)
+ * @return a mutable list of components to include in the help pages
+ */
+ List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage);
+
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageBorder.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageBorder.java index 1ae3561..2789975 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageBorder.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageBorder.java @@ -1,7 +1,7 @@ -package io.dico.dicore.command.chat.help; - -public interface IPageBorder extends IHelpComponent { - - void setPageCount(int pageCount); - -} +package io.dico.dicore.command.chat.help;
+
+public interface IPageBorder extends IHelpComponent {
+
+ void setPageCount(int pageCount);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageBuilder.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageBuilder.java index 86d9450..0584ade 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageBuilder.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageBuilder.java @@ -1,13 +1,13 @@ -package io.dico.dicore.command.chat.help; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import org.bukkit.permissions.Permissible; - -import java.util.List; - -public interface IPageBuilder { - - String getPage(List<IHelpTopic> helpTopics, IPageLayout pageLayout, ICommandAddress target, Permissible viewer, ExecutionContext context, int pageNum, int pageLen); - -} +package io.dico.dicore.command.chat.help;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import org.bukkit.permissions.Permissible;
+
+import java.util.List;
+
+public interface IPageBuilder {
+
+ String getPage(List<IHelpTopic> helpTopics, IPageLayout pageLayout, ICommandAddress target, Permissible viewer, ExecutionContext context, int pageNum, int pageLen);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageLayout.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageLayout.java index 3223e32..32c41d7 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageLayout.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/IPageLayout.java @@ -1,20 +1,20 @@ -package io.dico.dicore.command.chat.help; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import org.bukkit.permissions.Permissible; - -public interface IPageLayout { - - /** - * Get the page borders for a help page - * - * @param target the address that help is displayed for - * @param viewer the viewer of the help page, or null if irrelevant - * @param context the context of the execution - * @param pageNum the page number as displayed in the help page (so it's 1-bound and not 0-bound) - * @return the page borders. - */ - PageBorders getPageBorders(ICommandAddress target, Permissible viewer, ExecutionContext context, int pageNum); - -} +package io.dico.dicore.command.chat.help;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import org.bukkit.permissions.Permissible;
+
+public interface IPageLayout {
+
+ /**
+ * Get the page borders for a help page
+ *
+ * @param target the address that help is displayed for
+ * @param viewer the viewer of the help page, or null if irrelevant
+ * @param context the context of the execution
+ * @param pageNum the page number as displayed in the help page (so it's 1-bound and not 0-bound)
+ * @return the page borders.
+ */
+ PageBorders getPageBorders(ICommandAddress target, Permissible viewer, ExecutionContext context, int pageNum);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/PageBorders.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/PageBorders.java index 43c0514..cd105b1 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/PageBorders.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/PageBorders.java @@ -1,76 +1,76 @@ -package io.dico.dicore.command.chat.help; - -import java.util.Arrays; - -public class PageBorders { - private final IPageBorder header, footer; - - public PageBorders(IPageBorder header, IPageBorder footer) { - this.header = header; - this.footer = footer; - } - - public IPageBorder getHeader() { - return header; - } - - public IPageBorder getFooter() { - return footer; - } - - public static IPageBorder simpleBorder(String... lines) { - return new SimplePageBorder(lines); - } - - public static IPageBorder disappearingBorder(int pageNum, String... lines) { - return disappearingBorder(pageNum, 0, lines); - } - - public static IPageBorder disappearingBorder(int pageNum, int keptLines, String... lines) { - return new DisappearingPageBorder(pageNum, keptLines, lines); - } - - static class SimplePageBorder extends SimpleHelpComponent implements IPageBorder { - private final String replacedSequence; - - public SimplePageBorder(String replacedSequence, String... lines) { - super(lines); - this.replacedSequence = replacedSequence; - } - - public SimplePageBorder(String... lines) { - super(lines); - this.replacedSequence = "%pageCount%"; - } - - @Override - public void setPageCount(int pageCount) { - String[] lines = this.lines; - for (int i = 0; i < lines.length; i++) { - lines[i] = lines[i].replace(replacedSequence, Integer.toString(pageCount)); - } - } - - } - - static class DisappearingPageBorder extends SimpleHelpComponent implements IPageBorder { - private final int pageNum; - private final int keptLines; - - public DisappearingPageBorder(int pageNum, int keptLines, String... lines) { - super(lines); - this.pageNum = pageNum; - this.keptLines = keptLines; - } - - @Override - public void setPageCount(int pageCount) { - if (pageCount == pageNum) { - String[] lines = this.lines; - this.lines = Arrays.copyOfRange(lines, Math.max(0, lines.length - keptLines), lines.length); - } - } - - } - -} +package io.dico.dicore.command.chat.help;
+
+import java.util.Arrays;
+
+public class PageBorders {
+ private final IPageBorder header, footer;
+
+ public PageBorders(IPageBorder header, IPageBorder footer) {
+ this.header = header;
+ this.footer = footer;
+ }
+
+ public IPageBorder getHeader() {
+ return header;
+ }
+
+ public IPageBorder getFooter() {
+ return footer;
+ }
+
+ public static IPageBorder simpleBorder(String... lines) {
+ return new SimplePageBorder(lines);
+ }
+
+ public static IPageBorder disappearingBorder(int pageNum, String... lines) {
+ return disappearingBorder(pageNum, 0, lines);
+ }
+
+ public static IPageBorder disappearingBorder(int pageNum, int keptLines, String... lines) {
+ return new DisappearingPageBorder(pageNum, keptLines, lines);
+ }
+
+ static class SimplePageBorder extends SimpleHelpComponent implements IPageBorder {
+ private final String replacedSequence;
+
+ public SimplePageBorder(String replacedSequence, String... lines) {
+ super(lines);
+ this.replacedSequence = replacedSequence;
+ }
+
+ public SimplePageBorder(String... lines) {
+ super(lines);
+ this.replacedSequence = "%pageCount%";
+ }
+
+ @Override
+ public void setPageCount(int pageCount) {
+ String[] lines = this.lines;
+ for (int i = 0; i < lines.length; i++) {
+ lines[i] = lines[i].replace(replacedSequence, Integer.toString(pageCount));
+ }
+ }
+
+ }
+
+ static class DisappearingPageBorder extends SimpleHelpComponent implements IPageBorder {
+ private final int pageNum;
+ private final int keptLines;
+
+ public DisappearingPageBorder(int pageNum, int keptLines, String... lines) {
+ super(lines);
+ this.pageNum = pageNum;
+ this.keptLines = keptLines;
+ }
+
+ @Override
+ public void setPageCount(int pageCount) {
+ if (pageCount == pageNum) {
+ String[] lines = this.lines;
+ this.lines = Arrays.copyOfRange(lines, Math.max(0, lines.length - keptLines), lines.length);
+ }
+ }
+
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/SimpleHelpComponent.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/SimpleHelpComponent.java index 22707fd..1b74d0f 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/SimpleHelpComponent.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/SimpleHelpComponent.java @@ -1,27 +1,27 @@ -package io.dico.dicore.command.chat.help; - -public class SimpleHelpComponent implements IHelpComponent { - String[] lines; - - public SimpleHelpComponent(String... lines) { - this.lines = lines; - } - - @Override - public int lineCount() { - return lines.length; - } - - @Override - public void appendTo(StringBuilder sb) { - String[] lines = this.lines; - int len = lines.length; - if (0 < len) { - sb.append(lines[0]); - } - for (int i = 1; i < len; i++) { - sb.append('\n').append(lines[i]); - } - } - -} +package io.dico.dicore.command.chat.help;
+
+public class SimpleHelpComponent implements IHelpComponent {
+ String[] lines;
+
+ public SimpleHelpComponent(String... lines) {
+ this.lines = lines;
+ }
+
+ @Override
+ public int lineCount() {
+ return lines.length;
+ }
+
+ @Override
+ public void appendTo(StringBuilder sb) {
+ String[] lines = this.lines;
+ int len = lines.length;
+ if (0 < len) {
+ sb.append(lines[0]);
+ }
+ for (int i = 1; i < len; i++) {
+ sb.append('\n').append(lines[i]);
+ }
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DefaultPageBuilder.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DefaultPageBuilder.java index a584e7e..e06730f 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DefaultPageBuilder.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DefaultPageBuilder.java @@ -1,115 +1,115 @@ -package io.dico.dicore.command.chat.help.defaults; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.chat.help.*; -import org.bukkit.permissions.Permissible; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.ListIterator; - -public class DefaultPageBuilder implements IPageBuilder { - - @Override - public String getPage(List<IHelpTopic> helpTopics, IPageLayout pageLayout, ICommandAddress target, - Permissible viewer, ExecutionContext context, int pageNum, int pageLen) { - if (pageLen <= 0 || pageNum < 0) { - throw new IllegalArgumentException(); - } - - List<IHelpComponent> components = new LinkedList<>(); - for (IHelpTopic topic : helpTopics) { - components.addAll(topic.getComponents(target, viewer, context, true)); - } - - PageBorders pageBorders = null; - int componentStartIdx = -1; - int componentEndIdx = -1; - int totalPageCount = 0; - int curPageLines = 0; - - ListIterator<IHelpComponent> iterator = components.listIterator(); - - while (iterator.hasNext()) { - if (curPageLines == 0) { - - if (pageBorders != null) { - iterator.add(pageBorders.getFooter()); - } - - if (pageNum == totalPageCount) { - componentStartIdx = iterator.nextIndex(); - } else if (pageNum + 1 == totalPageCount) { - componentEndIdx = iterator.nextIndex(); - } - - pageBorders = pageLayout.getPageBorders(target, viewer, context, totalPageCount + 1); - - if (pageBorders != null) { - iterator.add(pageBorders.getHeader()); - iterator.previous(); - - curPageLines += pageBorders.getFooter().lineCount(); - } - - totalPageCount++; - } - - IHelpComponent component = iterator.next(); - int lineCount = component.lineCount(); - curPageLines += lineCount; - - if (curPageLines >= pageLen) { - curPageLines = 0; - } - } - - if (componentStartIdx == -1) { - // page does not exist - return ""; - } - - if (componentEndIdx == -1) { - componentEndIdx = components.size(); - } - - StringBuilder sb = new StringBuilder(); - iterator = components.listIterator(componentStartIdx); - int count = componentEndIdx - componentStartIdx; - boolean first = true; - - while (count-- > 0) { - IHelpComponent component = iterator.next(); - if (component instanceof IPageBorder) { - ((IPageBorder) component).setPageCount(totalPageCount); - } - if (first) { - first = false; - } else { - sb.append('\n'); - } - component.appendTo(sb); - - } - - return sb.toString(); - } - - public static String combine(List<IHelpComponent> components) { - StringBuilder rv = new StringBuilder(); - - Iterator<IHelpComponent> iterator = components.iterator(); - if (iterator.hasNext()) { - iterator.next().appendTo(rv); - } - while (iterator.hasNext()) { - rv.append('\n'); - iterator.next().appendTo(rv); - } - - return rv.toString(); - } - -} +package io.dico.dicore.command.chat.help.defaults;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.chat.help.*;
+import org.bukkit.permissions.Permissible;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ListIterator;
+
+public class DefaultPageBuilder implements IPageBuilder {
+
+ @Override
+ public String getPage(List<IHelpTopic> helpTopics, IPageLayout pageLayout, ICommandAddress target,
+ Permissible viewer, ExecutionContext context, int pageNum, int pageLen) {
+ if (pageLen <= 0 || pageNum < 0) {
+ throw new IllegalArgumentException();
+ }
+
+ List<IHelpComponent> components = new LinkedList<>();
+ for (IHelpTopic topic : helpTopics) {
+ components.addAll(topic.getComponents(target, viewer, context, true));
+ }
+
+ PageBorders pageBorders = null;
+ int componentStartIdx = -1;
+ int componentEndIdx = -1;
+ int totalPageCount = 0;
+ int curPageLines = 0;
+
+ ListIterator<IHelpComponent> iterator = components.listIterator();
+
+ while (iterator.hasNext()) {
+ if (curPageLines == 0) {
+
+ if (pageBorders != null) {
+ iterator.add(pageBorders.getFooter());
+ }
+
+ if (pageNum == totalPageCount) {
+ componentStartIdx = iterator.nextIndex();
+ } else if (pageNum + 1 == totalPageCount) {
+ componentEndIdx = iterator.nextIndex();
+ }
+
+ pageBorders = pageLayout.getPageBorders(target, viewer, context, totalPageCount + 1);
+
+ if (pageBorders != null) {
+ iterator.add(pageBorders.getHeader());
+ iterator.previous();
+
+ curPageLines += pageBorders.getFooter().lineCount();
+ }
+
+ totalPageCount++;
+ }
+
+ IHelpComponent component = iterator.next();
+ int lineCount = component.lineCount();
+ curPageLines += lineCount;
+
+ if (curPageLines >= pageLen) {
+ curPageLines = 0;
+ }
+ }
+
+ if (componentStartIdx == -1) {
+ // page does not exist
+ return "";
+ }
+
+ if (componentEndIdx == -1) {
+ componentEndIdx = components.size();
+ }
+
+ StringBuilder sb = new StringBuilder();
+ iterator = components.listIterator(componentStartIdx);
+ int count = componentEndIdx - componentStartIdx;
+ boolean first = true;
+
+ while (count-- > 0) {
+ IHelpComponent component = iterator.next();
+ if (component instanceof IPageBorder) {
+ ((IPageBorder) component).setPageCount(totalPageCount);
+ }
+ if (first) {
+ first = false;
+ } else {
+ sb.append('\n');
+ }
+ component.appendTo(sb);
+
+ }
+
+ return sb.toString();
+ }
+
+ public static String combine(List<IHelpComponent> components) {
+ StringBuilder rv = new StringBuilder();
+
+ Iterator<IHelpComponent> iterator = components.iterator();
+ if (iterator.hasNext()) {
+ iterator.next().appendTo(rv);
+ }
+ while (iterator.hasNext()) {
+ rv.append('\n');
+ iterator.next().appendTo(rv);
+ }
+
+ return rv.toString();
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DefaultPageLayout.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DefaultPageLayout.java index 8d3d004..d950b09 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DefaultPageLayout.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DefaultPageLayout.java @@ -1,40 +1,40 @@ -package io.dico.dicore.command.chat.help.defaults; - -import io.dico.dicore.command.EMessageType; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.ModifiableCommandAddress; -import io.dico.dicore.Formatting; -import io.dico.dicore.command.chat.IChatHandler; -import io.dico.dicore.command.chat.help.IPageBorder; -import io.dico.dicore.command.chat.help.IPageLayout; -import io.dico.dicore.command.chat.help.PageBorders; -import org.bukkit.permissions.Permissible; - -public class DefaultPageLayout implements IPageLayout { - - @Override - public PageBorders getPageBorders(ICommandAddress target, Permissible viewer, ExecutionContext context, int pageNum) { - IChatHandler c = context.getAddress().getChatHandler(); - String prefix = c.getMessagePrefixForType(EMessageType.INFORMATIVE); - Formatting informative = c.getChatFormatForType(EMessageType.INFORMATIVE); - Formatting number = c.getChatFormatForType(EMessageType.NEUTRAL); - - String nextPageCommand; - ICommandAddress executor = context.getAddress(); - if (((ModifiableCommandAddress) executor).hasHelpCommand()) { - nextPageCommand = ((ModifiableCommandAddress) executor).getHelpCommand().getAddress() + ' ' + (pageNum + 1); - } else { - nextPageCommand = executor.getAddress() + ' ' + (pageNum + 1); - } - - String header = prefix + informative + "Help page " + number + pageNum + informative + - '/' + number + "%pageCount%" + informative + " for /" + target.getAddress(); - String footer = informative + "Type /" + nextPageCommand + " for the next page"; - - IPageBorder headerBorder = PageBorders.simpleBorder("", header); - IPageBorder footerBorder = PageBorders.disappearingBorder(pageNum, footer); - return new PageBorders(headerBorder, footerBorder); - } - -} +package io.dico.dicore.command.chat.help.defaults;
+
+import io.dico.dicore.command.EMessageType;
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.ModifiableCommandAddress;
+import io.dico.dicore.Formatting;
+import io.dico.dicore.command.chat.IChatHandler;
+import io.dico.dicore.command.chat.help.IPageBorder;
+import io.dico.dicore.command.chat.help.IPageLayout;
+import io.dico.dicore.command.chat.help.PageBorders;
+import org.bukkit.permissions.Permissible;
+
+public class DefaultPageLayout implements IPageLayout {
+
+ @Override
+ public PageBorders getPageBorders(ICommandAddress target, Permissible viewer, ExecutionContext context, int pageNum) {
+ IChatHandler c = context.getAddress().getChatHandler();
+ String prefix = c.getMessagePrefixForType(EMessageType.INFORMATIVE);
+ Formatting informative = c.getChatFormatForType(EMessageType.INFORMATIVE);
+ Formatting number = c.getChatFormatForType(EMessageType.NEUTRAL);
+
+ String nextPageCommand;
+ ICommandAddress executor = context.getAddress();
+ if (((ModifiableCommandAddress) executor).hasHelpCommand()) {
+ nextPageCommand = ((ModifiableCommandAddress) executor).getHelpCommand().getAddress() + ' ' + (pageNum + 1);
+ } else {
+ nextPageCommand = executor.getAddress() + ' ' + (pageNum + 1);
+ }
+
+ String header = prefix + informative + "Help page " + number + pageNum + informative +
+ '/' + number + "%pageCount%" + informative + " for /" + target.getAddress();
+ String footer = informative + "Type /" + nextPageCommand + " for the next page";
+
+ IPageBorder headerBorder = PageBorders.simpleBorder("", header);
+ IPageBorder footerBorder = PageBorders.disappearingBorder(pageNum, footer);
+ return new PageBorders(headerBorder, footerBorder);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DescriptionHelpTopic.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DescriptionHelpTopic.java index ae88ea2..0899db4 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DescriptionHelpTopic.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/DescriptionHelpTopic.java @@ -1,45 +1,45 @@ -package io.dico.dicore.command.chat.help.defaults; - -import io.dico.dicore.command.Command; -import io.dico.dicore.command.EMessageType; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.Formatting; -import io.dico.dicore.command.chat.help.IHelpComponent; -import io.dico.dicore.command.chat.help.IHelpTopic; -import io.dico.dicore.command.chat.help.SimpleHelpComponent; -import org.bukkit.permissions.Permissible; - -import java.util.ArrayList; -import java.util.List; - -public class DescriptionHelpTopic implements IHelpTopic { - - @Override - public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) { - List<IHelpComponent> out = new ArrayList<>(); - Formatting format = context.getFormat(EMessageType.DESCRIPTION); - - if (!target.hasCommand()) { - return out; - } - Command command = target.getCommand(); - String[] description = command.getDescription(); - if (description.length == 0) { - String shortDescription = command.getShortDescription(); - if (shortDescription == null) { - return out; - } - - description = new String[]{shortDescription}; - } - - for (int i = 0; i < description.length; i++) { - description[i] = format + description[i]; - } - - out.add(new SimpleHelpComponent(description)); - return out; - } - -} +package io.dico.dicore.command.chat.help.defaults;
+
+import io.dico.dicore.command.Command;
+import io.dico.dicore.command.EMessageType;
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.Formatting;
+import io.dico.dicore.command.chat.help.IHelpComponent;
+import io.dico.dicore.command.chat.help.IHelpTopic;
+import io.dico.dicore.command.chat.help.SimpleHelpComponent;
+import org.bukkit.permissions.Permissible;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class DescriptionHelpTopic implements IHelpTopic {
+
+ @Override
+ public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) {
+ List<IHelpComponent> out = new ArrayList<>();
+ Formatting format = context.getFormat(EMessageType.DESCRIPTION);
+
+ if (!target.hasCommand()) {
+ return out;
+ }
+ Command command = target.getCommand();
+ String[] description = command.getDescription();
+ if (description.length == 0) {
+ String shortDescription = command.getShortDescription();
+ if (shortDescription == null) {
+ return out;
+ }
+
+ description = new String[]{shortDescription};
+ }
+
+ for (int i = 0; i < description.length; i++) {
+ description[i] = format + description[i];
+ }
+
+ out.add(new SimpleHelpComponent(description));
+ return out;
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SubcommandsHelpTopic.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SubcommandsHelpTopic.java index 0e680ae..b302769 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SubcommandsHelpTopic.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SubcommandsHelpTopic.java @@ -1,59 +1,59 @@ -package io.dico.dicore.command.chat.help.defaults; - -import io.dico.dicore.command.EMessageType; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.Formatting; -import io.dico.dicore.command.chat.help.IHelpComponent; -import io.dico.dicore.command.chat.help.IHelpTopic; -import io.dico.dicore.command.chat.help.SimpleHelpComponent; -import org.bukkit.command.CommandSender; -import org.bukkit.permissions.Permissible; - -import java.util.*; - -public class SubcommandsHelpTopic implements IHelpTopic { - - @Override - public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) { - Collection<String> mainKeys = target.getChildrenMainKeys(); - if (mainKeys.isEmpty()) { - return Collections.emptyList(); - } - - List<IHelpComponent> result = new ArrayList<>(); - - mainKeys = new ArrayList<>(target.getChildrenMainKeys()); - ((ArrayList<String>) mainKeys).sort(null); - - CommandSender sender = viewer instanceof CommandSender ? (CommandSender) viewer : context.getSender(); - for (String key : mainKeys) { - ICommandAddress child = target.getChild(key); - if ((child.hasChildren() || child.hasUserDeclaredCommand()) && child.getCommand().isVisibleTo(sender)) { - result.add(getComponent(child, viewer, context)); - } - } - - return result; - } - - public IHelpComponent getComponent(ICommandAddress child, Permissible viewer, ExecutionContext context) { - Formatting subcommand = colorOf(context, EMessageType.SUBCOMMAND); - Formatting highlight = colorOf(context, EMessageType.HIGHLIGHT); - - String address = subcommand + "/" + child.getParent().getAddress() + ' ' + highlight + child.getMainKey(); - - String description = child.hasCommand() ? child.getCommand().getShortDescription() : null; - if (description != null) { - Formatting descriptionFormat = colorOf(context, EMessageType.DESCRIPTION); - return new SimpleHelpComponent(address, descriptionFormat + description); - } - - return new SimpleHelpComponent(address); - } - - private static Formatting colorOf(ExecutionContext context, EMessageType type) { - return context.getAddress().getChatHandler().getChatFormatForType(type); - } - -} +package io.dico.dicore.command.chat.help.defaults;
+
+import io.dico.dicore.command.EMessageType;
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.Formatting;
+import io.dico.dicore.command.chat.help.IHelpComponent;
+import io.dico.dicore.command.chat.help.IHelpTopic;
+import io.dico.dicore.command.chat.help.SimpleHelpComponent;
+import org.bukkit.command.CommandSender;
+import org.bukkit.permissions.Permissible;
+
+import java.util.*;
+
+public class SubcommandsHelpTopic implements IHelpTopic {
+
+ @Override
+ public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) {
+ Collection<String> mainKeys = target.getChildrenMainKeys();
+ if (mainKeys.isEmpty()) {
+ return Collections.emptyList();
+ }
+
+ List<IHelpComponent> result = new ArrayList<>();
+
+ mainKeys = new ArrayList<>(target.getChildrenMainKeys());
+ ((ArrayList<String>) mainKeys).sort(null);
+
+ CommandSender sender = viewer instanceof CommandSender ? (CommandSender) viewer : context.getSender();
+ for (String key : mainKeys) {
+ ICommandAddress child = target.getChild(key);
+ if ((child.hasChildren() || child.hasUserDeclaredCommand()) && child.getCommand().isVisibleTo(sender)) {
+ result.add(getComponent(child, viewer, context));
+ }
+ }
+
+ return result;
+ }
+
+ public IHelpComponent getComponent(ICommandAddress child, Permissible viewer, ExecutionContext context) {
+ Formatting subcommand = colorOf(context, EMessageType.SUBCOMMAND);
+ Formatting highlight = colorOf(context, EMessageType.HIGHLIGHT);
+
+ String address = subcommand + "/" + child.getParent().getAddress() + ' ' + highlight + child.getMainKey();
+
+ String description = child.hasCommand() ? child.getCommand().getShortDescription() : null;
+ if (description != null) {
+ Formatting descriptionFormat = colorOf(context, EMessageType.DESCRIPTION);
+ return new SimpleHelpComponent(address, descriptionFormat + description);
+ }
+
+ return new SimpleHelpComponent(address);
+ }
+
+ private static Formatting colorOf(ExecutionContext context, EMessageType type) {
+ return context.getAddress().getChatHandler().getChatFormatForType(type);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SyntaxHelpTopic.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SyntaxHelpTopic.java index d0e3ebe..f0f820f 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SyntaxHelpTopic.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SyntaxHelpTopic.java @@ -1,92 +1,92 @@ -package io.dico.dicore.command.chat.help.defaults; - -import io.dico.dicore.command.Command; -import io.dico.dicore.command.EMessageType; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.Formatting; -import io.dico.dicore.command.chat.help.IHelpComponent; -import io.dico.dicore.command.chat.help.IHelpTopic; -import io.dico.dicore.command.chat.help.SimpleHelpComponent; -import io.dico.dicore.command.parameter.Parameter; -import io.dico.dicore.command.parameter.ParameterList; -import org.bukkit.permissions.Permissible; - -import java.util.Collections; -import java.util.List; -import java.util.Map; - -public class SyntaxHelpTopic implements IHelpTopic { - - @Override - public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) { - if (!target.hasCommand()) { - return Collections.emptyList(); - } - - if (target.hasChildren()) { - if (!isForPage) { - // HelpPages will send help instead of syntax, which might in turn include syntax as well. - return Collections.emptyList(); - } - - if (!target.hasUserDeclaredCommand() && !target.getCommand().getParameterList().hasAnyParameters()) { - // no point adding syntax at all - return Collections.emptyList(); - } - } - - StringBuilder line = new StringBuilder(); - if (isForPage) - line.append(context.getFormat(EMessageType.SYNTAX)) - .append("Syntax: "); - - line.append('/') - .append(context.getFormat(EMessageType.INSTRUCTION)) - .append(target.getAddress()) - .append(' '); - - addShortSyntax(line, target, context); - - return Collections.singletonList(new SimpleHelpComponent(line.toString())); - } - - private static void addShortSyntax(StringBuilder builder, ICommandAddress address, ExecutionContext ctx) { - if (address.hasCommand()) { - Formatting syntaxColor = ctx.getFormat(EMessageType.SYNTAX); - Formatting highlight = ctx.getFormat(EMessageType.HIGHLIGHT); - builder.append(syntaxColor); - - Command command = address.getCommand(); - ParameterList list = command.getParameterList(); - Parameter<?, ?> repeated = list.getRepeatedParameter(); - - int requiredCount = list.getRequiredCount(); - List<Parameter<?, ?>> indexedParameters = list.getIndexedParameters(); - for (int i = 0, n = indexedParameters.size(); i < n; i++) { - builder.append(i < requiredCount ? " <" : " ["); - Parameter<?, ?> param = indexedParameters.get(i); - builder.append(param.getName()); - if (param == repeated) { - builder.append(highlight).append("...").append(syntaxColor); - } - builder.append(i < requiredCount ? '>' : ']'); - } - - Map<String, Parameter<?, ?>> parametersByName = list.getParametersByName(); - for (Parameter<?, ?> param : parametersByName.values()) { - if (param.isFlag()) { - builder.append(" [").append(param.getName()); - if (param.expectsInput()) { - builder.append(" <").append(param.getName()).append(">"); - } - builder.append(']'); - } - } - - } else { - builder.append(' '); - } - } - -} +package io.dico.dicore.command.chat.help.defaults;
+
+import io.dico.dicore.command.Command;
+import io.dico.dicore.command.EMessageType;
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.Formatting;
+import io.dico.dicore.command.chat.help.IHelpComponent;
+import io.dico.dicore.command.chat.help.IHelpTopic;
+import io.dico.dicore.command.chat.help.SimpleHelpComponent;
+import io.dico.dicore.command.parameter.Parameter;
+import io.dico.dicore.command.parameter.ParameterList;
+import org.bukkit.permissions.Permissible;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class SyntaxHelpTopic implements IHelpTopic {
+
+ @Override
+ public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) {
+ if (!target.hasCommand()) {
+ return Collections.emptyList();
+ }
+
+ if (target.hasChildren()) {
+ if (!isForPage) {
+ // HelpPages will send help instead of syntax, which might in turn include syntax as well.
+ return Collections.emptyList();
+ }
+
+ if (!target.hasUserDeclaredCommand() && !target.getCommand().getParameterList().hasAnyParameters()) {
+ // no point adding syntax at all
+ return Collections.emptyList();
+ }
+ }
+
+ StringBuilder line = new StringBuilder();
+ if (isForPage)
+ line.append(context.getFormat(EMessageType.SYNTAX))
+ .append("Syntax: ");
+
+ line.append('/')
+ .append(context.getFormat(EMessageType.INSTRUCTION))
+ .append(target.getAddress())
+ .append(' ');
+
+ addShortSyntax(line, target, context);
+
+ return Collections.singletonList(new SimpleHelpComponent(line.toString()));
+ }
+
+ private static void addShortSyntax(StringBuilder builder, ICommandAddress address, ExecutionContext ctx) {
+ if (address.hasCommand()) {
+ Formatting syntaxColor = ctx.getFormat(EMessageType.SYNTAX);
+ Formatting highlight = ctx.getFormat(EMessageType.HIGHLIGHT);
+ builder.append(syntaxColor);
+
+ Command command = address.getCommand();
+ ParameterList list = command.getParameterList();
+ Parameter<?, ?> repeated = list.getRepeatedParameter();
+
+ int requiredCount = list.getRequiredCount();
+ List<Parameter<?, ?>> indexedParameters = list.getIndexedParameters();
+ for (int i = 0, n = indexedParameters.size(); i < n; i++) {
+ builder.append(i < requiredCount ? " <" : " [");
+ Parameter<?, ?> param = indexedParameters.get(i);
+ builder.append(param.getName());
+ if (param == repeated) {
+ builder.append(highlight).append("...").append(syntaxColor);
+ }
+ builder.append(i < requiredCount ? '>' : ']');
+ }
+
+ Map<String, Parameter<?, ?>> parametersByName = list.getParametersByName();
+ for (Parameter<?, ?> param : parametersByName.values()) {
+ if (param.isFlag()) {
+ builder.append(" [").append(param.getName());
+ if (param.expectsInput()) {
+ builder.append(" <").append(param.getName()).append(">");
+ }
+ builder.append(']');
+ }
+ }
+
+ } else {
+ builder.append(' ');
+ }
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/EInsertionStage.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/EInsertionStage.java index 4f0026d..eb3605d 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/EInsertionStage.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/EInsertionStage.java @@ -1,29 +1,29 @@ -package io.dico.dicore.command.chat.help.insertion; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.chat.help.IHelpComponent; -import org.bukkit.permissions.Permissible; - -import java.util.List; - -public enum EInsertionStage implements IInsertionFunction { - START { - @Override - public int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context) { - return 0; - } - }, - CENTER { - @Override - public int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context) { - return current.size() / 2; - } - }, - END { - @Override - public int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context) { - return current.size(); - } - } -} +package io.dico.dicore.command.chat.help.insertion;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.chat.help.IHelpComponent;
+import org.bukkit.permissions.Permissible;
+
+import java.util.List;
+
+public enum EInsertionStage implements IInsertionFunction {
+ START {
+ @Override
+ public int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context) {
+ return 0;
+ }
+ },
+ CENTER {
+ @Override
+ public int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context) {
+ return current.size() / 2;
+ }
+ },
+ END {
+ @Override
+ public int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context) {
+ return current.size();
+ }
+ }
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/HelpComponentInserter.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/HelpComponentInserter.java index f153165..75b52d0 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/HelpComponentInserter.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/HelpComponentInserter.java @@ -1,43 +1,43 @@ -package io.dico.dicore.command.chat.help.insertion; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.chat.help.HelpTopicModifier; -import io.dico.dicore.command.chat.help.IHelpComponent; -import io.dico.dicore.command.chat.help.IHelpTopic; -import org.bukkit.permissions.Permissible; - -import java.util.ArrayList; -import java.util.List; - -public class HelpComponentInserter extends HelpTopicModifier { - private List<IInsertion> insertions = new ArrayList<>(); - - public HelpComponentInserter(IHelpTopic delegate) { - super(delegate); - } - - @Override - protected List<IHelpComponent> modify(List<IHelpComponent> components, ICommandAddress target, Permissible viewer, ExecutionContext context) { - // int componentCount = components.size(); - - for (int i = insertions.size() - 1; i >= 0; i--) { - IInsertion insertion = insertions.get(i); - int idx = insertion.insertionIndex(components, target, viewer, context); - List<IHelpComponent> inserted = insertion.getComponents(target, viewer, context, true); - components.addAll(idx, inserted); - } - - return components; - } - - public HelpComponentInserter insert(IInsertionFunction insertionFunction, IHelpTopic helpTopic) { - return insert(Insertions.combine(helpTopic, insertionFunction)); - } - - public HelpComponentInserter insert(IInsertion insertion) { - insertions.add(insertion); - return this; - } - -} +package io.dico.dicore.command.chat.help.insertion;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.chat.help.HelpTopicModifier;
+import io.dico.dicore.command.chat.help.IHelpComponent;
+import io.dico.dicore.command.chat.help.IHelpTopic;
+import org.bukkit.permissions.Permissible;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class HelpComponentInserter extends HelpTopicModifier {
+ private List<IInsertion> insertions = new ArrayList<>();
+
+ public HelpComponentInserter(IHelpTopic delegate) {
+ super(delegate);
+ }
+
+ @Override
+ protected List<IHelpComponent> modify(List<IHelpComponent> components, ICommandAddress target, Permissible viewer, ExecutionContext context) {
+ // int componentCount = components.size();
+
+ for (int i = insertions.size() - 1; i >= 0; i--) {
+ IInsertion insertion = insertions.get(i);
+ int idx = insertion.insertionIndex(components, target, viewer, context);
+ List<IHelpComponent> inserted = insertion.getComponents(target, viewer, context, true);
+ components.addAll(idx, inserted);
+ }
+
+ return components;
+ }
+
+ public HelpComponentInserter insert(IInsertionFunction insertionFunction, IHelpTopic helpTopic) {
+ return insert(Insertions.combine(helpTopic, insertionFunction));
+ }
+
+ public HelpComponentInserter insert(IInsertion insertion) {
+ insertions.add(insertion);
+ return this;
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/IInsertion.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/IInsertion.java index 757cb91..92309d3 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/IInsertion.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/IInsertion.java @@ -1,7 +1,7 @@ -package io.dico.dicore.command.chat.help.insertion; - -import io.dico.dicore.command.chat.help.IHelpTopic; - -interface IInsertion extends IHelpTopic, IInsertionFunction { - -} +package io.dico.dicore.command.chat.help.insertion;
+
+import io.dico.dicore.command.chat.help.IHelpTopic;
+
+interface IInsertion extends IHelpTopic, IInsertionFunction {
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/IInsertionFunction.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/IInsertionFunction.java index e99c246..356d002 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/IInsertionFunction.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/IInsertionFunction.java @@ -1,14 +1,14 @@ -package io.dico.dicore.command.chat.help.insertion; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.chat.help.IHelpComponent; -import org.bukkit.permissions.Permissible; - -import java.util.List; - -public interface IInsertionFunction { - - int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context); - -} +package io.dico.dicore.command.chat.help.insertion;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.chat.help.IHelpComponent;
+import org.bukkit.permissions.Permissible;
+
+import java.util.List;
+
+public interface IInsertionFunction {
+
+ int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/Insertions.java b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/Insertions.java index 39b2784..220f05b 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/Insertions.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/insertion/Insertions.java @@ -1,31 +1,31 @@ -package io.dico.dicore.command.chat.help.insertion; - -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.chat.help.IHelpComponent; -import io.dico.dicore.command.chat.help.IHelpTopic; -import org.bukkit.permissions.Permissible; - -import java.util.List; - -public class Insertions { - - private Insertions() { - - } - - public static IInsertion combine(IHelpTopic topic, IInsertionFunction function) { - return new IInsertion() { - @Override - public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) { - return topic.getComponents(target, viewer, context, true); - } - - @Override - public int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context) { - return function.insertionIndex(current, target, viewer, context); - } - }; - } - -} +package io.dico.dicore.command.chat.help.insertion;
+
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.chat.help.IHelpComponent;
+import io.dico.dicore.command.chat.help.IHelpTopic;
+import org.bukkit.permissions.Permissible;
+
+import java.util.List;
+
+public class Insertions {
+
+ private Insertions() {
+
+ }
+
+ public static IInsertion combine(IHelpTopic topic, IInsertionFunction function) {
+ return new IInsertion() {
+ @Override
+ public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context, boolean isForPage) {
+ return topic.getComponents(target, viewer, context, true);
+ }
+
+ @Override
+ public int insertionIndex(List<IHelpComponent> current, ICommandAddress target, Permissible viewer, ExecutionContext context) {
+ return function.insertionIndex(current, target, viewer, context);
+ }
+ };
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentBuffer.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentBuffer.java index aa69730..5646814 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentBuffer.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentBuffer.java @@ -1,295 +1,295 @@ -package io.dico.dicore.command.parameter; - -import io.dico.dicore.command.CommandException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.*; - -/** - * Buffer for the arguments. - * Easy to traverse for the parser. - */ -public class ArgumentBuffer extends AbstractList<String> implements Iterator<String>, RandomAccess { - private String[] array; - private int cursor = 0; // index of the next return value - private transient ArgumentBuffer unaffectingCopy = null; // see #getUnaffectingCopy() - - public ArgumentBuffer(String label, String[] args) { - this(combine(label, args)); - } - - private static String[] combine(String label, String[] args) { - String[] result; - //if (args.length > 0 && "".equals(args[args.length - 1])) { - // // drop the last element of args if it is empty - // result = args; - //} else { - result = new String[args.length + 1]; - //} - System.arraycopy(args, 0, result, 1, result.length - 1); - result[0] = Objects.requireNonNull(label); - return result; - } - - /** - * Constructs a new ArgumentBuffer using the given array, without copying it first. - * None of the array its elements should be empty. - * - * @param array the array - * @throws NullPointerException if the array or any of its elements are null - */ - public ArgumentBuffer(String[] array) { - for (String elem : array) { - if (elem == null) throw new NullPointerException("ArgumentBuffer array element"); - } - this.array = array; - - } - - public int getCursor() { - return cursor; - } - - public @NotNull ArgumentBuffer setCursor(int cursor) { - if (cursor <= 0) { - cursor = 0; - } else if (size() <= cursor) { - cursor = size(); - } - this.cursor = cursor; - return this; - } - - @Override - public int size() { - return array.length; - } - - @Override - public @NotNull String get(int index) { - return array[index]; - } - - public int nextIndex() { - return cursor; - } - - public int previousIndex() { - return cursor - 1; - } - - public int remainingElements() { - return size() - nextIndex() - 1; - } - - @Override - public boolean hasNext() { - return nextIndex() < size(); - } - - public boolean hasPrevious() { - return 0 <= previousIndex(); - } - - /** - * Unlike conventional ListIterator implementations, this returns null if there is no next element - * - * @return the next value, or null - */ - @Override - public @Nullable String next() { - return hasNext() ? get(cursor++) : null; - } - - public @NotNull String requireNext(String parameterName) throws CommandException { - String next = next(); - if (next == null) { - throw CommandException.missingArgument(parameterName); - } - return next; - } - - // useful for completion code - public @NotNull String nextOrEmpty() { - return hasNext() ? get(cursor++) : ""; - } - - /** - * Unlike conventional ListIterator implementations, this returns null if there is no previous element - * - * @return the previous value, or null - */ - public @Nullable String previous() { - return hasPrevious() ? get(--cursor) : null; - } - - public @Nullable String peekNext() { - return hasNext() ? get(cursor) : null; - } - - public @Nullable String peekPrevious() { - return hasPrevious() ? get(cursor - 1) : null; - } - - public @NotNull ArgumentBuffer advance() { - return advance(1); - } - - public @NotNull ArgumentBuffer advance(int amount) { - cursor = Math.min(Math.max(0, cursor + amount), size()); - return this; - } - - public @NotNull ArgumentBuffer rewind() { - return rewind(1); - } - - public @NotNull ArgumentBuffer rewind(int amount) { - return advance(-amount); - } - - @NotNull String[] getArray() { - return array; - } - - public @NotNull String[] getArrayFromCursor() { - return getArrayFromIndex(cursor); - } - - public @NotNull String[] getArrayFromIndex(int index) { - return Arrays.copyOfRange(array, index, array.length); - } - - public @NotNull String getRawInput() { - return String.join(" ", array); - } - - public @NotNull String[] toArray() { - return array.clone(); - } - - @Override - public @NotNull Iterator<String> iterator() { - return this; - } - - @Override - public @NotNull ListIterator<String> listIterator() { - return new ListIterator<String>() { - @Override - public boolean hasNext() { - return ArgumentBuffer.this.hasNext(); - } - - @Override - public String next() { - if (!hasNext()) { - throw new NoSuchElementException(); - } - return ArgumentBuffer.this.next(); - } - - @Override - public boolean hasPrevious() { - return ArgumentBuffer.this.hasPrevious(); - } - - @Override - public String previous() { - if (!hasPrevious()) { - throw new NoSuchElementException(); - } - return ArgumentBuffer.this.previous(); - } - - @Override - public int nextIndex() { - return ArgumentBuffer.this.nextIndex(); - } - - @Override - public int previousIndex() { - return ArgumentBuffer.this.previousIndex(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - @Override - public void set(String s) { - throw new UnsupportedOperationException(); - } - - @Override - public void add(String s) { - throw new UnsupportedOperationException(); - } - }; - } - - public void dropTrailingEmptyElements() { - int removeCount = 0; - String[] array = this.array; - for (int i = array.length - 1; i >= 0; i--) { - if ("".equals(array[i])) { - removeCount++; - } - } - - if (removeCount > 0) { - String[] newArray = new String[array.length - removeCount]; - System.arraycopy(array, 0, newArray, 0, newArray.length); - this.array = newArray; - - if (cursor > newArray.length) { - cursor = newArray.length; - } - } - } - - /** - * Preprocess this argument buffer with the given preprocessor - * - * @param preProcessor preprocessor - * @return a new ArgumentBuffer with processed contents. Might be this buffer if nothing changed. - */ - public @NotNull ArgumentBuffer preprocessArguments(IArgumentPreProcessor preProcessor) { - return preProcessor.process(this, -1); - } - - /** - * Allows a piece of code to traverse this buffer without modifying its cursor. - * After this method has been called for the first time on this instance, if this method - * or the {@link #clone()} method are called, the operation carried out on the prior result has finished. - * As such, the same instance might be returned again. - * - * @return A view of this buffer that doesn't affect this buffer's cursor. - */ - public ArgumentBuffer getUnaffectingCopy() { - // the copy doesn't alter the cursor of this ArgumentBuffer when moved, but traverses the same array reference. - // there is only ever one copy of an ArgumentBuffer, the cursor of which is updated on every call to this method. - - ArgumentBuffer unaffectingCopy = this.unaffectingCopy; - if (unaffectingCopy == null) { - this.unaffectingCopy = unaffectingCopy = new ArgumentBuffer(array); - } - unaffectingCopy.cursor = this.cursor; - return unaffectingCopy; - } - - @SuppressWarnings("MethodDoesntCallSuperMethod") - public @NotNull ArgumentBuffer clone() { - ArgumentBuffer result = getUnaffectingCopy(); - this.unaffectingCopy = null; - return result; - } - - @Override - public String toString() { - return String.format("ArgumentBuffer(size = %d, cursor = %d)", size(), getCursor()); - } - -} +package io.dico.dicore.command.parameter;
+
+import io.dico.dicore.command.CommandException;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.*;
+
+/**
+ * Buffer for the arguments.
+ * Easy to traverse for the parser.
+ */
+public class ArgumentBuffer extends AbstractList<String> implements Iterator<String>, RandomAccess {
+ private String[] array;
+ private int cursor = 0; // index of the next return value
+ private transient ArgumentBuffer unaffectingCopy = null; // see #getUnaffectingCopy()
+
+ public ArgumentBuffer(String label, String[] args) {
+ this(combine(label, args));
+ }
+
+ private static String[] combine(String label, String[] args) {
+ String[] result;
+ //if (args.length > 0 && "".equals(args[args.length - 1])) {
+ // // drop the last element of args if it is empty
+ // result = args;
+ //} else {
+ result = new String[args.length + 1];
+ //}
+ System.arraycopy(args, 0, result, 1, result.length - 1);
+ result[0] = Objects.requireNonNull(label);
+ return result;
+ }
+
+ /**
+ * Constructs a new ArgumentBuffer using the given array, without copying it first.
+ * None of the array its elements should be empty.
+ *
+ * @param array the array
+ * @throws NullPointerException if the array or any of its elements are null
+ */
+ public ArgumentBuffer(String[] array) {
+ for (String elem : array) {
+ if (elem == null) throw new NullPointerException("ArgumentBuffer array element");
+ }
+ this.array = array;
+
+ }
+
+ public int getCursor() {
+ return cursor;
+ }
+
+ public @NotNull ArgumentBuffer setCursor(int cursor) {
+ if (cursor <= 0) {
+ cursor = 0;
+ } else if (size() <= cursor) {
+ cursor = size();
+ }
+ this.cursor = cursor;
+ return this;
+ }
+
+ @Override
+ public int size() {
+ return array.length;
+ }
+
+ @Override
+ public @NotNull String get(int index) {
+ return array[index];
+ }
+
+ public int nextIndex() {
+ return cursor;
+ }
+
+ public int previousIndex() {
+ return cursor - 1;
+ }
+
+ public int remainingElements() {
+ return size() - nextIndex() - 1;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return nextIndex() < size();
+ }
+
+ public boolean hasPrevious() {
+ return 0 <= previousIndex();
+ }
+
+ /**
+ * Unlike conventional ListIterator implementations, this returns null if there is no next element
+ *
+ * @return the next value, or null
+ */
+ @Override
+ public @Nullable String next() {
+ return hasNext() ? get(cursor++) : null;
+ }
+
+ public @NotNull String requireNext(String parameterName) throws CommandException {
+ String next = next();
+ if (next == null) {
+ throw CommandException.missingArgument(parameterName);
+ }
+ return next;
+ }
+
+ // useful for completion code
+ public @NotNull String nextOrEmpty() {
+ return hasNext() ? get(cursor++) : "";
+ }
+
+ /**
+ * Unlike conventional ListIterator implementations, this returns null if there is no previous element
+ *
+ * @return the previous value, or null
+ */
+ public @Nullable String previous() {
+ return hasPrevious() ? get(--cursor) : null;
+ }
+
+ public @Nullable String peekNext() {
+ return hasNext() ? get(cursor) : null;
+ }
+
+ public @Nullable String peekPrevious() {
+ return hasPrevious() ? get(cursor - 1) : null;
+ }
+
+ public @NotNull ArgumentBuffer advance() {
+ return advance(1);
+ }
+
+ public @NotNull ArgumentBuffer advance(int amount) {
+ cursor = Math.min(Math.max(0, cursor + amount), size());
+ return this;
+ }
+
+ public @NotNull ArgumentBuffer rewind() {
+ return rewind(1);
+ }
+
+ public @NotNull ArgumentBuffer rewind(int amount) {
+ return advance(-amount);
+ }
+
+ @NotNull String[] getArray() {
+ return array;
+ }
+
+ public @NotNull String[] getArrayFromCursor() {
+ return getArrayFromIndex(cursor);
+ }
+
+ public @NotNull String[] getArrayFromIndex(int index) {
+ return Arrays.copyOfRange(array, index, array.length);
+ }
+
+ public @NotNull String getRawInput() {
+ return String.join(" ", array);
+ }
+
+ public @NotNull String[] toArray() {
+ return array.clone();
+ }
+
+ @Override
+ public @NotNull Iterator<String> iterator() {
+ return this;
+ }
+
+ @Override
+ public @NotNull ListIterator<String> listIterator() {
+ return new ListIterator<String>() {
+ @Override
+ public boolean hasNext() {
+ return ArgumentBuffer.this.hasNext();
+ }
+
+ @Override
+ public String next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+ return ArgumentBuffer.this.next();
+ }
+
+ @Override
+ public boolean hasPrevious() {
+ return ArgumentBuffer.this.hasPrevious();
+ }
+
+ @Override
+ public String previous() {
+ if (!hasPrevious()) {
+ throw new NoSuchElementException();
+ }
+ return ArgumentBuffer.this.previous();
+ }
+
+ @Override
+ public int nextIndex() {
+ return ArgumentBuffer.this.nextIndex();
+ }
+
+ @Override
+ public int previousIndex() {
+ return ArgumentBuffer.this.previousIndex();
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void set(String s) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void add(String s) {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ public void dropTrailingEmptyElements() {
+ int removeCount = 0;
+ String[] array = this.array;
+ for (int i = array.length - 1; i >= 0; i--) {
+ if ("".equals(array[i])) {
+ removeCount++;
+ }
+ }
+
+ if (removeCount > 0) {
+ String[] newArray = new String[array.length - removeCount];
+ System.arraycopy(array, 0, newArray, 0, newArray.length);
+ this.array = newArray;
+
+ if (cursor > newArray.length) {
+ cursor = newArray.length;
+ }
+ }
+ }
+
+ /**
+ * Preprocess this argument buffer with the given preprocessor
+ *
+ * @param preProcessor preprocessor
+ * @return a new ArgumentBuffer with processed contents. Might be this buffer if nothing changed.
+ */
+ public @NotNull ArgumentBuffer preprocessArguments(IArgumentPreProcessor preProcessor) {
+ return preProcessor.process(this, -1);
+ }
+
+ /**
+ * Allows a piece of code to traverse this buffer without modifying its cursor.
+ * After this method has been called for the first time on this instance, if this method
+ * or the {@link #clone()} method are called, the operation carried out on the prior result has finished.
+ * As such, the same instance might be returned again.
+ *
+ * @return A view of this buffer that doesn't affect this buffer's cursor.
+ */
+ public ArgumentBuffer getUnaffectingCopy() {
+ // the copy doesn't alter the cursor of this ArgumentBuffer when moved, but traverses the same array reference.
+ // there is only ever one copy of an ArgumentBuffer, the cursor of which is updated on every call to this method.
+
+ ArgumentBuffer unaffectingCopy = this.unaffectingCopy;
+ if (unaffectingCopy == null) {
+ this.unaffectingCopy = unaffectingCopy = new ArgumentBuffer(array);
+ }
+ unaffectingCopy.cursor = this.cursor;
+ return unaffectingCopy;
+ }
+
+ @SuppressWarnings("MethodDoesntCallSuperMethod")
+ public @NotNull ArgumentBuffer clone() {
+ ArgumentBuffer result = getUnaffectingCopy();
+ this.unaffectingCopy = null;
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("ArgumentBuffer(size = %d, cursor = %d)", size(), getCursor());
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentMergingPreProcessor.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentMergingPreProcessor.java index ce818b7..a9ccd20 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentMergingPreProcessor.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentMergingPreProcessor.java @@ -1,177 +1,177 @@ -package io.dico.dicore.command.parameter; - -public class ArgumentMergingPreProcessor implements IArgumentPreProcessor { - private final String tokens; - private final char escapeChar; - - public ArgumentMergingPreProcessor(String tokens, char escapeChar) { - if ((tokens.length() & 1) != 0 || tokens.isEmpty()) throw new IllegalArgumentException(); - this.tokens = tokens; - this.escapeChar = escapeChar; - } - - @Override - public ArgumentBuffer process(ArgumentBuffer buffer, int count) { - Parser parser = new Parser(buffer.getArray().clone(), buffer.getCursor(), count); - String[] array = parser.doProcess(); - ArgumentBuffer result = new ArgumentBuffer(array); - parser.updateBuffer(result); - return result; - } - - private class Parser { - private final String[] args; - private final int start; - private final int count; - - private int foundSectionCount; - private int currentIndex; - private int sectionStart; - private char closingToken; - private int sectionEnd; - private int removeCount; - - Parser(String[] args, int start, int count) { - this.start = start; - this.args = args; - this.count = count; - } - - private void reset() { - foundSectionCount = 0; - currentIndex = start; - sectionStart = -1; - closingToken = 0; - sectionEnd = -1; - removeCount = 0; - } - - private boolean findNextSectionStart() { - if (count >= 0 && foundSectionCount >= count) return false; - - while (currentIndex < args.length) { - String arg = args[currentIndex]; - if (arg == null) { - throw new IllegalArgumentException(); - } - - if (arg.isEmpty()) { - ++currentIndex; - continue; - } - - int openingTokenIndex = tokens.indexOf(arg.charAt(0)); - if (openingTokenIndex == -1 || (openingTokenIndex & 1) != 0) { - ++currentIndex; - continue; - } - - // found - closingToken = tokens.charAt(openingTokenIndex | 1); - sectionStart = currentIndex; - return true; - } - - return false; - } - - private boolean findNextSectionEnd() { - while (currentIndex < args.length) { - String arg = args[currentIndex]; - if (arg == null) { - throw new IllegalArgumentException(); - } - - if (arg.isEmpty() - || arg.charAt(arg.length() - 1) != closingToken - || (sectionStart == currentIndex && arg.length() == 1)) { - ++currentIndex; - continue; - } - - if (escapeChar != 0 - && arg.length() > 1 - && arg.charAt(arg.length() - 2) == escapeChar) { - // escaped - ++currentIndex; - continue; - } - - // found - closingToken = 0; - sectionEnd = currentIndex; - ++currentIndex; - return true; - } - - return false; - } - - private void processFoundSection() { - if (sectionStart == sectionEnd) { - String arg = args[sectionStart]; - args[sectionStart] = arg.substring(1, arg.length() - 1); - return; - } - - removeCount += sectionEnd - sectionStart; - - StringBuilder sb = new StringBuilder(); - sb.append(args[sectionStart].substring(1)); - - for (int i = sectionStart + 1; i < sectionEnd; i++) { - sb.append(' '); - sb.append(args[i]); - args[i] = null; - } - sb.append(' '); - sb.append(args[sectionEnd].substring(0, args[sectionEnd].length() - 1)); - args[sectionEnd] = null; - - args[sectionStart] = sb.toString(); - - sectionStart = -1; - sectionEnd = -1; - - ++foundSectionCount; - } - - String[] doProcess() { - reset(); - - while (findNextSectionStart()) { - if (findNextSectionEnd()) { - processFoundSection(); - } else { - currentIndex = sectionStart + 1; - } - } - - if (removeCount == 0) { - return args; - } - - String[] result = new String[args.length - removeCount]; - int i = 0; - for (String arg : args) { - if (arg != null) { - result[i++] = arg; - } - } - - return result; - } - - void updateBuffer(ArgumentBuffer buffer) { - if (count < 0) { - buffer.setCursor(start); - } else { - buffer.setCursor(currentIndex); - } - } - - } - -} - - +package io.dico.dicore.command.parameter;
+
+public class ArgumentMergingPreProcessor implements IArgumentPreProcessor {
+ private final String tokens;
+ private final char escapeChar;
+
+ public ArgumentMergingPreProcessor(String tokens, char escapeChar) {
+ if ((tokens.length() & 1) != 0 || tokens.isEmpty()) throw new IllegalArgumentException();
+ this.tokens = tokens;
+ this.escapeChar = escapeChar;
+ }
+
+ @Override
+ public ArgumentBuffer process(ArgumentBuffer buffer, int count) {
+ Parser parser = new Parser(buffer.getArray().clone(), buffer.getCursor(), count);
+ String[] array = parser.doProcess();
+ ArgumentBuffer result = new ArgumentBuffer(array);
+ parser.updateBuffer(result);
+ return result;
+ }
+
+ private class Parser {
+ private final String[] args;
+ private final int start;
+ private final int count;
+
+ private int foundSectionCount;
+ private int currentIndex;
+ private int sectionStart;
+ private char closingToken;
+ private int sectionEnd;
+ private int removeCount;
+
+ Parser(String[] args, int start, int count) {
+ this.start = start;
+ this.args = args;
+ this.count = count;
+ }
+
+ private void reset() {
+ foundSectionCount = 0;
+ currentIndex = start;
+ sectionStart = -1;
+ closingToken = 0;
+ sectionEnd = -1;
+ removeCount = 0;
+ }
+
+ private boolean findNextSectionStart() {
+ if (count >= 0 && foundSectionCount >= count) return false;
+
+ while (currentIndex < args.length) {
+ String arg = args[currentIndex];
+ if (arg == null) {
+ throw new IllegalArgumentException();
+ }
+
+ if (arg.isEmpty()) {
+ ++currentIndex;
+ continue;
+ }
+
+ int openingTokenIndex = tokens.indexOf(arg.charAt(0));
+ if (openingTokenIndex == -1 || (openingTokenIndex & 1) != 0) {
+ ++currentIndex;
+ continue;
+ }
+
+ // found
+ closingToken = tokens.charAt(openingTokenIndex | 1);
+ sectionStart = currentIndex;
+ return true;
+ }
+
+ return false;
+ }
+
+ private boolean findNextSectionEnd() {
+ while (currentIndex < args.length) {
+ String arg = args[currentIndex];
+ if (arg == null) {
+ throw new IllegalArgumentException();
+ }
+
+ if (arg.isEmpty()
+ || arg.charAt(arg.length() - 1) != closingToken
+ || (sectionStart == currentIndex && arg.length() == 1)) {
+ ++currentIndex;
+ continue;
+ }
+
+ if (escapeChar != 0
+ && arg.length() > 1
+ && arg.charAt(arg.length() - 2) == escapeChar) {
+ // escaped
+ ++currentIndex;
+ continue;
+ }
+
+ // found
+ closingToken = 0;
+ sectionEnd = currentIndex;
+ ++currentIndex;
+ return true;
+ }
+
+ return false;
+ }
+
+ private void processFoundSection() {
+ if (sectionStart == sectionEnd) {
+ String arg = args[sectionStart];
+ args[sectionStart] = arg.substring(1, arg.length() - 1);
+ return;
+ }
+
+ removeCount += sectionEnd - sectionStart;
+
+ StringBuilder sb = new StringBuilder();
+ sb.append(args[sectionStart].substring(1));
+
+ for (int i = sectionStart + 1; i < sectionEnd; i++) {
+ sb.append(' ');
+ sb.append(args[i]);
+ args[i] = null;
+ }
+ sb.append(' ');
+ sb.append(args[sectionEnd].substring(0, args[sectionEnd].length() - 1));
+ args[sectionEnd] = null;
+
+ args[sectionStart] = sb.toString();
+
+ sectionStart = -1;
+ sectionEnd = -1;
+
+ ++foundSectionCount;
+ }
+
+ String[] doProcess() {
+ reset();
+
+ while (findNextSectionStart()) {
+ if (findNextSectionEnd()) {
+ processFoundSection();
+ } else {
+ currentIndex = sectionStart + 1;
+ }
+ }
+
+ if (removeCount == 0) {
+ return args;
+ }
+
+ String[] result = new String[args.length - removeCount];
+ int i = 0;
+ for (String arg : args) {
+ if (arg != null) {
+ result[i++] = arg;
+ }
+ }
+
+ return result;
+ }
+
+ void updateBuffer(ArgumentBuffer buffer) {
+ if (count < 0) {
+ buffer.setCursor(start);
+ } else {
+ buffer.setCursor(currentIndex);
+ }
+ }
+
+ }
+
+}
+
+
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ContextParser.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ContextParser.java index a5afce5..7418c4a 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ContextParser.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ContextParser.java @@ -89,6 +89,8 @@ public class ContextParser { m_curRepeatingList = null; assignDefaultValuesToUncomputedParams(); arrayifyRepeatedParamValue(); + + m_done = true; } } diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java index 0b8198e..5495cce 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java @@ -1,41 +1,41 @@ -package io.dico.dicore.command.parameter; - -/** - * An interface to process tokens such as quotes - */ -@Deprecated -public interface IArgumentPreProcessor { - - /** - * Preprocess the arguments contained within the given ArgumentBuffer. - * If no changes are made, this might return the same buffer. - * Any arguments preceding {@code buffer.getCursor()} will not be affected. - * - * <p> - * If {@code count} is non-negative, it declares a limit on the number of arguments after preprocessing. - * In that case, the buffer's cursor is set to the index of the first argument following processed arguments. - * </p> - * - * @param buffer the argument buffer - * @param count the maximum number of (processed) arguments - * @return the arguments after preprocessing - */ - ArgumentBuffer process(ArgumentBuffer buffer, int count); - - IArgumentPreProcessor NONE = (buffer, count) -> buffer; - - /** - * Get an IArgumentPreProcessor that merges arguments between any two tokens - * - * @param tokens The tokens that the merged arguments should be enclosed by, in subsequent pairs. - * Example: []{}"" - * This would mean the following would be merged: [ hello this is a merged argument] - * @param escapeChar the char that can be used to escape the given tokens - * @return The IArgumentPreProcessor - */ - static IArgumentPreProcessor mergeOnTokens(String tokens, char escapeChar) { - return new ArgumentMergingPreProcessor(tokens, escapeChar); - } - -} - +package io.dico.dicore.command.parameter;
+
+/**
+ * An interface to process tokens such as quotes
+ */
+@Deprecated
+public interface IArgumentPreProcessor {
+
+ /**
+ * Preprocess the arguments contained within the given ArgumentBuffer.
+ * If no changes are made, this might return the same buffer.
+ * Any arguments preceding {@code buffer.getCursor()} will not be affected.
+ *
+ * <p>
+ * If {@code count} is non-negative, it declares a limit on the number of arguments after preprocessing.
+ * In that case, the buffer's cursor is set to the index of the first argument following processed arguments.
+ * </p>
+ *
+ * @param buffer the argument buffer
+ * @param count the maximum number of (processed) arguments
+ * @return the arguments after preprocessing
+ */
+ ArgumentBuffer process(ArgumentBuffer buffer, int count);
+
+ IArgumentPreProcessor NONE = (buffer, count) -> buffer;
+
+ /**
+ * Get an IArgumentPreProcessor that merges arguments between any two tokens
+ *
+ * @param tokens The tokens that the merged arguments should be enclosed by, in subsequent pairs.
+ * Example: []{}""
+ * This would mean the following would be merged: [ hello this is a merged argument]
+ * @param escapeChar the char that can be used to escape the given tokens
+ * @return The IArgumentPreProcessor
+ */
+ static IArgumentPreProcessor mergeOnTokens(String tokens, char escapeChar) {
+ return new ArgumentMergingPreProcessor(tokens, escapeChar);
+ }
+
+}
+
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java index 613d057..f35628c 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java @@ -1,148 +1,148 @@ -package io.dico.dicore.command.parameter; - -import java.util.*; - -/** - * IParameter definition for a command - */ -@SuppressWarnings("UnusedReturnValue") -public class ParameterList { - //private ParameterList parent; - private List<Parameter<?, ?>> indexedParameters; - private Map<String, Parameter<?, ?>> byName; - //private IArgumentPreProcessor argumentPreProcessor = IArgumentPreProcessor.NONE; - private int requiredCount = -1; - private boolean repeatFinalParameter; - - // if the final parameter is repeated and the command is implemented through reflection, - // the repeated parameter is simply the last parameter of the method, rather than the last - // indexed parameter. This might be a flag. As such, this field exists to ensure the correct - // parameter is taken for repeating - private boolean finalParameterMayBeFlag; - - /* - public ParameterList(ParameterList parent) { - this(); - if (parent.repeatFinalParameter) { - throw new IllegalArgumentException("Parent may not have repeating parameters"); - } - this.parent = parent; - }*/ - - public ParameterList() { - this.indexedParameters = new ArrayList<>(); - this.byName = new LinkedHashMap<>(); - this.repeatFinalParameter = false; - } - - /* - public IArgumentPreProcessor getArgumentPreProcessor() { - return argumentPreProcessor; - } - - public ParameterList setArgumentPreProcessor(IArgumentPreProcessor argumentPreProcessor) { - this.argumentPreProcessor = argumentPreProcessor == null ? IArgumentPreProcessor.NONE : argumentPreProcessor; - return this; - }*/ - - public boolean repeatFinalParameter() { - return repeatFinalParameter; - } - - public ParameterList setRepeatFinalParameter(boolean repeatFinalParameter) { - this.repeatFinalParameter = repeatFinalParameter; - return this; - } - - public boolean finalParameterMayBeFlag() { - return finalParameterMayBeFlag; - } - - public ParameterList setFinalParameterMayBeFlag(boolean finalParameterMayBeFlag) { - this.finalParameterMayBeFlag = finalParameterMayBeFlag; - return this; - } - - public int getRequiredCount() { - return requiredCount == -1 ? indexedParameters.size() : requiredCount; - } - - public ParameterList setRequiredCount(int requiredCount) { - this.requiredCount = requiredCount; - return this; - } - - public boolean hasAnyParameters() { - return !byName.isEmpty(); - } - - public int getIndexedParameterCount() { - return indexedParameters.size(); - } - - public List<Parameter<?, ?>> getIndexedParameters() { - return Collections.unmodifiableList(indexedParameters); - } - - public Parameter<?, ?> getParameterByName(String name) { - return byName.get(name); - } - - public String getIndexedParameterName(int index) { - return indexedParameters.get(index).getName(); - } - - public Map<String, Parameter<?, ?>> getParametersByName() { - return Collections.unmodifiableMap(byName); - } - - /** - * Add the given parameter to the end of this parameter list - * Can be a flag - * - * @param parameter the parameter - * @return this - */ - public ParameterList addParameter(Parameter<?, ?> parameter) { - return addParameter(-1, parameter); - } - - /** - * Add the given parameter to this parameter list - * If the parameter is a flag, the index is ignored - * - * @param index parameter index number, -1 if end - * @param parameter the parameter - * @return this - * @throws NullPointerException if parameter is null - */ - public ParameterList addParameter(int index, Parameter<?, ?> parameter) { - //System.out.println("Added parameter " + parameter.getName() + ", flag: " + parameter.isFlag()); - byName.put(parameter.getName(), parameter); - if (!parameter.isFlag()) { - indexedParameters.add(index == -1 ? indexedParameters.size() : index, parameter); - } - return this; - } - - public Parameter<?, ?> getRepeatedParameter() { - if (!repeatFinalParameter) { - return null; - } - if (finalParameterMayBeFlag) { - Iterator<Parameter<?, ?>> iterator = byName.values().iterator(); - Parameter<?, ?> result = null; - while (iterator.hasNext()) { - result = iterator.next(); - } - return result; - } - - if (indexedParameters.isEmpty()) { - return null; - } - - return indexedParameters.get(indexedParameters.size() - 1); - } - -} +package io.dico.dicore.command.parameter;
+
+import java.util.*;
+
+/**
+ * IParameter definition for a command
+ */
+@SuppressWarnings("UnusedReturnValue")
+public class ParameterList {
+ //private ParameterList parent;
+ private List<Parameter<?, ?>> indexedParameters;
+ private Map<String, Parameter<?, ?>> byName;
+ //private IArgumentPreProcessor argumentPreProcessor = IArgumentPreProcessor.NONE;
+ private int requiredCount = -1;
+ private boolean repeatFinalParameter;
+
+ // if the final parameter is repeated and the command is implemented through reflection,
+ // the repeated parameter is simply the last parameter of the method, rather than the last
+ // indexed parameter. This might be a flag. As such, this field exists to ensure the correct
+ // parameter is taken for repeating
+ private boolean finalParameterMayBeFlag;
+
+ /*
+ public ParameterList(ParameterList parent) {
+ this();
+ if (parent.repeatFinalParameter) {
+ throw new IllegalArgumentException("Parent may not have repeating parameters");
+ }
+ this.parent = parent;
+ }*/
+
+ public ParameterList() {
+ this.indexedParameters = new ArrayList<>();
+ this.byName = new LinkedHashMap<>();
+ this.repeatFinalParameter = false;
+ }
+
+ /*
+ public IArgumentPreProcessor getArgumentPreProcessor() {
+ return argumentPreProcessor;
+ }
+
+ public ParameterList setArgumentPreProcessor(IArgumentPreProcessor argumentPreProcessor) {
+ this.argumentPreProcessor = argumentPreProcessor == null ? IArgumentPreProcessor.NONE : argumentPreProcessor;
+ return this;
+ }*/
+
+ public boolean repeatFinalParameter() {
+ return repeatFinalParameter;
+ }
+
+ public ParameterList setRepeatFinalParameter(boolean repeatFinalParameter) {
+ this.repeatFinalParameter = repeatFinalParameter;
+ return this;
+ }
+
+ public boolean finalParameterMayBeFlag() {
+ return finalParameterMayBeFlag;
+ }
+
+ public ParameterList setFinalParameterMayBeFlag(boolean finalParameterMayBeFlag) {
+ this.finalParameterMayBeFlag = finalParameterMayBeFlag;
+ return this;
+ }
+
+ public int getRequiredCount() {
+ return requiredCount == -1 ? indexedParameters.size() : requiredCount;
+ }
+
+ public ParameterList setRequiredCount(int requiredCount) {
+ this.requiredCount = requiredCount;
+ return this;
+ }
+
+ public boolean hasAnyParameters() {
+ return !byName.isEmpty();
+ }
+
+ public int getIndexedParameterCount() {
+ return indexedParameters.size();
+ }
+
+ public List<Parameter<?, ?>> getIndexedParameters() {
+ return Collections.unmodifiableList(indexedParameters);
+ }
+
+ public Parameter<?, ?> getParameterByName(String name) {
+ return byName.get(name);
+ }
+
+ public String getIndexedParameterName(int index) {
+ return indexedParameters.get(index).getName();
+ }
+
+ public Map<String, Parameter<?, ?>> getParametersByName() {
+ return Collections.unmodifiableMap(byName);
+ }
+
+ /**
+ * Add the given parameter to the end of this parameter list
+ * Can be a flag
+ *
+ * @param parameter the parameter
+ * @return this
+ */
+ public ParameterList addParameter(Parameter<?, ?> parameter) {
+ return addParameter(-1, parameter);
+ }
+
+ /**
+ * Add the given parameter to this parameter list
+ * If the parameter is a flag, the index is ignored
+ *
+ * @param index parameter index number, -1 if end
+ * @param parameter the parameter
+ * @return this
+ * @throws NullPointerException if parameter is null
+ */
+ public ParameterList addParameter(int index, Parameter<?, ?> parameter) {
+ //System.out.println("Added parameter " + parameter.getName() + ", flag: " + parameter.isFlag());
+ byName.put(parameter.getName(), parameter);
+ if (!parameter.isFlag()) {
+ indexedParameters.add(index == -1 ? indexedParameters.size() : index, parameter);
+ }
+ return this;
+ }
+
+ public Parameter<?, ?> getRepeatedParameter() {
+ if (!repeatFinalParameter) {
+ return null;
+ }
+ if (finalParameterMayBeFlag) {
+ Iterator<Parameter<?, ?>> iterator = byName.values().iterator();
+ Parameter<?, ?> result = null;
+ while (iterator.hasNext()) {
+ result = iterator.next();
+ }
+ return result;
+ }
+
+ if (indexedParameters.isEmpty()) {
+ return null;
+ }
+
+ return indexedParameters.get(indexedParameters.size() - 1);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/EnumParameterType.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/EnumParameterType.java index c23e09b..063d381 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/EnumParameterType.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/EnumParameterType.java @@ -1,46 +1,46 @@ -package io.dico.dicore.command.parameter.type; - -import io.dico.dicore.command.CommandException; -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.parameter.Parameter; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; - -import java.util.ArrayList; -import java.util.List; - -public class EnumParameterType<E extends Enum> extends SimpleParameterType<E, Void> { - private final E[] universe; - - public EnumParameterType(Class<E> returnType) { - super(returnType); - universe = returnType.getEnumConstants(); - if (universe == null) { - throw new IllegalArgumentException("returnType must be an enum"); - } - } - - @Override - protected E parse(Parameter<E, Void> parameter, CommandSender sender, String input) throws CommandException { - for (E constant : universe) { - if (constant.name().equalsIgnoreCase(input)) { - return constant; - } - } - - throw CommandException.invalidArgument(parameter.getName(), "the enum value does not exist"); - } - - @Override - public List<String> complete(Parameter<E, Void> parameter, CommandSender sender, Location location, ArgumentBuffer buffer) { - String input = buffer.next().toUpperCase(); - List<String> result = new ArrayList<>(); - for (E constant : universe) { - if (constant.name().toUpperCase().startsWith(input.toUpperCase())) { - result.add(constant.name().toLowerCase()); - } - } - return result; - } - -} +package io.dico.dicore.command.parameter.type;
+
+import io.dico.dicore.command.CommandException;
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.parameter.Parameter;
+import org.bukkit.Location;
+import org.bukkit.command.CommandSender;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class EnumParameterType<E extends Enum> extends SimpleParameterType<E, Void> {
+ private final E[] universe;
+
+ public EnumParameterType(Class<E> returnType) {
+ super(returnType);
+ universe = returnType.getEnumConstants();
+ if (universe == null) {
+ throw new IllegalArgumentException("returnType must be an enum");
+ }
+ }
+
+ @Override
+ protected E parse(Parameter<E, Void> parameter, CommandSender sender, String input) throws CommandException {
+ for (E constant : universe) {
+ if (constant.name().equalsIgnoreCase(input)) {
+ return constant;
+ }
+ }
+
+ throw CommandException.invalidArgument(parameter.getName(), "the enum value does not exist");
+ }
+
+ @Override
+ public List<String> complete(Parameter<E, Void> parameter, CommandSender sender, Location location, ArgumentBuffer buffer) {
+ String input = buffer.next().toUpperCase();
+ List<String> result = new ArrayList<>();
+ for (E constant : universe) {
+ if (constant.name().toUpperCase().startsWith(input.toUpperCase())) {
+ result.add(constant.name().toLowerCase());
+ }
+ }
+ return result;
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/IParameterTypeSelector.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/IParameterTypeSelector.java index 780ea0d..381b15c 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/IParameterTypeSelector.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/IParameterTypeSelector.java @@ -1,44 +1,44 @@ -package io.dico.dicore.command.parameter.type; - -import java.lang.annotation.Annotation; - -/** - * An interface for an object that stores parameter types by {@link ParameterKey} and finds appropriate types for {@link ParameterKey parameterKeys} - */ -public interface IParameterTypeSelector { - - <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(ParameterKey key); - - //<TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExactOrSubclass(ParameterKey key); - - <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(ParameterKey key); - - - default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(Class<?> returnType) { - return selectExact(returnType, null); - } - - default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(Class<?> returnType, Class<? extends Annotation> annotationClass) { - return selectExact(new ParameterKey(returnType, annotationClass)); - } - - /* - default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExactOrSubclass(Class<?> returnType) { - return selectExactOrSubclass(returnType, null); - } - - default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExactOrSubclass(Class<?> returnType, Class<? extends Annotation> annotationClass) { - return selectExactOrSubclass(new ParameterKey(returnType, annotationClass)); - } - */ - default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(Class<?> returnType) { - return selectAny(returnType, null); - } - - default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(Class<?> returnType, Class<? extends Annotation> annotationClass) { - return selectAny(new ParameterKey(returnType, annotationClass)); - } - - void addType(boolean infolessAlias, ParameterType<?, ?> type); - -} +package io.dico.dicore.command.parameter.type;
+
+import java.lang.annotation.Annotation;
+
+/**
+ * An interface for an object that stores parameter types by {@link ParameterKey} and finds appropriate types for {@link ParameterKey parameterKeys}
+ */
+public interface IParameterTypeSelector {
+
+ <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(ParameterKey key);
+
+ //<TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExactOrSubclass(ParameterKey key);
+
+ <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(ParameterKey key);
+
+
+ default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(Class<?> returnType) {
+ return selectExact(returnType, null);
+ }
+
+ default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(Class<?> returnType, Class<? extends Annotation> annotationClass) {
+ return selectExact(new ParameterKey(returnType, annotationClass));
+ }
+
+ /*
+ default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExactOrSubclass(Class<?> returnType) {
+ return selectExactOrSubclass(returnType, null);
+ }
+
+ default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExactOrSubclass(Class<?> returnType, Class<? extends Annotation> annotationClass) {
+ return selectExactOrSubclass(new ParameterKey(returnType, annotationClass));
+ }
+ */
+ default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(Class<?> returnType) {
+ return selectAny(returnType, null);
+ }
+
+ default <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(Class<?> returnType, Class<? extends Annotation> annotationClass) {
+ return selectAny(new ParameterKey(returnType, annotationClass));
+ }
+
+ void addType(boolean infolessAlias, ParameterType<?, ?> type);
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java index d407f87..ef86eab 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/MapBasedParameterTypeSelector.java @@ -1,114 +1,114 @@ -package io.dico.dicore.command.parameter.type; - -import java.lang.annotation.Annotation; -import java.util.HashMap; -import java.util.Map; - -/** - * Map based implementation of {@link IParameterTypeSelector} - */ -public class MapBasedParameterTypeSelector implements IParameterTypeSelector { - static final MapBasedParameterTypeSelector defaultSelector = new MapBasedParameterTypeSelector(false); - private final Map<ParameterKey, ParameterType<?, ?>> parameterTypeMap; - private final boolean useDefault; - - public MapBasedParameterTypeSelector(boolean useDefault) { - this.parameterTypeMap = new HashMap<>(); - this.useDefault = useDefault; - } - - @Override - public <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(ParameterKey key) { - ParameterType<?, ?> out = parameterTypeMap.get(key); - if (useDefault && out == null) { - out = defaultSelector.selectExact(key); - } - if (out == null && key.getReturnType().isEnum()) { - //noinspection unchecked - out = new EnumParameterType(key.getReturnType()); - addType(false, out); - } - return cast(out); - } - - @Override - public <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(ParameterKey key) { - ParameterType<TReturn, TParamInfo> exact = selectExact(key); - if (exact != null) { - return exact; - } - - if (key.getAnnotationClass() != null) { - exact = selectExact(new ParameterKey(key.getReturnType())); - if (exact != null) { - return exact; - } - } - - Class<?> returnType = key.getReturnType(); - Class<? extends Annotation> annotationClass = key.getAnnotationClass(); - - ParameterType<?, ?> out = selectByReturnType(parameterTypeMap, returnType, annotationClass, false); - if (out == null && useDefault) { - out = selectByReturnType(defaultSelector.parameterTypeMap, returnType, annotationClass, false); - } - if (out == null) { - out = selectByReturnType(parameterTypeMap, returnType, annotationClass, true); - } - if (out == null && useDefault) { - out = selectByReturnType(defaultSelector.parameterTypeMap, returnType, annotationClass, true); - } - return cast(out); - } - - private static ParameterType<?, ?> selectByReturnType(Map<ParameterKey, ParameterType<?, ?>> map, Class<?> returnType, - Class<? extends Annotation> annotationClass, boolean allowSubclass) { - ParameterType<?, ?> out = null; - if (allowSubclass) { - for (ParameterType<?, ?> type : map.values()) { - if (returnType.isAssignableFrom(type.getReturnType())) { - if (annotationClass == type.getAnnotationClass()) { - out = type; - break; - } - if (out == null) { - out = type; - } - } - } - } else { - for (ParameterType<?, ?> type : map.values()) { - if (returnType == type.getReturnType()) { - if (annotationClass == type.getAnnotationClass()) { - out = type; - break; - } - if (out == null) { - out = type; - } - } - } - } - return out; - } - - private static <T> T cast(Object o) { - //noinspection unchecked - return (T) o; - } - - @Override - public void addType(boolean infolessAlias, ParameterType<?, ?> type) { - parameterTypeMap.put(type.getTypeKey(), type); - - if (infolessAlias) { - parameterTypeMap.putIfAbsent(type.getInfolessTypeKey(), type); - } - } - - static { - // registers default parameter types - ParameterTypes.clinit(); - } - -} +package io.dico.dicore.command.parameter.type;
+
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Map based implementation of {@link IParameterTypeSelector}
+ */
+public class MapBasedParameterTypeSelector implements IParameterTypeSelector {
+ static final MapBasedParameterTypeSelector defaultSelector = new MapBasedParameterTypeSelector(false);
+ private final Map<ParameterKey, ParameterType<?, ?>> parameterTypeMap;
+ private final boolean useDefault;
+
+ public MapBasedParameterTypeSelector(boolean useDefault) {
+ this.parameterTypeMap = new HashMap<>();
+ this.useDefault = useDefault;
+ }
+
+ @Override
+ public <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectExact(ParameterKey key) {
+ ParameterType<?, ?> out = parameterTypeMap.get(key);
+ if (useDefault && out == null) {
+ out = defaultSelector.selectExact(key);
+ }
+ if (out == null && key.getReturnType().isEnum()) {
+ //noinspection unchecked
+ out = new EnumParameterType(key.getReturnType());
+ addType(false, out);
+ }
+ return cast(out);
+ }
+
+ @Override
+ public <TReturn, TParamInfo> ParameterType<TReturn, TParamInfo> selectAny(ParameterKey key) {
+ ParameterType<TReturn, TParamInfo> exact = selectExact(key);
+ if (exact != null) {
+ return exact;
+ }
+
+ if (key.getAnnotationClass() != null) {
+ exact = selectExact(new ParameterKey(key.getReturnType()));
+ if (exact != null) {
+ return exact;
+ }
+ }
+
+ Class<?> returnType = key.getReturnType();
+ Class<? extends Annotation> annotationClass = key.getAnnotationClass();
+
+ ParameterType<?, ?> out = selectByReturnType(parameterTypeMap, returnType, annotationClass, false);
+ if (out == null && useDefault) {
+ out = selectByReturnType(defaultSelector.parameterTypeMap, returnType, annotationClass, false);
+ }
+ if (out == null) {
+ out = selectByReturnType(parameterTypeMap, returnType, annotationClass, true);
+ }
+ if (out == null && useDefault) {
+ out = selectByReturnType(defaultSelector.parameterTypeMap, returnType, annotationClass, true);
+ }
+ return cast(out);
+ }
+
+ private static ParameterType<?, ?> selectByReturnType(Map<ParameterKey, ParameterType<?, ?>> map, Class<?> returnType,
+ Class<? extends Annotation> annotationClass, boolean allowSubclass) {
+ ParameterType<?, ?> out = null;
+ if (allowSubclass) {
+ for (ParameterType<?, ?> type : map.values()) {
+ if (returnType.isAssignableFrom(type.getReturnType())) {
+ if (annotationClass == type.getAnnotationClass()) {
+ out = type;
+ break;
+ }
+ if (out == null) {
+ out = type;
+ }
+ }
+ }
+ } else {
+ for (ParameterType<?, ?> type : map.values()) {
+ if (returnType == type.getReturnType()) {
+ if (annotationClass == type.getAnnotationClass()) {
+ out = type;
+ break;
+ }
+ if (out == null) {
+ out = type;
+ }
+ }
+ }
+ }
+ return out;
+ }
+
+ private static <T> T cast(Object o) {
+ //noinspection unchecked
+ return (T) o;
+ }
+
+ @Override
+ public void addType(boolean infolessAlias, ParameterType<?, ?> type) {
+ parameterTypeMap.put(type.getTypeKey(), type);
+
+ if (infolessAlias) {
+ parameterTypeMap.putIfAbsent(type.getInfolessTypeKey(), type);
+ }
+ }
+
+ static {
+ // registers default parameter types
+ ParameterTypes.clinit();
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/ParameterConfig.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/ParameterConfig.java index dbd7590..d33932b 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/ParameterConfig.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/ParameterConfig.java @@ -1,80 +1,80 @@ -package io.dico.dicore.command.parameter.type; - -import io.dico.dicore.Reflection; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Constructor; - -/** - * This class serves the purpose of having annotated parameter configurations (such as ranges for number parameters). - * Such configurations must be possible to obtain without using annotations, and as such, there should be a class conveying the information - * that is separate from the annotation itself. This class acts as a bridge from the annotation to said class conveying the information. - * - * @param <TAnnotation> the annotation type for parameters - * @param <TParamInfo> the object type that holds the information required in memory - */ -public abstract class ParameterConfig<TAnnotation extends Annotation, TParamInfo> implements Comparable<ParameterConfig<?, ?>> { - private final Class<TAnnotation> annotationClass; - // protected final TParamInfo defaultValue; - - public ParameterConfig(Class<TAnnotation> annotationClass/*, TParamInfo defaultValue*/) { - this.annotationClass = annotationClass; - //this.defaultValue = defaultValue; - } - - public final Class<TAnnotation> getAnnotationClass() { - return annotationClass; - } - /* - public TParamInfo getDefaultValue() { - return defaultValue; - }*/ - - protected abstract TParamInfo toParameterInfo(TAnnotation annotation); - - public TParamInfo getParameterInfo(Annotation annotation) { - //noinspection unchecked - return toParameterInfo((TAnnotation) annotation); - } - - public static <TAnnotation extends Annotation, TParamInfo> ParameterConfig<TAnnotation, TParamInfo> - includeMemoryClass(Class<TAnnotation> annotationClass, Class<TParamInfo> memoryClass) { - Constructor<TParamInfo> constructor; - //TParamInfo defaultValue; - try { - constructor = memoryClass.getConstructor(annotationClass); - //defaultValue = Reflection.getStaticFieldValue(annotationClass, "DEFAULT"); - } catch (NoSuchMethodException | IllegalArgumentException ex) { - throw new IllegalArgumentException(ex); - } - /* - if (defaultValue == null) try { - defaultValue = memoryClass.newInstance(); - } catch (IllegalAccessException | InstantiationException ex) { - throw new IllegalArgumentException("Failed to get a default value for the param info", ex); - }*/ - - return new ParameterConfig<TAnnotation, TParamInfo>(annotationClass/*, defaultValue*/) { - - @Override - public TParamInfo toParameterInfo(TAnnotation annotation) { - try { - return constructor.newInstance(annotation); - } catch (Exception ex) { - throw new RuntimeException(ex); - } - } - }; - } - - public static <TAnnotation extends Annotation, TParamInfo> ParameterConfig<TAnnotation, TParamInfo> getMemoryClassFromField(Class<TAnnotation> annotationClass) { - return ParameterConfig.includeMemoryClass(annotationClass, Reflection.getStaticFieldValue(annotationClass, "MEMORY_CLASS")); - } - - @Override - public int compareTo(ParameterConfig<?, ?> o) { - return 0; - } - -} - +package io.dico.dicore.command.parameter.type;
+
+import io.dico.dicore.Reflection;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+
+/**
+ * This class serves the purpose of having annotated parameter configurations (such as ranges for number parameters).
+ * Such configurations must be possible to obtain without using annotations, and as such, there should be a class conveying the information
+ * that is separate from the annotation itself. This class acts as a bridge from the annotation to said class conveying the information.
+ *
+ * @param <TAnnotation> the annotation type for parameters
+ * @param <TParamInfo> the object type that holds the information required in memory
+ */
+public abstract class ParameterConfig<TAnnotation extends Annotation, TParamInfo> implements Comparable<ParameterConfig<?, ?>> {
+ private final Class<TAnnotation> annotationClass;
+ // protected final TParamInfo defaultValue;
+
+ public ParameterConfig(Class<TAnnotation> annotationClass/*, TParamInfo defaultValue*/) {
+ this.annotationClass = annotationClass;
+ //this.defaultValue = defaultValue;
+ }
+
+ public final Class<TAnnotation> getAnnotationClass() {
+ return annotationClass;
+ }
+ /*
+ public TParamInfo getDefaultValue() {
+ return defaultValue;
+ }*/
+
+ protected abstract TParamInfo toParameterInfo(TAnnotation annotation);
+
+ public TParamInfo getParameterInfo(Annotation annotation) {
+ //noinspection unchecked
+ return toParameterInfo((TAnnotation) annotation);
+ }
+
+ public static <TAnnotation extends Annotation, TParamInfo> ParameterConfig<TAnnotation, TParamInfo>
+ includeMemoryClass(Class<TAnnotation> annotationClass, Class<TParamInfo> memoryClass) {
+ Constructor<TParamInfo> constructor;
+ //TParamInfo defaultValue;
+ try {
+ constructor = memoryClass.getConstructor(annotationClass);
+ //defaultValue = Reflection.getStaticFieldValue(annotationClass, "DEFAULT");
+ } catch (NoSuchMethodException | IllegalArgumentException ex) {
+ throw new IllegalArgumentException(ex);
+ }
+ /*
+ if (defaultValue == null) try {
+ defaultValue = memoryClass.newInstance();
+ } catch (IllegalAccessException | InstantiationException ex) {
+ throw new IllegalArgumentException("Failed to get a default value for the param info", ex);
+ }*/
+
+ return new ParameterConfig<TAnnotation, TParamInfo>(annotationClass/*, defaultValue*/) {
+
+ @Override
+ public TParamInfo toParameterInfo(TAnnotation annotation) {
+ try {
+ return constructor.newInstance(annotation);
+ } catch (Exception ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ };
+ }
+
+ public static <TAnnotation extends Annotation, TParamInfo> ParameterConfig<TAnnotation, TParamInfo> getMemoryClassFromField(Class<TAnnotation> annotationClass) {
+ return ParameterConfig.includeMemoryClass(annotationClass, Reflection.getStaticFieldValue(annotationClass, "MEMORY_CLASS"));
+ }
+
+ @Override
+ public int compareTo(ParameterConfig<?, ?> o) {
+ return 0;
+ }
+
+}
+
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/ParameterType.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/ParameterType.java index e1a62fa..e9cca7f 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/ParameterType.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/ParameterType.java @@ -1,149 +1,149 @@ -package io.dico.dicore.command.parameter.type; - -import io.dico.dicore.Reflection; -import io.dico.dicore.command.CommandException; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.annotation.Range; -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.parameter.Parameter; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Objects; - -/** - * A parameter type. - * Takes care of parsing, default values as well as completions. - * - * @param <TReturn> type of the parameter - * @param <TParamInfo> the info object type for the parameter (Example: {@link Range.Memory} - */ -public abstract class ParameterType<TReturn, TParamInfo> { - private final Class<TReturn> returnType; - private final ParameterConfig<?, TParamInfo> parameterConfig; - protected final ParameterType<TReturn, TParamInfo> otherType; // flag or non-flag, depending on current - - public ParameterType(Class<TReturn> returnType) { - this(returnType, null); - } - - public ParameterType(Class<TReturn> returnType, ParameterConfig<?, TParamInfo> paramConfig) { - this.returnType = Objects.requireNonNull(returnType); - this.parameterConfig = paramConfig; - - ParameterType<TReturn, TParamInfo> otherType = flagTypeParameter(); - this.otherType = otherType == null ? this : otherType; - } - - protected ParameterType(Class<TReturn> returnType, ParameterConfig<?, TParamInfo> parameterConfig, ParameterType<TReturn, TParamInfo> otherType) { - this.returnType = returnType; - this.parameterConfig = parameterConfig; - this.otherType = otherType; - } - - public int getExpectedAmountOfConsumedArguments() { - return 1; - } - - public boolean canBeFlag() { - return this == otherType; - } - - public boolean isFlagExplicitly() { - return this instanceof FlagParameterType; - } - - /** - * @return The return type - */ - public final Class<TReturn> getReturnType() { - return returnType; - } - - public final Class<?> getAnnotationClass() { - return parameterConfig == null ? null : parameterConfig.getAnnotationClass(); - } - - public final ParameterConfig<?, TParamInfo> getParameterConfig() { - return parameterConfig; - } - - public ParameterKey getTypeKey() { - return new ParameterKey(returnType, parameterConfig != null ? parameterConfig.getAnnotationClass() : null); - } - - public ParameterKey getInfolessTypeKey() { - return new ParameterKey(returnType, null); - } - - protected FlagParameterType<TReturn, TParamInfo> flagTypeParameter() { - return null; - } - - public ParameterType<TReturn, TParamInfo> asFlagParameter() { - return canBeFlag() ? this : otherType; - } - - public ParameterType<TReturn, TParamInfo> asNormalParameter() { - return isFlagExplicitly() ? otherType : this; - } - - public abstract TReturn parse(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException; - - public TReturn parseForContext(Parameter<TReturn, TParamInfo> parameter, ExecutionContext context, ArgumentBuffer buffer) throws CommandException { - return parse(parameter, context.getSender(), buffer); - } - - public TReturn getDefaultValue(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException { - return null; - } - - public TReturn getDefaultValueForContext(Parameter<TReturn, TParamInfo> parameter, ExecutionContext context, ArgumentBuffer buffer) throws CommandException { - return getDefaultValue(parameter, context.getSender(), buffer); - } - - public List<String> complete(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, Location location, ArgumentBuffer buffer) { - return Collections.emptyList(); - } - - public List<String> completeForContext(Parameter<TReturn, TParamInfo> parameter, ExecutionContext context, Location location, ArgumentBuffer buffer) { - return complete(parameter, context.getSender(), location, buffer); - } - - protected static abstract class FlagParameterType<TResult, TParamInfo> extends ParameterType<TResult, TParamInfo> { - - protected FlagParameterType(ParameterType<TResult, TParamInfo> otherType) { - super(otherType.returnType, otherType.parameterConfig, otherType); - } - - @Override - public int getExpectedAmountOfConsumedArguments() { - return otherType.getExpectedAmountOfConsumedArguments(); - } - - @Override - public boolean canBeFlag() { - return true; - } - - @Override - protected final FlagParameterType<TResult, TParamInfo> flagTypeParameter() { - return this; - } - - @Override - public ParameterType<TResult, TParamInfo> asFlagParameter() { - return this; - } - - @Override - public ParameterType<TResult, TParamInfo> asNormalParameter() { - return otherType; - } - - } - -} +package io.dico.dicore.command.parameter.type;
+
+import io.dico.dicore.Reflection;
+import io.dico.dicore.command.CommandException;
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.annotation.Range;
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.parameter.Parameter;
+import org.bukkit.Location;
+import org.bukkit.command.CommandSender;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * A parameter type.
+ * Takes care of parsing, default values as well as completions.
+ *
+ * @param <TReturn> type of the parameter
+ * @param <TParamInfo> the info object type for the parameter (Example: {@link Range.Memory}
+ */
+public abstract class ParameterType<TReturn, TParamInfo> {
+ private final Class<TReturn> returnType;
+ private final ParameterConfig<?, TParamInfo> parameterConfig;
+ protected final ParameterType<TReturn, TParamInfo> otherType; // flag or non-flag, depending on current
+
+ public ParameterType(Class<TReturn> returnType) {
+ this(returnType, null);
+ }
+
+ public ParameterType(Class<TReturn> returnType, ParameterConfig<?, TParamInfo> paramConfig) {
+ this.returnType = Objects.requireNonNull(returnType);
+ this.parameterConfig = paramConfig;
+
+ ParameterType<TReturn, TParamInfo> otherType = flagTypeParameter();
+ this.otherType = otherType == null ? this : otherType;
+ }
+
+ protected ParameterType(Class<TReturn> returnType, ParameterConfig<?, TParamInfo> parameterConfig, ParameterType<TReturn, TParamInfo> otherType) {
+ this.returnType = returnType;
+ this.parameterConfig = parameterConfig;
+ this.otherType = otherType;
+ }
+
+ public int getExpectedAmountOfConsumedArguments() {
+ return 1;
+ }
+
+ public boolean canBeFlag() {
+ return this == otherType;
+ }
+
+ public boolean isFlagExplicitly() {
+ return this instanceof FlagParameterType;
+ }
+
+ /**
+ * @return The return type
+ */
+ public final Class<TReturn> getReturnType() {
+ return returnType;
+ }
+
+ public final Class<?> getAnnotationClass() {
+ return parameterConfig == null ? null : parameterConfig.getAnnotationClass();
+ }
+
+ public final ParameterConfig<?, TParamInfo> getParameterConfig() {
+ return parameterConfig;
+ }
+
+ public ParameterKey getTypeKey() {
+ return new ParameterKey(returnType, parameterConfig != null ? parameterConfig.getAnnotationClass() : null);
+ }
+
+ public ParameterKey getInfolessTypeKey() {
+ return new ParameterKey(returnType, null);
+ }
+
+ protected FlagParameterType<TReturn, TParamInfo> flagTypeParameter() {
+ return null;
+ }
+
+ public ParameterType<TReturn, TParamInfo> asFlagParameter() {
+ return canBeFlag() ? this : otherType;
+ }
+
+ public ParameterType<TReturn, TParamInfo> asNormalParameter() {
+ return isFlagExplicitly() ? otherType : this;
+ }
+
+ public abstract TReturn parse(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException;
+
+ public TReturn parseForContext(Parameter<TReturn, TParamInfo> parameter, ExecutionContext context, ArgumentBuffer buffer) throws CommandException {
+ return parse(parameter, context.getSender(), buffer);
+ }
+
+ public TReturn getDefaultValue(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException {
+ return null;
+ }
+
+ public TReturn getDefaultValueForContext(Parameter<TReturn, TParamInfo> parameter, ExecutionContext context, ArgumentBuffer buffer) throws CommandException {
+ return getDefaultValue(parameter, context.getSender(), buffer);
+ }
+
+ public List<String> complete(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, Location location, ArgumentBuffer buffer) {
+ return Collections.emptyList();
+ }
+
+ public List<String> completeForContext(Parameter<TReturn, TParamInfo> parameter, ExecutionContext context, Location location, ArgumentBuffer buffer) {
+ return complete(parameter, context.getSender(), location, buffer);
+ }
+
+ protected static abstract class FlagParameterType<TResult, TParamInfo> extends ParameterType<TResult, TParamInfo> {
+
+ protected FlagParameterType(ParameterType<TResult, TParamInfo> otherType) {
+ super(otherType.returnType, otherType.parameterConfig, otherType);
+ }
+
+ @Override
+ public int getExpectedAmountOfConsumedArguments() {
+ return otherType.getExpectedAmountOfConsumedArguments();
+ }
+
+ @Override
+ public boolean canBeFlag() {
+ return true;
+ }
+
+ @Override
+ protected final FlagParameterType<TResult, TParamInfo> flagTypeParameter() {
+ return this;
+ }
+
+ @Override
+ public ParameterType<TResult, TParamInfo> asFlagParameter() {
+ return this;
+ }
+
+ @Override
+ public ParameterType<TResult, TParamInfo> asNormalParameter() {
+ return otherType;
+ }
+
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/SimpleParameterType.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/SimpleParameterType.java index ecf19ce..667b2c7 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/SimpleParameterType.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/type/SimpleParameterType.java @@ -1,31 +1,31 @@ -package io.dico.dicore.command.parameter.type; - -import io.dico.dicore.command.CommandException; -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.parameter.Parameter; -import org.bukkit.command.CommandSender; - -/** - * An abstraction for parameter types that only parse a single argument - * - * @param <TReturn> the parameter type - * @param <TParamInfo> parameter info object type - */ -public abstract class SimpleParameterType<TReturn, TParamInfo> extends ParameterType<TReturn, TParamInfo> { - - public SimpleParameterType(Class<TReturn> returnType) { - super(returnType); - } - - public SimpleParameterType(Class<TReturn> returnType, ParameterConfig<?, TParamInfo> paramConfig) { - super(returnType, paramConfig); - } - - protected abstract TReturn parse(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, String input) throws CommandException; - - @Override - public TReturn parse(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException { - return parse(parameter, sender, buffer.requireNext(parameter.getName())); - } - -} +package io.dico.dicore.command.parameter.type;
+
+import io.dico.dicore.command.CommandException;
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.parameter.Parameter;
+import org.bukkit.command.CommandSender;
+
+/**
+ * An abstraction for parameter types that only parse a single argument
+ *
+ * @param <TReturn> the parameter type
+ * @param <TParamInfo> parameter info object type
+ */
+public abstract class SimpleParameterType<TReturn, TParamInfo> extends ParameterType<TReturn, TParamInfo> {
+
+ public SimpleParameterType(Class<TReturn> returnType) {
+ super(returnType);
+ }
+
+ public SimpleParameterType(Class<TReturn> returnType, ParameterConfig<?, TParamInfo> paramConfig) {
+ super(returnType, paramConfig);
+ }
+
+ protected abstract TReturn parse(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, String input) throws CommandException;
+
+ @Override
+ public TReturn parse(Parameter<TReturn, TParamInfo> parameter, CommandSender sender, ArgumentBuffer buffer) throws CommandException {
+ return parse(parameter, sender, buffer.requireNext(parameter.getName()));
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/predef/DefaultGroupCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/predef/DefaultGroupCommand.java index e664cef..6828fcc 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/predef/DefaultGroupCommand.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/predef/DefaultGroupCommand.java @@ -1,56 +1,56 @@ -package io.dico.dicore.command.predef; - -import io.dico.dicore.command.CommandException; -import io.dico.dicore.command.ExecutionContext; -import io.dico.dicore.command.IContextFilter; -import org.bukkit.command.CommandSender; - -public class DefaultGroupCommand extends PredefinedCommand<DefaultGroupCommand> { - private static final DefaultGroupCommand instance; - private static final IContextFilter noArgumentFilter; - - public static DefaultGroupCommand getInstance() { - return instance; - } - - private DefaultGroupCommand(boolean modifiable) { - addContextFilter(IContextFilter.INHERIT_PERMISSIONS); - addContextFilter(noArgumentFilter); - this.modifiable = modifiable; - } - - public DefaultGroupCommand() { - this(true); - } - - @Override - protected DefaultGroupCommand newModifiableInstance() { - return new DefaultGroupCommand(true); - } - - @Override - public String execute(CommandSender sender, ExecutionContext context) throws CommandException { - context.getAddress().getChatHandler().sendHelpMessage(sender, context, context.getAddress(), 1); - return null; - } - - static { - noArgumentFilter = new IContextFilter() { - @Override - public void filterContext(ExecutionContext context) throws CommandException { - if (context.getBuffer().hasNext()) { - throw new CommandException("No such command: /" + context.getAddress().getAddress() - + " " + context.getBuffer().next()); - } - } - - @Override - public Priority getPriority() { - return Priority.EARLY; - } - }; - - instance = new DefaultGroupCommand(false); - } - -} +package io.dico.dicore.command.predef;
+
+import io.dico.dicore.command.CommandException;
+import io.dico.dicore.command.ExecutionContext;
+import io.dico.dicore.command.IContextFilter;
+import org.bukkit.command.CommandSender;
+
+public class DefaultGroupCommand extends PredefinedCommand<DefaultGroupCommand> {
+ private static final DefaultGroupCommand instance;
+ private static final IContextFilter noArgumentFilter;
+
+ public static DefaultGroupCommand getInstance() {
+ return instance;
+ }
+
+ private DefaultGroupCommand(boolean modifiable) {
+ addContextFilter(IContextFilter.INHERIT_PERMISSIONS);
+ addContextFilter(noArgumentFilter);
+ this.modifiable = modifiable;
+ }
+
+ public DefaultGroupCommand() {
+ this(true);
+ }
+
+ @Override
+ protected DefaultGroupCommand newModifiableInstance() {
+ return new DefaultGroupCommand(true);
+ }
+
+ @Override
+ public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
+ context.getAddress().getChatHandler().sendHelpMessage(sender, context, context.getAddress(), 1);
+ return null;
+ }
+
+ static {
+ noArgumentFilter = new IContextFilter() {
+ @Override
+ public void filterContext(ExecutionContext context) throws CommandException {
+ if (context.getBuffer().hasNext()) {
+ throw new CommandException("No such command: /" + context.getAddress().getAddress()
+ + " " + context.getBuffer().next());
+ }
+ }
+
+ @Override
+ public Priority getPriority() {
+ return Priority.EARLY;
+ }
+ };
+
+ instance = new DefaultGroupCommand(false);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java index def0db1..985e055 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/predef/HelpCommand.java @@ -1,76 +1,76 @@ -package io.dico.dicore.command.predef; - -import io.dico.dicore.command.*; -import io.dico.dicore.command.annotation.Range; -import io.dico.dicore.command.parameter.ArgumentBuffer; -import io.dico.dicore.command.parameter.Parameter; -import io.dico.dicore.command.parameter.type.NumberParameterType; -import org.bukkit.command.CommandSender; - -/** - * The help command - */ -public class HelpCommand extends PredefinedCommand<HelpCommand> { - private static final Parameter<Integer, Range.Memory> pageParameter; - public static final HelpCommand INSTANCE; - - private HelpCommand(boolean modifiable) { - super(modifiable); - getParameterList().addParameter(pageParameter); - getParameterList().setRequiredCount(0); - setDescription("Shows this help page"); - } - - @Override - protected HelpCommand newModifiableInstance() { - return new HelpCommand(true); - } - - @Override - public String execute(CommandSender sender, ExecutionContext context) throws CommandException { - ICommandAddress target = context.getAddress(); - if (context.getAddress().getCommand() == this) { - target = target.getParent(); - } - - context.getAddress().getChatHandler().sendHelpMessage(sender, context, target, context.<Integer>get("page") - 1); - return null; - } - - public static void registerAsChild(ICommandAddress address) { - registerAsChild(address, "help"); - } - - public static void registerAsChild(ICommandAddress address, String main, String... aliases) { - ((ModifiableCommandAddress) address).addChild(new ChildCommandAddress(INSTANCE, main, aliases)); - } - - static { - pageParameter = new Parameter<>("page", "the page number", - new NumberParameterType<Integer>(Integer.TYPE) { - @Override - protected Integer parse(String input) throws NumberFormatException { - return Integer.parseInt(input); - } - - @Override - protected Integer select(Number number) { - return number.intValue(); - } - - @Override - public Integer parseForContext(Parameter<Integer, Range.Memory> parameter, ExecutionContext context, ArgumentBuffer buffer) throws CommandException { - if (context.getAddress().getCommand() == null || context.getAddress().getCommand().getClass() != HelpCommand.class) { - // An address was executed with its help command as target - buffer.next(); - return 1; - } - return parse(parameter, context.getSender(), buffer); - } - }, - new Range.Memory(1, Integer.MAX_VALUE, 1)); - - INSTANCE = new HelpCommand(false); - } - -} +package io.dico.dicore.command.predef;
+
+import io.dico.dicore.command.*;
+import io.dico.dicore.command.annotation.Range;
+import io.dico.dicore.command.parameter.ArgumentBuffer;
+import io.dico.dicore.command.parameter.Parameter;
+import io.dico.dicore.command.parameter.type.NumberParameterType;
+import org.bukkit.command.CommandSender;
+
+/**
+ * The help command
+ */
+public class HelpCommand extends PredefinedCommand<HelpCommand> {
+ private static final Parameter<Integer, Range.Memory> pageParameter;
+ public static final HelpCommand INSTANCE;
+
+ private HelpCommand(boolean modifiable) {
+ super(modifiable);
+ getParameterList().addParameter(pageParameter);
+ getParameterList().setRequiredCount(0);
+ setDescription("Shows this help page");
+ }
+
+ @Override
+ protected HelpCommand newModifiableInstance() {
+ return new HelpCommand(true);
+ }
+
+ @Override
+ public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
+ ICommandAddress target = context.getAddress();
+ if (context.getAddress().getCommand() == this) {
+ target = target.getParent();
+ }
+
+ context.getAddress().getChatHandler().sendHelpMessage(sender, context, target, context.<Integer>get("page") - 1);
+ return null;
+ }
+
+ public static void registerAsChild(ICommandAddress address) {
+ registerAsChild(address, "help");
+ }
+
+ public static void registerAsChild(ICommandAddress address, String main, String... aliases) {
+ ((ModifiableCommandAddress) address).addChild(new ChildCommandAddress(INSTANCE, main, aliases));
+ }
+
+ static {
+ pageParameter = new Parameter<>("page", "the page number",
+ new NumberParameterType<Integer>(Integer.TYPE) {
+ @Override
+ protected Integer parse(String input) throws NumberFormatException {
+ return Integer.parseInt(input);
+ }
+
+ @Override
+ protected Integer select(Number number) {
+ return number.intValue();
+ }
+
+ @Override
+ public Integer parseForContext(Parameter<Integer, Range.Memory> parameter, ExecutionContext context, ArgumentBuffer buffer) throws CommandException {
+ if (context.getAddress().getCommand() == null || context.getAddress().getCommand().getClass() != HelpCommand.class) {
+ // An address was executed with its help command as target
+ buffer.next();
+ return 1;
+ }
+ return parse(parameter, context.getSender(), buffer);
+ }
+ },
+ new Range.Memory(1, Integer.MAX_VALUE, 1));
+
+ INSTANCE = new HelpCommand(false);
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java index 4340356..b7c5587 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java @@ -1,50 +1,50 @@ -package io.dico.dicore.command.predef; - -import io.dico.dicore.command.CommandBuilder; -import io.dico.dicore.command.ExtendedCommand; -import io.dico.dicore.command.ICommandAddress; - -import java.util.HashMap; -import java.util.Map; -import java.util.function.Consumer; - -/** - * Marker class for commands that are generated. These commands can be replaced using methods in {@link CommandBuilder} - */ -public abstract class PredefinedCommand<T extends PredefinedCommand<T>> extends ExtendedCommand<T> { - static final Map<String, Consumer<ICommandAddress>> predefinedCommandGenerators = new HashMap<>(); - - /** - * Get a predefined command - * - * @param name the name - * @return the subscriber - */ - public static Consumer<ICommandAddress> getPredefinedCommandGenerator(String name) { - return predefinedCommandGenerators.get(name); - } - - /** - * Register a predefined command - * - * @param name the name - * @param consumer the generator which adds the child to the address - * @return true if and only if the subscriber was registered (false if the name exists) - */ - public static boolean registerPredefinedCommandGenerator(String name, Consumer<ICommandAddress> consumer) { - return predefinedCommandGenerators.putIfAbsent(name, consumer) == null; - } - - static { - registerPredefinedCommandGenerator("help", HelpCommand::registerAsChild); - //noinspection StaticInitializerReferencesSubClass - registerPredefinedCommandGenerator("syntax", SyntaxCommand::registerAsChild); - } - - public PredefinedCommand() { - } - - public PredefinedCommand(boolean modifiable) { - super(modifiable); - } -} +package io.dico.dicore.command.predef;
+
+import io.dico.dicore.command.CommandBuilder;
+import io.dico.dicore.command.ExtendedCommand;
+import io.dico.dicore.command.ICommandAddress;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Consumer;
+
+/**
+ * Marker class for commands that are generated. These commands can be replaced using methods in {@link CommandBuilder}
+ */
+public abstract class PredefinedCommand<T extends PredefinedCommand<T>> extends ExtendedCommand<T> {
+ static final Map<String, Consumer<ICommandAddress>> predefinedCommandGenerators = new HashMap<>();
+
+ /**
+ * Get a predefined command
+ *
+ * @param name the name
+ * @return the subscriber
+ */
+ public static Consumer<ICommandAddress> getPredefinedCommandGenerator(String name) {
+ return predefinedCommandGenerators.get(name);
+ }
+
+ /**
+ * Register a predefined command
+ *
+ * @param name the name
+ * @param consumer the generator which adds the child to the address
+ * @return true if and only if the subscriber was registered (false if the name exists)
+ */
+ public static boolean registerPredefinedCommandGenerator(String name, Consumer<ICommandAddress> consumer) {
+ return predefinedCommandGenerators.putIfAbsent(name, consumer) == null;
+ }
+
+ static {
+ registerPredefinedCommandGenerator("help", HelpCommand::registerAsChild);
+ //noinspection StaticInitializerReferencesSubClass
+ registerPredefinedCommandGenerator("syntax", SyntaxCommand::registerAsChild);
+ }
+
+ public PredefinedCommand() {
+ }
+
+ public PredefinedCommand(boolean modifiable) {
+ super(modifiable);
+ }
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/predef/SyntaxCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/predef/SyntaxCommand.java index 7ae8638..8532584 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/predef/SyntaxCommand.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/predef/SyntaxCommand.java @@ -1,36 +1,36 @@ -package io.dico.dicore.command.predef; - -import io.dico.dicore.command.*; -import org.bukkit.command.CommandSender; - -/** - * The syntax command - */ -public class SyntaxCommand extends PredefinedCommand<SyntaxCommand> { - public static final SyntaxCommand INSTANCE = new SyntaxCommand(false); - - private SyntaxCommand(boolean modifiable) { - super(modifiable); - setDescription("Describes how to use the command"); - } - - @Override - protected SyntaxCommand newModifiableInstance() { - return new SyntaxCommand(true); - } - - @Override - public String execute(CommandSender sender, ExecutionContext context) throws CommandException { - context.getAddress().getChatHandler().sendSyntaxMessage(sender, context, context.getAddress().getParent()); - return null; - } - - public static void registerAsChild(ICommandAddress address) { - registerAsChild(address, "syntax"); - } - - public static void registerAsChild(ICommandAddress address, String main, String... aliases) { - ((ModifiableCommandAddress) address).addChild(new ChildCommandAddress(INSTANCE, main, aliases)); - } - -} +package io.dico.dicore.command.predef;
+
+import io.dico.dicore.command.*;
+import org.bukkit.command.CommandSender;
+
+/**
+ * The syntax command
+ */
+public class SyntaxCommand extends PredefinedCommand<SyntaxCommand> {
+ public static final SyntaxCommand INSTANCE = new SyntaxCommand(false);
+
+ private SyntaxCommand(boolean modifiable) {
+ super(modifiable);
+ setDescription("Describes how to use the command");
+ }
+
+ @Override
+ protected SyntaxCommand newModifiableInstance() {
+ return new SyntaxCommand(true);
+ }
+
+ @Override
+ public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
+ context.getAddress().getChatHandler().sendSyntaxMessage(sender, context, context.getAddress().getParent());
+ return null;
+ }
+
+ public static void registerAsChild(ICommandAddress address) {
+ registerAsChild(address, "syntax");
+ }
+
+ public static void registerAsChild(ICommandAddress address, String main, String... aliases) {
+ ((ModifiableCommandAddress) address).addChild(new ChildCommandAddress(INSTANCE, main, aliases));
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java index b5346d0..40de6ae 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java @@ -1,122 +1,122 @@ -package io.dico.dicore.command.registration; - -import io.dico.dicore.command.ICommandAddress; -import io.dico.dicore.command.ICommandDispatcher; -import org.bukkit.Location; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.List; - -/** - * This class extends the bukkit's command class. - * Instances are injected into the command map. - */ -public class BukkitCommand extends Command { - private ICommandDispatcher dispatcher; - private ICommandAddress origin; - - public BukkitCommand(ICommandAddress address) { - super(validateTree(address).getNames().get(0), "", "", address.getNames().subList(1, address.getNames().size())); - this.dispatcher = address.getDispatcherForTree(); - this.origin = address; - - setTimingsIfNecessary(this); - } - - private static ICommandAddress validateTree(ICommandAddress tree) { - if (!tree.hasParent()) { - throw new IllegalArgumentException(); - } - if (tree.getNames().isEmpty()) { - throw new IllegalArgumentException(); - } - return tree; - } - - public ICommandAddress getOrigin() { - return origin; - } - - @Override - public boolean execute(CommandSender sender, String label, String[] args) { - if (!dispatcher.dispatchCommand(sender, label, args)) { - //System.out.println("failed to dispatch command"); - // target command not found, send a message in the future TODO - } - return true; - } - - @Override - public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { - return this.tabComplete(sender, alias, args, null); - } - - //@Override - public List<String> tabComplete(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException { - return dispatcher.getTabCompletions(sender, alias, location, args); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - BukkitCommand that = (BukkitCommand) o; - - return getName().equals(that.getName()) && dispatcher == that.dispatcher; - } - - @Override - public int hashCode() { - return dispatcher.hashCode() | getName().hashCode(); - } - - private static void setTimingsIfNecessary(Command object) { - // with paper spigot, the timings are not set by super constructor but by CommandMap.register(), which is not invoked for this system - // I use reflection so that the project does not require paper spigot to build - try { - // public field - Field field = Command.class.getDeclaredField("timings"); - if (field.get(object) != null) return; - Class<?> clazz = Class.forName("co.aikar.timings.TimingsManager"); - // public method - Method method = clazz.getDeclaredMethod("getCommandTiming", String.class, Command.class); - Object timings = method.invoke(null, "", object); - field.set(object, timings); - } catch (Throwable ignored) { - } - } - - /* - public static void registerToMap(ICommandAddress tree, Map<String, Command> map) { - BukkitCommand command = new BukkitCommand(tree); - Iterator<String> iterator = tree.getNames().iterator(); - map.put(iterator.next(), command); - while (iterator.hasNext()) { - map.putIfAbsent(iterator.next(), command); - } - } - - public static void unregisterFromMap(ICommandAddress tree, Map<String, Command> map) { - map.values().remove(new BukkitCommand(tree)); - } - - public static void registerChildrenToMap(ICommandAddress tree, Map<String, Command> map) { - for (Map.Entry<String, ? extends ICommandAddress> entry : tree.getChildren().entrySet()) { - ICommandAddress child = entry.getValue(); - registerToMap(child, map); - } - } - - public static void unregisterChildenFromMap(ICommandAddress tree, Map<String, Command> map) { - for (Map.Entry<String, ? extends ICommandAddress> entry : tree.getChildren().entrySet()) { - ICommandAddress child = entry.getValue(); - unregisterFromMap(child, map); - } - } - */ - -} +package io.dico.dicore.command.registration;
+
+import io.dico.dicore.command.ICommandAddress;
+import io.dico.dicore.command.ICommandDispatcher;
+import org.bukkit.Location;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.List;
+
+/**
+ * This class extends the bukkit's command class.
+ * Instances are injected into the command map.
+ */
+public class BukkitCommand extends Command {
+ private ICommandDispatcher dispatcher;
+ private ICommandAddress origin;
+
+ public BukkitCommand(ICommandAddress address) {
+ super(validateTree(address).getNames().get(0), "", "", address.getNames().subList(1, address.getNames().size()));
+ this.dispatcher = address.getDispatcherForTree();
+ this.origin = address;
+
+ setTimingsIfNecessary(this);
+ }
+
+ private static ICommandAddress validateTree(ICommandAddress tree) {
+ if (!tree.hasParent()) {
+ throw new IllegalArgumentException();
+ }
+ if (tree.getNames().isEmpty()) {
+ throw new IllegalArgumentException();
+ }
+ return tree;
+ }
+
+ public ICommandAddress getOrigin() {
+ return origin;
+ }
+
+ @Override
+ public boolean execute(CommandSender sender, String label, String[] args) {
+ if (!dispatcher.dispatchCommand(sender, label, args)) {
+ //System.out.println("failed to dispatch command");
+ // target command not found, send a message in the future TODO
+ }
+ return true;
+ }
+
+ @Override
+ public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
+ return this.tabComplete(sender, alias, args, null);
+ }
+
+ //@Override
+ public List<String> tabComplete(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException {
+ return dispatcher.getTabCompletions(sender, alias, location, args);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ BukkitCommand that = (BukkitCommand) o;
+
+ return getName().equals(that.getName()) && dispatcher == that.dispatcher;
+ }
+
+ @Override
+ public int hashCode() {
+ return dispatcher.hashCode() | getName().hashCode();
+ }
+
+ private static void setTimingsIfNecessary(Command object) {
+ // with paper spigot, the timings are not set by super constructor but by CommandMap.register(), which is not invoked for this system
+ // I use reflection so that the project does not require paper spigot to build
+ try {
+ // public field
+ Field field = Command.class.getDeclaredField("timings");
+ if (field.get(object) != null) return;
+ Class<?> clazz = Class.forName("co.aikar.timings.TimingsManager");
+ // public method
+ Method method = clazz.getDeclaredMethod("getCommandTiming", String.class, Command.class);
+ Object timings = method.invoke(null, "", object);
+ field.set(object, timings);
+ } catch (Throwable ignored) {
+ }
+ }
+
+ /*
+ public static void registerToMap(ICommandAddress tree, Map<String, Command> map) {
+ BukkitCommand command = new BukkitCommand(tree);
+ Iterator<String> iterator = tree.getNames().iterator();
+ map.put(iterator.next(), command);
+ while (iterator.hasNext()) {
+ map.putIfAbsent(iterator.next(), command);
+ }
+ }
+
+ public static void unregisterFromMap(ICommandAddress tree, Map<String, Command> map) {
+ map.values().remove(new BukkitCommand(tree));
+ }
+
+ public static void registerChildrenToMap(ICommandAddress tree, Map<String, Command> map) {
+ for (Map.Entry<String, ? extends ICommandAddress> entry : tree.getChildren().entrySet()) {
+ ICommandAddress child = entry.getValue();
+ registerToMap(child, map);
+ }
+ }
+
+ public static void unregisterChildenFromMap(ICommandAddress tree, Map<String, Command> map) {
+ for (Map.Entry<String, ? extends ICommandAddress> entry : tree.getChildren().entrySet()) {
+ ICommandAddress child = entry.getValue();
+ unregisterFromMap(child, map);
+ }
+ }
+ */
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/CommandMap.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/CommandMap.java index 780ec66..2008b29 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/registration/CommandMap.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/CommandMap.java @@ -1,59 +1,59 @@ -package io.dico.dicore.command.registration; - -import io.dico.dicore.Reflection; -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.SimpleCommandMap; -import org.bukkit.plugin.SimplePluginManager; - -import java.util.*; - -/** - * Provides access to bukkit's {@code Map<String, org.bukkit.command.Command>} command map. - */ -@SuppressWarnings("ConstantConditions") -public class CommandMap { - private static final Map<String, Command> commandMap = findCommandMap(); - - private CommandMap() { - - } - - public static Map<String, Command> getCommandMap() { - return Objects.requireNonNull(commandMap); - } - - public static boolean isAvailable() { - return commandMap != null; - } - - public static Command get(String key) { - return commandMap.get(key); - } - - public static void put(String key, Command command) { - commandMap.put(key, command); - } - - public static Collection<String> replace(Command command, Command replacement) { - List<String> result = new ArrayList<>(); - for (Map.Entry<String, Command> entry : commandMap.entrySet()) { - if (entry.getValue() == command) { - entry.setValue(replacement); - result.add(entry.getKey()); - } - } - return result; - } - - private static Map<String, Command> findCommandMap() { - try { - return Reflection.getFieldValue(SimpleCommandMap.class, "knownCommands", - Reflection.getFieldValue(SimplePluginManager.class, "commandMap", Bukkit.getPluginManager())); - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } - } - -} +package io.dico.dicore.command.registration;
+
+import io.dico.dicore.Reflection;
+import org.bukkit.Bukkit;
+import org.bukkit.command.Command;
+import org.bukkit.command.SimpleCommandMap;
+import org.bukkit.plugin.SimplePluginManager;
+
+import java.util.*;
+
+/**
+ * Provides access to bukkit's {@code Map<String, org.bukkit.command.Command>} command map.
+ */
+@SuppressWarnings("ConstantConditions")
+public class CommandMap {
+ private static final Map<String, Command> commandMap = findCommandMap();
+
+ private CommandMap() {
+
+ }
+
+ public static Map<String, Command> getCommandMap() {
+ return Objects.requireNonNull(commandMap);
+ }
+
+ public static boolean isAvailable() {
+ return commandMap != null;
+ }
+
+ public static Command get(String key) {
+ return commandMap.get(key);
+ }
+
+ public static void put(String key, Command command) {
+ commandMap.put(key, command);
+ }
+
+ public static Collection<String> replace(Command command, Command replacement) {
+ List<String> result = new ArrayList<>();
+ for (Map.Entry<String, Command> entry : commandMap.entrySet()) {
+ if (entry.getValue() == command) {
+ entry.setValue(replacement);
+ result.add(entry.getKey());
+ }
+ }
+ return result;
+ }
+
+ private static Map<String, Command> findCommandMap() {
+ try {
+ return Reflection.getFieldValue(SimpleCommandMap.class, "knownCommands",
+ Reflection.getFieldValue(SimplePluginManager.class, "commandMap", Bukkit.getPluginManager()));
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ return null;
+ }
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/CommandParseException.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/CommandParseException.java index 478c70c..0b7ce3c 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/CommandParseException.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/CommandParseException.java @@ -1,27 +1,27 @@ -package io.dico.dicore.command.registration.reflect; - -/** - * Thrown if an error occurs while 'parsing' a reflection command method - * Other errors can be thrown too in there that may not be directly relevant to a parsing error. - */ -public class CommandParseException extends Exception { - - public CommandParseException() { - } - - public CommandParseException(String message) { - super(message); - } - - public CommandParseException(String message, Throwable cause) { - super(message, cause); - } - - public CommandParseException(Throwable cause) { - super(cause); - } - - public CommandParseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} +package io.dico.dicore.command.registration.reflect;
+
+/**
+ * Thrown if an error occurs while 'parsing' a reflection command method
+ * Other errors can be thrown too in there that may not be directly relevant to a parsing error.
+ */
+public class CommandParseException extends Exception {
+
+ public CommandParseException() {
+ }
+
+ public CommandParseException(String message) {
+ super(message);
+ }
+
+ public CommandParseException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public CommandParseException(Throwable cause) {
+ super(cause);
+ }
+
+ public CommandParseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
+ super(message, cause, enableSuppression, writableStackTrace);
+ }
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ICommandInterceptor.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ICommandInterceptor.java index 67b65e4..1e56b8a 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ICommandInterceptor.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ICommandInterceptor.java @@ -1,36 +1,36 @@ -package io.dico.dicore.command.registration.reflect; - -import io.dico.dicore.command.ExecutionContext; - -import java.lang.reflect.Method; - -public interface ICommandInterceptor { - - /** - * Get the receiver of the command, if applicable. - * A command has a receiver if its first parameter implements {@link ICommandReceiver} - * and its instance object implements this interface. - * - * @param context the context of execution - * @param target the method of the command - * @param cmdName the name of the command - * @return the receiver - */ - default ICommandReceiver getReceiver(ExecutionContext context, Method target, String cmdName) { - return null; - } - - /** - * If applicable, get the coroutine context to use in suspend functions (Kotlin only). - * The return type is object to avoid depending on the kotlin runtime. - * - * @param context the context of execution - * @param target the method of the command - * @param cmdName the name of the command - * @return the coroutine context - */ - default Object getCoroutineContext(ExecutionContext context, Method target, String cmdName) { - return null; - } - -} +package io.dico.dicore.command.registration.reflect;
+
+import io.dico.dicore.command.ExecutionContext;
+
+import java.lang.reflect.Method;
+
+public interface ICommandInterceptor {
+
+ /**
+ * Get the receiver of the command, if applicable.
+ * A command has a receiver if its first parameter implements {@link ICommandReceiver}
+ * and its instance object implements this interface.
+ *
+ * @param context the context of execution
+ * @param target the method of the command
+ * @param cmdName the name of the command
+ * @return the receiver
+ */
+ default ICommandReceiver getReceiver(ExecutionContext context, Method target, String cmdName) {
+ return null;
+ }
+
+ /**
+ * If applicable, get the coroutine context to use in suspend functions (Kotlin only).
+ * The return type is object to avoid depending on the kotlin runtime.
+ *
+ * @param context the context of execution
+ * @param target the method of the command
+ * @param cmdName the name of the command
+ * @return the coroutine context
+ */
+ default Object getCoroutineContext(ExecutionContext context, Method target, String cmdName) {
+ return null;
+ }
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ICommandReceiver.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ICommandReceiver.java index bdcb568..d0681b1 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ICommandReceiver.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ICommandReceiver.java @@ -1,5 +1,5 @@ -package io.dico.dicore.command.registration.reflect; - -public interface ICommandReceiver { - -} +package io.dico.dicore.command.registration.reflect;
+
+public interface ICommandReceiver {
+
+}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveCallFlags.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveCallFlags.java new file mode 100644 index 0000000..fa198c2 --- /dev/null +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveCallFlags.java @@ -0,0 +1,186 @@ +package io.dico.dicore.command.registration.reflect; + +import io.dico.dicore.command.CommandException; +import io.dico.dicore.command.ExecutionContext; +import io.dico.dicore.exceptions.checkedfunctions.CheckedSupplier; + +/** + * Call flags store which extra parameters the target function expects on top of command parameters. + * All 4 possible extra parameters are listed below. + * <p> + * Extra parameters are ordered by the bit that represents them in the call flags. + * They can either be leading or trailing the command's parameters. + */ +public class ReflectiveCallFlags { + + /** + * Receiver ({@code this} in some kotlin functions - always first parameter) + * + * @see ICommandInterceptor#getReceiver(io.dico.dicore.command.ExecutionContext, java.lang.reflect.Method, String) + */ + public static final int RECEIVER_BIT = 1 << 0; + + /** + * CommandSender + * + * @see org.bukkit.command.CommandSender + */ + public static final int SENDER_BIT = 1 << 1; + + /** + * ExecutionContext + * + * @see io.dico.dicore.command.ExecutionContext + */ + public static final int CONTEXT_BIT = 1 << 2; + + /** + * Continuation (trailing parameters of kotlin suspended functions) + * + * @see kotlin.coroutines.Continuation + */ + public static final int CONTINUATION_BIT = 1 << 3; + + /** + * Mask of extra parameters that trail the command's parameters, instead of leading. + */ + public static final int TRAILING_MASK = CONTINUATION_BIT; + + /** + * Check if the call arg is trailing the command's parameters. + * + * @param bit the bit used for the call flag + * @return true if the call arg is trailing the command's parameters + */ + public static boolean isTrailingCallArg(int bit) { + return (bit & TRAILING_MASK) != 0; + } + + /** + * Number of call arguments leading the command parameters. + * + * @param flags the call flags + * @return the number of call arguments leading the command parameters + */ + public static int getLeadingCallArgNum(int flags) { + return Integer.bitCount(flags & ~TRAILING_MASK); + } + + /** + * Number of call arguments trailing the command parameters. + * + * @param flags the call flags + * @return the number of call arguments trailing the command parameters + */ + public static int getTrailingCallArgNum(int flags) { + return Integer.bitCount(flags & TRAILING_MASK); + } + + /** + * Check if the flags contain the call arg. + * + * @param flags the call flags + * @param bit the bit used for the call flag + * @return true if the flags contain the call arg + */ + public static boolean hasCallArg(int flags, int bit) { + return (flags & bit) != 0; + } + + /** + * Get the index used for the call arg when calling the reflective function + * + * @param flags the call flags + * @param bit the bit used for the call flag + * @param cmdParameterNum the number of parameters of the command + * @return the index used for the call arg + */ + public static int getCallArgIndex(int flags, int bit, int cmdParameterNum) { + if ((bit & TRAILING_MASK) == 0) { + // Leading. + + int preceding = precedingMaskFrom(bit); + int mask = flags & precedingMaskFrom(bit) & ~TRAILING_MASK; + + // Count the number of present call args that are leading and precede the given bit + return Integer.bitCount(flags & precedingMaskFrom(bit) & ~TRAILING_MASK); + } else { + // Trailing. + + // Count the number of present call args that are leading + // plus the number of present call args that are trailing and precede the given bit + // plus the command's parameters + + return Integer.bitCount(flags & ~TRAILING_MASK) + + Integer.bitCount(flags & precedingMaskFrom(bit) & TRAILING_MASK) + + cmdParameterNum; + } + } + + /** + * Get the mask for all bits trailing the given fromBit + * + * <p> + * For example, if the bit is 00010000 + * This function returns 00001111 + * <p> + * + * @param fromBit number with the bit set there the ones should stop. + * @return the mask for all bits trailing the given fromBit + */ + private static int precedingMaskFrom(int fromBit) { + int trailingZeros = Integer.numberOfTrailingZeros(fromBit); + if (trailingZeros == 0) return 0; + return -1 >>> -trailingZeros; + } + + /** + * Get the object array used to call the function. + * + * @param callFlags the call flags + * @param context the context + * @param parameterOrder the order of parameters in the function + * @param receiverFunction the function that will create the receiver for this call, if applicable + * @return the call args + */ + public static Object[] getCallArgs( + int callFlags, + ExecutionContext context, + String[] parameterOrder, + CheckedSupplier<Object, CommandException> receiverFunction + ) throws CommandException { + int leadingParameterNum = getLeadingCallArgNum(callFlags); + int cmdParameterNum = parameterOrder.length; + int trailingParameterNum = getTrailingCallArgNum(callFlags); + + Object[] result = new Object[leadingParameterNum + cmdParameterNum + trailingParameterNum]; + + if (hasCallArg(callFlags, RECEIVER_BIT)) { + int index = getCallArgIndex(callFlags, RECEIVER_BIT, cmdParameterNum); + result[index] = receiverFunction.get(); + } + + if (hasCallArg(callFlags, SENDER_BIT)) { + int index = getCallArgIndex(callFlags, SENDER_BIT, cmdParameterNum); + result[index] = context.getSender(); + } + + if (hasCallArg(callFlags, CONTEXT_BIT)) { + int index = getCallArgIndex(callFlags, CONTEXT_BIT, cmdParameterNum); + result[index] = context; + } + + if (hasCallArg(callFlags, CONTINUATION_BIT)) { + int index = getCallArgIndex(callFlags, CONTINUATION_BIT, cmdParameterNum); + result[index] = null; // filled in later. + } + + for (int i = 0; i < parameterOrder.length; i++) { + String parameterName = parameterOrder[i]; + result[leadingParameterNum + i] = context.get(parameterName); + } + + return result; + } + +} diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveCommand.java index 2d7c333..f4ddc70 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveCommand.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveCommand.java @@ -4,6 +4,8 @@ import io.dico.dicore.command.*; import io.dico.dicore.command.annotation.Cmd; import io.dico.dicore.command.annotation.GenerateCommands; import io.dico.dicore.command.parameter.type.IParameterTypeSelector; +import io.dico.dicore.exceptions.checkedfunctions.CheckedSupplier; +import kotlin.coroutines.CoroutineContext; import org.bukkit.command.CommandSender; import java.lang.reflect.InvocationTargetException; @@ -11,14 +13,11 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; public final class ReflectiveCommand extends Command { - private static final int continuationMask = 1 << 3; private final Cmd cmdAnnotation; private final Method method; private final Object instance; private String[] parameterOrder; - - // hasContinuation | hasContext | hasSender | hasReceiver - private final int flags; + private final int callFlags; ReflectiveCommand(IParameterTypeSelector selector, Method method, Object instance) throws CommandParseException { if (!method.isAnnotationPresent(Cmd.class)) { @@ -48,7 +47,7 @@ public final class ReflectiveCommand extends Command { this.method = method; this.instance = instance; - this.flags = ReflectiveRegistration.parseCommandAttributes(selector, method, this, parameters); + this.callFlags = ReflectiveRegistration.parseCommandAttributes(selector, method, this, parameters); } public Method getMethod() { @@ -59,12 +58,22 @@ public final class ReflectiveCommand extends Command { return instance; } - public String getCmdName() { return cmdAnnotation.value(); } + public String getCmdName() { + return cmdAnnotation.value(); + } + + public int getCallFlags() { + return callFlags; + } void setParameterOrder(String[] parameterOrder) { this.parameterOrder = parameterOrder; } + public int getParameterNum() { + return parameterOrder.length; + } + ICommandAddress getAddress() { ChildCommandAddress result = new ChildCommandAddress(); result.setCommand(this); @@ -86,54 +95,24 @@ public final class ReflectiveCommand extends Command { @Override public String execute(CommandSender sender, ExecutionContext context) throws CommandException { - String[] parameterOrder = this.parameterOrder; - int extraArgumentCount = Integer.bitCount(flags); - int parameterStartIndex = Integer.bitCount(flags & ~continuationMask); - - Object[] args = new Object[parameterOrder.length + extraArgumentCount]; - int i = 0; - - int mask = 1; - if ((flags & mask) != 0) { - // Has receiver + CheckedSupplier<Object, CommandException> receiverFunction = () -> { try { - args[i++] = ((ICommandInterceptor) instance).getReceiver(context, method, getCmdName()); + return ((ICommandInterceptor) instance).getReceiver(context, method, getCmdName()); } catch (Exception ex) { handleException(ex); return null; // unreachable } - } + }; - mask <<= 1; - if ((flags & mask) != 0) { - // Has sender - args[i++] = sender; - } - - mask <<= 1; - if ((flags & mask) != 0) { - // Has context - args[i++] = context; - } - - mask <<= 1; - if ((flags & mask) != 0) { - // Has continuation - - extraArgumentCount--; - } - - for (int n = args.length; i < n; i++) { - args[i] = context.get(parameterOrder[i - extraArgumentCount]); - } + Object[] callArgs = ReflectiveCallFlags.getCallArgs(callFlags, context, parameterOrder, receiverFunction); - if ((flags & mask) != 0) { - // Since it has continuation, call as coroutine - return callAsCoroutine(context, args); + if (ReflectiveCallFlags.hasCallArg(callFlags, ReflectiveCallFlags.CONTINUATION_BIT)) { + // If it has a continuation, call as coroutine + return callAsCoroutine(context, callArgs); } - return callSynchronously(args); + return callSynchronously(callArgs); } private boolean isSuspendFunction() { @@ -180,8 +159,11 @@ public final class ReflectiveCommand extends Command { throw new CommandException("An internal error occurred while executing this command.", ex); } - private String callAsCoroutine(ExecutionContext context, Object[] args) { - return KotlinReflectiveRegistrationKt.callAsCoroutine(this, (ICommandInterceptor) instance, context, args); + private String callAsCoroutine(ExecutionContext executionContext, Object[] args) throws CommandException { + ICommandInterceptor factory = (ICommandInterceptor) instance; + CoroutineContext coroutineContext = (CoroutineContext) factory.getCoroutineContext(executionContext, method, getCmdName()); + int continuationIndex = ReflectiveCallFlags.getCallArgIndex(callFlags, ReflectiveCallFlags.CONTINUATION_BIT, parameterOrder.length); + return KotlinReflectiveRegistrationKt.callCommandAsCoroutine(executionContext, coroutineContext, continuationIndex, method, instance, args); } } diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveRegistration.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveRegistration.java index 6b1965d..ddd5420 100644 --- a/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveRegistration.java +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/reflect/ReflectiveRegistration.java @@ -22,6 +22,8 @@ import java.util.function.Consumer; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import static io.dico.dicore.command.registration.reflect.ReflectiveCallFlags.*; + /** * Takes care of turning a reflection {@link Method} into a command and more. */ @@ -196,47 +198,43 @@ public class ReflectiveRegistration { return new ReflectiveCommand(selector, method, instance).getAddress(); } - static int parseCommandAttributes(IParameterTypeSelector selector, Method method, ReflectiveCommand command, java.lang.reflect.Parameter[] parameters) throws CommandParseException { + static int parseCommandAttributes(IParameterTypeSelector selector, Method method, ReflectiveCommand command, java.lang.reflect.Parameter[] callParameters) throws CommandParseException { ParameterList list = command.getParameterList(); - boolean hasReceiverParameter = false; - boolean hasSenderParameter = false; - boolean hasContextParameter = false; - boolean hasContinuationParameter = false; - - int start = 0; - int end = parameters.length; - Class<?> senderParameterType = null; + int flags = 0; + int start = 0; + int end = callParameters.length; - if (parameters.length > start + if (callParameters.length > start && command.getInstance() instanceof ICommandInterceptor - && ICommandReceiver.class.isAssignableFrom(parameters[start].getType())) { - hasReceiverParameter = true; - start++; + && ICommandReceiver.class.isAssignableFrom(callParameters[start].getType())) { + flags |= RECEIVER_BIT; + ++start; } - if (parameters.length > start && CommandSender.class.isAssignableFrom(senderParameterType = parameters[start].getType())) { - hasSenderParameter = true; - start++; + if (callParameters.length > start && CommandSender.class.isAssignableFrom(senderParameterType = callParameters[start].getType())) { + flags |= SENDER_BIT; + ++start; } - if (parameters.length > start && parameters[start].getType() == ExecutionContext.class) { - hasContextParameter = true; - start++; + if (callParameters.length > start && callParameters[start].getType() == ExecutionContext.class) { + flags |= CONTEXT_BIT; + ++start; } - if (parameters.length > start && parameters[end - 1].getType().getName().equals("kotlin.coroutines.Continuation")) { - hasContinuationParameter = true; - end--; + if (callParameters.length > start && callParameters[end - 1].getType().getName().equals("kotlin.coroutines.Continuation")) { + flags |= CONTINUATION_BIT; + --end; } - String[] parameterNames = lookupParameterNames(method, parameters, start); + String[] parameterNames = lookupParameterNames(method, callParameters, start); for (int i = start, n = end; i < n; i++) { - Parameter<?, ?> parameter = parseParameter(selector, method, parameters[i], parameterNames[i - start]); + Parameter<?, ?> parameter = parseParameter(selector, method, callParameters[i], parameterNames[i - start]); list.addParameter(parameter); } - command.setParameterOrder(hasContinuationParameter ? Arrays.copyOfRange(parameterNames, 0, parameterNames.length - 1) : parameterNames); + + command.setParameterOrder(hasCallArg(flags, CONTINUATION_BIT) ? Arrays.copyOfRange(parameterNames, 0, parameterNames.length - 1) : parameterNames); RequirePermissions cmdPermissions = method.getAnnotation(RequirePermissions.class); if (cmdPermissions != null) { @@ -277,6 +275,7 @@ public class ReflectiveRegistration { command.setDescription(); } + boolean hasSenderParameter = hasCallArg(flags, SENDER_BIT); if (hasSenderParameter && Player.class.isAssignableFrom(senderParameterType)) { command.addContextFilter(IContextFilter.PLAYER_ONLY); } else if (hasSenderParameter && ConsoleCommandSender.class.isAssignableFrom(senderParameterType)) { @@ -287,17 +286,9 @@ public class ReflectiveRegistration { command.addContextFilter(IContextFilter.CONSOLE_ONLY); } - list.setRepeatFinalParameter(parameters.length > start && parameters[parameters.length - 1].isVarArgs()); + list.setRepeatFinalParameter(callParameters.length > start && callParameters[callParameters.length - 1].isVarArgs()); list.setFinalParameterMayBeFlag(true); - int flags = 0; - if (hasContinuationParameter) flags |= 1; - flags <<= 1; - if (hasContextParameter) flags |= 1; - flags <<= 1; - if (hasSenderParameter) flags |= 1; - flags <<= 1; - if (hasReceiverParameter) flags |= 1; return flags; } |