feat(core): add repeatable flags (#378)

implements #209.
This commit is contained in:
Alexander Söderberg 2022-06-14 17:21:51 +02:00 committed by Jason
parent d3864414aa
commit ec535dad7f
9 changed files with 459 additions and 80 deletions

View file

@ -28,6 +28,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.BiFunction;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
@ -94,4 +95,13 @@ public @interface Flag {
* @since 1.6.0
*/
@NonNull String permission() default "";
/**
* Whether the flag can be repeated.
*
* @return whether the flag can be repeated
* @since 1.7.0
*/
@API(status = API.Status.STABLE, since = "1.7.0")
boolean repeatable() default false;
}

View file

@ -30,6 +30,7 @@ import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.arguments.parser.ParserRegistry;
import cloud.commandframework.permission.Permission;
import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@ -65,15 +66,34 @@ final class FlagExtractor implements Function<@NonNull Method, Collection<@NonNu
}
final Flag flag = parameter.getAnnotation(Flag.class);
final String flagName = this.annotationParser.processString(flag.value());
final CommandFlag.Builder<Void> builder = this.commandManager
CommandFlag.Builder<Void> builder = this.commandManager
.flagBuilder(this.annotationParser.processString(flagName))
.withDescription(ArgumentDescription.of(this.annotationParser.processString(flag.description())))
.withAliases(this.annotationParser.processStrings(flag.aliases()))
.withPermission(Permission.of(this.annotationParser.processString(flag.permission())));
if (flag.repeatable()) {
builder = builder.asRepeatable();
}
if (parameter.getType().equals(boolean.class)) {
flags.add(builder.build());
} else {
final TypeToken<?> token = TypeToken.get(parameter.getType());
final TypeToken<?> token;
if (flag.repeatable() && Collection.class.isAssignableFrom(parameter.getType())) {
token = TypeToken.get(GenericTypeReflector.getTypeParameter(
parameter.getParameterizedType(),
Collection.class.getTypeParameters()[0]
));
} else {
token = TypeToken.get(parameter.getType());
}
if (token.equals(TypeToken.get(boolean.class))) {
flags.add(builder.build());
continue;
}
final Collection<Annotation> annotations = Arrays.asList(parameter.getAnnotations());
final ParserRegistry<?> registry = this.commandManager.parserRegistry();
final ArgumentParser<?, ?> parser;

View file

@ -126,8 +126,10 @@ public class MethodCommandExecutionHandler<C> implements CommandExecutionHandler
} else if (parameter.isAnnotationPresent(Flag.class)) {
final Flag flag = parameter.getAnnotation(Flag.class);
final String flagName = this.annotationParser.processString(flag.value());
if (parameter.getType() == boolean.class) {
if (parameter.getType().equals(boolean.class)) {
arguments.add(flagContext.isPresent(flagName));
} else if (flag.repeatable() && parameter.getType().isAssignableFrom(List.class)) {
arguments.add(flagContext.getAll(flagName));
} else {
arguments.add(flagContext.getValue(flagName, null));
}