コード例 #1
0
  /**
   * 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);
    }
  }
コード例 #2
0
  @SuppressWarnings("unchecked")
  public void register(Command command, Map<String, Object> cmdParams) {
    String cmdName = command.getCommandName();

    log.devDebug("register() command=", command, ",cmdParams=", cmdParams);
    command.setPlugin(plugin);
    command.setCommandParameters(cmdParams);

    if (cmdParams.containsKey("name")) cmdName = (String) cmdParams.get("name");

    // we never load the same command twice
    if (loadedCommands.contains(cmdName)) return;

    CraftServer cs = (CraftServer) Bukkit.getServer();
    SimpleCommandMap commandMap = cs.getCommandMap();

    try {
      Constructor<PluginCommand> constructor =
          PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
      constructor.setAccessible(true);

      // construct a new PluginCommand object
      PluginCommand pc = constructor.newInstance(cmdName, plugin);
      pc.setExecutor(command);
      pc.setLabel(cmdName);
      if (command.getUsage() != null) pc.setUsage(command.getUsage());

      // don't set Permission node, not all permission plugins behave
      // with superperms command permissions nicely.
      //			pc.setPermission(command.getCommandPermissionNode());

      // check for aliases defined in the configuration
      Object o = cmdParams.get("aliases");
      if (o != null) {
        List<String> aliases = null;
        if (o instanceof List) {
          aliases = (List<String>) o;
        } else if (o instanceof String) {
          aliases = new ArrayList<String>(2);
          aliases.add((String) o);
        } else log.warn("invalid aliases defined for command ", cmdName, ": ", o);

        if (aliases == null) aliases = new ArrayList<String>(1);

        aliases.add("hsp" + command.getCommandName()); // all commands have "hsp" prefix alias
        pc.setAliases(aliases);
      }
      // otherwise set whatever the command has defined
      else {
        List<String> aliases = new ArrayList<String>(5);
        String[] strAliases = command.getCommandAliases();
        if (strAliases != null) {
          for (String alias : strAliases) {
            aliases.add(alias);
          }
        }
        aliases.add("hsp" + command.getCommandName()); // all commands have "hsp" prefix alias
        pc.setAliases(aliases);
      }

      // register it
      commandMap.register("hsp", pc);
      loadedCommands.add(cmdName);

      Debug.getInstance().devDebug("register() command ", command, " registered");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }