Skip to content

Commit

Permalink
Fix account handling and responses in VaultEconomyServiceWrapper
Browse files Browse the repository at this point in the history
Corrected account creation to handle null world names. Ensured account operations consider the specified world and fixed response types for withdrawals and deposits to reflect success accurately.
  • Loading branch information
NonSwag committed Sep 5, 2024
1 parent 6fa83f5 commit 1eba249
Showing 1 changed file with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public boolean hasAccount(OfflinePlayer player) {

@Override
public boolean hasAccount(String playerName, String worldName) {
return getAccount(playerName, worldName).isPresent();
var player = playerName != null ? plugin.getServer().getOfflinePlayerIfCached(playerName) : null;
return hasAccount(player, worldName);
}

@Override
Expand Down Expand Up @@ -150,10 +151,10 @@ public EconomyResponse withdrawPlayer(String playerName, String worldName, doubl

@Override
public EconomyResponse withdrawPlayer(OfflinePlayer player, String worldName, double amount) {
return getAccount(player, null).map(account -> {
return getAccount(player, worldName).map(account -> {
var balance = account.getBalance();
var withdraw = account.withdraw(amount);
var responseType = balance.equals(withdraw) ? SUCCESS : FAILURE;
var responseType = amount != 0 && balance.equals(withdraw) ? FAILURE : SUCCESS;
return new EconomyResponse(amount, withdraw.doubleValue(), responseType, null);
}).orElseGet(() -> new EconomyResponse(amount, 0, FAILURE, null));
}
Expand All @@ -176,10 +177,10 @@ public EconomyResponse depositPlayer(String name, String worldName, double amoun

@Override
public EconomyResponse depositPlayer(OfflinePlayer player, String worldName, double amount) {
return getAccount(player, null).map(account -> {
return getAccount(player, worldName).map(account -> {
var balance = account.getBalance();
var deposit = account.deposit(amount);
var responseType = balance.equals(deposit) ? SUCCESS : FAILURE;
var responseType = amount != 0 && balance.equals(deposit) ? FAILURE : SUCCESS;
return new EconomyResponse(amount, deposit.doubleValue(), responseType, null);
}).orElseGet(() -> new EconomyResponse(amount, 0, FAILURE, null));
}
Expand Down Expand Up @@ -310,18 +311,13 @@ public boolean createPlayerAccount(String playerName, String worldName) {

@Override
public boolean createPlayerAccount(OfflinePlayer player, String worldName) {
return Optional.ofNullable(player).flatMap(offline -> Optional.ofNullable(worldName)
return Optional.ofNullable(player).map(offline -> Optional.ofNullable(worldName)
.map(plugin.getServer()::getWorld)
.map(world -> economyController.createAccount(player, world).join()))
.map(world -> economyController.createAccount(offline, world).join())
.orElseGet(() -> economyController.createAccount(offline).join()))
.isPresent();
}

private Optional<Account> getAccount(String playerName, String worldName) {
return Optional.ofNullable(playerName)
.map(plugin.getServer()::getOfflinePlayerIfCached)
.flatMap(player -> getAccount(player, worldName));
}

private Optional<Account> getAccount(OfflinePlayer player, String worldName) {
return Optional.ofNullable(player).flatMap(offline -> Optional.ofNullable(worldName)
.map(plugin.getServer()::getWorld)
Expand Down

0 comments on commit 1eba249

Please sign in to comment.