New options parameter to command.

keep-around/23b8b9e8830874d5f04b57600c3660bddce1287b
Pingex aka Raphaël 9 years ago
parent cf25b259d5
commit 23b8b9e883

@ -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<ICommandOption> 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<String> aliases, String description, boolean isEnabled, String usage, DefaultPermission defaultPermission, CommandScope commandScope)
public Command(String name, List<String> aliases, String description, boolean isEnabled, String usage, DefaultPermission defaultPermission, CommandScope commandScope, Set<ICommandOption> 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<ICommandOption> OPTIONS = Collections.emptySet();
}
/**
@ -121,6 +130,11 @@ public abstract class Command implements ICommandExecutor
*/
private CommandScope commandScope = Defaults.COMMAND_SCOPE;
/**
* Command options
*/
private Set<ICommandOption> 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<ICommandOption> 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<String> 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<String> arguments) throws Throwable
@ -270,6 +290,11 @@ public abstract class Command implements ICommandExecutor
return commandScope;
}
public Set<ICommandOption> getCommandOptions()
{
return Collections.unmodifiableSet(commandOptions);
}
public void setEnabled(boolean enabled)
{
isEnabled = enabled;

@ -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();
}

@ -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<ICommandOption> 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<ICommandOption> 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