Skip to content

Commit

Permalink
Refactor pay command for cleaner account handling.
Browse files Browse the repository at this point in the history
Introduced methods to encapsulate repeated logic and improve readability. Separated account retrieval and payment processing into distinct functions. Added specific handling for account not found cases.
  • Loading branch information
NonSwag committed Sep 7, 2024
1 parent b421298 commit d711561
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions src/main/java/net/thenextlvl/economist/command/PayCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.economist.EconomistPlugin;
import net.thenextlvl.economist.api.Account;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
Expand All @@ -19,6 +20,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
Expand Down Expand Up @@ -70,41 +72,48 @@ private int pay(CommandContext<CommandSourceStack> context, List<? extends Offli
return 0;
} else if (players.size() > 1) players.remove(sender);

Optional.ofNullable(world).map(w -> plugin.economyController().tryGetAccount(sender, w))
.orElseGet(() -> plugin.economyController().tryGetAccount(sender))
.thenAccept(optional -> optional.ifPresentOrElse(account -> players.forEach(player ->
getAccount(sender, world).thenAccept(optional -> optional.ifPresentOrElse(account ->
players.forEach(player -> getAccount(player, world).thenAccept(optional1 ->
optional1.ifPresentOrElse(target ->
pay(sender, player, account, target, amount, minimum),
() -> missingAccount(world, sender, player)
))),
() -> missingAccount(world, sender, sender)));

plugin.economyController().tryGetAccount(player).thenAccept(optional1 ->

optional1.ifPresentOrElse(target -> {
if (account.getBalance().doubleValue() - amount >= minimum) {

account.withdraw(amount);
target.deposit(amount);

plugin.bundle().sendMessage(sender, "player.pay.outgoing",
Placeholder.parsed("amount", plugin.economyController().format(amount, sender.locale())),
Placeholder.parsed("player", player.getName() != null ? player.getName() : "null"),
Placeholder.parsed("symbol", plugin.economyController().getCurrencySymbol()));

var online = player.getPlayer();
if (online != null) plugin.bundle().sendMessage(online, "player.pay.incoming",
Placeholder.parsed("amount", plugin.economyController().format(amount, online.locale())),
Placeholder.parsed("symbol", plugin.economyController().getCurrencySymbol()),
Placeholder.parsed("player", sender.getName()));
return players.size();
}

} else plugin.bundle().sendMessage(sender, "account.funds");
}, () -> {
var message = world != null ? "account.not-found.world.other" : "account.not-found.other";
var placeholder = Placeholder.parsed("world", world != null ? world.key().asString() : "null");
plugin.bundle().sendMessage(sender, message, placeholder);
}))), () -> {
private CompletableFuture<Optional<Account>> getAccount(OfflinePlayer player, @Nullable World world) {
if (world == null) return plugin.economyController().tryGetAccount(player);
return plugin.economyController().tryGetAccount(player, world);
}

var message = world != null ? "account.not-found.world.self" : "account.not-found.self";
var placeholder = Placeholder.parsed("world", world != null ? world.key().asString() : "null");
plugin.bundle().sendMessage(sender, message, placeholder);
}));
private void pay(Player sender, OfflinePlayer player, Account source, Account target, double amount, double minimum) {
if (source.getBalance().doubleValue() - amount < minimum) {
plugin.bundle().sendMessage(sender, "account.funds");
return;
}

source.withdraw(amount);
target.deposit(amount);

plugin.bundle().sendMessage(sender, "player.pay.outgoing",
Placeholder.parsed("amount", plugin.economyController().format(amount, sender.locale())),
Placeholder.parsed("player", player.getName() != null ? player.getName() : "null"),
Placeholder.parsed("symbol", plugin.economyController().getCurrencySymbol()));

var online = player.getPlayer();
if (online != null) plugin.bundle().sendMessage(online, "player.pay.incoming",
Placeholder.parsed("amount", plugin.economyController().format(amount, online.locale())),
Placeholder.parsed("symbol", plugin.economyController().getCurrencySymbol()),
Placeholder.parsed("player", sender.getName()));
}

return players.size();
private void missingAccount(@Nullable World world, Player sender, OfflinePlayer player) {
plugin.bundle().sendMessage(sender, world != null
? (player.equals(sender) ? "account.not-found.world.self" : "account.not-found.world.other")
: (player.equals(sender) ? "account.not-found.self" : "account.not-found.other"),
Placeholder.parsed("player", player.getName() != null ? player.getName() : "null"),
Placeholder.parsed("world", world != null ? world.key().asString() : "null"));
}
}

0 comments on commit d711561

Please sign in to comment.