private void removeDuplicateCommands(MinecraftServer server) {
    if (server.getCommandManager() instanceof CommandHandler) {
      try {
        Set<String> commandNames = new HashSet<String>();
        Set<String> toRemoveNames = new HashSet<String>();
        CommandHandler cmdMng = (CommandHandler) server.getCommandManager();

        for (Object cmdObj : cmdMng.commandSet) {
          ICommand cmd = (ICommand) cmdObj;
          if (!commandNames.add(cmd.getCommandName())) {
            OutputHandler.debug("Duplicate command found! Name:" + cmd.getCommandName());
            toRemoveNames.add(cmd.getCommandName());
          }
        }
        Set toRemove = new HashSet();
        for (Object cmdObj : cmdMng.commandSet) {
          ICommand cmd = (ICommand) cmdObj;
          if (toRemoveNames.contains(cmd.getCommandName())) {
            Class<?> cmdClass = cmd.getClass();
            if (!cmdClass.getPackage().getName().contains("ForgeEssentials")) {
              OutputHandler.debug(
                  "Removing command '"
                      + cmd.getCommandName()
                      + "' from class: "
                      + cmdClass.getName());
              toRemove.add(cmd.getCommandName());
            }
          }
        }
        cmdMng.commandSet.removeAll(toRemove);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  /** @return 1 if successfully executed, 0 if wrong usage, it doesn't exist or it was canceled. */
  @Override
  public int executeCommand(ICommandSender sender, String message) {
    message = message.trim();

    if (message.startsWith("/")) {
      message = message.substring(1);
    }

    String[] temp = message.split(" ");
    String[] args = new String[temp.length - 1];
    String commandName = temp[0];
    System.arraycopy(temp, 1, args, 0, args.length);
    ICommand icommand = (ICommand) getCommands().get(commandName);

    try {
      if (icommand == null) {
        return 0;
      }

      if (icommand.canCommandSenderUseCommand(sender)) {
        CommandEvent event = new CommandEvent(icommand, sender, args);
        if (MinecraftForge.EVENT_BUS.post(event)) {
          if (event.exception != null) {
            throw event.exception;
          }
          return 0;
        }

        icommand.processCommand(sender, args);
        return 1;
      } else {
        sender.func_145747_a(format(RED, "commands.generic.permission"));
      }
    } catch (WrongUsageException wue) {
      sender.func_145747_a(
          format(
              RED, "commands.generic.usage", format(RED, wue.getMessage(), wue.getErrorOjbects())));
    } catch (CommandException ce) {
      sender.func_145747_a(format(RED, ce.getMessage(), ce.getErrorOjbects()));
    } catch (Throwable t) {
      sender.func_145747_a(format(RED, "commands.generic.exception"));
      t.printStackTrace();
    }

    return 0;
  }
  @Override
  public List getPossibleCommands(ICommandSender sender) {
    ArrayList arraylist = new ArrayList();
    Iterator iterator = super.getCommands().keySet().iterator();
    while (iterator.hasNext()) {
      Object next = iterator.next();

      if (true) {
        ICommand icommand = (ICommand) super.getCommands().get(next);

        if (FML.isModLoaded("fihgu's Permission Mod") && sender instanceof EntityPlayerMP) {
          Player player = new Player(sender.getCommandSenderName());
          fihgu.permission.element.PermissionOwner owner =
              new fihgu.permission.element.PermissionOwner(player);
          if (owner.canUse(new TryCommandEvent(sender, icommand.getCommandName())))
            if (!arraylist.contains(icommand)) arraylist.add(icommand);
        } else if (icommand.canCommandSenderUseCommand(sender)) {
          arraylist.add(icommand);
        }
      }
    }

    return arraylist;
  }
  @Override
  public int executeCommand(ICommandSender par1ICommandSender, String par2Str) {
    par2Str = par2Str.trim();

    if (par2Str.startsWith("/")) {
      par2Str = par2Str.substring(1);
    }

    String[] astring = par2Str.split(" ");
    String s1 = astring[0];

    // astring = dropFirstString(astring);
    String[] var1 = new String[astring.length - 1];
    for (int var2 = 1; var2 < astring.length; ++var2) {
      var1[var2 - 1] = astring[var2];
    }

    astring = var1;
    /////////////////////////////////////

    ICommand icommand = (ICommand) super.getCommands().get(s1);

    // int i = this.getUsernameIndex(icommand, astring);
    int i;
    if (icommand == null) {
      i = -1;
    } else {
      for (int temp = 0; temp < astring.length; ++temp) {
        if (icommand.isUsernameIndex(astring, temp)
            && PlayerSelector.matchesMultiplePlayers(astring[temp])) {
          i = temp;
        }
      }

      i = -1;
    }
    //////////////////////////////////////////////////

    int j = 0;

    try {
      if (icommand == null) {
        throw new CommandNotFoundException();
      }

      if (!MinecraftForge.EVENT_BUS.post(new TryCommandEvent(par1ICommandSender, par2Str))
          && (icommand.canCommandSenderUseCommand(par1ICommandSender)
              || FML.isModLoaded("fihgu's Permission Mod"))) {
        CommandEvent event = new CommandEvent(icommand, par1ICommandSender, astring);
        if (MinecraftForge.EVENT_BUS.post(event)) {
          if (event.exception != null) {
            throw event.exception;
          }
          return 1;
        }

        if (i > -1) {
          EntityPlayerMP[] aentityplayermp =
              PlayerSelector.matchPlayers(par1ICommandSender, astring[i]);
          String s2 = astring[i];
          EntityPlayerMP[] aentityplayermp1 = aentityplayermp;
          int k = aentityplayermp.length;

          for (int l = 0; l < k; ++l) {
            EntityPlayerMP entityplayermp = aentityplayermp1[l];
            astring[i] = entityplayermp.getEntityName();

            try {
              icommand.processCommand(par1ICommandSender, astring);
              ++j;
            } catch (CommandException commandexception) {
              par1ICommandSender.sendChatToPlayer(
                  EnumChatFormatting.RED
                      + par1ICommandSender.translateString(
                          commandexception.getMessage(), commandexception.getErrorOjbects()));
            }
          }

          astring[i] = s2;
        } else {
          icommand.processCommand(par1ICommandSender, astring);
          ++j;
        }
      } else {
        if (!FML.isModLoaded("fihgu's Permission Mod"))
          par1ICommandSender.sendChatToPlayer(
              "" + EnumChatFormatting.RED + "You do not have permission to use this command.");
      }
    } catch (WrongUsageException wrongusageexception) {
      par1ICommandSender.sendChatToPlayer(
          EnumChatFormatting.RED
              + par1ICommandSender.translateString(
                  "commands.generic.usage",
                  new Object[] {
                    par1ICommandSender.translateString(
                        wrongusageexception.getMessage(), wrongusageexception.getErrorOjbects())
                  }));
    } catch (CommandException commandexception1) {
      par1ICommandSender.sendChatToPlayer(
          EnumChatFormatting.RED
              + par1ICommandSender.translateString(
                  commandexception1.getMessage(), commandexception1.getErrorOjbects()));
    } catch (Throwable throwable) {
      par1ICommandSender.sendChatToPlayer(
          EnumChatFormatting.RED
              + par1ICommandSender.translateString("commands.generic.exception", new Object[0]));
      throwable.printStackTrace();
    }

    return j;
  }