Custom command argument parser

create your own command argument parser.

Argument parser creation as below:

@ELDPlugin(
        registry = TesterRegistry.class,
        lifeCycle = TesterLifeCycle.class
)
public class ELDTester extends ELDBukkitPlugin {

    @Override
    protected void bindServices(ServiceCollection serviceCollection) {
    }

    @Override
    protected void manageProvider(ManagerProvider provider) {
        var parser = provider.getArgumentManager(); //argument parser
        // create new class parser
        parser.registerParser(Integer.class, (iterator, commandSender, argParser) -> {
            try{
                return Integer.parseInt(iterator.next());
            }catch (NumberFormatException e){
                throw new ArgumentParseException("not a valid integer."); 
            }
        });
    }
}

throwing an ArgumentParseException will not throw an error to console. It will just stop executing command and notify to command sender.

The code showed above is for creating Integer class parser which use for parsing integer argument, like below:

You can mark an identifier to create multiple parser with same class.

Example as below:

when you input /say this is a text , message argument will be "this is a text"

Using parser inside argument parser creation

when you create anargument parser like org.bukkit.Location , you will find that x y z is a double class,then you can use the parser inside your creation.

you can also inject ArgParserService into your injectable instance.

最后更新于