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