New ScopeOption class for options framework.

Also moved the scope enum to ScopeOption class. Refactored every occurence of the scope enum in the project for transitionning purpose.
keep-around/3c354c0f00ae6e9d0b7ccd747b995217fbd6c147
Pingex aka Raphaël 9 years ago
parent f4fe2c5a6a
commit ee2c6cb38c

@ -1,5 +1,6 @@
package net.pingex.dcf.commands; package net.pingex.dcf.commands;
import net.pingex.dcf.commands.options.ScopeOption;
import net.pingex.dcf.permissions.DefaultPermission; import net.pingex.dcf.permissions.DefaultPermission;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@ -46,5 +47,5 @@ public @interface AnnotatedCommand
/** /**
* Tells where the command should run, ie. PM or guild chat, or both * Tells where the command should run, ie. PM or guild chat, or both
*/ */
CommandScope scope() default CommandScope.NOWHERE; ScopeOption.CommandScope scope() default ScopeOption.CommandScope.NOWHERE;
} }

@ -1,6 +1,7 @@
package net.pingex.dcf.commands; package net.pingex.dcf.commands;
import net.pingex.dcf.commands.options.ICommandOption; import net.pingex.dcf.commands.options.ICommandOption;
import net.pingex.dcf.commands.options.ScopeOption;
import net.pingex.dcf.permissions.DefaultPermission; import net.pingex.dcf.permissions.DefaultPermission;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent; import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -47,7 +48,7 @@ public abstract class Command implements ICommandExecutor
/** /**
* Tells where the command should run, ie. PM or guild chat, or both * Tells where the command should run, ie. PM or guild chat, or both
*/ */
private CommandScope commandScope; private ScopeOption.CommandScope commandScope;
/** /**
* Contains all options for this command. * Contains all options for this command.
@ -65,7 +66,7 @@ public abstract class Command implements ICommandExecutor
* @param commandScope Tells where the command should run, ie. PM or guild chat, or both * @param commandScope Tells where the command should run, ie. PM or guild chat, or both
* @param options Command options. * @param options Command options.
*/ */
public Command(String name, List<String> aliases, String description, boolean isEnabled, String usage, DefaultPermission defaultPermission, CommandScope commandScope, Set<ICommandOption> options) public Command(String name, List<String> aliases, String description, boolean isEnabled, String usage, DefaultPermission defaultPermission, ScopeOption.CommandScope commandScope, Set<ICommandOption> options)
{ {
this.name = name; this.name = name;
this.aliases = aliases; this.aliases = aliases;
@ -87,7 +88,7 @@ public abstract class Command implements ICommandExecutor
public static final boolean IS_ENABLED = true; public static final boolean IS_ENABLED = true;
public static final String USAGE = "No command usage provided."; public static final String USAGE = "No command usage provided.";
public static final DefaultPermission DEFAULT_PERMISSION = DefaultPermission.EVERYONE; 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 ScopeOption.CommandScope COMMAND_SCOPE = ScopeOption.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(); public static final Set<ICommandOption> OPTIONS = Collections.emptySet();
} }
@ -129,7 +130,7 @@ public abstract class Command implements ICommandExecutor
/** /**
* Tells where the command should run, ie. PM or guild chat, or both * Tells where the command should run, ie. PM or guild chat, or both
*/ */
private CommandScope commandScope = Defaults.COMMAND_SCOPE; private ScopeOption.CommandScope commandScope = Defaults.COMMAND_SCOPE;
/** /**
* Command options * Command options
@ -181,7 +182,7 @@ public abstract class Command implements ICommandExecutor
return this; return this;
} }
public Builder commandScope(CommandScope commandScope) public Builder commandScope(ScopeOption.CommandScope commandScope)
{ {
this.commandScope = commandScope; this.commandScope = commandScope;
return this; return this;
@ -286,7 +287,7 @@ public abstract class Command implements ICommandExecutor
return defaultPermission; return defaultPermission;
} }
public CommandScope getScope() public ScopeOption.CommandScope getScope()
{ {
return commandScope; return commandScope;
} }

@ -1,45 +0,0 @@
package net.pingex.dcf.commands;
import sx.blah.discord.handle.obj.IChannel;
import java.util.function.Predicate;
/**
* CommandScope allows devs to tell where Commands should run.
* ie. PM, guild chat, etc
*/
public enum CommandScope
{
/**
* Allows only in a guild chat
*/
GUILD_CHAT(iChannel -> !iChannel.isPrivate()),
/**
* Only via PM with the bot
*/
PRIVATE_MESSAGE(IChannel::isPrivate),
/**
* Allows unconditionally
*/
ANYWHERE(iChannel -> true),
/**
* Denies unconditionally
* Default value
*/
NOWHERE(iChannel -> false);
private Predicate<IChannel> channel;
CommandScope(Predicate<IChannel> channel)
{
this.channel = channel;
}
public boolean test(IChannel iChannel)
{
return channel.test(iChannel);
}
}

@ -1,6 +1,7 @@
package net.pingex.dcf.commands; package net.pingex.dcf.commands;
import net.pingex.dcf.commands.options.ICommandOption; import net.pingex.dcf.commands.options.ICommandOption;
import net.pingex.dcf.commands.options.ScopeOption;
import net.pingex.dcf.core.Configuration; import net.pingex.dcf.core.Configuration;
import net.pingex.dcf.permissions.DefaultPermission; import net.pingex.dcf.permissions.DefaultPermission;
import net.pingex.dcf.permissions.PermissionsHandler; import net.pingex.dcf.permissions.PermissionsHandler;
@ -33,7 +34,7 @@ public class InternalCommands implements IWithCommands
private static final boolean IS_ENABLED = true; private static final boolean IS_ENABLED = true;
private static final String USAGE = "Page"; private static final String USAGE = "Page";
private static final DefaultPermission DEFAULT_PERMISSION = DefaultPermission.EVERYONE; private static final DefaultPermission DEFAULT_PERMISSION = DefaultPermission.EVERYONE;
private static final CommandScope COMMAND_SCOPE = CommandScope.ANYWHERE; private static final ScopeOption.CommandScope COMMAND_SCOPE = ScopeOption.CommandScope.ANYWHERE;
private static final Set<ICommandOption> OPTIONS = Defaults.OPTIONS; private static final Set<ICommandOption> OPTIONS = Defaults.OPTIONS;
/** /**
@ -116,7 +117,7 @@ public class InternalCommands implements IWithCommands
private static final boolean IS_ENABLED = true; private static final boolean IS_ENABLED = true;
private static final String USAGE = "Command"; private static final String USAGE = "Command";
private static final DefaultPermission DEFAULT_PERMISSION = DefaultPermission.EVERYONE; private static final DefaultPermission DEFAULT_PERMISSION = DefaultPermission.EVERYONE;
private static final CommandScope COMMAND_SCOPE = CommandScope.ANYWHERE; private static final ScopeOption.CommandScope COMMAND_SCOPE = ScopeOption.CommandScope.ANYWHERE;
private static final Set<ICommandOption> OPTIONS = Defaults.OPTIONS; private static final Set<ICommandOption> OPTIONS = Defaults.OPTIONS;
static final UsageCommand INSTANCE = new UsageCommand(); static final UsageCommand INSTANCE = new UsageCommand();

@ -0,0 +1,74 @@
package net.pingex.dcf.commands.options;
import sx.blah.discord.handle.obj.IChannel;
import java.util.function.Predicate;
/**
* This option allows to specify where the command should run.
*/
public class ScopeOption implements ICommandOption
{
private CommandScope commandScope;
public ScopeOption(CommandScope commandScope)
{
this.commandScope = commandScope;
}
public CommandScope getCommandScope()
{
return commandScope;
}
@Override
public String getOptionName()
{
return "Command scope";
}
@Override
public String getOptionDescription()
{
return "This option allows to specify where the command can be run.";
}
/**
* CommandScope allows devs to tell where Commands should run.
* ie. PM, guild chat, etc
*/
public enum CommandScope
{
/**
* Allows only in a guild chat
*/
GUILD_CHAT(iChannel -> !iChannel.isPrivate()),
/**
* Only via PM with the bot
*/
PRIVATE_MESSAGE(IChannel::isPrivate),
/**
* Allows unconditionally
*/
ANYWHERE(iChannel -> true),
/**
* Denies unconditionally
* Default value
*/
NOWHERE(iChannel -> false);
private Predicate<IChannel> channel;
CommandScope(Predicate<IChannel> channel)
{
this.channel = channel;
}
public boolean test(IChannel iChannel)
{
return channel.test(iChannel);
}
}
}