22
33import org .bukkit .command .Command ;
44import org .bukkit .command .CommandSender ;
5+ import org .bukkit .command .PluginCommand ;
56import org .bukkit .command .TabExecutor ;
67import org .bukkit .entity .Player ;
8+ import org .bukkit .plugin .java .JavaPlugin ;
79import org .jetbrains .annotations .NotNull ;
810
911import java .util .ArrayList ;
@@ -25,6 +27,33 @@ public abstract class BaseCommand implements TabExecutor {
2527 private String name ;
2628 private final List <SubCommand > subCommands = new ArrayList <>();
2729
30+ /**
31+ * 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>
37+ *
38+ * @param plugin the JavaPlugin instance used to register the command
39+ * @throws IllegalStateException if the command name has not been set or is empty
40+ * @since 0.0.8-alpha
41+ */
42+ public void register (@ NotNull JavaPlugin plugin ) {
43+ if (name == null || name .isEmpty ()) {
44+ throw new IllegalStateException ("Command name cannot be null or empty!" );
45+ }
46+ PluginCommand command = plugin .getCommand (name );
47+ if (command == null ) {
48+ plugin .getLogger ().warning ("Command '" + name + "' is not defined in plugin.yml!" );
49+ return ;
50+ }
51+ command .setExecutor (this );
52+ command .setTabCompleter (this );
53+ plugin .getLogger ().info ("Registered command: " + name );
54+ }
55+
56+
2857 /**
2958 * Sets the main command name.
3059 *
0 commit comments