Skip to content

Instantly share code, notes, and snippets.

@vanZeben
Created August 3, 2013 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanZeben/6145498 to your computer and use it in GitHub Desktop.
Save vanZeben/6145498 to your computer and use it in GitHub Desktop.
package com.zephyrmc.api.cmds;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import com.zephyrmc.api.managers.MessageManager.LogSeverity;
import com.zephyrmc.api.obj.ZephyrCommand;
import com.zephyrmc.api.obj.ZephyrPlugin;
import com.zephyrmc.api.obj.ZephyrUser;
import com.zephyrmc.api.utils.PaginationUtils;
import com.zephyrmc.api.utils.StringMgmt;
/**
* This class should be extended in all main command handler classes with the
* name <Command>CommandExecutor
*
* @author vanZeben
*/
public abstract class ZephyrCommandHandler implements CommandExecutor {
protected final ZephyrPlugin plugin;
private Map<String, ZephyrCommand> registeredCommands = new HashMap<String, ZephyrCommand>();
private String baseCommandName;
/**
* Initializes the commands
*
* @param pluginName
* @param baseCommand
*/
public ZephyrCommandHandler(ZephyrPlugin plugin, String baseCommandName) {
this.plugin = plugin;
this.baseCommandName = baseCommandName;
plugin.getCommand(baseCommandName).setExecutor(this);
this.initRegisteredCommands();
this.registerCommand("help", new String[] { "?" }, null, "Gives you some information on what this command does",
null, null);
}
/**
* Bukkit command handler. Will parse out sub-commands and the /command help
* command as well
*/
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
String subCommand = "";
if (args.length == 0) {
if (this.registeredCommands.get("") != null) {
subCommand = "";
} else {
subCommand = "help";
}
args = new String[0];
} else {
if (this.getOriginalCommand(args[0]) != null) {
subCommand = args[0].toLowerCase();
args = StringMgmt.remFirstArg(args);
} else if (this.registeredCommands.get("") != null) {
subCommand = "";
}
}
if (this.getOriginalCommand(subCommand) == null
&& !this.registeredCommands.containsKey(subCommand)) {
this.invalidSubCommand(sender);
return true;
}
if (this.getOriginalCommand(subCommand) != null) {
subCommand = this.getOriginalCommand(subCommand);
}
ZephyrCommand cmd = this.registeredCommands.get(subCommand);
if (cmd.getSubCommandName().equalsIgnoreCase("help")) {
int page = 1;
if (args.length >= 1) {
try {
page = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
page = 1;
}
}
showHelp(sender, page);
return true;
}
if (sender instanceof Player) {
ZephyrUser user = plugin.getPermissions().getUser(((Player) sender).getName());
if (cmd.hasPermission(user)) {
if (!cmd.getCommandReceiver().onPlayerRunCommand((Player) sender, args)) {
this.invalidSubCommand(sender);
}
} else {
this.invalidPerms(sender);
}
} else {
if (!cmd.getCommandReceiver().onConsoleRunCommand(args)) {
this.invalidSubCommand(sender);
}
}
return true;
}
/**
* Function to register commands
*/
protected abstract void initRegisteredCommands();
/**
* Function to register a command
*
* @param commandName
* Sub-command that relates to the ZephyrCommandReceiver
* @param aliases
* Aliases for the commandName
* @param arguments
* Any additional parameters excluding the sub-command
* @param description
* Description of what the command does
* @param permissions
* Permission level
* @param commandReceiver
* Where the Command Receiver is located
*/
protected void registerCommand(String subCommandName, String[] aliases, String arguments, String description,
String[] permissions,
ZephyrCommandReceiver commandReceiver) {
ZephyrCommand cmd = new ZephyrCommand(this.getName(), subCommandName, aliases, arguments, description, permissions,
commandReceiver);
this.registeredCommands.put(subCommandName.toLowerCase(), cmd);
}
/**
* Sends an invalid command message to the user
*
* @param sender
* Sender who called the command
* @param command
* Command that was called
*/
private void invalidSubCommand(CommandSender sender) {
if (sender instanceof Player) {
plugin.getMessageManager()
.sendMessage(((Player) sender),
"&fYou entered an &cinvalid &fsub-command");
plugin.getMessageManager().sendMessage(((Player) sender),
"&fType &e/" + this.getLowerCaseName() + " help &ffor help");
} else {
plugin.getMessageManager().log(LogSeverity.WARNING,
"[" + this.plugin.getName() + "] You entered an invalid command");
plugin.getMessageManager().log(LogSeverity.WARNING,
"[" + this.plugin.getName() + "] Type /" + this.getLowerCaseName() + " help for help");
}
}
/**
* Sends an invalid command/perms error to the user
*
* @param sender
* Sender who called the command
* @param command
* Command that was called
*/
private void invalidPerms(CommandSender sender) {
if (sender instanceof Player) {
plugin.getMessageManager()
.sendMessage(((Player) sender), "&fYou have &cinvalid &fpermissions");
plugin.getMessageManager()
.sendMessage(
((Player) sender),
"&fType &e/" + this.getLowerCaseName()
+ " help &ffor a list of valid commands");
}
}
/**
* Sends the sender the help pages related to this command
*
* @param sender
* @param page
*/
protected void showHelp(CommandSender sender, int page) {
List<String> helpOutput = new ArrayList<String>();
List<String> output = new ArrayList<String>();
// Permissions
for (ZephyrCommand command : this.registeredCommands.values()) {
if (command.getPermissions() == null || command.getPermissions().length == 0) {
helpOutput.add(command.getHelpMessage());
continue;
} else {
if (sender instanceof ConsoleCommandSender) {
helpOutput.add(command.getHelpMessage());
} else if (sender instanceof Player) {
ZephyrUser user = plugin.getPermissions().getUser(((Player) sender).getName());
if (command.hasPermission(user)) {
helpOutput.add(command.getHelpMessage());
}
}
}
}
// pagination
int numPages = PaginationUtils.getNumPages(helpOutput);
page = PaginationUtils.getCurrentPage(page, numPages);
output = PaginationUtils.paginateInput(helpOutput, page);
if (sender instanceof Player) {
Player player = (Player) sender;
// title
plugin.getMessageManager()
.sendMessage(
player, "&f" + this.getName() + " Help Page &8[&7" + page + "/" + numPages + "&8]");
plugin.getMessageManager()
.sendMessage(
player, "-------------------------");
// content
for (String s : output) {
String[] split = s.split(":");
String newFormat = "&3"
+ split[0].trim()
+ "&7: &b"
+ StringMgmt.join(StringMgmt.remFirstArg(split)).trim();
plugin.getMessageManager()
.sendMessage(
player, newFormat);
}
} else {
// title
sender.sendMessage(this.getName() + " Help Page [" + page + "/" + numPages + "]");
sender.sendMessage("-------------------------");
// content
for (String s : output) {
sender.sendMessage(s);
}
}
}
/**
* Returns the file name of the command, excluding the CommandHandler
*
* @return
*/
public String getName() {
return this.baseCommandName;
}
/**
* Returns the file name of the command, excluding the CommandHandler in lower
* case
*
* @return
*/
public String getLowerCaseName() {
return getName().toLowerCase();
}
public String getOriginalCommand(String alias) {
for (ZephyrCommand c : this.registeredCommands.values()) {
if (c.hasAlias(alias)) { return c.getSubCommandName().toLowerCase(); }
}
return null;
}
}
package com.zephyrmc.api.cmds;
import org.bukkit.entity.Player;
import com.zephyrmc.api.obj.ZephyrPlugin;
/**
* This class should be extended by all sub-command classes
*
* @author vanZeben
*/
public abstract class ZephyrCommandReceiver {
public final ZephyrPlugin plugin;
public ZephyrCommandReceiver(ZephyrPlugin plugin) {
this.plugin = plugin;
}
/**
* Called when a player runs the sub-command
*
* @param player
* Player who ran the sub-command
* @param args
* Arguments the player entered
* @return boolean Whether or not the command has valid args/Whether the
* player has permission
*/
public abstract boolean onPlayerRunCommand(Player player, String[] args);
/**
* Called when console runs the sub-command
*
* @param args
* Arguments the console entered
* @return boolean Whether or not the command has valid args
*/
public abstract boolean onConsoleRunCommand(String[] args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment