Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
*CommandController now send command examples and required permission …
Browse files Browse the repository at this point in the history
…to user. Some javadoc in commandController class
  • Loading branch information
Zolli committed Feb 19, 2013
1 parent 6d1cbe7 commit e029e36
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 9 deletions.
5 changes: 4 additions & 1 deletion resources/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
1 change: 1 addition & 0 deletions resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/com/Zolli/EnderCore/Commands/ECCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ public interface ECCommand {

public boolean execute(CommandSender sender, String args[], String chainedParams);
public String getName();
public boolean isAccessibleFromConsole();
public List<String> getPermission();
public int getArgsLength();
public List<String> getExample();

}

Expand Down
16 changes: 15 additions & 1 deletion src/com/Zolli/EnderCore/Commands/command/infoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
public class infoCommand implements ECCommand {

private List<String> permissions = new ArrayList<String>();
private List<String> examples = new ArrayList<String>();

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] <param2>");
}

@Override
Expand All @@ -26,12 +30,22 @@ public String getName() {
public List<String> getPermission() {
return this.permissions;
}


@Override
public List<String> 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");
Expand Down
86 changes: 80 additions & 6 deletions src/com/Zolli/EnderCore/Commands/commandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ECCommand> handledCommands = new HashMap<String, ECCommand>();

/**
* The main class instance
*/
private EnderCore plugin;
private List<String> 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 = "";
Expand All @@ -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<String> 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<String> examples, CommandSender sender) {
for(String s : examples) {
sender.sendMessage(" - " + s);
}
}

private void sendPermissions(List<String> 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;
}

Expand Down
15 changes: 14 additions & 1 deletion src/com/Zolli/EnderCore/EnderCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down

0 comments on commit e029e36

Please sign in to comment.