Skip to content

Commit 41d760b

Browse files
author
david
committed
Update permission checks and simplify balance command handling
Revised the permission requirements for the balance command, ensuring more granular control over access. Simplified the logic for determining account balance messages and error handling, removing redundant checks to improve clarity and maintainability.
1 parent 33ddc2a commit 41d760b

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/main/java/net/thenextlvl/economist/command/BalanceCommand.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ public class BalanceCommand {
2626

2727
public void register() {
2828
var command = Commands.literal("balance")
29-
.requires(stack -> stack.getSender().hasPermission("economist.admin"))
29+
.requires(stack -> stack.getSender().hasPermission("economist.balance"))
3030
.then(Commands.argument("player", CustomArgumentTypes.cachedOfflinePlayer())
31+
.requires(stack -> stack.getSender().hasPermission("economist.balance.others"))
3132
.then(Commands.argument("world", ArgumentTypes.world())
33+
.requires(stack -> stack.getSender().hasPermission("economist.balance.world"))
3234
.executes(context -> {
3335
var player = context.getArgument("player", OfflinePlayer.class);
3436
var world = context.getArgument("world", World.class);
3537
return balance(context, player, world);
3638
}))
3739
.executes(context -> {
38-
var target = context.getArgument("player", OfflinePlayer.class);
39-
if (context.getSource().getSender() instanceof Player player)
40-
return balance(context, target, player.getWorld());
41-
return balance(context, target, null);
40+
var player = context.getArgument("player", OfflinePlayer.class);
41+
return balance(context, player, null);
4242
}))
4343
.executes(context -> {
4444
if (context.getSource().getSender() instanceof Player player)
45-
return balance(context, player, player.getWorld());
45+
return balance(context, player, null);
4646
throw new IllegalArgumentException("No player defined");
4747
})
4848
.build();
@@ -59,26 +59,22 @@ private int balance(CommandContext<CommandSourceStack> context, OfflinePlayer pl
5959
.thenAccept(optional -> optional.ifPresentOrElse(account -> {
6060
var locale = sender instanceof Player p ? p.locale() : Locale.US;
6161

62-
var message = world != null && world.equals(sender instanceof Player p ? p.getWorld() : null)
63-
? (player.equals(sender) ? "account.balance.self" : "account.balance.other")
64-
: (player.equals(sender) ? "account.balance.world.self" : "account.balance.world.other");
62+
var message = world != null
63+
? (player.equals(sender) ? "account.balance.world.self" : "account.balance.world.other")
64+
: (player.equals(sender) ? "account.balance.self" : "account.balance.other");
6565

6666
plugin.bundle().sendMessage(sender, message,
6767
Placeholder.parsed("player", String.valueOf(player.getName())),
6868
Placeholder.parsed("balance", controller.format(account.getBalance(), locale)),
69-
Placeholder.parsed("currency", account.getBalance().intValueExact() == 1
69+
Placeholder.parsed("currency", account.getBalance().intValue() == 1
7070
? controller.getCurrencyNameSingular(locale)
7171
: controller.getCurrencyNamePlural(locale)),
72-
Placeholder.parsed("symbol", controller.getCurrencySymbol()));
73-
74-
}, () -> {
75-
var message = world != null && !world.equals(sender instanceof Player p ? p.getWorld() : null)
76-
? "account.not-found.world" : "account.not-found";
77-
78-
plugin.bundle().sendMessage(sender, message,
79-
Placeholder.parsed("player", String.valueOf(player.getName())),
72+
Placeholder.parsed("symbol", controller.getCurrencySymbol()),
8073
Placeholder.parsed("world", world != null ? world.key().asString() : "null"));
81-
}));
74+
75+
}, () -> plugin.bundle().sendMessage(sender, world != null ? "account.not-found.world" : "account.not-found",
76+
Placeholder.parsed("player", String.valueOf(player.getName())),
77+
Placeholder.parsed("world", world != null ? world.key().asString() : "null"))));
8278

8379
return Command.SINGLE_SUCCESS;
8480
}

0 commit comments

Comments
 (0)