-
Notifications
You must be signed in to change notification settings - Fork 3
Command API: Custom parameter types
Lorenzo Rutayisire edited this page Jun 13, 2020
·
1 revision
Uppercore by itself can parse the main parameter types such as Java primitives and some Bukkit utility.
If you want Uppercore's command-api to be able to parse a custom class you need to register your own ParameterHandler.
A ParameterHandler is composed of a Parser and a Prompter. The parser is the function in charge of translating the stringy arguments into your Object instance. The prompter suggests and tab-complete the parameter while the player is entering it.
Registering a new type is simple:
ParameterHandler.register(
// The classes to use this ParameterHandler for.
Collections.singletonList(QuakeArena.class),
// Parser
args -> {
String arg = args.take();
QuakeArena arena = (QuakeArena) Quake.get().getArenaManager().get(arg);
if (arena == null) {
throw args.areWrong(); // The inserted arg doesn't correspond to any arena.
}
return arena;
},
// Prompter
args -> {
if (args.remaining() > 1) // This parameter has already been skipped, what do we suggest?
return Collections.emptyList();
String arg = args.take();
// Suggests all the arenas whose name starts with arg.
return Quake.get().getArenaManager().getArenas()
.stream()
.map(Arena::getName)
.filter(name -> StringUtil.startsWithIgnoreCase(name, arg))
.collect(Collectors.toList());
});Done! ParameterHandler#register should be called when the plugin starts up, soon after Uppercore#hook.