Skip to content

Commit f91f27d

Browse files
committed
Refactor commands to not require player sender
1 parent ff72add commit f91f27d

20 files changed

Lines changed: 229 additions & 123 deletions

realty-paper/src/main/java/io/github/md5sha256/realty/command/AddCommand.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,18 @@ public record AddCommand(@NotNull MessageContainer messages) implements CustomCo
5151

5252
private void execute(@NotNull CommandContext<CommandSourceStack> ctx) {
5353
CommandSender sender = ctx.sender().getSender();
54-
if (!(sender instanceof Player player)) {
55-
return;
56-
}
5754
String playerOrGroup = ctx.get("player");
5855
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
59-
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(player.getLocation()));
56+
.orElseGet(() -> sender instanceof Player player
57+
? WorldGuardRegionResolver.resolveAtLocation(player.getLocation()) : null);
6058
if (region == null) {
61-
player.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
59+
sender.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
6260
return;
6361
}
6462
String regionId = region.region().getId();
6563

66-
if (!sender.hasPermission("realty.command.add.others")
64+
if (sender instanceof Player player
65+
&& !sender.hasPermission("realty.command.add.others")
6766
&& !region.region().getOwners().contains(player.getUniqueId())) {
6867
sender.sendMessage(messages.messageFor(MessageKeys.ADD_NO_PERMISSION));
6968
return;

realty-paper/src/main/java/io/github/md5sha256/realty/command/AgentInviteAcceptCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.github.md5sha256.realty.api.NotificationService;
44
import io.github.md5sha256.realty.command.util.WorldGuardRegion;
55
import io.github.md5sha256.realty.command.util.WorldGuardRegionParser;
6+
import io.github.md5sha256.realty.command.util.WorldGuardRegionResolver;
67
import io.github.md5sha256.realty.database.RealtyLogicImpl;
78
import io.github.md5sha256.realty.localisation.MessageContainer;
89
import io.github.md5sha256.realty.localisation.MessageKeys;
@@ -38,17 +39,23 @@ public record AgentInviteAcceptCommand(@NotNull ExecutorState executorState,
3839
.literal("invite")
3940
.literal("accept")
4041
.permission("realty.command.agent.invite.accept")
41-
.required("region", WorldGuardRegionParser.worldGuardRegion())
42+
.optional("region", WorldGuardRegionResolver.worldGuardRegionResolver())
4243
.handler(this::execute)
4344
.build();
4445
}
4546

4647
private void execute(@NotNull CommandContext<CommandSourceStack> ctx) {
4748
CommandSender sender = ctx.sender().getSender();
4849
if (!(sender instanceof Player player)) {
50+
sender.sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
51+
return;
52+
}
53+
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
54+
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(player.getLocation()));
55+
if (region == null) {
56+
player.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
4957
return;
5058
}
51-
WorldGuardRegion region = ctx.get("region");
5259
String regionId = region.region().getId();
5360
UUID worldId = region.world().getUID();
5461
UUID inviteeId = player.getUniqueId();

realty-paper/src/main/java/io/github/md5sha256/realty/command/AgentInviteCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.md5sha256.realty.command.util.AuthorityParser;
55
import io.github.md5sha256.realty.command.util.WorldGuardRegion;
66
import io.github.md5sha256.realty.command.util.WorldGuardRegionParser;
7+
import io.github.md5sha256.realty.command.util.WorldGuardRegionResolver;
78
import io.github.md5sha256.realty.database.RealtyLogicImpl;
89
import io.github.md5sha256.realty.localisation.MessageContainer;
910
import io.github.md5sha256.realty.localisation.MessageKeys;
@@ -40,18 +41,24 @@ public record AgentInviteCommand(@NotNull ExecutorState executorState,
4041
.literal("invite")
4142
.permission("realty.command.agent.invite")
4243
.required("player", AuthorityParser.authority())
43-
.required("region", WorldGuardRegionParser.worldGuardRegion())
44+
.optional("region", WorldGuardRegionResolver.worldGuardRegionResolver())
4445
.handler(this::execute)
4546
.build();
4647
}
4748

4849
private void execute(@NotNull CommandContext<CommandSourceStack> ctx) {
4950
CommandSender sender = ctx.sender().getSender();
5051
if (!(sender instanceof Player player)) {
52+
sender.sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
5153
return;
5254
}
5355
UUID inviteeId = ctx.get("player");
54-
WorldGuardRegion region = ctx.get("region");
56+
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
57+
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(player.getLocation()));
58+
if (region == null) {
59+
player.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
60+
return;
61+
}
5562
String regionId = region.region().getId();
5663
UUID worldId = region.world().getUID();
5764
String inviteeName = resolveName(inviteeId);

realty-paper/src/main/java/io/github/md5sha256/realty/command/AgentInviteRejectCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.github.md5sha256.realty.api.NotificationService;
44
import io.github.md5sha256.realty.command.util.WorldGuardRegion;
55
import io.github.md5sha256.realty.command.util.WorldGuardRegionParser;
6+
import io.github.md5sha256.realty.command.util.WorldGuardRegionResolver;
67
import io.github.md5sha256.realty.database.RealtyLogicImpl;
78
import io.github.md5sha256.realty.localisation.MessageContainer;
89
import io.github.md5sha256.realty.localisation.MessageKeys;
@@ -38,17 +39,23 @@ public record AgentInviteRejectCommand(@NotNull ExecutorState executorState,
3839
.literal("invite")
3940
.literal("reject")
4041
.permission("realty.command.agent.invite.reject")
41-
.required("region", WorldGuardRegionParser.worldGuardRegion())
42+
.optional("region", WorldGuardRegionResolver.worldGuardRegionResolver())
4243
.handler(this::execute)
4344
.build();
4445
}
4546

4647
private void execute(@NotNull CommandContext<CommandSourceStack> ctx) {
4748
CommandSender sender = ctx.sender().getSender();
4849
if (!(sender instanceof Player player)) {
50+
sender.sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
51+
return;
52+
}
53+
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
54+
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(player.getLocation()));
55+
if (region == null) {
56+
player.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
4957
return;
5058
}
51-
WorldGuardRegion region = ctx.get("region");
5259
String regionId = region.region().getId();
5360
UUID worldId = region.world().getUID();
5461
UUID inviteeId = player.getUniqueId();

realty-paper/src/main/java/io/github/md5sha256/realty/command/AgentInviteWithdrawCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.md5sha256.realty.command.util.AuthorityParser;
55
import io.github.md5sha256.realty.command.util.WorldGuardRegion;
66
import io.github.md5sha256.realty.command.util.WorldGuardRegionParser;
7+
import io.github.md5sha256.realty.command.util.WorldGuardRegionResolver;
78
import io.github.md5sha256.realty.database.RealtyLogicImpl;
89
import io.github.md5sha256.realty.localisation.MessageContainer;
910
import io.github.md5sha256.realty.localisation.MessageKeys;
@@ -40,18 +41,24 @@ public record AgentInviteWithdrawCommand(@NotNull ExecutorState executorState,
4041
.literal("withdraw")
4142
.permission("realty.command.agent.invite.withdraw")
4243
.required("player", AuthorityParser.authority())
43-
.required("region", WorldGuardRegionParser.worldGuardRegion())
44+
.optional("region", WorldGuardRegionResolver.worldGuardRegionResolver())
4445
.handler(this::execute)
4546
.build();
4647
}
4748

4849
private void execute(@NotNull CommandContext<CommandSourceStack> ctx) {
4950
CommandSender sender = ctx.sender().getSender();
5051
if (!(sender instanceof Player player)) {
52+
sender.sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
5153
return;
5254
}
5355
UUID inviteeId = ctx.get("player");
54-
WorldGuardRegion region = ctx.get("region");
56+
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
57+
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(player.getLocation()));
58+
if (region == null) {
59+
player.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
60+
return;
61+
}
5562
String regionId = region.region().getId();
5663
UUID worldId = region.world().getUID();
5764
String inviteeName = resolveName(inviteeId);

realty-paper/src/main/java/io/github/md5sha256/realty/command/AgentRemoveCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.md5sha256.realty.command.util.AuthorityParser;
55
import io.github.md5sha256.realty.command.util.WorldGuardRegion;
66
import io.github.md5sha256.realty.command.util.WorldGuardRegionParser;
7+
import io.github.md5sha256.realty.command.util.WorldGuardRegionResolver;
78
import io.github.md5sha256.realty.database.RealtyLogicImpl;
89
import io.github.md5sha256.realty.localisation.MessageContainer;
910
import io.github.md5sha256.realty.localisation.MessageKeys;
@@ -39,18 +40,24 @@ public record AgentRemoveCommand(@NotNull ExecutorState executorState,
3940
.literal("remove")
4041
.permission("realty.command.agent.remove")
4142
.required("player", AuthorityParser.authority())
42-
.required("region", WorldGuardRegionParser.worldGuardRegion())
43+
.optional("region", WorldGuardRegionResolver.worldGuardRegionResolver())
4344
.handler(this::execute)
4445
.build();
4546
}
4647

4748
private void execute(@NotNull CommandContext<CommandSourceStack> ctx) {
4849
CommandSender sender = ctx.sender().getSender();
4950
if (!(sender instanceof Player player)) {
51+
sender.sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
5052
return;
5153
}
5254
UUID targetId = ctx.get("player");
53-
WorldGuardRegion region = ctx.get("region");
55+
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
56+
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(player.getLocation()));
57+
if (region == null) {
58+
player.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
59+
return;
60+
}
5461
String regionId = region.region().getId();
5562
UUID worldId = region.world().getUID();
5663
String targetName = resolveName(targetId);

realty-paper/src/main/java/io/github/md5sha256/realty/command/AuctionCommandGroup.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io.github.md5sha256.realty.command.util.DurationParser;
1414
import io.github.md5sha256.realty.command.util.SubregionLandlordUpdater;
1515
import io.github.md5sha256.realty.command.util.WorldGuardRegion;
16-
import io.github.md5sha256.realty.command.util.WorldGuardRegionParser;
1716
import io.github.md5sha256.realty.command.util.WorldGuardRegionResolver;
1817
import io.github.md5sha256.realty.database.RealtyLogicImpl;
1918
import io.github.md5sha256.realty.database.RealtyLogicImpl.CreateAuctionResult;
@@ -83,7 +82,7 @@ public record AuctionCommandGroup(
8382
.required("paymentDuration", DurationParser.duration())
8483
.required("minBid", DoubleParser.doubleParser(0))
8584
.required("minBidStep", DoubleParser.doubleParser(0))
86-
.required("region", WorldGuardRegionParser.worldGuardRegion())
85+
.optional("region", WorldGuardRegionResolver.worldGuardRegionResolver())
8786
.handler(this::executeCreate)
8887
.build(),
8988
base.literal("cancel")
@@ -94,13 +93,13 @@ public record AuctionCommandGroup(
9493
base.literal("bid")
9594
.permission("realty.command.auction.bid")
9695
.required("bid", DoubleParser.doubleParser(0))
97-
.required("region", WorldGuardRegionParser.worldGuardRegion())
96+
.optional("region", WorldGuardRegionResolver.worldGuardRegionResolver())
9897
.handler(this::executeBid)
9998
.build(),
10099
base.literal("paybid")
101100
.permission("realty.command.auction.paybid")
102101
.required("amount", DoubleParser.doubleParser(0, Double.MAX_VALUE))
103-
.required("region", WorldGuardRegionParser.worldGuardRegion())
102+
.optional("region", WorldGuardRegionResolver.worldGuardRegionResolver())
104103
.handler(this::executePayBid)
105104
.build()
106105
);
@@ -110,11 +109,9 @@ public record AuctionCommandGroup(
110109

111110
private void executeInfo(@NotNull CommandContext<CommandSourceStack> ctx) {
112111
CommandSender sender = ctx.sender().getSender();
113-
if (!(sender instanceof Player player)) {
114-
return;
115-
}
116112
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
117-
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(player.getLocation()));
113+
.orElseGet(() -> sender instanceof Player player
114+
? WorldGuardRegionResolver.resolveAtLocation(player.getLocation()) : null);
118115
if (region == null) {
119116
sender.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
120117
return;
@@ -171,13 +168,19 @@ private void executeInfo(@NotNull CommandContext<CommandSourceStack> ctx) {
171168
private void executeCreate(@NotNull CommandContext<CommandSourceStack> ctx) {
172169
CommandSender sender = ctx.sender().getSender();
173170
if (!(sender instanceof Player player)) {
171+
sender.sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
174172
return;
175173
}
176174
Duration bidDuration = ctx.get("bidDuration");
177175
Duration paymentDuration = ctx.get("paymentDuration");
178176
double minBid = ctx.get("minBid");
179177
double minBidStep = ctx.get("minBidStep");
180-
WorldGuardRegion region = ctx.get("region");
178+
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
179+
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(player.getLocation()));
180+
if (region == null) {
181+
sender.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
182+
return;
183+
}
181184
String regionId = region.region().getId();
182185
CompletableFuture.runAsync(() -> {
183186
try {
@@ -211,11 +214,10 @@ private void executeCreate(@NotNull CommandContext<CommandSourceStack> ctx) {
211214
// ── /realty auction cancel [region] ──
212215

213216
private void executeCancel(@NotNull CommandContext<CommandSourceStack> ctx) {
214-
if (!(ctx.sender().getSender() instanceof Player sender)) {
215-
return;
216-
}
217+
CommandSender sender = ctx.sender().getSender();
217218
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
218-
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(sender.getLocation()));
219+
.orElseGet(() -> sender instanceof Player player
220+
? WorldGuardRegionResolver.resolveAtLocation(player.getLocation()) : null);
219221
if (region == null) {
220222
sender.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
221223
return;
@@ -246,10 +248,16 @@ private void executeCancel(@NotNull CommandContext<CommandSourceStack> ctx) {
246248

247249
private void executeBid(@NotNull CommandContext<CommandSourceStack> ctx) {
248250
if (!(ctx.sender().getSender() instanceof Player sender)) {
251+
ctx.sender().getSender().sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
249252
return;
250253
}
251254
double bidAmount = ctx.<Double>get("bid");
252-
WorldGuardRegion region = ctx.get("region");
255+
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
256+
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(sender.getLocation()));
257+
if (region == null) {
258+
sender.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
259+
return;
260+
}
253261
String regionId = region.region().getId();
254262
CompletableFuture.runAsync(() -> {
255263
try {
@@ -296,7 +304,12 @@ private void executePayBid(@NotNull CommandContext<CommandSourceStack> ctx) {
296304
return;
297305
}
298306
double amount = ctx.get("amount");
299-
WorldGuardRegion region = ctx.get("region");
307+
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")
308+
.orElseGet(() -> WorldGuardRegionResolver.resolveAtLocation(sender.getLocation()));
309+
if (region == null) {
310+
sender.sendMessage(messages.messageFor(MessageKeys.ERROR_NO_REGION));
311+
return;
312+
}
300313
String regionId = region.region().getId();
301314
// Balance check on main thread
302315
double balance = economy.getBalance(sender);

realty-paper/src/main/java/io/github/md5sha256/realty/command/BuyCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public record BuyCommand(
6262

6363
private void execute(@NotNull CommandContext<CommandSourceStack> ctx) {
6464
if (!(ctx.sender().getSender() instanceof Player sender)) {
65+
ctx.sender().getSender().sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
6566
return;
6667
}
6768
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")

realty-paper/src/main/java/io/github/md5sha256/realty/command/ExtendCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public record ExtendCommand(
4949

5050
private void execute(@NotNull CommandContext<CommandSourceStack> ctx) {
5151
if (!(ctx.sender().getSender() instanceof Player sender)) {
52+
ctx.sender().getSender().sendMessage(messages.messageFor(MessageKeys.COMMON_PLAYERS_ONLY));
5253
return;
5354
}
5455
WorldGuardRegion region = ctx.<WorldGuardRegion>optional("region")

0 commit comments

Comments
 (0)