Skip to content

Commit b1d3819

Browse files
authored
Add Vault support (#113)
2 parents 8c58cbb + 7fd1df5 commit b1d3819

File tree

11 files changed

+503
-64
lines changed

11 files changed

+503
-64
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ See the history of your account’s transactions using the `/bank history` comma
2020
### Payment Requests (Invoices)
2121
Request money from players and track the payment status.
2222

23+
### Vault Support
24+
25+
If [Vault](https://github.com/MilkBowl/Vault/releases/latest) is installed on your server,
26+
you can [enable its integration in the configuration](https://github.com/cloudnode-pro/BankAccounts/blob/dad253525b6bc3ee9647cd01c75e2c425a921f58/src/main/resources/config.yml#L38-L44).
27+
This allows BankAccounts to function as a Vault economy provider,
28+
enabling compatibility with third-party plugins that support Vault.
29+
2330
### POS and Bank Cards
2431

2532
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`).

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
<id>placeholderapi</id>
6666
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
6767
</repository>
68+
<repository>
69+
<id>jitpack.io</id>
70+
<url>https://jitpack.io</url>
71+
</repository>
6872
</repositories>
6973

7074
<dependencies>
@@ -90,5 +94,11 @@
9094
<version>2.11.6</version>
9195
<scope>provided</scope>
9296
</dependency>
97+
<dependency>
98+
<groupId>com.github.MilkBowl</groupId>
99+
<artifactId>VaultAPI</artifactId>
100+
<version>1.7</version>
101+
<scope>provided</scope>
102+
</dependency>
93103
</dependencies>
94104
</project>

src/main/java/pro/cloudnode/smp/bankaccounts/Account.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Account(final @NotNull ResultSet rs) throws @NotNull SQLException {
106106
}
107107

108108
public final @NotNull String name() {
109-
return this.name == null ? (this.type == Type.PERSONAL && this.owner.getName() != null ? this.owner.getName() : this.id) : this.name;
109+
return this.name == null ? (this.type == Type.VAULT && this.owner.getName() != null ? this.owner.getName() : this.id) : this.name;
110110
}
111111

112112
public final @NotNull Component ownerName() {
@@ -267,6 +267,12 @@ public static boolean isInstrument(final @NotNull ItemStack item) {
267267
}
268268
}
269269

270+
public static @NotNull Optional<@NotNull Account> getVaultAccount(final @NotNull OfflinePlayer player) {
271+
final @NotNull Account @NotNull [] accounts = get(player, Type.VAULT);
272+
if (accounts.length == 0) return Optional.empty();
273+
return Optional.of(accounts[0]);
274+
}
275+
270276
/**
271277
* Get accounts sorted by balance
272278
*
@@ -307,7 +313,6 @@ else if (limit != null) {
307313
* Get the server account
308314
*/
309315
public static @NotNull Optional<@NotNull Account> getServerAccount() {
310-
if (!BankAccounts.getInstance().config().serverAccountEnabled()) return Optional.empty();
311316
final @NotNull Account @NotNull [] accounts = get(BankAccounts.getConsoleOfflinePlayer());
312317
if (accounts.length == 0) return Optional.empty();
313318
return Optional.of(accounts[0]);
@@ -377,7 +382,11 @@ public enum Type {
377382
/**
378383
* Account owned by a company or other corporate entity
379384
*/
380-
BUSINESS;
385+
BUSINESS,
386+
/**
387+
* Vault integration account
388+
*/
389+
VAULT;
381390

382391
/**
383392
* Get type name (as set in config)

src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import pro.cloudnode.smp.bankaccounts.events.Join;
2424
import pro.cloudnode.smp.bankaccounts.events.PlayerInteract;
2525
import pro.cloudnode.smp.bankaccounts.integrations.PAPIIntegration;
26+
import pro.cloudnode.smp.bankaccounts.integrations.VaultIntegration;
2627

2728
import java.io.IOException;
2829
import java.io.InputStream;
@@ -97,6 +98,8 @@ public void onEnable() {
9798
} else {
9899
getLogger().log(Level.INFO, "PlaceholderAPI not found. Skipping integration.");
99100
}
101+
102+
VaultIntegration.setup();
100103
}
101104

102105
@Override
@@ -144,8 +147,11 @@ private void setupDbSource() {
144147
* Reload plugin
145148
*/
146149
public static void reload() {
150+
final boolean vaultConfigEnabled = getInstance().config().integrationsVaultEnabled();
147151
getInstance().reloadConfig();
148152
getInstance().config.config = getInstance().getConfig();
153+
if (vaultConfigEnabled != getInstance().config().integrationsVaultEnabled())
154+
getInstance().getLogger().warning("Vault integration has been " + (getInstance().config().integrationsVaultEnabled() ? "enabled" : "disabled") + " in the configuration. To activate this change, please restart the server.");
149155
getInstance().setupDbSource();
150156
getInstance().initDbWrapper();
151157
createServerAccount();
@@ -323,14 +329,12 @@ public static String formatCurrencyShort(final @Nullable BigDecimal amount) {
323329
* Create server account, if enabled in config
324330
*/
325331
private static void createServerAccount() {
326-
if (getInstance().config().serverAccountEnabled()) {
327-
final @NotNull Account[] accounts = Account.get(getConsoleOfflinePlayer());
328-
if (accounts.length > 0) return;
329-
final @Nullable String name = getInstance().config().serverAccountName();
330-
final @NotNull Account.Type type = getInstance().config().serverAccountType();
331-
final @Nullable BigDecimal balance = getInstance().config().serverAccountStartingBalance();
332-
new Account(getConsoleOfflinePlayer(), type, name, balance, false).insert();
333-
}
332+
final @NotNull Account @NotNull [] accounts = Account.get(getConsoleOfflinePlayer());
333+
if (accounts.length > 0) return;
334+
final @Nullable String name = getInstance().config().serverAccountName();
335+
final @NotNull Account.Type type = getInstance().config().serverAccountType();
336+
final @Nullable BigDecimal balance = getInstance().config().serverAccountStartingBalance();
337+
new Account(getConsoleOfflinePlayer(), type, name, balance, false).insert();
334338
}
335339

336340
/**

src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ public boolean dbMaintainTimeStats() {
108108
return config.getBoolean("db.maintainTimeStats");
109109
}
110110

111+
// integrations.vault.enabled
112+
public boolean integrationsVaultEnabled() {
113+
return config.getBoolean("integrations.vault.enabled");
114+
}
115+
116+
// integrations.vault.description
117+
public @NotNull String integrationsVaultDescription() {
118+
return Objects.requireNonNull(config.getString("integrations.vault.description"));
119+
}
120+
111121
// currency.symbol
112122
public @NotNull String currencySymbol() {
113123
return Objects.requireNonNull(config.getString("currency.symbol"));
@@ -119,20 +129,10 @@ public boolean dbMaintainTimeStats() {
119129
}
120130

121131
// starting-balance
122-
public @NotNull Optional<@NotNull Double> startingBalance() {
132+
public @NotNull BigDecimal startingBalance() {
123133
if (Objects.requireNonNull(config.getString("starting-balance")).equalsIgnoreCase("null"))
124-
return Optional.empty();
125-
else return Optional.of(config.getDouble("starting-balance"));
126-
}
127-
128-
// prevent-close-last-personal
129-
public boolean preventCloseLastPersonal() {
130-
return config.getBoolean("prevent-close-last-personal");
131-
}
132-
133-
// server-account.enabled
134-
public boolean serverAccountEnabled() {
135-
return config.getBoolean("server-account.enabled");
134+
return BigDecimal.ZERO;
135+
else return new BigDecimal(Objects.requireNonNull(config.getString("starting-balance")));
136136
}
137137

138138
// server-account.name
@@ -466,9 +466,9 @@ public int invoicePerPage() {
466466
);
467467
}
468468

469-
// messages.errors.rename-personal
470-
public @NotNull Component messagesErrorsRenamePersonal() {
471-
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.rename-personal")));
469+
// messages.errors.rename-vault-account
470+
public @NotNull Component messagesErrorsRenameVaultAccount() {
471+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.rename-vault-account")));
472472
}
473473

474474
// messages.errors.not-account-owner
@@ -546,11 +546,6 @@ public int invoicePerPage() {
546546
);
547547
}
548548

549-
// messages.errors.closing-personal
550-
public @NotNull Component messagesErrorsClosingPersonal() {
551-
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.closing-personal")));
552-
}
553-
554549
// messages.errors.player-only
555550
public @NotNull Component messagesErrorsPlayerOnly() {
556551
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.player-only")));
@@ -699,6 +694,11 @@ public int invoicePerPage() {
699694
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.async-failed")));
700695
}
701696

697+
// messages.errors.delete-vault-account
698+
public @NotNull Component messagesErrorsDeleteVaultAccount() {
699+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.delete-vault-account")));
700+
}
701+
702702
// messages.balance
703703
public @NotNull Component messagesBalance(final @NotNull Account account) {
704704
return MiniMessage.miniMessage().deserialize(

src/main/java/pro/cloudnode/smp/bankaccounts/Permissions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ public final class Permissions {
3434
public static @NotNull String INSTRUMENT_CREATE_BYPASS = "bank.instrument.create.bypass";
3535
public static @NotNull String SET_BALANCE = "bank.set.balance";
3636
public static @NotNull String SET_NAME_OTHER = "bank.set.name.other";
37-
public static @NotNull String SET_NAME_PERSONAL = "bank.set.name.personal";
37+
public static @NotNull String SET_NAME_VAULT = "bank.set.name.vault";
3838
public static @NotNull String FREEZE_OTHER = "bank.freeze.other";
3939
public static @NotNull String DELETE_OTHER = "bank.delete.other";
40-
public static @NotNull String DELETE_PERSONAL = "bank.delete.personal";
4140
public static @NotNull String POS_CREATE_OTHER = "bank.pos.create.other";
4241
public static @NotNull String POS_CREATE_PERSONAL = "bank.pos.create.personal";
4342
public static @NotNull String POS_USE_OTHER = "bank.pos.use.other";

src/main/java/pro/cloudnode/smp/bankaccounts/commands/BankCommand.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ else if (args.length == 4 && args[2].equals("--player") && sender.hasPermission(
106106
if (!sender.hasPermission(Permissions.DELETE)) return suggestions;
107107
if (args.length == 2) suggestions.addAll(Arrays
108108
.stream(sender.hasPermission(Permissions.DELETE_OTHER) ? Account.get() : Account.get(BankAccounts.getOfflinePlayer(sender)))
109-
.filter(account -> sender.hasPermission(Permissions.DELETE_PERSONAL) || account.type != Account.Type.PERSONAL)
110109
.map(account -> account.id).collect(Collectors.toSet()));
111110
}
112111
case "transfer", "send", "pay" -> {
@@ -377,8 +376,8 @@ public static boolean setName(final @NotNull CommandSender sender, final @NotNul
377376
if (!sender.hasPermission(Permissions.SET_NAME_OTHER) && !account.get().owner.getUniqueId()
378377
.equals(BankAccounts.getOfflinePlayer(sender).getUniqueId()))
379378
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNotAccountOwner());
380-
if (!sender.hasPermission(Permissions.SET_NAME_PERSONAL) && account.get().type == Account.Type.PERSONAL)
381-
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsRenamePersonal());
379+
if (!sender.hasPermission(Permissions.SET_NAME_VAULT) && account.get().type == Account.Type.VAULT)
380+
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsRenameVaultAccount());
382381
@Nullable String name = String.join(" ", Arrays.copyOfRange(args, 1, args.length)).trim();
383382
name = name.length() > 32 ? name.substring(0, 32) : name;
384383
name = name.isEmpty() ? null : name;
@@ -433,14 +432,13 @@ public static boolean delete(final @NotNull CommandSender sender, final @NotNull
433432
if (!sender.hasPermission(Permissions.DELETE_OTHER) && !account.get().owner.getUniqueId()
434433
.equals(BankAccounts.getOfflinePlayer(sender).getUniqueId()))
435434
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsNotAccountOwner());
435+
if (account.get().type == Account.Type.VAULT)
436+
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsDeleteVaultAccount());
436437
final @NotNull Optional<@NotNull BigDecimal> balance = Optional.ofNullable(account.get().balance);
437438
if (balance.isPresent() && balance.get().compareTo(BigDecimal.ZERO) != 0)
438439
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsClosingBalance(account.get()));
439440
if (account.get().frozen)
440441
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsFrozen(account.get()));
441-
if (BankAccounts.getInstance().config()
442-
.preventCloseLastPersonal() && account.get().type == Account.Type.PERSONAL && !sender.hasPermission(Permissions.DELETE_PERSONAL) && Account.get(account.get().owner, Account.Type.PERSONAL).length == 1)
443-
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsClosingPersonal());
444442
account.get().delete();
445443
return sendMessage(sender, BankAccounts.getInstance().config().messagesAccountDeleted(account.get()));
446444
}

src/main/java/pro/cloudnode/smp/bankaccounts/events/Join.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@
1010
import pro.cloudnode.smp.bankaccounts.Permissions;
1111

1212
import java.math.BigDecimal;
13-
import java.util.Optional;
1413

1514
public final class Join implements Listener {
1615
@EventHandler
1716
public void onPlayerJoin(final @NotNull PlayerJoinEvent event) {
18-
final Player player = event.getPlayer();
19-
final @NotNull Optional<@NotNull Double> startingBalance = BankAccounts.getInstance().config()
20-
.startingBalance();
21-
startingBalance.ifPresent(aDouble -> BankAccounts.getInstance().getServer().getScheduler()
22-
.runTaskAsynchronously(BankAccounts.getInstance(), () -> {
23-
final @NotNull Account[] accounts = Account.get(player, Account.Type.PERSONAL);
24-
if (accounts.length == 0) {
25-
new Account(player, Account.Type.PERSONAL, null, BigDecimal.valueOf(aDouble), false).insert();
26-
}
27-
}));
17+
final @NotNull Player player = event.getPlayer();
18+
final @NotNull BigDecimal startingBalance = BankAccounts.getInstance().config().startingBalance();
19+
BankAccounts.getInstance().getServer().getScheduler().runTaskAsynchronously(BankAccounts.getInstance(), () -> {
20+
if (Account.getVaultAccount(player).isEmpty()) {
21+
// if the player already has a personal account, they will not be given starting balance
22+
final @NotNull BigDecimal balance = startingBalance.compareTo(BigDecimal.ZERO) <= 0 || Account.get(player, Account.Type.PERSONAL).length > 0 ? BigDecimal.ZERO : startingBalance;
23+
new Account(player, Account.Type.VAULT, null, balance, false).insert();
24+
}
25+
});
2826
if (player.hasPermission(Permissions.NOTIFY_UPDATE)) {
2927
BankAccounts.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(BankAccounts.getInstance(), () -> BankAccounts.checkForUpdates().ifPresent(latestVersion -> {
3028
player.sendMessage(BankAccounts.getInstance().config().messagesUpdateAvailable(latestVersion));

0 commit comments

Comments
 (0)