2020 * 3. Tab completion for both subcommands and the main command.
2121 *
2222 * @author jonagamerpro1234
23- * @version 0.0.7 -alpha
23+ * @version 0.0.9 -alpha
2424 */
2525public abstract class BaseCommand implements TabExecutor {
2626
@@ -29,11 +29,6 @@ public abstract class BaseCommand implements TabExecutor {
2929
3030 /**
3131 * Registers this command automatically using the provided JavaPlugin.
32- * <p>
33- * This method sets this BaseCommand instance as both the executor and
34- * the tab completer for the command defined in the plugin's plugin.yml.
35- * It will log a warning if the command is not defined in plugin.yml.
36- * </p>
3732 *
3833 * @param plugin the JavaPlugin instance used to register the command
3934 * @throws IllegalStateException if the command name has not been set or is empty
@@ -43,16 +38,18 @@ public void register(@NotNull JavaPlugin plugin) {
4338 if (name == null || name .isEmpty ()) {
4439 throw new IllegalStateException ("Command name cannot be null or empty!" );
4540 }
41+
4642 PluginCommand command = plugin .getCommand (name );
4743 if (command == null ) {
48- plugin . getLogger (). warning ("Command '" + name + "' is not defined in plugin.yml!" );
44+ CommandApi . log ("Command '" + name + "' is not defined in plugin.yml!" );
4945 return ;
5046 }
47+
5148 command .setExecutor (this );
5249 command .setTabCompleter (this );
53- plugin .getLogger ().info ("Registered command: " + name );
54- }
5550
51+ CommandApi .log ("Registered command: " + name );
52+ }
5653
5754 /**
5855 * Sets the main command name.
@@ -92,7 +89,6 @@ public BaseCommand addSubCommand(SubCommand... subCommands) {
9289
9390 /**
9491 * Logic executed when no subcommand is provided.
95- * Override this in the concrete class if needed.
9692 *
9793 * @param sender the CommandSender executing the command
9894 * @param args the command arguments
@@ -105,8 +101,7 @@ public boolean onCommandMain(@NotNull CommandSender sender, String[] args) {
105101 }
106102
107103 /**
108- * Tab completion suggestions for the main command
109- * (when not completing a subcommand)
104+ * Tab completion suggestions for the main command.
110105 *
111106 * @param sender the CommandSender requesting tab completion
112107 * @param args the command arguments
@@ -118,84 +113,93 @@ public List<String> onTabMain(@NotNull CommandSender sender, String[] args) {
118113 }
119114
120115 /**
121- * Handles command execution.
122- * Routes to subcommands if one matches, otherwise calls onCommandMain.
123- *
124- * @param sender the CommandSender executing the command
125- * @param command the Command object
126- * @param label the command label
127- * @param args the command arguments
128- * @return true if execution was handled
129- * @since 0.0.7-alpha
116+ * Handles command execution with debug, logging, and performance tracking.
117+ * @since 0.0.8-alpha
130118 */
131119 @ Override
132- public boolean onCommand (@ NotNull CommandSender sender , @ NotNull Command command , @ NotNull String label , String @ NotNull [] args ) {
133- if (args .length < 1 ) {
134- return onCommandMain (sender , args );
135- }
120+ public boolean onCommand (
121+ @ NotNull CommandSender sender ,
122+ @ NotNull Command command ,
123+ @ NotNull String label ,
124+ String @ NotNull [] args
125+ ) {
126+ long startTime = System .nanoTime ();
127+
128+ try {
129+
130+ if (args .length < 1 ) {
131+ return onCommandMain (sender , args );
132+ }
136133
137- String subName = args [0 ];
138- for (SubCommand sub : subCommands ) {
139- if (subName .equalsIgnoreCase (sub .name ()) || sub .aliases ().contains (subName )) {
134+ String subName = args [0 ];
140135
141- if (!sub .isEnabled ()) {
142- sender .sendMessage (sub .disabledMessage ());
143- return true ;
144- }
136+ for (SubCommand sub : subCommands ) {
137+ if (subName .equalsIgnoreCase (sub .name ()) || sub .aliases ().contains (subName )) {
145138
146- if ( sub . requiresPermission () && ! sender . hasPermission ( "plugin." + sub .permission () )) {
147- sender . sendMessage ( "§cYou do not have permission to execute this command!" );
148- return true ;
149- }
139+ if (! sub .isEnabled ( )) {
140+ sender . sendMessage ( sub . disabledMessage () );
141+ return true ;
142+ }
150143
151- if (! sub .allowConsole () && !( sender instanceof Player )) {
152- sender .sendMessage ("§cThis command cannot be executed from console !" );
153- return true ;
154- }
144+ if (sub .requiresPermission () && !sender . hasPermission ( "plugin." + sub . permission () )) {
145+ sender .sendMessage ("§cYou do not have permission to execute this command !" );
146+ return true ;
147+ }
155148
156- return sub .onCommand (sender , args );
149+ if (!sub .allowConsole () && !(sender instanceof Player )) {
150+ sender .sendMessage ("§cThis command cannot be executed from console!" );
151+ return true ;
152+ }
153+
154+ return sub .onCommand (sender , args );
155+ }
157156 }
158- }
159157
160- return onCommandMain (sender , args );
158+ return onCommandMain (sender , args );
159+
160+ } catch (Exception exception ) {
161+
162+ CommandApi .handleError (name , sender , exception );
163+ return true ;
164+
165+ } finally {
166+
167+ long executionTimeMs = (System .nanoTime () - startTime ) / 1_000_000 ;
168+ CommandApi .logCommandExecution (name , sender , executionTimeMs );
169+ }
161170 }
162171
163172 /**
164173 * Handles tab completion for the main command and its subcommands.
165174 *
166- * @param sender the CommandSender requesting tab completion
167- * @param command the Command object
168- * @param alias the command alias
169- * @param args the command arguments
170- * @return a list of tab completion suggestions
171175 * @since 0.0.7-alpha
172176 */
173177 @ Override
174- public List <String > onTabComplete (@ NotNull CommandSender sender , @ NotNull Command command , @ NotNull String alias , String @ NotNull [] args ) {
178+ public List <String > onTabComplete (
179+ @ NotNull CommandSender sender ,
180+ @ NotNull Command command ,
181+ @ NotNull String alias ,
182+ String @ NotNull [] args
183+ ) {
175184
176185 List <String > suggestions = new ArrayList <>();
177186
178187 if (args .length == 1 ) {
179188 String start = args [0 ].toLowerCase ();
180189
181- // Subcommand suggestions
182190 List <String > subNames = subCommands .stream ()
183191 .map (SubCommand ::name )
184192 .filter (n -> n .toLowerCase ().startsWith (start ))
185193 .collect (Collectors .toList ());
186194
187- // Main command suggestions
188- List <String > mainSuggestions = onTabMain (sender , args );
189-
190- subNames .addAll (mainSuggestions );
195+ subNames .addAll (onTabMain (sender , args ));
191196 suggestions .addAll (subNames );
192197
193198 } else if (args .length > 1 ) {
194199 for (SubCommand sub : subCommands ) {
195200 if (args [0 ].equalsIgnoreCase (sub .name ()) || sub .aliases ().contains (args [0 ])) {
196201 List <String > tab = sub .onTabList (sender , args );
197- if (tab != null ) return tab ;
198- return new ArrayList <>();
202+ return tab != null ? tab : new ArrayList <>();
199203 }
200204 }
201205
@@ -205,22 +209,10 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
205209 return suggestions ;
206210 }
207211
208- /**
209- * Returns the list of registered subcommands.
210- *
211- * @return the list of subcommands
212- * @since 0.0.7-alpha
213- */
214212 public List <SubCommand > getSubCommands () {
215213 return subCommands ;
216214 }
217215
218- /**
219- * Returns the name of the main command.
220- *
221- * @return the command name
222- * @since 0.0.7-alpha
223- */
224216 public String getName () {
225217 return name ;
226218 }
0 commit comments