Scope checking option implementation.

keep-around/3c354c0f00ae6e9d0b7ccd747b995217fbd6c147
Pingex aka Raphaël 9 years ago
parent ee2c6cb38c
commit 0c9db2814d

@ -292,7 +292,7 @@ public abstract class Command implements ICommandExecutor
return commandScope;
}
public Set<ICommandOption> getCommandOptions()
public Set<ICommandOption> getOptions()
{
return Collections.unmodifiableSet(commandOptions);
}

@ -3,6 +3,9 @@ package net.pingex.dcf.commands.audit.basic;
import net.pingex.dcf.commands.Context;
import net.pingex.dcf.commands.audit.AuditResult;
import net.pingex.dcf.commands.audit.IAuditComponentProvider;
import net.pingex.dcf.commands.options.ICommandOption;
import net.pingex.dcf.commands.options.ScopeOption;
import java.util.Optional;
/**
* This component checks whether the command is invoked in the right scope.
@ -12,9 +15,12 @@ public class ScopeCheck implements IAuditComponentProvider
@Override
public AuditResult doAudit(Context context)
{
if(context.getCommand().getScope().test(context.getChannel()))
Optional<ICommandOption> option = context.getCommand().getOptions().stream().filter(i -> i instanceof ScopeOption).findFirst();
if(!option.isPresent()) return new AuditResult(AuditResult.ResultCode.NOOP, "ScopeOption not present.");
if(((ScopeOption)option.get()).getScope().test(context.getChannel()))
return new AuditResult(AuditResult.ResultCode.PASS, null);
else return new AuditResult(AuditResult.ResultCode.FAIL, "Cannot run this command outside of its intended scope. Valid scope is: " + context.getCommand().getScope() + ".");
else return new AuditResult(AuditResult.ResultCode.FAIL, "Cannot run this command outside of its intended scope. Valid scope is: " + ((ScopeOption)option.get()).getScope() + ".");
}
@Override

@ -15,7 +15,7 @@ public class ScopeOption implements ICommandOption
this.commandScope = commandScope;
}
public CommandScope getCommandScope()
public CommandScope getScope()
{
return commandScope;
}