Skip to content

Commit 927989f

Browse files
authored
Add server Vault account to use for proxying Vault transactions (#140)
2 parents 92bb014 + 2b36349 commit 927989f

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.time.LocalDateTime;
2323
import java.time.ZoneOffset;
2424
import java.util.ArrayList;
25+
import java.util.Arrays;
2526
import java.util.List;
2627
import java.util.Objects;
2728
import java.util.Optional;
@@ -341,9 +342,17 @@ else if (limit != null) {
341342
* Get the server account
342343
*/
343344
public static @NotNull Optional<@NotNull Account> getServerAccount() {
344-
final @NotNull Account @NotNull [] accounts = get(BankAccounts.getConsoleOfflinePlayer());
345-
if (accounts.length == 0) return Optional.empty();
346-
return Optional.of(accounts[0]);
345+
if (!BankAccounts.getInstance().config().serverAccountEnabled()) return Optional.empty();
346+
final @NotNull Optional<@NotNull Account> account = Arrays.stream(get(BankAccounts.getConsoleOfflinePlayer())).filter(a -> a.type != Type.VAULT).findFirst();
347+
return account;
348+
}
349+
350+
/**
351+
* Get the server Vault account
352+
*/
353+
public static @NotNull Optional<@NotNull Account> getServerVaultAccount() {
354+
if (!BankAccounts.getInstance().config().integrationsVaultEnabled()) return Optional.empty();
355+
return getVaultAccount(BankAccounts.getConsoleOfflinePlayer());
347356
}
348357

349358
/**

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public static void reload() {
155155
getInstance().setupDbSource();
156156
getInstance().initDbWrapper();
157157
createServerAccount();
158+
createServerVaultAccount();
158159
getInstance().getServer().getScheduler().runTaskAsynchronously(getInstance(), () -> checkForUpdates().ifPresent(latestVersion -> {
159160
getInstance().getLogger().warning("An update is available: " + latestVersion);
160161
getInstance().getLogger().warning("Please update to the latest version to benefit from bug fixes, security patches, new features and support.");
@@ -329,12 +330,28 @@ public static String formatCurrencyShort(final @Nullable BigDecimal amount) {
329330
* Create server account, if enabled in config
330331
*/
331332
private static void createServerAccount() {
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();
333+
if (getInstance().config().serverAccountEnabled()) {
334+
final @NotNull Optional<@NotNull Account> account = Account.getServerAccount();
335+
if (account.isPresent()) return;
336+
337+
final @Nullable String name = getInstance().config().serverAccountName();
338+
final @NotNull Account.Type type = getInstance().config().serverAccountType();
339+
final @Nullable BigDecimal balance = getInstance().config().serverAccountStartingBalance();
340+
new Account(getConsoleOfflinePlayer(), type, name, balance, false).insert();
341+
}
342+
}
343+
344+
/**
345+
* Create server Vault account, if Vault enabled
346+
*/
347+
private static void createServerVaultAccount() {
348+
if (getInstance().config().integrationsVaultEnabled()) {
349+
final @NotNull Optional<@NotNull Account> serverAccount = Account.getServerVaultAccount();
350+
if (serverAccount.isPresent()) return;
351+
352+
final @Nullable String name = getInstance().config().integrationsVaultServerAccount();
353+
new Account(getConsoleOfflinePlayer(), Account.Type.VAULT, name, BigDecimal.ZERO, true);
354+
}
338355
}
339356

340357
/**

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public boolean integrationsVaultEnabled() {
120120
return Objects.requireNonNull(config.getString("integrations.vault.description"));
121121
}
122122

123+
// integrations.vault.server-account
124+
public @NotNull String integrationsVaultServerAccount() {
125+
return Objects.requireNonNull(config.getString("integrations.vault.server-account"));
126+
}
127+
123128
// currency.symbol
124129
public @NotNull String currencySymbol() {
125130
return Objects.requireNonNull(config.getString("currency.symbol"));
@@ -137,6 +142,11 @@ public boolean integrationsVaultEnabled() {
137142
else return Optional.of(new BigDecimal(Objects.requireNonNull(config.getString("starting-balance"))));
138143
}
139144

145+
// server-account.enabled
146+
public boolean serverAccountEnabled() {
147+
return config.getBoolean("server-account.enabled");
148+
}
149+
140150
// server-account.name
141151
public @NotNull String serverAccountName() {
142152
return Objects.requireNonNull(config.getString("server-account.name"));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public boolean has(final @NotNull String player, final @NotNull String world, fi
235235
if (account.isEmpty()) return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "Account not found");
236236
if (!account.get().hasFunds(BigDecimal.valueOf(amount)))
237237
return new EconomyResponse(amount, Optional.ofNullable(account.get().balance).map(BigDecimal::doubleValue).orElse(Double.MAX_VALUE), EconomyResponse.ResponseType.FAILURE, "Insufficient funds");
238-
final @NotNull Account serverAccount = Account.getServerAccount().orElse(new Account.ClosedAccount());
238+
final @NotNull Account serverAccount = Account.getServerVaultAccount().orElse(new Account.ClosedAccount());
239239
// transfer funds to the server account since Vault just wants them "gone"
240240
account.get().transfer(serverAccount, BigDecimal.valueOf(amount), BankAccounts.getInstance().config().integrationsVaultDescription(), null);
241241
// remove funds from the server account without a transaction
@@ -287,7 +287,7 @@ public boolean has(final @NotNull String player, final @NotNull String world, fi
287287
public @NotNull EconomyResponse depositPlayer(final @NotNull OfflinePlayer player, final double amount) {
288288
final @NotNull Optional<@NotNull Account> account = Account.getVaultAccount(player);
289289
if (account.isEmpty()) return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "Account not found");
290-
final @NotNull Account serverAccount = Account.getServerAccount().orElse(new Account.ClosedAccount());
290+
final @NotNull Account serverAccount = Account.getServerVaultAccount().orElse(new Account.ClosedAccount());
291291
// add money to the server account and then transfer it to the player
292292
serverAccount.updateBalance(BigDecimal.valueOf(amount));
293293
serverAccount.transfer(account.get(), BigDecimal.valueOf(amount), BankAccounts.getInstance().config().integrationsVaultDescription(), null);

src/main/resources/config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ integrations:
4343
# Transaction description for all vault operations
4444
description: Vault Transaction
4545

46+
# Name of the server Vault account
47+
# All Vault transactions will appear as to/from this account.
48+
server-account: Server Vault
49+
4650
currency:
4751
# Currency symbol
4852
symbol: $
@@ -58,6 +62,9 @@ starting-balance: 0
5862

5963
# Server account
6064
server-account:
65+
# If enabled, when the plugin is loaded an account will be created as player with UUID 00000000-0000-0000-0000-000000000000
66+
# This is required for features like interest to work.
67+
enabled: true
6168
# Display name for the server account
6269
name: Central Bank
6370
# Account type

0 commit comments

Comments
 (0)