From 23b8b9e8830874d5f04b57600c3660bddce1287b Mon Sep 17 00:00:00 2001 From: Pingex Date: Tue, 27 Dec 2016 20:57:01 +0100 Subject: [PATCH] New options parameter to command. --- .../java/net/pingex/dcf/commands/Command.java | 31 +++++++++++++++++-- .../pingex/dcf/commands/ICommandOption.java | 18 +++++++++++ .../pingex/dcf/commands/InternalCommands.java | 6 ++-- 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/pingex/dcf/commands/ICommandOption.java diff --git a/src/main/java/net/pingex/dcf/commands/Command.java b/src/main/java/net/pingex/dcf/commands/Command.java index ccd0299..8589ea5 100644 --- a/src/main/java/net/pingex/dcf/commands/Command.java +++ b/src/main/java/net/pingex/dcf/commands/Command.java @@ -6,6 +6,7 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Set; /** * Represents a single command. @@ -47,6 +48,11 @@ public abstract class Command implements ICommandExecutor */ private CommandScope commandScope; + /** + * Contains all options for this command. + */ + private Set commandOptions; + /** * Basic constructor. * @param name Name of the command @@ -56,8 +62,9 @@ public abstract class Command implements ICommandExecutor * @param usage Command usage help * @param defaultPermission Default permission, ie. when the permissions provider doesn't return anything. * @param commandScope Tells where the command should run, ie. PM or guild chat, or both + * @param options Command options. */ - public Command(String name, List aliases, String description, boolean isEnabled, String usage, DefaultPermission defaultPermission, CommandScope commandScope) + public Command(String name, List aliases, String description, boolean isEnabled, String usage, DefaultPermission defaultPermission, CommandScope commandScope, Set options) { this.name = name; this.aliases = aliases; @@ -66,6 +73,7 @@ public abstract class Command implements ICommandExecutor this.usage = usage; this.defaultPermission = defaultPermission; this.commandScope = commandScope; + this.commandOptions = options; } /** @@ -79,6 +87,7 @@ public abstract class Command implements ICommandExecutor public static final String USAGE = "No command usage provided."; public static final DefaultPermission DEFAULT_PERMISSION = DefaultPermission.EVERYONE; public static final CommandScope COMMAND_SCOPE = CommandScope.NOWHERE; // NOWHERE is enforced as a default value to force devs to specify a real scope. + public static final Set OPTIONS = Collections.emptySet(); } /** @@ -121,6 +130,11 @@ public abstract class Command implements ICommandExecutor */ private CommandScope commandScope = Defaults.COMMAND_SCOPE; + /** + * Command options + */ + private Set commandOptions = Defaults.OPTIONS; + public Builder(String name) { this.name = name; @@ -172,6 +186,12 @@ public abstract class Command implements ICommandExecutor return this; } + public Builder options(Set options) + { + commandOptions = options; + return this; + } + /** * Build a new Command using a supplied executor. * @param toExecute The body of the command. @@ -179,7 +199,7 @@ public abstract class Command implements ICommandExecutor */ public Command build(ICommandExecutor toExecute) { - return new Command(name, aliases, description, isEnabled, usage, defaultPermission, commandScope) + return new Command(name, aliases, description, isEnabled, usage, defaultPermission, commandScope, commandOptions) { @Override public void execute(MessageReceivedEvent event, List arguments) throws Throwable @@ -197,7 +217,7 @@ public abstract class Command implements ICommandExecutor */ public Command build(Method target, Object invokable) { - return new Command(name, aliases, description, isEnabled, usage, defaultPermission, commandScope) + return new Command(name, aliases, description, isEnabled, usage, defaultPermission, commandScope, commandOptions) { @Override public void execute(MessageReceivedEvent event, List arguments) throws Throwable @@ -270,6 +290,11 @@ public abstract class Command implements ICommandExecutor return commandScope; } + public Set getCommandOptions() + { + return Collections.unmodifiableSet(commandOptions); + } + public void setEnabled(boolean enabled) { isEnabled = enabled; diff --git a/src/main/java/net/pingex/dcf/commands/ICommandOption.java b/src/main/java/net/pingex/dcf/commands/ICommandOption.java new file mode 100644 index 0000000..2e0ca17 --- /dev/null +++ b/src/main/java/net/pingex/dcf/commands/ICommandOption.java @@ -0,0 +1,18 @@ +package net.pingex.dcf.commands; + +/** + * Interface to add specific behaviors to a command. + * Behaviors are stored in a Set object in the main Command object. + */ +public interface ICommandOption +{ + /** + * Give the name of the option. + */ + String getOptionName(); + + /** + * Gives the description of the option. + */ + String getOptionDescription(); +} diff --git a/src/main/java/net/pingex/dcf/commands/InternalCommands.java b/src/main/java/net/pingex/dcf/commands/InternalCommands.java index 45d68c1..7f28ea1 100644 --- a/src/main/java/net/pingex/dcf/commands/InternalCommands.java +++ b/src/main/java/net/pingex/dcf/commands/InternalCommands.java @@ -33,6 +33,7 @@ public class InternalCommands implements IWithCommands private static final String USAGE = "Page"; private static final DefaultPermission DEFAULT_PERMISSION = DefaultPermission.EVERYONE; private static final CommandScope COMMAND_SCOPE = CommandScope.ANYWHERE; + private static final Set OPTIONS = Defaults.OPTIONS; /** * How many commands should be displayed on each page @@ -43,7 +44,7 @@ public class InternalCommands implements IWithCommands private ListCommand() { - super(NAME, ALIASES, DESCRIPTION, IS_ENABLED, USAGE, DEFAULT_PERMISSION, COMMAND_SCOPE); + super(NAME, ALIASES, DESCRIPTION, IS_ENABLED, USAGE, DEFAULT_PERMISSION, COMMAND_SCOPE, OPTIONS); } @Override @@ -115,12 +116,13 @@ public class InternalCommands implements IWithCommands private static final String USAGE = "Command"; private static final DefaultPermission DEFAULT_PERMISSION = DefaultPermission.EVERYONE; private static final CommandScope COMMAND_SCOPE = CommandScope.ANYWHERE; + private static final Set OPTIONS = Defaults.OPTIONS; static final UsageCommand INSTANCE = new UsageCommand(); private UsageCommand() { - super(NAME, ALIASES, DESCRIPTION, IS_ENABLED, USAGE, DEFAULT_PERMISSION, COMMAND_SCOPE); + super(NAME, ALIASES, DESCRIPTION, IS_ENABLED, USAGE, DEFAULT_PERMISSION, COMMAND_SCOPE, OPTIONS); } @Override