Skip to content

Commit 02780ef

Browse files
0.0.8-alpha, bug fix BaseCommand
1 parent 5bcff3b commit 02780ef

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>jss.commandapi</groupId>
88
<artifactId>command-api</artifactId>
9-
<version>0.0.7-alpha</version>
9+
<version>0.0.8-alpha</version>
1010

1111
<properties>
1212
<java.version>8</java.version>

src/main/java/jss/commandapi/BaseCommand.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.bukkit.command.Command;
44
import org.bukkit.command.CommandSender;
5+
import org.bukkit.command.PluginCommand;
56
import org.bukkit.command.TabExecutor;
67
import org.bukkit.entity.Player;
8+
import org.bukkit.plugin.java.JavaPlugin;
79
import org.jetbrains.annotations.NotNull;
810

911
import 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

Comments
 (0)