Skip to content

Commit c29e392

Browse files
committed
Improve group's worldname validation and add bypass option
1 parent 4dddcd7 commit c29e392

File tree

3 files changed

+87
-16
lines changed

3 files changed

+87
-16
lines changed

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

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package org.mvplugins.multiverse.inventories.commands;
22

3+
import org.bukkit.Bukkit;
34
import org.jvnet.hk2.annotations.Service;
45
import org.mvplugins.multiverse.core.command.MVCommandIssuer;
6+
import org.mvplugins.multiverse.core.command.flag.CommandFlag;
7+
import org.mvplugins.multiverse.core.command.flag.CommandFlagsManager;
8+
import org.mvplugins.multiverse.core.command.flag.FlagBuilder;
9+
import org.mvplugins.multiverse.core.command.flag.ParsedCommandFlags;
510
import org.mvplugins.multiverse.core.utils.REPatterns;
611
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandCompletion;
712
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandPermission;
@@ -13,6 +18,7 @@
1318
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
1419
import org.mvplugins.multiverse.inventories.profile.group.WorldGroup;
1520
import org.mvplugins.multiverse.inventories.profile.group.WorldGroupManager;
21+
import org.mvplugins.multiverse.inventories.util.GroupWorldNameValidator;
1622
import org.mvplugins.multiverse.inventories.util.MVInvi18n;
1723

1824
import java.util.Arrays;
@@ -24,16 +30,24 @@
2430
final class AddWorldsCommand extends InventoriesCommand {
2531

2632
private final WorldGroupManager worldGroupManager;
33+
private final GroupWorldNameValidator groupWorldNameValidator;
34+
private final Flags flags;
2735

2836
@Inject
29-
AddWorldsCommand(@NotNull WorldGroupManager worldGroupManager) {
37+
AddWorldsCommand(
38+
@NotNull WorldGroupManager worldGroupManager,
39+
@NotNull GroupWorldNameValidator groupWorldNameValidator,
40+
@NotNull Flags flags
41+
) {
3042
this.worldGroupManager = worldGroupManager;
43+
this.groupWorldNameValidator = groupWorldNameValidator;
44+
this.flags = flags;
3145
}
3246

3347
@Subcommand("add-worlds")
3448
@CommandPermission("multiverse.inventories.addworlds")
35-
@CommandCompletion("@worldGroups @mvworlds:multiple,scope=both")
36-
@Syntax("<group> <world[,extra]>")
49+
@CommandCompletion("@worldGroups @mvworlds:multiple,scope=both, @flags:groupName=" + Flags.NAME)
50+
@Syntax("<group> <world[,extra]> [--skip-exist-check]")
3751
@Description("Adds a World to a World Group.")
3852
void onAddWorldCommand(
3953
MVCommandIssuer issuer,
@@ -42,14 +56,25 @@ void onAddWorldCommand(
4256
@Description("Group you want to add the world to.")
4357
WorldGroup group,
4458

45-
@Single
4659
@Syntax("<world>")
4760
@Description("World name to add.")
48-
String worlds
61+
String worlds,
62+
63+
@Syntax("[--skip-exist-check]")
64+
String[] flagArray
4965
) {
66+
ParsedCommandFlags parsedFlags = flags.parse(flagArray);
5067
List<String> worldNames = Arrays.stream(REPatterns.COMMA.split(worlds))
5168
.map(String::toLowerCase)
5269
.toList();
70+
if (!parsedFlags.hasFlag(flags.skipExistCheck)) {
71+
for (String worldName : worldNames) {
72+
if (!groupWorldNameValidator.validateWorldName(worldName)) {
73+
issuer.sendError(MVInvi18n.ERROR_NOWORLD, replace("{world}").with(worldName));
74+
return;
75+
}
76+
}
77+
}
5378
String worldNamesString = String.join(", ", worldNames);
5479
if (!group.getConfigWorlds().addAll(worldNames)) {
5580
issuer.sendError(MVInvi18n.ADDWORLD_WORLDALREADYEXISTS,
@@ -62,4 +87,18 @@ void onAddWorldCommand(
6287
replace("{group}").with(group.getName()),
6388
replace("{world}").with(worldNamesString));
6489
}
90+
91+
@Service
92+
private static final class Flags extends FlagBuilder {
93+
private static final String NAME = "mvinvaddworlds";
94+
95+
@Inject
96+
private Flags(@NotNull CommandFlagsManager flagsManager) {
97+
super(NAME, flagsManager);
98+
}
99+
100+
private final CommandFlag skipExistCheck = flag(CommandFlag.builder("--skip-exist-check")
101+
.addAlias("-s")
102+
.build());
103+
}
65104
}

src/main/java/org/mvplugins/multiverse/inventories/commands/prompts/GroupWorldsPrompt.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bukkit.World;
1111
import org.bukkit.conversations.ConversationContext;
1212
import org.bukkit.conversations.Prompt;
13+
import org.mvplugins.multiverse.inventories.util.GroupWorldNameValidator;
1314
import org.mvplugins.multiverse.inventories.util.MVInvi18n;
1415

1516
import java.util.HashSet;
@@ -23,6 +24,7 @@ final class GroupWorldsPrompt extends InventoriesPrompt {
2324
protected final Prompt nextPrompt;
2425
protected final boolean isCreating;
2526
protected final Set<String> worlds;
27+
private final GroupWorldNameValidator groupWorldNameValidator;
2628

2729
public GroupWorldsPrompt(final MultiverseInventories plugin, final MVCommandIssuer issuer,
2830
final WorldGroup group, final Prompt nextPrompt,
@@ -32,6 +34,7 @@ public GroupWorldsPrompt(final MultiverseInventories plugin, final MVCommandIssu
3234
this.nextPrompt = nextPrompt;
3335
this.isCreating = creatingGroup;
3436
this.worlds = new HashSet<>(group.getConfigWorlds());
37+
this.groupWorldNameValidator = plugin.getServiceLocator().getService(GroupWorldNameValidator.class);
3538
}
3639

3740
@NotNull
@@ -70,28 +73,25 @@ public Prompt acceptInput(@NotNull final ConversationContext conversationContext
7073
return nextPrompt;
7174
}
7275

73-
boolean negative = false;
74-
World world = Bukkit.getWorld(input);
75-
if (world == null && input.startsWith("-") && input.length() > 1) {
76-
negative = true;
77-
world = Bukkit.getWorld(input.substring(1));
78-
}
79-
80-
if (world == null) {
76+
boolean negative = input.startsWith("-");
77+
String worldName = negative ? input.substring(1) : input;
78+
if (!groupWorldNameValidator.validateWorldName(worldName)) {
8179
issuer.sendError(MVInvi18n.ERROR_NOWORLD, replace("{world}").with(input));
8280
return this;
8381
}
82+
8483
if (negative) {
85-
if (!worlds.contains(world.getName())) {
84+
if (!worlds.contains(worldName)) {
8685
issuer.sendError(MVInvi18n.REMOVEWORLD_WORLDNOTINGROUP,
8786
replace("{world}").with(input),
8887
replace("{group}").with(group.getName()));
8988
return this;
9089
}
91-
worlds.remove(world.getName());
90+
worlds.remove(worldName);
9291
return this;
9392
}
94-
worlds.add(world.getName());
93+
94+
worlds.add(worldName);
9595
return this;
9696
}
9797
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.mvplugins.multiverse.inventories.util;
2+
3+
import org.bukkit.Bukkit;
4+
import org.jetbrains.annotations.ApiStatus;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jvnet.hk2.annotations.Service;
7+
import org.mvplugins.multiverse.core.world.WorldManager;
8+
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
9+
10+
@Service
11+
@ApiStatus.Internal
12+
public class GroupWorldNameValidator {
13+
14+
private final WorldManager worldManager;
15+
16+
@Inject
17+
GroupWorldNameValidator(@NotNull WorldManager worldManager) {
18+
this.worldManager = worldManager;
19+
}
20+
21+
@ApiStatus.Internal
22+
public boolean validateWorldName(String worldName) {
23+
if (worldName == null || worldName.isEmpty()) {
24+
return false;
25+
}
26+
// For the new wildcard and regex support
27+
if (worldName.contains("*") || worldName.startsWith("r=")) {
28+
return true;
29+
}
30+
return worldManager.isWorld(worldName) || Bukkit.getWorld(worldName) != null;
31+
}
32+
}

0 commit comments

Comments
 (0)