Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix account handling and responses in VaultEconomyServiceWrapper #15

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading