Update some stuff to strings

Dieser Commit ist enthalten in:
yoyosource 2022-11-12 15:19:36 +01:00
Ursprung 0c68dfd19d
Commit d4f9246f68
4 geänderte Dateien mit 68 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -67,17 +67,54 @@ public abstract class AbstractSWCommand<T> {
protected void sendMessage(T sender, String message, Object[] args) {} protected void sendMessage(T sender, String message, Object[] args) {}
private String[] quote(String[] args) {
List<String> list = new ArrayList<>();
StringBuilder builder = new StringBuilder();
boolean quote = false;
for (String arg : args) {
if (arg.startsWith("\"") && arg.endsWith("\"")) {
list.add(arg);
continue;
}
if (arg.startsWith("\"")) {
quote = true;
builder.append(arg);
continue;
}
if (arg.endsWith("\"")) {
quote = false;
builder.append(" ").append(arg);
list.add(builder.toString());
builder = new StringBuilder();
continue;
}
if (quote) {
builder.append(" ").append(arg);
continue;
}
list.add(arg);
}
if (quote) {
commandSystemWarning(() -> "Missing closing quote in command");
}
if (builder.length() > 0) {
list.add(builder.toString());
}
return list.toArray(new String[0]);
}
protected final void execute(T sender, String alias, String[] args) { protected final void execute(T sender, String alias, String[] args) {
initialize(); initialize();
String[] finalArgs = quote(args);
List<Runnable> errors = new ArrayList<>(); List<Runnable> errors = new ArrayList<>();
try { try {
if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, args))) { if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, finalArgs))) {
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
errors.forEach(Runnable::run); errors.forEach(Runnable::run);
return; return;
} }
commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> { commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> {
}, sender, alias, args)); }, sender, alias, finalArgs));
} }
} catch (CommandNoHelpException e) { } catch (CommandNoHelpException e) {
// Ignored // Ignored
@ -89,10 +126,11 @@ public abstract class AbstractSWCommand<T> {
protected final List<String> tabComplete(T sender, String alias, String[] args) throws IllegalArgumentException { protected final List<String> tabComplete(T sender, String alias, String[] args) throws IllegalArgumentException {
initialize(); initialize();
String[] finalArgs = quote(args);
String string = args[args.length - 1].toLowerCase(); String string = args[args.length - 1].toLowerCase();
return commandList.stream() return commandList.stream()
.filter(s -> !s.noTabComplete) .filter(s -> !s.noTabComplete)
.map(s -> s.tabComplete(sender, args)) .map(s -> s.tabComplete(sender, finalArgs))
.filter(Objects::nonNull) .filter(Objects::nonNull)
.flatMap(Collection::stream) .flatMap(Collection::stream)
.filter(s -> !s.isEmpty()) .filter(s -> !s.isEmpty())
@ -371,4 +409,12 @@ public abstract class AbstractSWCommand<T> {
@Target({ElementType.PARAMETER}) @Target({ElementType.PARAMETER})
protected @interface AllowNull { protected @interface AllowNull {
} }
/**
* This annotation is used to mark a String to be quotable with multiple words.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
protected @interface Quotable {
}
} }

Datei anzeigen

@ -56,6 +56,9 @@ class CommandPart<T> {
@Setter @Setter
private boolean allowNullValues = false; private boolean allowNullValues = false;
@Setter
private boolean quotable = false;
private Parameter parameter; private Parameter parameter;
private int parameterIndex; private int parameterIndex;
@ -183,6 +186,9 @@ class CommandPart<T> {
} catch (Exception e) { } catch (Exception e) {
return new CheckArgumentResult(false, null); return new CheckArgumentResult(false, null);
} }
if (value instanceof String && !quotable && ((String) value).contains(" ")) {
return new CheckArgumentResult(false, null);
}
if (validator != null && errors != null) { if (validator != null && errors != null) {
try { try {
if (!validator.validate(sender, value, (s, objects) -> { if (!validator.validate(sender, value, (s, objects) -> {

Datei anzeigen

@ -62,7 +62,15 @@ public class SWCommandUtils {
addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble))); addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble)));
addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt))); addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt)));
addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong))); addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong)));
MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList)); MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> {
if (s.startsWith("\"") && s.endsWith("\"")) {
return s.substring(1, s.length() - 1);
}
if (s.startsWith("'") && s.endsWith("'")) {
return s.substring(1, s.length() - 1);
}
return s;
}, Collections::singletonList));
} }
public static <T extends AbstractTypeMapper<K, V>, K, V> void init(SWTypeMapperCreator<T, K, V> swTypeMapperCreator) { public static <T extends AbstractTypeMapper<K, V>, K, V> void init(SWTypeMapperCreator<T, K, V> swTypeMapperCreator) {
@ -95,10 +103,12 @@ public class SWCommandUtils {
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null; Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class); AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class);
AbstractSWCommand.AllowNull allowNull = parameter.getAnnotation(AbstractSWCommand.AllowNull.class); AbstractSWCommand.AllowNull allowNull = parameter.getAnnotation(AbstractSWCommand.AllowNull.class);
AbstractSWCommand.Quotable quotable = parameter.getAnnotation(AbstractSWCommand.Quotable.class);
CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, varArgType, optionalValue != null ? optionalValue.value() : null, parameter, i); CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, varArgType, optionalValue != null ? optionalValue.value() : null, parameter, i);
commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG()); commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG());
commandPart.setAllowNullValues(allowNull != null); commandPart.setAllowNullValues(allowNull != null);
commandPart.setQuotable(quotable != null);
if (current != null) { if (current != null) {
current.setNext(commandPart); current.setNext(commandPart);
} }

Datei anzeigen

@ -97,6 +97,8 @@ public class ArgumentCommandTest {
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with String"); assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with String");
} }
cmd.execute("test", "", new String[]{"\"Hello World\""});
cmd.execute("test", "", new String[]{"\"Hello", "World\""});
} }
@Test @Test