-
Notifications
You must be signed in to change notification settings - Fork 7
Setting up the CommandHandler
Note: Like all BukkitLib implementations, using this is completely optional and up to you.
Note: When handling commands with BukkitLib, registering commands in plugin.yml is still required.
Handling commands in plugins isn't usually a developer's favorite task as it is a redundant and lengthy. Especially in larger projects, organizing commands is a pain. Often, a design solution won't work due to certain requirements it doesn't provide. Most agree that for an optimal solution, commands would have to be spread across several classes, one per class is usually optimal, but the structure and organizing is usually stressful.
BukkitLib provides a full featured suite for handling commands. Over the next tutorials in this section, I'll show you every aspect that comes in to play when handling commands with BukkitLib. Stay tuned for more awesomeness. ;)
With BukkitLib, all commands are loaded as separate classes and instantiated per execution using an instance of CommandHandler. CommandHandler has a few handy methods which allow tweaking of certain settings, we'll get to those in a second. After initialization, you have to call a required method .setCommandLocation(Package). This sets the location where all commands are stored. The easiest way to obtain the package with some trickery is by calling .class.getPackage() from a sample command. For this example, we haven't created a command yet but that will come in the following tutorial. It is suggested you make a separate package for this. This is what it should look like:
package net.pravian.bukkitlibtest.tut;
import net.pravian.bukkitlib.BukkitLib;
import net.pravian.bukkitlib.command.BukkitCommandHandler;
import net.pravian.bukkitlib.implementation.BukkitPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
public class BukkitLibTest extends BukkitPlugin {
public BukkitLibTest plugin;
public BukkitCommandHandler handler;
@Override
public void onLoad() {
this.plugin = this;
this.handler = new BukkitCommandHandler(plugin); // Initialize the command handler
}
@Override
public void onEnable() {
BukkitLib.init(plugin);
handler.setCommandLocation(Command_test.class.getPackage()); // Set the location of the commands.
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
return handler.handleCommand(sender, cmd, commandLabel, args);
}
}BukkitLib will automatically attempt to load commands from the package specified. Note that all commands are prefixed with Command_. This is configurable, however. See below for more details.
There are some other options to tweak command handling. All of them are optional, have a pretty basic default and should run fine 'as is' but they provide lots of flexibility. Most methods are self evident, but if you have any questions, feel free to leave a message or see the javadoc.
@Override
public void onEnable() {
BukkitLib.init(plugin);
handler.setCommandLocation(Command_test.class.getPackage());
handler.setOnlyGameMessage(ChatColor.RED + "Only players may use this command!");
handler.setOnlyConsoleMessage(ChatColor.RED + "This command may only be used from console!");
handler.setPermissionMessage(ChatColor.RED + "You don't have permissions for this command!");
handler.setPermissionHolder(new MyPermissionHolder()); // May hold permissions, more on this in a later tutorial.
}Combined with the information in the previous the tutorial, here's a quick example of what you should be able to do now.
package net.pravian.bukkitlibtest;
import net.pravian.bukkitlib.BukkitLib;
import net.pravian.bukkitlib.command.BukkitCommandHandler;
import net.pravian.bukkitlib.config.YamlConfig;
import net.pravian.bukkitlib.implementation.BukkitLogger;
import net.pravian.bukkitlib.implementation.BukkitPlugin;
import net.pravian.bukkitlibtest.command.Command_test;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
public class BukkitLibTest extends BukkitPlugin {
public BukkitLibTest plugin;
public BukkitLogger logger;
public YamlConfig config;
public BukkitCommandHandler handler;
@Override
public void onLoad() {
this.plugin = this;
this.logger = new BukkitLogger(plugin);
this.config = new YamlConfig(plugin, "config.yml");
this.handler = new BukkitCommandHandler(plugin);
}
@Override
public void onEnable() {
BukkitLib.init(plugin);
config.load();
logger.setDebugMode(config.getBoolean("debug"));
logger.debug("Debug mode enabled!");
handler.setCommandLocation(Command_test.class.getPackage());
logger.info(plugin.getName() + " v" + plugin.getVersion() + " is enabled");
}
@Override
public void onDisable() {
logger.info(plugin.getName() + " is disabled");
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
return handler.handleCommand(sender, cmd, commandLabel, args);
}
}Previous: Using PathContainer in Configs, Next: How commands work
Aero is not affiliated with Bukkit or Mojang in any way. All rights belong to their respective owners.
- Home
- About
- Tutorial
- Getting Started
- Extending BukkitPlugin
- Logging with BukkitLogger
- Config Handling
- Handling Commands
- Utility Classes
- Implementations
- Using Metrics - mcstats.org
- Concurrency
- Proxy Classes
- Serializable Objects
- The BukkitLib class
- Example projects
- Online Javadocs
- BukkitDev Page
- Changelogs
- Credits