Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.tristana.commons.command;
- import java.util.HashMap;
- import java.util.Map;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import it.tristana.commons.helper.CommonsHelper;
- public class MainCommand implements CommandExecutor {
- private String command;
- private Map<String, SubCommand> commands;
- public MainCommand(String command) {
- this.command = command;
- commands = new HashMap<String, SubCommand>();
- registerSubCommand(CommandHelp.COMMAND, new CommandHelp(this));
- }
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (args.length > 0) {
- SubCommand subCommand = commands.get(args[0].toLowerCase());
- if (subCommand != null && canExecute(sender, subCommand)) {
- subCommand.execute(sender, args);
- }
- else {
- help(sender);
- }
- }
- else {
- help(sender);
- }
- return true;
- }
- static boolean canExecute(CommandSender sender, SubCommand command) {
- boolean result = false;
- String permission = command.getPermission();
- if (permission == null || sender.hasPermission(permission)) {
- if (command.isOnlyPlayers()) {
- if (sender instanceof Player) {
- result = true;
- }
- }
- else {
- result = true;
- }
- }
- return result;
- }
- private void help(CommandSender sender) {
- CommonsHelper.info(sender, String.format("Type '/%s %s' for a list of commands", command, CommandHelp.COMMAND));
- }
- public void registerSubCommand(String name, SubCommand command) {
- commands.put(name.toLowerCase(), command);
- }
- public String getCommand() {
- return command;
- }
- public Map<String, SubCommand> getCommands() {
- return commands;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement