@SuppressWarnings("unchecked") public <T> T getCommand(T instance) { for (CommandConfig cc : commands) { if (cc.getClass().equals(instance.getClass())) { return (T) cc; } } throw new NoSuchCommandException(instance.getClass().getName()); }
/** * Register all known HSP commands. This includes those defined by the admin in the config file as * well as all commands found automatically on the command path. */ public void registerAllCommands() { // loop through all config-defined command and load them up Set<String> commands = commandConfig.getDefinedCommands(); for (String cmd : commands) { registerConfigCommand(cmd); } Set<Class<? extends Command>> commandClasses = getCommandClasses(); // now loop through all normal commands in the class path for (Class<? extends Command> clazz : commandClasses) { log.devDebug("checking found class ", clazz); registerDefaultCommand(clazz); } }
/** * Given a class file (which must be of our Command interface), register that Command with Bukkit. * * @param clazz */ private void registerDefaultCommand(Class<? extends Command> clazz) { try { Debug.getInstance().devDebug("registering command class ", clazz); Command cmd = (Command) clazz.newInstance(); String cmdName = cmd.getCommandName(); // do nothing if the command is disabled if (commandConfig.isDisabledCommand(cmdName)) { log.debug( "registerDefaultCommand() skipping ", cmdName, " because it is flagged as disabled"); return; } register(cmd); } catch (Exception e) { log.severe(e, "error trying to load command class " + clazz); } }
/** * Given a command name, look for and register that command from the admin-configured command * definitions. * * @param cmd * @param classes */ private void registerConfigCommand(String cmd) { log.devDebug("processing config defined command ", cmd); Map<String, Object> cmdParams = commandConfig.getCommandParameters(cmd); Class<? extends Command> cmdClass = null; String className = null; Object clazz = cmdParams.get("class"); // if no class given, just assume the name of the commmand if (clazz == null) clazz = cmd; if (clazz != null && clazz instanceof String) { className = (String) clazz; // if class given, but no package given, assume default package if (className.indexOf('.') == -1) { String firstChar = className.substring(0, 1); String theRest = className.substring(1); className = firstChar.toUpperCase() + theRest; cmdClass = findCommandClass(className); } } // if we have no commandClass yet, but we do have a className, then // try to find that className. if (cmdClass == null && className != null) { cmdClass = findCommandClass(className); } if (cmdClass == null) { log.warn("No class defined or found for command ", cmd); return; } try { Command command = (Command) cmdClass.newInstance(); command.setCommandName(cmd.toLowerCase()); // default to name of instance key register(command, cmdParams); } catch (ClassCastException e) { log.warn("class " + cmdClass + " does not implement Command interface"); } catch (Exception e) { log.warn(e, "error loading class " + cmdClass); } }
/** * Return true if the given command is defined as a custom command by the admin in the config * file. * * @param cmd * @return */ private boolean isDefinedConfigCommand(String cmd) { return commandConfig.getDefinedCommands().contains(cmd); }
public void register(Command command) { Map<String, Object> cmdParams = commandConfig.getCommandParameters(command.getCommandName()); register(command, cmdParams); }