Fix CommandPart.generateTabComplete for optional arguments
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Dieser Commit ist enthalten in:
Ursprung
7d94d8dd6c
Commit
b5f9284bb5
@ -44,17 +44,13 @@ public class CommandPart {
|
|||||||
private CommandPart next = null;
|
private CommandPart next = null;
|
||||||
|
|
||||||
public CommandPart(TypeMapper<?> typeMapper, GuardChecker guard, Class<?> varArgType, String optional, GuardCheckType guardCheckType) {
|
public CommandPart(TypeMapper<?> typeMapper, GuardChecker guard, Class<?> varArgType, String optional, GuardCheckType guardCheckType) {
|
||||||
if (guardCheckType == GuardCheckType.TAB_COMPLETE) {
|
|
||||||
throw new IllegalArgumentException("Tab complete is not allowed as a guard check type!");
|
|
||||||
}
|
|
||||||
this.typeMapper = typeMapper;
|
this.typeMapper = typeMapper;
|
||||||
this.guard = guard;
|
this.guard = guard;
|
||||||
if (optional != null && varArgType != null) {
|
|
||||||
throw new IllegalArgumentException("A vararg part can't have an optional part!");
|
|
||||||
}
|
|
||||||
this.varArgType = varArgType;
|
this.varArgType = varArgType;
|
||||||
this.optional = optional;
|
this.optional = optional;
|
||||||
this.guardCheckType = guardCheckType;
|
this.guardCheckType = guardCheckType;
|
||||||
|
|
||||||
|
validatePart();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNext(CommandPart next) {
|
public void setNext(CommandPart next) {
|
||||||
@ -64,9 +60,26 @@ public class CommandPart {
|
|||||||
this.next = next;
|
this.next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validatePart() {
|
||||||
|
if (guardCheckType == GuardCheckType.TAB_COMPLETE) {
|
||||||
|
throw new IllegalArgumentException("Tab complete is not allowed as a guard check type!");
|
||||||
|
}
|
||||||
|
if (optional != null && varArgType != null) {
|
||||||
|
throw new IllegalArgumentException("A vararg part can't have an optional part!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optional != null) {
|
||||||
|
try {
|
||||||
|
typeMapper.map(null, EMPTY_ARRAY, optional);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalArgumentException("The optional part is not valid!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public 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;
|
throw new CommandParseException();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (varArgType != null) {
|
if (varArgType != null) {
|
||||||
@ -86,7 +99,7 @@ public class CommandPart {
|
|||||||
if (!validArgument.success && optional == null) {
|
if (!validArgument.success && optional == null) {
|
||||||
throw new CommandParseException();
|
throw new CommandParseException();
|
||||||
}
|
}
|
||||||
if (optional != null) {
|
if (!validArgument.success) {
|
||||||
current.add(typeMapper.map(commandSender, EMPTY_ARRAY, optional));
|
current.add(typeMapper.map(commandSender, EMPTY_ARRAY, optional));
|
||||||
if (next != null) {
|
if (next != null) {
|
||||||
next.generateArgumentArray(current, commandSender, args, startIndex);
|
next.generateArgumentArray(current, commandSender, args, startIndex);
|
||||||
|
@ -24,6 +24,9 @@ import org.bukkit.command.CommandSender;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface TypeMapper<T> {
|
public interface TypeMapper<T> {
|
||||||
|
/**
|
||||||
|
* The CommandSender can be null!
|
||||||
|
*/
|
||||||
default T map(CommandSender commandSender, String[] previousArguments, String s) {
|
default T map(CommandSender commandSender, String[] previousArguments, String s) {
|
||||||
return map(previousArguments, s);
|
return map(previousArguments, s);
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,8 @@ public class SimpleCommandPartTest {
|
|||||||
|
|
||||||
private CommandPart simpleGuardPart;
|
private CommandPart simpleGuardPart;
|
||||||
|
|
||||||
|
private CommandPart optionalCommandPart;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
|
stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
|
||||||
@ -49,6 +51,9 @@ public class SimpleCommandPartTest {
|
|||||||
varArgCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, String.class, null, GuardCheckType.COMMAND);
|
varArgCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, String.class, 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);
|
simpleGuardPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), (commandSender, guardCheckType, previousArguments, s) -> s.equals("hello") ? GuardResult.DENIED : GuardResult.ALLOWED, null, null, GuardCheckType.COMMAND);
|
||||||
|
|
||||||
|
optionalCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, "hello", GuardCheckType.COMMAND);
|
||||||
|
optionalCommandPart.setNext(new CommandPart(SWCommandUtils.createMapper("hello2", "world2"), null, null, null, GuardCheckType.COMMAND));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -187,4 +192,52 @@ public class SimpleCommandPartTest {
|
|||||||
boolean guardResult = simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"world"}, 0);
|
boolean guardResult = simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"world"}, 0);
|
||||||
assertThat(guardResult, is(true));
|
assertThat(guardResult, is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOptionalCommandPartTabComplete() {
|
||||||
|
List<String> tabCompletes = new ArrayList<>();
|
||||||
|
optionalCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{""}, 0);
|
||||||
|
assertThat(tabCompletes.size(), is(4));
|
||||||
|
assertThat(tabCompletes.get(0), is("hello"));
|
||||||
|
assertThat(tabCompletes.get(1), is("world"));
|
||||||
|
assertThat(tabCompletes.get(2), is("hello2"));
|
||||||
|
assertThat(tabCompletes.get(3), is("world2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOptionalCommandPartTabCompleteSecond() {
|
||||||
|
List<String> tabCompletes = new ArrayList<>();
|
||||||
|
optionalCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0);
|
||||||
|
assertThat(tabCompletes.size(), is(2));
|
||||||
|
assertThat(tabCompletes.get(0), is("hello2"));
|
||||||
|
assertThat(tabCompletes.get(1), is("world2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = CommandParseException.class)
|
||||||
|
public void testOptionalCommandPartExecution() {
|
||||||
|
optionalCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{""}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOptionalCommandPartExecutionValid() {
|
||||||
|
List<Object> argumentArray = new ArrayList<>();
|
||||||
|
optionalCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello2"}, 0);
|
||||||
|
assertThat(argumentArray.size(), is(2));
|
||||||
|
assertThat(argumentArray.get(0), is("hello"));
|
||||||
|
assertThat(argumentArray.get(1), is("hello2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = CommandParseException.class)
|
||||||
|
public void testOptionalCommandPartExecutionInvalid() {
|
||||||
|
optionalCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{"hello"}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOptionalCommandPartExecutionFullyValid() {
|
||||||
|
List<Object> argumentArray = new ArrayList<>();
|
||||||
|
optionalCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world", "hello2"}, 0);
|
||||||
|
assertThat(argumentArray.size(), is(2));
|
||||||
|
assertThat(argumentArray.get(0), is("world"));
|
||||||
|
assertThat(argumentArray.get(1), is("hello2"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren