Skip to content

Commit 089605a

Browse files
authored
Add placeholders for total balance and list of accounts (#172)
2 parents e3f49c5 + 14c4635 commit 089605a

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ enabling compatibility with third-party plugins that support Vault.
3333

3434
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`).
3535

36+
### PlaceholderAPI Support
37+
38+
BankAccounts provides several *PlaceholderAPI* placeholders that you can use. See
39+
the [Placeholders Wiki](https://github.com/cloudnode-pro/BankAccounts/wiki/Placeholders) for an exhaustive list.
40+
3641
### Extensive Configuration
3742

3843
All functionality is fully configurable. See [default config](https://github.com/cloudnode-pro/BankAccounts/blob/master/src/main/resources/config.yml).

src/main/java/pro/cloudnode/smp/bankaccounts/integrations/PAPIIntegration.java

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import pro.cloudnode.smp.bankaccounts.Account;
77
import pro.cloudnode.smp.bankaccounts.BankAccounts;
88

9+
import java.math.BigDecimal;
10+
import java.util.Arrays;
11+
import java.util.stream.Collectors;
12+
913
public final class PAPIIntegration extends PlaceholderExpansion {
1014
@Override
1115
public @NotNull String getIdentifier() {
@@ -22,29 +26,60 @@ public final class PAPIIntegration extends PlaceholderExpansion {
2226
return BankAccounts.getInstance().getPluginMeta().getVersion();
2327
}
2428

25-
/**
26-
* Adds the following placeholders:
27-
* <ul>
28-
* <li>%bankaccounts_balance_&lt;accountID&gt;% - returns balance of account with specified ID</li>
29-
* <li>%bankaccounts_balance_formatted_&lt;accountID&gt;% - returns formatted balance of account with specified ID</li>
30-
* <li>%bankaccounts_owner_&lt;accountID&gt;% - returns name of the owner of account with specified ID</li>
31-
* <li>%bankaccounts_type_&lt;accountID&gt;% - returns type of account with specified ID</li>
32-
* <li>%bankaccounts_name_&lt;accountID&gt;% - returns name of account with specified ID</li>
33-
* </ul>
34-
*/
3529
@Override
3630
public String onRequest(final @NotNull OfflinePlayer player, final @NotNull String params) {
3731
final @NotNull String[] args = params.split("_");
3832

3933
if (args.length < 1) return null;
4034
return switch (args[0]) {
41-
case "balance" -> args.length < 2 ? null : switch (args[1]) {
42-
case "formatted" -> args.length != 3 ? null : Account.get(Account.Tag.from(args[2])).map(value -> BankAccounts.formatCurrency(value.balance)).orElse(null);
43-
default -> Account.get(Account.Tag.from(args[1])).map(value -> String.valueOf(value.balance)).orElse(null);
44-
};
45-
case "owner" -> args.length < 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.owner.getName()).orElse(null);
46-
case "type" -> args.length < 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.type.getName()).orElse(null);
47-
case "name" -> args.length < 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.name).orElse(null);
35+
case "balance" -> {
36+
if (args.length == 1)
37+
yield String.valueOf(
38+
Arrays.stream(Account.get(player))
39+
.map(account -> account.balance)
40+
.reduce(BigDecimal.ZERO, BigDecimal::add)
41+
);
42+
yield switch (args[1]) {
43+
case "formatted" -> {
44+
if (args.length == 3)
45+
yield Account.get(Account.Tag.from(args[2])).map(value -> BankAccounts.formatCurrency(value.balance)).orElse(null);
46+
if (args.length == 2)
47+
yield BankAccounts.formatCurrency(
48+
Arrays.stream(Account.get(player))
49+
.map(account -> account.balance)
50+
.reduce(BigDecimal.ZERO, BigDecimal::add)
51+
);
52+
yield null;
53+
}
54+
case "short" -> {
55+
if (args.length == 3)
56+
yield Account.get(Account.Tag.from(args[2])).map(value -> BankAccounts.formatCurrencyShort(value.balance)).orElse(null);
57+
if (args.length == 2)
58+
yield BankAccounts.formatCurrencyShort(
59+
Arrays.stream(Account.get(player))
60+
.map(account -> account.balance)
61+
.reduce(BigDecimal.ZERO, BigDecimal::add)
62+
);
63+
yield null;
64+
}
65+
default -> Account.get(Account.Tag.from(args[1])).map(value -> String.valueOf(value.balance)).orElse(null);
66+
};
67+
}
68+
case "owner" -> args.length != 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.owner.getName()).orElse(null);
69+
case "type" -> args.length != 2 ? null : Account.get(Account.Tag.from(args[1])).map(value -> value.type.getName()).orElse(null);
70+
case "name" -> args.length != 2 ? null : Account.get(Account.Tag.from(args[1])).map(Account::name).orElse(null);
71+
case "account" -> {
72+
if (args.length == 2) {
73+
final @NotNull Account @NotNull [] accounts = Account.get(player);
74+
yield switch (args[1]) {
75+
case "list" -> Arrays.stream(accounts).map(account -> account.id).collect(Collectors.joining(", "));
76+
case "names" -> Arrays.stream(accounts).map(Account::name).collect(Collectors.joining(", "));
77+
case "count" -> String.valueOf(accounts.length);
78+
default -> null;
79+
};
80+
}
81+
yield null;
82+
}
4883
default -> null;
4984
};
5085
}

0 commit comments

Comments
 (0)