Creating Parameter Types
Parameter types are an important part of the command library. It uses the interface ParameterType<?, CommandSender>. The reason for the second type being CommandSender is because it's being used by other modules in this library (such as in the JDA module).
Creating a parameter type
First, you must have a class to use. I will use an example User and UserHandler class as here:
@Data // lombok, provides getters and setters
public class User {
private final int id;
private String username;
private final UUID uuid;
}
public class UserHandler {
@Getter // lombok, provides a getter for `users`.
private final List<User> users = new ArrayList<>();
// ...
@Nullable
public void getUser(String username) {
return this.users.stream().filter((u) -> u.getUsername() == username).findFirst().orElse(null);
}
// ...
}So, first of all, create a new class and call it whatever you'd like. I recommend something that gives people notice of what it is, for example "UserParameter" or "UserParameterType". And implement the ParameterType interface into your class and implement the required methods.
This is how it should look like:
This is the required implementation of the interface. You may also add a tab completion by implementing the ParameterType#completer method. This is how you can use it.
You can also do this more complex by using the Stream#map method.
Registering the parameter type
To register a parameter type is basically the same as registering a command. Please register the parameter types before registering the commands.
With the User class example I showed earlier, I will register it using the CommandHandler#registerParameter method. This method is also static, giving you direct access to the method.
The method requires two arguments. The first one is a Class<?> which is the class you want to register. The second one is the class where you implemented the ParameterType<?, CommandSender> interface.
Here's an example of registering the parameter type:
Now you may use the registered class in your parameters as an argument for your command.
Last updated