1818import java .lang .reflect .Field ;
1919import java .util .ArrayList ;
2020import java .util .Arrays ;
21+ import java .util .Collection ;
2122import java .util .Collections ;
2223import java .util .Comparator ;
2324import java .util .List ;
3637public class CommandRegister implements CommandRegistering {
3738 private final Logging log = new Logging (CommandRegister .class );
3839
39- private final List <CommandProperty > commands = Collections .synchronizedList (new ArrayList <>());
40+ private final List <CommandProperty > commandsLegacy = Collections .synchronizedList (new ArrayList <>());
4041 private final Map <String , MainCommandHandler > commandsNew = new ConcurrentHashMap <>();
4142
4243 private String commandLabelMessage ;
@@ -47,30 +48,6 @@ public class CommandRegister implements CommandRegistering {
4748 private boolean registeredMainCommand ;
4849 private List <String > descriptions ;
4950
50- @ Override
51- public CommandRegistering registerSubCommand (final CommandProperty subCommand ) {
52- Set <String > commandLabels = subCommand .getCommandLabels ();
53-
54- if (addCommands (subCommand , commandLabels )) {
55- return this ;
56- }
57- commands .removeIf (oldCommandBuilder -> oldCommandBuilder .equals (subCommand ));
58- commands .removeIf (oldCommandBuilder -> oldCommandBuilder .getCommandLabels ().equals (subCommand .getCommandLabels ()));
59- commands .add (subCommand );
60- commands .sort (Comparator .comparing (CommandProperty ::getFirstSortedLabel , Comparator .nullsLast (String ::compareTo )));
61- return this ;
62- }
63-
64- @ Override
65- public CommandRegistering registerSubCommands (final CommandProperty ... subCommands ) {
66- if (subCommands == null )
67- return this ;
68- for (CommandProperty registerSubCommand : subCommands ) {
69- this .registerSubCommand (registerSubCommand );
70- }
71- return this ;
72- }
73-
7451 /**
7552 * Registers a new command entry point for this plugin.
7653 *
@@ -86,8 +63,19 @@ public CommandRegistering registerSubCommands(final CommandProperty... subComman
8663 */
8764 public CommandBuilder registerCommand (final Plugin plugin , final String mainCommand ) {
8865 final CommandBuilder commandBuilder = new CommandBuilder ();
89- commandsNew .put (mainCommand , commandBuilder .getMainCommandHandler ());
90- this .registerMainCommand (plugin .getName ().toLowerCase (Locale .ROOT ), mainCommand , commandBuilder );
66+ commandsNew .compute (mainCommand , (s , mainCommandHandler ) -> {
67+ if (mainCommandHandler != null ) {
68+ final CommandProperty command = mainCommandHandler .getMainCommand ();
69+ final Collection <CommandProperty > commands = mainCommandHandler .getSubcommands ();
70+ if (command != null )
71+ log .log (() -> "The command is already registered: '" + mainCommand + "' and have this command registered:" + command );
72+ if (commands != null )
73+ log .log (() -> "The command is already registered: '" + mainCommand + "' and have this sub commands registered: '" + commands + "'" );
74+ return null ;
75+ }
76+ this .registerMainCommand (plugin .getName ().toLowerCase (Locale .ROOT ), mainCommand , commandBuilder );
77+ return commandBuilder .getMainCommandHandler ();
78+ });
9179 return commandBuilder ;
9280 }
9381
@@ -102,14 +90,24 @@ public CommandBuilder registerCommand(final Plugin plugin, final String mainComm
10290 *
10391 * @param plugin the owning plugin instance (used for namespace and registration context)
10492 * @param mainCommand the root command label (e.g. "plugin" in "/plugin menu")
105- * @param callback The builder to set the command.
93+ * @param callback The builder to set the command.
10694 */
10795 public void registerCommand (@ Nonnull final Plugin plugin , @ Nonnull final String mainCommand , @ Nonnull final Consumer <CommandBuilder > callback ) {
10896 final CommandBuilder commandBuilder = new CommandBuilder ();
109-
11097 callback .accept (commandBuilder );
111- commandsNew .put (mainCommand , commandBuilder .getMainCommandHandler ());
112- this .registerMainCommand (plugin .getName ().toLowerCase (Locale .ROOT ), mainCommand , commandBuilder );
98+ commandsNew .compute (mainCommand , (s , mainCommandHandler ) -> {
99+ if (mainCommandHandler != null ) {
100+ final CommandProperty command = mainCommandHandler .getMainCommand ();
101+ final Collection <CommandProperty > commands = mainCommandHandler .getSubcommands ();
102+ if (command != null )
103+ log .log (() -> "The command is already registered: '" + mainCommand + "' and have this command registered:" + command );
104+ if (commands != null )
105+ log .log (() -> "The command is already registered: '" + mainCommand + "' and have this sub commands registered: '" + commands + "'" );
106+ return null ;
107+ }
108+ this .registerMainCommand (plugin .getName ().toLowerCase (Locale .ROOT ), mainCommand , commandBuilder );
109+ return commandBuilder .getMainCommandHandler ();
110+ });
113111 }
114112
115113 /**
@@ -122,6 +120,30 @@ public MainCommandHandler getCommand(@Nonnull final String command) {
122120 return commandsNew .get (command .toLowerCase (Locale .ROOT ));
123121 }
124122
123+ @ Override
124+ public CommandRegistering registerSubCommand (final CommandProperty subCommand ) {
125+ Set <String > commandLabels = subCommand .getCommandLabels ();
126+
127+ if (addCommands (subCommand , commandLabels )) {
128+ return this ;
129+ }
130+ commandsLegacy .removeIf (oldCommandBuilder -> oldCommandBuilder .equals (subCommand ));
131+ commandsLegacy .removeIf (oldCommandBuilder -> oldCommandBuilder .getCommandLabels ().equals (subCommand .getCommandLabels ()));
132+ commandsLegacy .add (subCommand );
133+ commandsLegacy .sort (Comparator .comparing (CommandProperty ::getFirstSortedLabel , Comparator .nullsLast (String ::compareTo )));
134+ return this ;
135+ }
136+
137+ @ Override
138+ public CommandRegistering registerSubCommands (final CommandProperty ... subCommands ) {
139+ if (subCommands == null )
140+ return this ;
141+ for (CommandProperty registerSubCommand : subCommands ) {
142+ this .registerSubCommand (registerSubCommand );
143+ }
144+ return this ;
145+ }
146+
125147 /**
126148 * Returns the message to display as the command label.
127149 *
@@ -301,7 +323,7 @@ public CommandRegistering setRegisteredMainCommand(final boolean registeredMainC
301323 */
302324 @ Override
303325 public void unregisterSubCommand (String subLabel ) {
304- commands .forEach (commandBuilder -> commandBuilder .getCommandLabels ().removeIf (label -> label .equals (subLabel )));
326+ commandsLegacy .forEach (commandBuilder -> commandBuilder .getCommandLabels ().removeIf (label -> label .equals (subLabel )));
305327 }
306328
307329 /**
@@ -312,7 +334,7 @@ public void unregisterSubCommand(String subLabel) {
312334 */
313335 @ Override
314336 public List <CommandProperty > getCommands () {
315- return Collections .unmodifiableList (commands );
337+ return Collections .unmodifiableList (commandsLegacy );
316338 }
317339
318340 /**
@@ -337,7 +359,7 @@ public CommandProperty getCommandBuilder(String label) {
337359 @ Nullable
338360 @ Override
339361 public CommandProperty getCommandBuilder (String label , boolean startsWith ) {
340- for (final CommandProperty command : commands ) {
362+ for (final CommandProperty command : commandsLegacy ) {
341363 if (startsWith && (label .isEmpty () || command .firstLabelMatch (label , true ) != null ))
342364 return command ;
343365 if (command .firstLabelMatch (label ) != null )
@@ -405,8 +427,8 @@ public boolean addCommands(CommandProperty subCommand, Set<String> commandLabels
405427 if (label == null )
406428 throw new CommandException ("&c" + "You can´t register a command with a label set to null." );
407429 }
408- commands .add (subCommand );
409- commands .sort (Comparator .comparing (CommandProperty ::getFirstSortedLabel , Comparator .nullsLast (String ::compareTo )));
430+ commandsLegacy .add (subCommand );
431+ commandsLegacy .sort (Comparator .comparing (CommandProperty ::getFirstSortedLabel , Comparator .nullsLast (String ::compareTo )));
410432 return true ;
411433 } else {
412434 throw new CommandException ("&c" + "You can´t register a command without labels" );
@@ -446,11 +468,11 @@ public boolean equals(final Object o) {
446468 if (this == o ) return true ;
447469 if (!(o instanceof CommandRegister )) return false ;
448470 final CommandRegister that = (CommandRegister ) o ;
449- return registeredMainCommand == that .registeredMainCommand && commands .equals (that .commands ) && Objects .equals (commandLabelMessage , that .commandLabelMessage ) && Objects .equals (commandLabelMessageNoPerms , that .commandLabelMessageNoPerms ) && Objects .equals (prefixMessage , that .prefixMessage ) && Objects .equals (suffixMessage , that .suffixMessage );
471+ return registeredMainCommand == that .registeredMainCommand && commandsLegacy .equals (that .commandsLegacy ) && Objects .equals (commandLabelMessage , that .commandLabelMessage ) && Objects .equals (commandLabelMessageNoPerms , that .commandLabelMessageNoPerms ) && Objects .equals (prefixMessage , that .prefixMessage ) && Objects .equals (suffixMessage , that .suffixMessage );
450472 }
451473
452474 @ Override
453475 public int hashCode () {
454- return Objects .hash (commands , commandLabelMessage , commandLabelMessageNoPerms , prefixMessage , suffixMessage , registeredMainCommand );
476+ return Objects .hash (commandsLegacy , commandLabelMessage , commandLabelMessageNoPerms , prefixMessage , suffixMessage , registeredMainCommand );
455477 }
456478}
0 commit comments