示例#1
0
  /** Processes all the commands registered in the server and creates help topics for them. */
  public synchronized void initializeCommands() {
    // ** Load topics from highest to lowest priority order **
    Set<String> ignoredPlugins = new HashSet<String>(yaml.getIgnoredPlugins());

    // Don't load any automatic help topics if All is ignored
    if (ignoredPlugins.contains("All")) {
      return;
    }

    // Initialize help topics from the server's command map
    outer:
    for (Command command : server.getCommandMap().getCommands()) {
      if (commandInIgnoredPlugin(command, ignoredPlugins)) {
        continue;
      }

      // Register a topic
      for (Class c : topicFactoryMap.keySet()) {
        if (c.isAssignableFrom(command.getClass())) {
          HelpTopic t = topicFactoryMap.get(c).createTopic(command);
          if (t != null) addTopic(t);
          continue outer;
        }
        if (command instanceof PluginCommand
            && c.isAssignableFrom(((PluginCommand) command).getExecutor().getClass())) {
          HelpTopic t = topicFactoryMap.get(c).createTopic(command);
          if (t != null) addTopic(t);
          continue outer;
        }
      }
      addTopic(new GenericCommandHelpTopic(command));
    }

    // Initialize command alias help topics
    for (Command command : server.getCommandMap().getCommands()) {
      if (commandInIgnoredPlugin(command, ignoredPlugins)) {
        continue;
      }
      for (String alias : command.getAliases()) {
        if (!helpTopics.containsKey("/" + alias)) {
          addTopic(new CommandAliasHelpTopic("/" + alias, "/" + command.getLabel(), this));
        }
      }
    }

    // Initialize help topics from the server's fallback commands
    for (VanillaCommand command : server.getCommandMap().getFallbackCommands()) {
      if (!commandInIgnoredPlugin(command, ignoredPlugins)) {
        addTopic(new GenericCommandHelpTopic(command));
      }
    }

    // Add alias sub-index
    addTopic(
        new IndexHelpTopic(
            "Aliases",
            "Lists command aliases",
            null,
            Collections2.filter(
                helpTopics.values(), Predicates.instanceOf(CommandAliasHelpTopic.class))));

    // Initialize plugin-level sub-topics
    Map<String, Set<HelpTopic>> pluginIndexes = new HashMap<String, Set<HelpTopic>>();
    fillPluginIndexes(pluginIndexes, server.getCommandMap().getCommands());
    fillPluginIndexes(pluginIndexes, server.getCommandMap().getFallbackCommands());

    for (Map.Entry<String, Set<HelpTopic>> entry : pluginIndexes.entrySet()) {
      addTopic(
          new IndexHelpTopic(
              entry.getKey(),
              "All commands for " + entry.getKey(),
              null,
              entry.getValue(),
              "Below is a list of all " + entry.getKey() + " commands:"));
    }

    // Amend help topics from the help.yml file
    for (HelpTopicAmendment amendment : yaml.getTopicAmendments()) {
      if (helpTopics.containsKey(amendment.getTopicName())) {
        helpTopics
            .get(amendment.getTopicName())
            .amendTopic(amendment.getShortText(), amendment.getFullText());
        if (amendment.getPermission() != null) {
          helpTopics.get(amendment.getTopicName()).amendCanSee(amendment.getPermission());
        }
      }
    }
  }
  @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();
    }
  }