public static void unregisterCommand(String commandName) {
   CommandMap commandMap = getCommandMap();
   if (commandMap != null) {
     Command command = commandMap.getCommand(commandName.toLowerCase());
     if (command != null) {
       unregisterCommand(command, commandMap);
     }
   }
 }
 /**
  * Get a command, include server commands [subject to change].
  *
  * @param alias
  * @return
  */
 public static Command getCommand(final String alias) {
   final String lcAlias = alias.trim().toLowerCase();
   final CommandMap map = getCommandMap();
   if (map != null) {
     return map.getCommand(lcAlias);
   } else {
     // TODO: maybe match versus plugin commands.
     return null;
   }
 }
Beispiel #3
0
 private void register() {
   try {
     Field f =
         Class.forName("org.bukkit.craftbukkit." + VERSION + ".CraftServer")
             .getDeclaredField("commandMap");
     f.setAccessible(true);
     CommandMap map = (CommandMap) f.get(Bukkit.getServer());
     map.register(this.plugin.getName(), this);
   } catch (Exception exc) {
     exc.printStackTrace();
   }
 }
Beispiel #4
0
  /**
   * Used by the {@link CmdRegistration} to register the command.
   *
   * <p><b>Do not call this to register a command!</b>
   *
   * <p>You'll have to use {@link CmdRegistration#register(Plugin, BaseCmd)} to register a command
   * properly! Also don't forget to call {@link CmdRegistration#unregister(Plugin)} in onDisable()
   *
   * @param plugin The plugin that registered the command.
   * @throws CmdAlreadyRegisteredException When the command is already registered.
   */
  public void register(Plugin plugin) throws CmdAlreadyRegisteredException {
    setUsage(getUsage(plugin.getServer().getConsoleSender(), getBaseCmd().getName()));

    if (this.plugin != null) {
      throw new CmdAlreadyRegisteredException(plugin, this);
    }
    this.plugin = plugin;

    try {
      Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
      f.setAccessible(true);
      CommandMap commandMap = (CommandMap) f.get(Bukkit.getServer());

      commandMap.register(plugin.getName(), this);
    } catch (NoSuchFieldException | IllegalAccessException e) {
      e.printStackTrace();
    }
  }
 public boolean register(List<CommandInfo> registered) {
   CommandMap commandMap = getCommandMap();
   if (registered == null || commandMap == null) {
     return false;
   }
   for (CommandInfo command : registered) {
     DynamicPluginCommand cmd =
         new DynamicPluginCommand(
             command.getAliases(),
             command.getDesc(),
             "/" + command.getAliases()[0] + " " + command.getUsage(),
             executor,
             command.getRegisteredWith(),
             plugin);
     cmd.setPermissions(command.getPermissions());
     commandMap.register(plugin.getDescription().getName(), cmd);
   }
   return true;
 }
  public static void scan() {
    CommandMap commandMap = getCommandMap();
    if (commandMap == null) {
      TFM_Log.severe("Error loading commandMap.");
      return;
    }
    COMMAND_LIST.clear();
    COMMAND_LIST.addAll(getCommands());

    for (TFM_CommandInfo commandInfo : COMMAND_LIST) {
      TFM_DynamicCommand dynamicCommand = new TFM_DynamicCommand(commandInfo);

      Command existing = commandMap.getCommand(dynamicCommand.getName());
      if (existing != null) {
        unregisterCommand(existing, commandMap);
      }

      commandMap.register(TotalFreedomMod.plugin.getDescription().getName(), dynamicCommand);
    }

    TFM_Log.info("TFM commands loaded.");
  }
 public static void registerCommand(final Command command) {
   CommandMap commandMap = getCommandMap();
   if (commandMap != null) {
     commandMap.register("/", command);
   }
 }
  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.");
  }