@Command(
      aliases = {"butcher"},
      usage = "[radius]",
      flags = "plangbf",
      desc = "Kill all or nearby mobs",
      help =
          "Kills nearby mobs, based on radius, if none is given uses default in configuration.\n"
              + "Flags:"
              + "  -p also kills pets.\n"
              + "  -n also kills NPCs.\n"
              + "  -g also kills Golems.\n"
              + "  -a also kills animals.\n"
              + "  -b also kills ambient mobs.\n"
              + "  -f compounds all previous flags.\n"
              + "  -l strikes lightning on each killed mob.",
      min = 0,
      max = 1)
  @CommandPermissions("worldedit.butcher")
  @Logging(PLACEMENT)
  @Console
  public void butcher(
      CommandContext args, LocalSession session, LocalPlayer player, EditSession editSession)
      throws WorldEditException {

    LocalConfiguration config = we.getConfiguration();

    // technically the default can be larger than the max, but that's not my problem
    int radius = config.butcherDefaultRadius;

    // there might be a better way to do this but my brain is fried right now
    if (args.argsLength() > 0) { // user inputted radius, override the default
      radius = args.getInteger(0);
      if (config.butcherMaxRadius != -1) { // clamp if there is a max
        if (radius == -1) {
          radius = config.butcherMaxRadius;
        } else { // Math.min does not work if radius is -1 (actually highest possible value)
          radius = Math.min(radius, config.butcherMaxRadius);
        }
      }
    }

    FlagContainer flags = new FlagContainer(player);
    flags.or(KillFlags.FRIENDLY, args.hasFlag('f'));
    flags.or(KillFlags.PETS, args.hasFlag('p'), "worldedit.butcher.pets");
    flags.or(KillFlags.NPCS, args.hasFlag('n'), "worldedit.butcher.npcs");
    flags.or(KillFlags.GOLEMS, args.hasFlag('g'), "worldedit.butcher.golems");
    flags.or(KillFlags.ANIMALS, args.hasFlag('a'), "worldedit.butcher.animals");
    flags.or(KillFlags.AMBIENT, args.hasFlag('b'), "worldedit.butcher.ambient");
    flags.or(KillFlags.WITH_LIGHTNING, args.hasFlag('l'), "worldedit.butcher.lightning");

    int killed;
    if (player.isPlayer()) {
      killed =
          player.getWorld().killMobs(session.getPlacementPosition(player), radius, flags.flags);
    } else {
      killed = 0;
      for (LocalWorld world : we.getServer().getWorlds()) {
        killed += world.killMobs(new Vector(), radius, flags.flags);
      }
    }

    if (radius < 0) {
      player.print("Killed " + killed + " mobs.");
    } else {
      player.print("Killed " + killed + " mobs in a radius of " + radius + ".");
    }
  }