Skip to content

Instantly share code, notes, and snippets.

@Eric-liucn
Created August 9, 2021 17:55
Show Gist options
  • Save Eric-liucn/671c8552e90c3129ba48b55aa95b2d38 to your computer and use it in GitHub Desktop.
Save Eric-liucn/671c8552e90c3129ba48b55aa95b2d38 to your computer and use it in GitHub Desktop.
final Parameter.Key<CraftCurrency> currencyKey = Parameter.key("currency", CraftCurrency.class);
final Parameter.Value<CraftCurrency> currencyPara = Parameter.builder(CraftCurrency.class)
.key(currencyKey)
.addParser(new CraftCurrencyParser())
.completer(new CraftCurrencyParser.CraftCurrencyCompleter())
.build();
final Command.Parameterized bal = Command.builder()
.addParameter(userPara)
.addParameter(currencyPara)
.executor(context -> {
CraftEcoService service = Main.instance.getCraftEcoService();
Audience audience = (Audience) context.cause().root();
service.findOrCreateAccount(context.requireOne(userPara)).ifPresent(acc -> {
audience.sendMessage(Util.toComponent(acc.balance(context.one(currencyKey).orElse(((CraftCurrency) service.defaultCurrency()))).toString()));
});
return CommandResult.success();
})
.build();
---------------------------------------------------------------------------------
class CraftCurrencyParser implements ValueParser<CraftCurrency> {
@Override
public Optional<? extends CraftCurrency> parseValue(Parameter.Key<? super CraftCurrency> parameterKey, ArgumentReader.Mutable reader, CommandContext.Builder context) throws ArgumentParseException {
String currencyName = reader.peekString();
for (CraftCurrency currency : ConfigLoader.instance.getConfig().currencies) {
if (Util.toPlain(currency.displayName()).equalsIgnoreCase(currencyName)){
return Optional.of(currency);
}
}
return Optional.empty();
}
public static class CraftCurrencyCompleter implements ValueCompleter {
@Override
public List<CommandCompletion> complete(CommandContext context, String currentInput) {
return ConfigLoader.instance.getConfig().currencies.stream()
.map(cur -> CommandCompletion.of(Util.toPlain(cur.displayName())))
.collect(Collectors.toList());
}
}
}
---------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment