Rollczi / LiteCommands

ā˜„ļø LiteCommands - Command framework for Velocity, Bukkit, Paper, BungeeCord, Minestom, Sponge, Fabric, JDA and future implementations.
https://docs.rollczi.dev/
Apache License 2.0
146 stars 21 forks source link

GH-427 Support async parse result. #435

Closed Rollczi closed 1 month ago

Rollczi commented 1 month ago

Resolve: #427 šŸŸ¢ Add alias for @Context annotation

@Command(name = "hello")
public class HelloCommand {
    @Execute
    void command(@Sender CommandSender sender, @Arg String name) {
        // ...
    }
}

šŸŸ¢ Add an option to create Annotation aliases like as @Sender You can implement your annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@RequirementDefinition(type = RequirementDefinition.Type.CONTEXT)
public @interface MySender {
}

And use it in your commands:

    @Execute
    void command(@MySender CommandSender sender, @Arg String name) {
        // ...
    }

šŸŸ¢ Support completableFuture/async parse/context result:

public class UserArgumentResolver extends ArgumentResolver<CommandSender, User> {

    private final Pattern VALID_USER_PATTERN = Pattern.compile("^[a-zA-Z0-9_]{3,16}$");
    private final UserService userService;

    public UserArgumentResolver(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected ParseResult<User> parse(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
        CompletableFuture<ParseResult<User>> userNotFound = userService.getUser(argument)
            .thenApply(user -> user != null ? ParseResult.success(user) : ParseResult.failure("User not found"));

        return ParseResult.completableFuture(userNotFound);
    }

    @Override
    public boolean match(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
        return VALID_USER_PATTERN.matcher(argument).matches();
    }
}

šŸŸ¢ when you call blocking methods you can use ParseResult.async()

    @Override
    protected ParseResult<User> parse(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
        return ParseResult.async(() -> userDatabase.getUser(argument));
    }

āš ļø When you return async/completableFuture result then you must also implement match method for correct suggestion validation

šŸŸ¢ See also same API for ContextResult

ContextResult.completableFuture()
ContextResult.async()

šŸ”“ Rmoved wrapper API

šŸ”“ Breaking changes (Internal API)

incompatible:

AbstractCollectorArgumentResolver
Meta.ARGUMENT_KEY
Parser#canParse
SchematicFastGenerator#isOptional
SimpleArgument#constructor
AnnotationHolder#getFormat
Requirement#getWrapperFormat
Argument#getWrapperFormat
BindRequirement.of(Supplier, WrapFormat) -> BindRequirement.of(Supplier, TypeToken)
ContextRequirement.of(Supplier, WrapFormat) -> ContextRequirement.of(Supplier, TypeToken)

relocation:

litecommands.annotations.argument.collector -> [...]litecommands.annotations.argument.collection
litecommands.requirement.BindRequirement -> litecommands.bind.BindRequirement
litecommands.requirement.ContextRequirement -> litecommands.context.ContextRequirement

CollectionArgument -> CollectionArgumentProfile
JoinArgument -> JoinProfile
FlagArgument -> FlagProfile

removed:

WrapperRegistry, Wrap, WrapFormat, ValueToWrap
Wrapper, CompletableFutureWrapper, OptionaWrapper, ValueWrapper
TypedArgumentResolver, TypedParser, TypedSuggester
LiteCommandsBuilder#wrapper()
RequirementProcessor
AnnotationHolder