11package org .mvplugins .multiverse .inventories .commands ;
22
3+ import org .bukkit .Bukkit ;
34import org .jvnet .hk2 .annotations .Service ;
45import 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 ;
510import org .mvplugins .multiverse .core .utils .REPatterns ;
611import org .mvplugins .multiverse .external .acf .commands .annotation .CommandCompletion ;
712import org .mvplugins .multiverse .external .acf .commands .annotation .CommandPermission ;
1318import org .mvplugins .multiverse .external .jetbrains .annotations .NotNull ;
1419import org .mvplugins .multiverse .inventories .profile .group .WorldGroup ;
1520import org .mvplugins .multiverse .inventories .profile .group .WorldGroupManager ;
21+ import org .mvplugins .multiverse .inventories .util .GroupWorldNameValidator ;
1622import org .mvplugins .multiverse .inventories .util .MVInvi18n ;
1723
1824import java .util .Arrays ;
2430final 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}
0 commit comments