diff --git a/resources/en.yml b/resources/en.yml index 0ece3e6..21afb3f 100644 --- a/resources/en.yml +++ b/resources/en.yml @@ -2,4 +2,7 @@ about: langName: 'English' author: 'Zolli' -test: 'Test' +commands: + noPermission: 'You dont have permission to access this command! You need one of the following permission:' + noConsole: 'This command must be running as player!' + badUsage: 'Bad syntax! Try this:' diff --git a/resources/plugin.yml b/resources/plugin.yml index 64d6dc7..38fbf2f 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -3,6 +3,7 @@ version: 0.1 authors: [Zolli] main: com.Zolli.EnderCore.EnderCore load: POSTWORLD +softdepend: [PermissionsEx,Permissions,GroupManager,Vault,Essentials,iConomy,BOSEconomy] commands: ec: description: Short version of endercore comamnd diff --git a/src/com/Zolli/EnderCore/Commands/ECCommand.java b/src/com/Zolli/EnderCore/Commands/ECCommand.java index db65e55..4066aec 100644 --- a/src/com/Zolli/EnderCore/Commands/ECCommand.java +++ b/src/com/Zolli/EnderCore/Commands/ECCommand.java @@ -8,8 +8,10 @@ public interface ECCommand { public boolean execute(CommandSender sender, String args[], String chainedParams); public String getName(); + public boolean isAccessibleFromConsole(); public List getPermission(); public int getArgsLength(); + public List getExample(); } diff --git a/src/com/Zolli/EnderCore/Commands/command/infoCommand.java b/src/com/Zolli/EnderCore/Commands/command/infoCommand.java index 4e69444..60a7993 100644 --- a/src/com/Zolli/EnderCore/Commands/command/infoCommand.java +++ b/src/com/Zolli/EnderCore/Commands/command/infoCommand.java @@ -11,10 +11,14 @@ public class infoCommand implements ECCommand { private List permissions = new ArrayList(); + private List examples = new ArrayList(); public infoCommand() { this.permissions.add("ec.basic.info"); this.permissions.add("ec.info"); + + this.examples.add("/ec info [param1] [param2]"); + this.examples.add("/ec info [param1] "); } @Override @@ -26,12 +30,22 @@ public String getName() { public List getPermission() { return this.permissions; } - + + @Override + public List getExample() { + return this.examples; + } + @Override public int getArgsLength() { return 2; } + @Override + public boolean isAccessibleFromConsole() { + return false; + } + @Override public boolean execute(CommandSender sender, String[] allArgs, String chainedParams) { consoleMenuBuilder m = new consoleMenuBuilder("EnderCore - Info"); diff --git a/src/com/Zolli/EnderCore/Commands/commandHandler.java b/src/com/Zolli/EnderCore/Commands/commandHandler.java index 0c27b41..931eb0a 100644 --- a/src/com/Zolli/EnderCore/Commands/commandHandler.java +++ b/src/com/Zolli/EnderCore/Commands/commandHandler.java @@ -11,21 +11,43 @@ import com.Zolli.EnderCore.EnderCore; - public class commandHandler implements CommandExecutor { - + + /** + * The map storing all comands handled by this class + */ private Map handledCommands = new HashMap(); + + /** + * The main class instance + */ private EnderCore plugin; - private List commandPermissions; + /** + * Constructor + * @param instance Tha main class + */ public commandHandler(EnderCore instance) { this.plugin = instance; } + /** + * Register a command for handling. Put it's name to handledComamnds map + * @param name the command name + * @param command The ECCommand object + */ public void registerCommand(String name, ECCommand command) { this.handledCommands.put(name, command); } + /** + * Build arguments and chain the rest pieces of the array + * The parameters length is defined by the ECCommand object itself on the + * getArgsLength() function + * @param str All arguments in a string array + * @param length The length of required argument + * @return String chained parameters + */ private String buildArgs(String[] str, int length) { int arrayLength = str.length; String param = ""; @@ -35,14 +57,66 @@ private String buildArgs(String[] str, int length) { return param; } - + + /** + * Check all defined permission for the command sender + * @param nodes A list of permission nodes + * @param sender CommandSender object + * @return Boolean False if CommandSender does not have the defined permission + */ + private boolean checkPerm(List nodes, CommandSender sender) { + for(String node : nodes) { + if(!this.plugin.permission.has(sender, node)) { + sender.sendMessage(this.plugin.local.getLocalizedString("commands.noPermission")); + this.sendPermissions(nodes, sender); + return false; + } + } + return false; + } + + /** + * Send example commands defined in ECCOmmand class + * @param examples Command examples + * @param sender CommandSender object + */ + private void sendCommandExamples(List examples, CommandSender sender) { + for(String s : examples) { + sender.sendMessage(" - " + s); + } + } + + private void sendPermissions(List permissions, CommandSender sender) { + for(String s : permissions) { + sender.sendMessage(" - " + s); + } + } + @Override public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] arg3) { ECCommand command = this.handledCommands.get(arg3[0]); - this.commandPermissions = command.getPermission(); String chainedParam = this.buildArgs(arg3, command.getArgsLength()); - command.execute(sender, arg3, chainedParam); + if(command.getArgsLength() > arg3.length-1) { + sender.sendMessage(this.plugin.local.getLocalizedString("commands.badUsage")); + this.sendCommandExamples(command.getExample(), sender); + } else { + if(sender instanceof ConsoleCommandSender) { + if(command.isAccessibleFromConsole()) { + if(!this.checkPerm(command.getPermission(), sender)) { + return false; + } + command.execute(sender, arg3, chainedParam); + } else { + sender.sendMessage(this.plugin.local.getLocalizedString("commands.noConsole")); + } + } else { + if(!this.checkPerm(command.getPermission(), sender)) { + return false; + } + command.execute(sender, arg3, chainedParam); + } + } return false; } diff --git a/src/com/Zolli/EnderCore/EnderCore.java b/src/com/Zolli/EnderCore/EnderCore.java index ccac37f..3430b58 100644 --- a/src/com/Zolli/EnderCore/EnderCore.java +++ b/src/com/Zolli/EnderCore/EnderCore.java @@ -10,6 +10,7 @@ import com.Zolli.EnderCore.Commands.commandHandler; import com.Zolli.EnderCore.Commands.command.infoCommand; import com.Zolli.EnderCore.Configuration.Configuration; +import com.Zolli.EnderCore.Economy.economyHandler; import com.Zolli.EnderCore.Listeners.blockListener; import com.Zolli.EnderCore.Listeners.entityListener; import com.Zolli.EnderCore.Listeners.inventoryListener; @@ -19,6 +20,7 @@ import com.Zolli.EnderCore.Localization.localizationManager; import com.Zolli.EnderCore.Logger.simpleLogger; import com.Zolli.EnderCore.Logger.simpleLogger.Level; +import com.Zolli.EnderCore.Permission.permissionHandler; import com.Zolli.EnderCore.Storage.Storage; import com.Zolli.EnderCore.Storage.storageActions; @@ -79,13 +81,22 @@ public class EnderCore extends JavaPlugin { */ public commandHandler command; + /** + * Permission handler object + */ + public permissionHandler permission; + + /** + * Economy handler object + */ + public economyHandler economy; + /** * Runs when plugin initialization started */ public void onLoad() { this.pluginDescription = getDescription(); this.pluginManager = this.getServer().getPluginManager(); - this.dataFolder = this.getDataFolder(); this.logger = new simpleLogger(this.pluginDescription, this.dataFolder, "EnderCore.Log"); this.mainConfig = new Configuration(this, "config.yml"); @@ -98,6 +109,8 @@ public void onEnable() { config = mainConfig.config; String driver = config.getString("database.type"); + this.permission = new permissionHandler(this); + this.economy = new economyHandler(this); this.storage = new Storage(this, driver, this.dataFolder, this.config, this.logger); this.dbAction = new storageActions(this.storage); this.local = new localizationManager(this);