예제 #1
0
 public boolean unregisterCommands() {
   CommandMap commandMap = getCommandMap();
   List<String> toRemove = new ArrayList<String>();
   Map<String, org.bukkit.command.Command> knownCommands =
       ReflectionUtil.getField(commandMap, "knownCommands");
   Set<String> aliases = ReflectionUtil.getField(commandMap, "aliases");
   if (knownCommands == null || aliases == null) {
     return false;
   }
   for (Iterator<org.bukkit.command.Command> i = knownCommands.values().iterator();
       i.hasNext(); ) {
     org.bukkit.command.Command cmd = i.next();
     if (cmd instanceof DynamicPluginCommand
         && ((DynamicPluginCommand) cmd).getOwner().equals(executor)) {
       i.remove();
       for (String alias : cmd.getAliases()) {
         org.bukkit.command.Command aliasCmd = knownCommands.get(alias);
         if (cmd.equals(aliasCmd)) {
           aliases.remove(alias);
           toRemove.add(alias);
         }
       }
     }
   }
   for (String string : toRemove) {
     knownCommands.remove(string);
   }
   return true;
 }
예제 #2
0
  /**
   * Unregisters a command from the server.
   *
   * <p>After a command has been unregistered, it will no longer be able to be executed.
   *
   * @param command The command to unregister.
   * @param commandMap The commandMap to unregister the command from.
   */
  public static void unregisterCommand(Command command, CommandMap commandMap) {
    command.unregister(commandMap);
    HashMap<String, Command> knownCommands = getKnownCommands(commandMap);

    if (knownCommands == null) {
      return;
    }

    knownCommands.remove(command.getName());

    for (String alias : command.getAliases()) {
      knownCommands.remove(alias);
    }
  }
 public static void unregisterCommand(Command command, CommandMap commandMap) {
   try {
     command.unregister(commandMap);
     HashMap<String, Command> knownCommands = getKnownCommands(commandMap);
     if (knownCommands != null) {
       knownCommands.remove(command.getName());
       for (String alias : command.getAliases()) {
         knownCommands.remove(alias);
       }
     }
   } catch (Exception ex) {
     TFM_Log.severe(ex);
   }
 }
예제 #4
0
  /** {@inheritDoc} */
  public boolean register(String label, String fallbackPrefix, Command command) {
    label = label.toLowerCase().trim();
    fallbackPrefix = fallbackPrefix.toLowerCase().trim();
    boolean registered = register(label, command, false, fallbackPrefix);

    Iterator<String> iterator = command.getAliases().iterator();
    while (iterator.hasNext()) {
      if (!register(iterator.next(), command, true, fallbackPrefix)) {
        iterator.remove();
      }
    }

    // If we failed to register under the real name, we need to set the command label to the direct
    // address
    if (!registered) {
      command.setLabel(fallbackPrefix + ":" + label);
    }

    // Register to us so further updates of the commands label and aliases are postponed until its
    // reregistered
    command.register(this);

    return registered;
  }
예제 #5
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());
        }
      }
    }
  }
예제 #6
0
  public void help(CommandSender sender, Command command, String[] args) {
    Integer page = 1;

    if (args != null && args.length > 1) {
      try {
        page = Integer.valueOf(args[1]);
      } catch (Exception e) {
        sendMessage(
            sender, ChatColor.RED + " " + args[1] + " is not a number, showing help for page 1.");
      }
    }

    List<String> available = new ArrayList<String>();
    List<String> unavailable = new ArrayList<String>();
    List<String> onlyop = new ArrayList<String>();
    Set<Method> dups = new HashSet<Method>();
    for (MethodWrapper mw : usage) {
      if (!dups.add(mw.method)) continue;
      MCCommand cmd = mw.getCommand();
      final String use = "&6/" + command.getName() + " " + mw.usage;
      if (cmd.op() && !sender.isOp()) continue;
      else if (cmd.admin() && !hasAdminPerms(sender)) continue;
      else if (!cmd.perm().isEmpty() && !sender.hasPermission(cmd.perm())) unavailable.add(use);
      else available.add(use);
    }
    int npages = available.size() + unavailable.size();
    if (sender.isOp()) npages += onlyop.size();
    npages = (int) Math.ceil((float) npages / LINES_PER_PAGE);
    if (page > npages || page <= 0) {
      if (npages <= 0) {
        sendMessage(sender, "&4There are no methods for this command");
      } else {
        sendMessage(sender, "&4That page doesnt exist, try 1-" + npages);
      }
      return;
    }
    if (command != null && command.getAliases() != null && !command.getAliases().isEmpty()) {
      String aliases = StringUtils.join(command.getAliases(), ", ");
      sendMessage(
          sender,
          "&eShowing page &6"
              + page
              + "/"
              + npages
              + "&6 : /"
              + command.getName()
              + " help <page number>");
      sendMessage(sender, "&e    command &6" + command.getName() + "&e has aliases: &6" + aliases);
    } else {
      sendMessage(
          sender, "&eShowing page &6" + page + "/" + npages + "&6 : /cmd help <page number>");
    }
    int i = 0;
    for (String use : available) {
      i++;
      if (i < (page - 1) * LINES_PER_PAGE || i >= page * LINES_PER_PAGE) continue;
      sendMessage(sender, use);
    }
    for (String use : unavailable) {
      i++;
      if (i < (page - 1) * LINES_PER_PAGE || i >= page * LINES_PER_PAGE) continue;
      sendMessage(sender, ChatColor.RED + "[Insufficient Perms] " + use);
    }
    if (sender.isOp()) {
      for (String use : onlyop) {
        i++;
        if (i < (page - 1) * LINES_PER_PAGE || i >= page * LINES_PER_PAGE) continue;
        sendMessage(sender, ChatColor.AQUA + "[OP only] &6" + use);
      }
    }
  }
  public final void load() {
    blockedCommands.clear();

    final CommandMap commandMap = TFM_CommandLoader.getInstance().getCommandMap();
    if (commandMap == null) {
      TFM_Log.severe("Error loading commandMap.");
      return;
    }

    List<String> _blockedCommands = (List<String>) TFM_ConfigEntry.BLOCKED_COMMANDS.getList();
    for (String rawEntry : _blockedCommands) {
      final String[] parts = rawEntry.split(":");
      if (parts.length < 3 || parts.length > 4) {
        continue;
      }

      final CommandBlockerRank rank = CommandBlockerRank.fromToken(parts[0]);
      if (rank == null) {
        continue;
      }

      final CommandBlockerAction action = CommandBlockerAction.fromToken(parts[1]);
      if (action == null) {
        continue;
      }

      String command = parts[2];
      if (command == null || command.isEmpty()) {
        continue;
      }
      final Matcher matcher = COMMAND_PATTERN.matcher(command);
      if (matcher.find()) {
        command = matcher.group(1);
        if (command == null) {
          continue;
        } else {
          command = command.toLowerCase().trim();
        }
      } else {
        continue;
      }

      String message = null;
      if (parts.length == 4) {
        message = parts[3];
      }

      final CommandBlockerEntry blockedCommandEntry =
          new CommandBlockerEntry(rank, action, command, message);

      final Command bukkitCommand = commandMap.getCommand(command);
      if (bukkitCommand == null) {
        // TFM_Log.info("Blocking unknown command: " + blockedCommandEntry.getCommand());
        blockedCommands.put(blockedCommandEntry.getCommand(), blockedCommandEntry);
      } else {
        blockedCommandEntry.setCommand(bukkitCommand.getName().toLowerCase());

        // TFM_Log.info("Blocking command: " + blockedCommandEntry.getCommand());
        blockedCommands.put(blockedCommandEntry.getCommand(), blockedCommandEntry);

        for (String alias : bukkitCommand.getAliases()) {
          // TFM_Log.info("Blocking alias: " + alias.toLowerCase() + " of " +
          // blockedCommandEntry.getCommand());
          blockedCommands.put(alias.toLowerCase(), blockedCommandEntry);
        }
      }
    }

    TFM_Log.info("Loaded " + blockedCommands.size() + " blocked commands.");
  }