Add CommandPart.checkGuard
Dieser Commit ist enthalten in:
Ursprung
93156a0976
Commit
86ef8e2773
@ -39,11 +39,11 @@ public class CommandPart {
|
||||
private GuardChecker guard;
|
||||
private Class<?> varArgType;
|
||||
private String optional;
|
||||
private boolean help;
|
||||
private GuardCheckType guardCheckType;
|
||||
|
||||
private CommandPart next = null;
|
||||
|
||||
CommandPart(TypeMapper<?> typeMapper, GuardChecker guard, Class<?> varArgType, String optional, boolean help) {
|
||||
public CommandPart(TypeMapper<?> typeMapper, GuardChecker guard, Class<?> varArgType, String optional, GuardCheckType guardCheckType) {
|
||||
this.typeMapper = typeMapper;
|
||||
this.guard = guard;
|
||||
if (optional != null && varArgType != null) {
|
||||
@ -51,17 +51,17 @@ public class CommandPart {
|
||||
}
|
||||
this.varArgType = varArgType;
|
||||
this.optional = optional;
|
||||
this.help = help;
|
||||
this.guardCheckType = guardCheckType;
|
||||
}
|
||||
|
||||
void setNext(CommandPart next) {
|
||||
public void setNext(CommandPart next) {
|
||||
if (varArgType != null) {
|
||||
throw new IllegalArgumentException("There can't be a next part if this is a vararg part!");
|
||||
}
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
void generateArgumentArray(List<Object> current, CommandSender commandSender, String[] args, int startIndex) {
|
||||
public void generateArgumentArray(List<Object> current, CommandSender commandSender, String[] args, int startIndex) {
|
||||
if (startIndex >= args.length) {
|
||||
return;
|
||||
}
|
||||
@ -69,7 +69,7 @@ public class CommandPart {
|
||||
if (varArgType != null) {
|
||||
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
||||
for (int i = startIndex; i < args.length; i++) {
|
||||
CheckArgumentResult validArgument = checkArgument(help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, commandSender, args, i);
|
||||
CheckArgumentResult validArgument = checkArgument(null, commandSender, args, i);
|
||||
if (!validArgument.success) {
|
||||
throw new CommandParseException();
|
||||
}
|
||||
@ -79,7 +79,7 @@ public class CommandPart {
|
||||
return;
|
||||
}
|
||||
|
||||
CheckArgumentResult validArgument = checkArgument(help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, commandSender, args, startIndex);
|
||||
CheckArgumentResult validArgument = checkArgument(null, commandSender, args, startIndex);
|
||||
if (!validArgument.success && optional == null) {
|
||||
throw new CommandParseException();
|
||||
}
|
||||
@ -96,7 +96,40 @@ public class CommandPart {
|
||||
}
|
||||
}
|
||||
|
||||
void generateTabComplete(List<String> current, CommandSender commandSender, String[] args, int startIndex) {
|
||||
public boolean guardCheck(CommandSender commandSender, String[] args, int startIndex) {
|
||||
if (varArgType != null) {
|
||||
for (int i = startIndex; i < args.length; i++) {
|
||||
GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, i);
|
||||
if (guardResult == GuardResult.DENIED) {
|
||||
throw new CommandNoHelpException();
|
||||
}
|
||||
if (guardResult == GuardResult.DENIED_WITH_HELP) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, startIndex);
|
||||
if (guardResult == GuardResult.DENIED) {
|
||||
if (optional != null && next != null) {
|
||||
return next.guardCheck(commandSender, args, startIndex);
|
||||
}
|
||||
throw new CommandNoHelpException();
|
||||
}
|
||||
if (guardResult == GuardResult.DENIED_WITH_HELP) {
|
||||
if (optional != null && next != null) {
|
||||
return next.guardCheck(commandSender, args, startIndex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (next != null) {
|
||||
return next.guardCheck(commandSender, args, startIndex + 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void generateTabComplete(List<String> current, CommandSender commandSender, String[] args, int startIndex) {
|
||||
if (args.length - 1 > startIndex) {
|
||||
CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, commandSender, args, startIndex);
|
||||
if (checkArgumentResult.success && next != null) {
|
||||
@ -120,8 +153,7 @@ public class CommandPart {
|
||||
if (value == null) {
|
||||
return new CheckArgumentResult(false, null);
|
||||
}
|
||||
if (guard != null) {
|
||||
GuardResult guardResult = guard.guard(commandSender, guardCheckType, Arrays.copyOf(args, index), args[index]);
|
||||
GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, index);
|
||||
switch (guardResult) {
|
||||
case ALLOWED:
|
||||
return new CheckArgumentResult(true, value);
|
||||
@ -131,10 +163,15 @@ public class CommandPart {
|
||||
default:
|
||||
return new CheckArgumentResult(false, null);
|
||||
}
|
||||
}
|
||||
return new CheckArgumentResult(true, value);
|
||||
} catch (Exception e) {
|
||||
return new CheckArgumentResult(false, null);
|
||||
}
|
||||
}
|
||||
|
||||
private GuardResult checkGuard(GuardCheckType guardCheckType, CommandSender commandSender, String[] args, int index) {
|
||||
if (guard != null && guardCheckType != null) {
|
||||
return guard.guard(commandSender, guardCheckType, Arrays.copyOf(args, index), args[index]);
|
||||
}
|
||||
return GuardResult.ALLOWED;
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandMap;
|
||||
@ -27,6 +28,7 @@ import org.bukkit.command.SimpleCommandMap;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
@UtilityClass
|
||||
class CommandRegistering {
|
||||
|
||||
private static final CommandMap commandMap;
|
||||
|
@ -36,13 +36,17 @@ public class SimpleCommandPartTest {
|
||||
private CommandPart intCommandPart;
|
||||
private CommandPart chainedCommandPart;
|
||||
|
||||
private CommandPart simpleGuardPart;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, false);
|
||||
intCommandPart = new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, true);
|
||||
stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
|
||||
intCommandPart = new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND);
|
||||
|
||||
chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, false);
|
||||
chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, true));
|
||||
chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
|
||||
chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND));
|
||||
|
||||
simpleGuardPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), (commandSender, guardCheckType, previousArguments, s) -> s.equals("hello") ? GuardResult.DENIED : GuardResult.ALLOWED, null, null, GuardCheckType.COMMAND);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -118,4 +122,29 @@ public class SimpleCommandPartTest {
|
||||
System.out.println(tabCompletes);
|
||||
assertThat(tabCompletes.size(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGuardCommandExecute() {
|
||||
List<Object> argumentArray = new ArrayList<>();
|
||||
simpleGuardPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0);
|
||||
assertThat(argumentArray.size(), is(1));
|
||||
}
|
||||
|
||||
@Test(expected = CommandNoHelpException.class)
|
||||
public void testGuardGuardCheck() {
|
||||
simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"hello"}, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGuardCommandExecuteValid() {
|
||||
List<Object> argumentArray = new ArrayList<>();
|
||||
simpleGuardPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world"}, 0);
|
||||
assertThat(argumentArray.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGuardGuardCheckValid() {
|
||||
boolean guardResult = simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"world"}, 0);
|
||||
assertThat(guardResult, is(true));
|
||||
}
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren