diff --git a/README.md b/README.md index 4ce323f..b766e18 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,11 @@ enabling compatibility with third-party plugins that support Vault. You can create a [Point of Sale](https://github.com/cloudnode-pro/BankAccounts/wiki/POS), which is a type of single-use chest shop. Players can pay using a bank card (`/bank card`). +### PlaceholderAPI Support + +BankAccounts provides several *PlaceholderAPI* placeholders that you can use. See +the [Placeholders Wiki](https://github.com/cloudnode-pro/BankAccounts/wiki/Placeholders) for an exhaustive list. + ### Extensive Configuration All functionality is fully configurable. See [default config](https://github.com/cloudnode-pro/BankAccounts/blob/master/src/main/resources/config.yml). diff --git a/src/main/java/pro/cloudnode/smp/bankaccounts/integrations/PAPIIntegration.java b/src/main/java/pro/cloudnode/smp/bankaccounts/integrations/PAPIIntegration.java index ed6fe06..d785d09 100644 --- a/src/main/java/pro/cloudnode/smp/bankaccounts/integrations/PAPIIntegration.java +++ b/src/main/java/pro/cloudnode/smp/bankaccounts/integrations/PAPIIntegration.java @@ -6,6 +6,10 @@ import pro.cloudnode.smp.bankaccounts.Account; import pro.cloudnode.smp.bankaccounts.BankAccounts; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.stream.Collectors; + public final class PAPIIntegration extends PlaceholderExpansion { @Override public @NotNull String getIdentifier() { @@ -22,29 +26,60 @@ public final class PAPIIntegration extends PlaceholderExpansion { return BankAccounts.getInstance().getPluginMeta().getVersion(); } - /** - * Adds the following placeholders: - * - */ @Override public String onRequest(final @NotNull OfflinePlayer player, final @NotNull String params) { final @NotNull String[] args = params.split("_"); if (args.length < 1) return null; return switch (args[0]) { - case "balance" -> args.length < 2 ? null : switch (args[1]) { - case "formatted" -> args.length != 3 ? null : Account.get(Account.Tag.from(args[2])).map(value -> BankAccounts.formatCurrency(value.balance)).orElse(null); - default -> Account.get(Account.Tag.from(args[1])).map(value -> String.valueOf(value.balance)).orElse(null); - }; - case "owner" -> args.length < 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.owner.getName()).orElse(null); - case "type" -> args.length < 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.type.getName()).orElse(null); - case "name" -> args.length < 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.name).orElse(null); + case "balance" -> { + if (args.length == 1) + yield String.valueOf( + Arrays.stream(Account.get(player)) + .map(account -> account.balance) + .reduce(BigDecimal.ZERO, BigDecimal::add) + ); + yield switch (args[1]) { + case "formatted" -> { + if (args.length == 3) + yield Account.get(Account.Tag.from(args[2])).map(value -> BankAccounts.formatCurrency(value.balance)).orElse(null); + if (args.length == 2) + yield BankAccounts.formatCurrency( + Arrays.stream(Account.get(player)) + .map(account -> account.balance) + .reduce(BigDecimal.ZERO, BigDecimal::add) + ); + yield null; + } + case "short" -> { + if (args.length == 3) + yield Account.get(Account.Tag.from(args[2])).map(value -> BankAccounts.formatCurrencyShort(value.balance)).orElse(null); + if (args.length == 2) + yield BankAccounts.formatCurrencyShort( + Arrays.stream(Account.get(player)) + .map(account -> account.balance) + .reduce(BigDecimal.ZERO, BigDecimal::add) + ); + yield null; + } + default -> Account.get(Account.Tag.from(args[1])).map(value -> String.valueOf(value.balance)).orElse(null); + }; + } + case "owner" -> args.length != 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.owner.getName()).orElse(null); + case "type" -> args.length != 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.type.getName()).orElse(null); + case "name" -> args.length != 2 ? null : Account.get(Account.Tag.from(args[1])).map(Account::name).orElse(null); + case "account" -> { + if (args.length == 2) { + final @NotNull Account @NotNull [] accounts = Account.get(player); + yield switch (args[1]) { + case "list" -> Arrays.stream(accounts).map(account -> account.id).collect(Collectors.joining(", ")); + case "names" -> Arrays.stream(accounts).map(Account::name).collect(Collectors.joining(", ")); + case "count" -> String.valueOf(accounts.length); + default -> null; + }; + } + yield null; + } default -> null; }; }