Class CommandManager
java.lang.Object
org.bukkit.command.Command
org.bukkit.command.defaults.BukkitCommand
tk.airshipcraft.commonlib.commands.CommandManager
- Direct Known Subclasses:
PreferenceCommand
public abstract class CommandManager
extends org.bukkit.command.defaults.BukkitCommand
This abstract class CommandManager extends BukkitCommand to facilitate the registration and management of custom commands in a Minecraft server.
It allows for the registration of commands directly to the server's CommandMap without the need for specifying them in the plugin.yml file.
This approach offers greater flexibility and programmatic control over command registration and handling.
- Since:
- 2022-07-28
- Version:
- 0.0.1
- Author:
- notzune
-
Field Summary
Fields inherited from class org.bukkit.command.Command
description, timings, usageMessage
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
CommandManager
(@NotNull String command, @NotNull String description, @NotNull String permission, @NotNull String[] aliases) Constructs and registers a new command with the provided details. -
Method Summary
Modifier and TypeMethodDescriptionboolean
execute
(@NotNull org.bukkit.command.CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) This method is invoked when a command registered to this CommandManager is executed.abstract void
Abstract method to be implemented by subclasses to define the command's behavior.onTabComplete
(org.bukkit.command.CommandSender sender, String[] args) Abstract method to provide custom tab completion options.tabComplete
(@NotNull org.bukkit.command.CommandSender sender, @NotNull String alias, @NotNull String[] args) Provides tab completion options for this command.Methods inherited from class org.bukkit.command.Command
broadcastCommandMessage, broadcastCommandMessage, broadcastCommandMessage, broadcastCommandMessage, getAliases, getDescription, getLabel, getName, getPermission, getPermissionMessage, getTimingName, getUsage, isRegistered, permissionMessage, permissionMessage, register, setAliases, setDescription, setLabel, setName, setPermission, setPermissionMessage, setUsage, tabComplete, testPermission, testPermissionSilent, toString, unregister
-
Constructor Details
-
CommandManager
protected CommandManager(@NotNull @NotNull String command, @NotNull @NotNull String description, @NotNull @NotNull String permission, @NotNull @NotNull String[] aliases) Constructs and registers a new command with the provided details. The command is immediately registered to the server's CommandMap upon creation of the instance.- Parameters:
command
- The primary name of the command (e.g., "teleport").description
- A brief description of what the command does, displayed in command help.permission
- The permission node required to use this command.aliases
- Alternative names for this command (e.g., "tp" as an alias for "teleport").
-
-
Method Details
-
execute
public boolean execute(@NotNull @NotNull org.bukkit.command.CommandSender sender, @NotNull @NotNull String commandLabel, @NotNull @NotNull String[] args) This method is invoked when a command registered to this CommandManager is executed. It should be overridden in subclasses to define the specific behavior of the command. This method wraps the abstract execute method with exception handling for SQL exceptions.- Specified by:
execute
in classorg.bukkit.command.Command
- Parameters:
sender
- The entity that sent the command (player, console, command block, etc.).commandLabel
- The exact command alias used by the sender.args
- Command arguments separated by spaces.- Returns:
- true if the command was executed successfully, false otherwise.
-
execute
public abstract void execute(org.bukkit.command.CommandSender sender, String[] args) throws SQLException Abstract method to be implemented by subclasses to define the command's behavior. This method should contain the core logic for the command's execution.- Parameters:
sender
- Source object which is executing this command.args
- Command arguments passed to the command.- Throws:
SQLException
- If a database access error occurs.
-
tabComplete
@NotNull public @NotNull List<String> tabComplete(@NotNull @NotNull org.bukkit.command.CommandSender sender, @NotNull @NotNull String alias, @NotNull @NotNull String[] args) throws IllegalArgumentException Provides tab completion options for this command. This method can be overridden to customize the tab completion behavior for the command.- Overrides:
tabComplete
in classorg.bukkit.command.Command
- Parameters:
sender
- Source object which is executing this command.alias
- The alias of the command used.args
- Command arguments passed to the command.- Returns:
- A List of Strings containing potential tab complete options.
- Throws:
IllegalArgumentException
- If the arguments are not valid for tab completion.
-
onTabComplete
Abstract method to provide custom tab completion options. This method is called by tabComplete and should be overridden in subclasses to define specific tab completion logic.Example:
if (args.length == 2) { List<String> names = new ArrayList<>(); for (Player player : Bukkit.getOnlinePlayers()) { names.add(player.getName()); } return StringUtil.copyPartialMatches(args[1], names, new ArrayList<>()); }
- Parameters:
sender
- The entity that sent the command.args
- Command arguments passed to the command.- Returns:
- A List of Strings containing tab completion options.
-