33import org .broken .arrow .library .color .TextTranslator ;
44import org .broken .arrow .library .command .CommandRegister ;
55import org .broken .arrow .library .command .command .CommandProperty ;
6+ import org .broken .arrow .library .command .subcommand .CommandDisplayConfig ;
67import org .bukkit .Location ;
78import org .bukkit .command .Command ;
89import org .bukkit .command .CommandSender ;
910import org .bukkit .entity .Player ;
11+ import org .checkerframework .checker .nullness .qual .NonNull ;
1012
1113import javax .annotation .Nonnull ;
1214import javax .annotation .Nullable ;
@@ -54,16 +56,31 @@ public CommandExecutor(@Nonnull CommandRegister commandRegister, @Nonnull final
5456 */
5557 @ Override
5658 public boolean execute (@ Nonnull final CommandSender sender , @ Nonnull final String commandLabel , @ Nonnull final String [] args ) {
59+ final MainCommandHandler commandHandler = commandRegister .getCommand (commandLabel );
60+
61+ if (commandHandler != null ) {
62+ CommandProperty mainCommand = commandHandler .getMainCommand ();
63+ if (mainCommand != null ) {
64+ final String permission = mainCommand .getPermission ();
65+ if (permission != null && !permission .isEmpty () && !sender .hasPermission (permission )) {
66+ sender .sendMessage ((colors (placeholders (mainCommand .getPermissionMessage ().replace ("{perm}" , permission ), commandLabel , null ))));
67+ return false ;
68+ }
69+ return mainCommand .executeCommand (sender , commandLabel , args );
70+ }
71+ return this .handleSubCommand (sender , commandLabel , commandHandler , args );
72+ }
73+
5774 if (args .length == 0 ) {
5875 this .sendMessage (sender , commandLabel );
5976 }
6077 if (args .length > 0 ) {
6178 if (!this .sendDescriptions (sender , commandLabel , args ))
6279 return false ;
80+
6381 final CommandProperty executor = commandRegister .getCommandBuilder (args [0 ]);
6482 if (executor != null ) {
6583 if (sendDescription (sender , commandLabel , args , executor )) return false ;
66-
6784 if (sendNoPermission (sender , commandLabel , executor )) return false ;
6885
6986 boolean executeCommand = executor .executeCommand (sender , commandLabel , Arrays .copyOfRange (args , 1 , args .length ));
@@ -85,6 +102,26 @@ public boolean execute(@Nonnull final CommandSender sender, @Nonnull final Strin
85102 @ Nonnull
86103 @ Override
87104 public List <String > tabComplete (@ Nonnull final CommandSender sender , @ Nonnull final String alias , @ Nonnull final String [] args ) throws IllegalArgumentException {
105+ final MainCommandHandler commandHandler = commandRegister .getCommand (alias );
106+
107+ if (commandHandler != null ) {
108+ CommandProperty mainCommand = commandHandler .getMainCommand ();
109+ if (mainCommand != null ) {
110+ final List <String > tabComplete = mainCommand .executeTabComplete (sender ,alias ,args );
111+ return tabComplete != null && checkPermission (sender , mainCommand ) ? tabComplete : new ArrayList <>();
112+ }
113+ if (args .length > 0 ) {
114+ final CommandProperty subcommand = commandRegister .getCommandBuilder (args [0 ], true );
115+ if (subcommand == null ) return new ArrayList <>();
116+ if (args .length == 1 ) {
117+ return tabCompleteSubcommands (sender , commandHandler .getSubcommands (), args [0 ], subcommand .isHideLabel ());
118+ }
119+ final List <String > tabComplete = subcommand .executeTabComplete (sender , alias , Arrays .copyOfRange (args , 1 , args .length ));
120+ return tabComplete != null && checkPermission (sender , subcommand ) ? tabComplete : new ArrayList <>();
121+ }
122+ return new ArrayList <>();
123+ }
124+
88125 if (args .length > 0 ) {
89126 final CommandProperty subcommand = commandRegister .getCommandBuilder (args [0 ], true );
90127 if (subcommand == null ) return new ArrayList <>();
@@ -101,6 +138,19 @@ public List<String> tabComplete(@Nonnull final CommandSender sender, @Nonnull fi
101138 return tabComplete (sender , alias , args );
102139 }
103140
141+ /**
142+ * Send sub command.
143+ *
144+ * @param sender the sender of the command.
145+ * @param commandLabel the label of the sub command.
146+ */
147+ public void sendSubDescription (final CommandSender sender , final String commandLabel ) {
148+ for (final CommandProperty subcommand : commandRegister .getCommands ()) {
149+ if (isSendLabelMessage (sender , subcommand )) continue ;
150+ sender .sendMessage (placeholders (subcommand .getDescription (), commandLabel , subcommand ));
151+ }
152+ }
153+
104154 private boolean checkPermission (final CommandSender sender , final CommandProperty commandBuilder ) {
105155 if (commandBuilder .getPermission () == null || commandBuilder .getPermission ().isEmpty ()) return true ;
106156 return permissionCheck (sender , commandBuilder .getPermission ());
@@ -129,17 +179,39 @@ private List<String> tabCompleteSubcommands(final CommandSender sender, String p
129179 return tab ;
130180 }
131181
132- /**
133- * Send sub command.
134- *
135- * @param sender the sender of the command.
136- * @param commandLabel the label of the sub command.
137- */
138- public void sendSubDescription (final CommandSender sender , final String commandLabel ) {
139- for (final CommandProperty subcommand : commandRegister .getCommands ()) {
140- if (isSendLabelMessage (sender , subcommand )) continue ;
141- sender .sendMessage (placeholders (subcommand .getDescription (), commandLabel , subcommand ));
182+ private List <String > tabCompleteSubcommands (@ Nonnull final CommandSender sender , @ Nonnull final List <CommandProperty > subcommandsList , @ Nonnull String param , final boolean overridePermission ) {
183+ param = param .toLowerCase ();
184+ final List <String > tab = new ArrayList <>();
185+ for (final CommandProperty subcommand : subcommandsList ) {
186+ final Set <String > setOfLabels = subcommand .getCommandLabels ();
187+ if (!checkPermission (sender , subcommand ) && overridePermission ) {
188+ continue ;
189+ }
190+ for (String label : setOfLabels ) {
191+ if (!label .trim ().isEmpty () && label .startsWith (param )) tab .add (label );
192+ }
193+ }
194+ return tab ;
195+ }
196+
197+ private boolean handleSubCommand (@ NonNull final CommandSender sender , @ NonNull final String commandLabel , @ NonNull final MainCommandHandler commandHandler , @ NonNull final String [] args ) {
198+ if (commandHandler .isSubCommandsSet ()) return false ;
199+
200+ if (args .length == 0 ) {
201+ this .sendMessage (sender , commandHandler .getCommandDisplayConfig (), commandLabel );
202+ return false ;
203+ }
204+
205+ final CommandProperty subCommand = commandHandler .getCommandBuilder (args [0 ]);
206+ if (subCommand != null ) {
207+ if (this .sendNoPermission (sender , commandLabel , subCommand )) return false ;
208+ if (this .sendDescription (sender , commandLabel , args , subCommand )) return false ;
209+
210+ final boolean executeCommand = subCommand .executeCommand (sender , commandLabel , Arrays .copyOfRange (args , 1 , args .length ));
211+ this .sendUsageMessage (sender , commandLabel , subCommand , executeCommand );
212+ return executeCommand ;
142213 }
214+ return false ;
143215 }
144216
145217 private boolean isSendLabelMessage (final CommandSender sender , final CommandProperty subcommand ) {
@@ -150,7 +222,6 @@ private boolean isSendLabelMessage(final CommandSender sender, final CommandProp
150222 }
151223
152224 private void sendMessage (final CommandSender sender , final String commandLabel ) {
153-
154225 final List <String > helpPrefixMessage = commandRegister .getPrefixMessage ();
155226 if (helpPrefixMessage != null && !helpPrefixMessage .isEmpty ())
156227 for (final String prefixMessage : helpPrefixMessage )
@@ -170,6 +241,27 @@ private void sendMessage(final CommandSender sender, final String commandLabel)
170241 sender .sendMessage (colors (suffixMessage ));
171242 }
172243
244+ private void sendMessage (@ Nonnull final CommandSender sender , @ Nonnull final CommandDisplayConfig commandDisplayConfig , @ Nonnull final String commandLabel ) {
245+
246+ final List <String > helpPrefixMessage = commandDisplayConfig .getPrefixMessage ();
247+ if (helpPrefixMessage != null && !helpPrefixMessage .isEmpty ())
248+ for (final String prefixMessage : helpPrefixMessage )
249+ sender .sendMessage (colors (prefixMessage ));
250+
251+ final String commandLabelMessage = commandDisplayConfig .getCommandLabelMessage ();
252+ final String labelMessageNoPerms = commandDisplayConfig .getCommandLabelMessageNoPerms ();
253+ if (labelMessageNoPerms != null && !labelMessageNoPerms .isEmpty () && !permissionCheck (sender , commandDisplayConfig .getCommandLabelPermission ())) {
254+ sender .sendMessage (colors (placeholders (labelMessageNoPerms , commandLabel , null )));
255+
256+ } else if (commandLabelMessage != null && !commandLabelMessage .isEmpty ()) {
257+ sendToSender (sender , commandLabel , commandLabelMessage , labelMessageNoPerms );
258+ }
259+ final List <String > helpSuffixMessage = commandDisplayConfig .getSuffixMessage ();
260+ if (helpSuffixMessage != null && !helpSuffixMessage .isEmpty ())
261+ for (final String suffixMessage : helpSuffixMessage )
262+ sender .sendMessage (colors (suffixMessage ));
263+ }
264+
173265 private void sendToSender (final CommandSender sender , final String commandLabel , final String commandLabelMessage , final String labelMessageNoPerms ) {
174266 for (final CommandProperty subcommand : commandRegister .getCommands ()) {
175267 if (subcommand .isHideLabel () && !checkPermission (sender , subcommand )) {
0 commit comments