Skip to content

Commit a09f33e

Browse files
committed
Reduce need for validation checks in view and modify commands
1 parent e3d7e54 commit a09f33e

2 files changed

Lines changed: 18 additions & 39 deletions

File tree

src/main/java/org/mvplugins/multiverse/inventories/commands/InventoryModifyCommand.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandCompletion;
1414
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandPermission;
1515
import org.mvplugins.multiverse.external.acf.commands.annotation.Description;
16+
import org.mvplugins.multiverse.external.acf.commands.annotation.Flags;
1617
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
1718
import org.mvplugins.multiverse.external.acf.commands.annotation.Syntax;
1819
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
@@ -48,32 +49,25 @@ final class InventoryModifyCommand extends InventoriesCommand {
4849
@Description("Modify a player's inventory in a specific world.")
4950
void onInventoryModifyCommand(
5051
@NotNull MVCommandIssuer issuer,
52+
53+
// to make sure the command is only run by players
54+
@Flags("resolve=issuerOnly")
55+
@NotNull Player player,
56+
5157
@Syntax("<player>")
5258
@Description("Online or offline player")
5359
OfflinePlayer targetPlayer,
5460

5561
@Syntax("<world>")
5662
@Description("The world the player's inventory is in")
57-
MultiverseWorld[] worlds
63+
MultiverseWorld world
5864
) {
59-
if (!(issuer.getIssuer() instanceof Player viewer)) {
60-
issuer.sendError(ChatColor.RED + "Only players can modify inventories.");
61-
return;
62-
}
63-
if (worlds == null || worlds.length == 0 || worlds[0] == null) {
64-
issuer.sendError(ChatColor.RED + "You must specify a valid world.");
65-
return;
66-
}
67-
if (targetPlayer == null || targetPlayer.getName() == null) {
68-
issuer.sendError(ChatColor.RED + "You must specify a valid player.");
69-
return;
70-
}
71-
if (viewer.getUniqueId().equals(targetPlayer.getUniqueId())) {
65+
if (player.getUniqueId().equals(targetPlayer.getUniqueId())) {
7266
issuer.sendError(ChatColor.RED + "You cannot modify your own inventory using this command. Use your regular inventory.");
7367
return;
7468
}
7569

76-
String worldName = worlds[0].getName();
70+
String worldName = world.getName();
7771
// Asynchronously load data using InventoryDataProvider
7872
issuer.sendInfo(ChatColor.YELLOW + "Loading inventory data for " + targetPlayer.getName() + "...");
7973

@@ -137,7 +131,7 @@ void onInventoryModifyCommand(
137131
inv.setItem(i, inventoryGUIHelper.createFillerItemForSlot(i));
138132
}
139133

140-
viewer.openInventory(inv);
134+
player.openInventory(inv);
141135
issuer.sendInfo(ChatColor.GREEN + "Opened editable inventory for " + targetPlayer.getName() + " in world " + worldName + ". Changes will save on close.");
142136
}); // End of Bukkit.getScheduler().runTask()
143137
})
@@ -150,4 +144,3 @@ void onInventoryModifyCommand(
150144
});
151145
}
152146
}
153-

src/main/java/org/mvplugins/multiverse/inventories/commands/InventoryViewCommand.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandCompletion;
1414
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandPermission;
1515
import org.mvplugins.multiverse.external.acf.commands.annotation.Description;
16+
import org.mvplugins.multiverse.external.acf.commands.annotation.Flags;
1617
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
1718
import org.mvplugins.multiverse.external.acf.commands.annotation.Syntax;
1819
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
@@ -48,34 +49,19 @@ final class InventoryViewCommand extends InventoriesCommand {
4849
void onInventoryViewCommand(
4950
@NotNull MVCommandIssuer issuer,
5051

52+
// Ensure the command is run by a player
53+
@Flags("resolve=issuerOnly")
54+
@NotNull Player player,
55+
5156
@Syntax("<player>")
5257
@Description("Online or offline player")
53-
//Player targetPlayer,
5458
OfflinePlayer targetPlayer,
5559

5660
@Syntax("<world>")
5761
@Description("The world the player's inventory is in")
58-
MultiverseWorld[] worlds
62+
MultiverseWorld world
5963
) {
60-
// Check if the command sender is a player
61-
if (!(issuer.getIssuer() instanceof Player viewer)) {
62-
issuer.sendError(ChatColor.RED + "Only players can view inventories.");
63-
return;
64-
}
65-
66-
// Validate world argument
67-
if (worlds == null || worlds.length == 0 || worlds[0] == null) {
68-
issuer.sendError(ChatColor.RED + "You must specify a valid world.");
69-
return;
70-
}
71-
72-
// Validate targetPlayer player
73-
if (targetPlayer == null || targetPlayer.getName() == null) {
74-
issuer.sendError(ChatColor.RED + "You must specify a valid player.");
75-
return;
76-
}
77-
78-
String worldName = worlds[0].getName();
64+
String worldName = world.getName();
7965

8066
// Asynchronously load data using InventoryDataProvider
8167
issuer.sendInfo(ChatColor.YELLOW + "Loading inventory data for " + targetPlayer.getName() + "...");
@@ -131,7 +117,7 @@ void onInventoryViewCommand(
131117
inv.setItem(i, inventoryGUIHelper.createFillerItemForSlot(i));
132118
}
133119

134-
viewer.openInventory(inv);
120+
player.openInventory(inv);
135121
issuer.sendInfo(ChatColor.GREEN + playerInventoryData.statusMessage);
136122
}); // End of Bukkit.getScheduler().runTask()
137123
})

0 commit comments

Comments
 (0)