예제 #1
0
  @Command(
      name = "unclaim",
      permission = "mytown.cmd.assistant.unclaim",
      parentName = "mytown.cmd",
      syntax = "/town unclaim")
  public static CommandResponse unclaimCommand(ICommandSender sender, List<String> args) {
    EntityPlayer player = (EntityPlayer) sender;
    Resident res = MyTownUniverse.instance.getOrMakeResident(sender);
    TownBlock block = getBlockAtResident(res);
    Town town = getTownFromResident(res);

    if (town != block.getTown())
      throw new MyTownCommandException("mytown.cmd.err.unclaim.notInTown");
    if (block.isPointIn(town.getSpawn().getDim(), town.getSpawn().getX(), town.getSpawn().getZ()))
      throw new MyTownCommandException("mytown.cmd.err.unclaim.spawnPoint");
    if (!checkNearby(block.getDim(), block.getX(), block.getZ(), town)
        && town.townBlocksContainer.size() <= 1) {
      throw new MyTownCommandException("mytown.cmd.err.unclaim.lastClaim");
    }

    getDatasource().deleteBlock(block);
    res.sendMessage(
        getLocal()
            .getLocalization(
                "mytown.notification.block.removed",
                block.getX() << 4,
                block.getZ() << 4,
                block.getX() << 4 + 15,
                block.getZ() << 4 + 15,
                town.getName()));
    makeBankRefund(player, town, block.getPricePaid());
    return CommandResponse.DONE;
  }
예제 #2
0
  @Command(
      name = "claim",
      permission = "mytown.cmd.assistant.claim",
      parentName = "mytown.cmd",
      syntax = "/town claim [range]")
  public static CommandResponse claimCommand(ICommandSender sender, List<String> args) {
    EntityPlayer player = (EntityPlayer) sender;
    Resident res = MyTownUniverse.instance.getOrMakeResident(player);
    Town town = getTownFromResident(res);

    boolean isFarClaim = false;

    if (args.size() < 1) {
      if (town.townBlocksContainer.size() >= town.getMaxBlocks())
        throw new MyTownCommandException("mytown.cmd.err.town.maxBlocks", 1);
      if (getUniverse().blocks.contains(player.dimension, player.chunkCoordX, player.chunkCoordZ))
        throw new MyTownCommandException("mytown.cmd.err.claim.already");
      if (!checkNearby(player.dimension, player.chunkCoordX, player.chunkCoordZ, town)) {
        if (town.townBlocksContainer.getFarClaims() >= town.getMaxFarClaims())
          throw new MyTownCommandException("mytown.cmd.err.claim.far.notAllowed");
        isFarClaim = true;
      }
      for (int x = player.chunkCoordX - Config.instance.distanceBetweenTowns.get();
          x <= player.chunkCoordX + Config.instance.distanceBetweenTowns.get();
          x++) {
        for (int z = player.chunkCoordZ - Config.instance.distanceBetweenTowns.get();
            z <= player.chunkCoordZ + Config.instance.distanceBetweenTowns.get();
            z++) {
          Town nearbyTown = MyTownUtils.getTownAtPosition(player.dimension, x, z);
          if (nearbyTown != null
              && nearbyTown != town
              && !(Boolean) nearbyTown.flagsContainer.getValue(FlagType.NEARBY))
            throw new MyTownCommandException(
                "mytown.cmd.err.claim.tooClose",
                nearbyTown.getName(),
                Config.instance.distanceBetweenTowns.get());
        }
      }

      if (isFarClaim && town.townBlocksContainer.getFarClaims() + 1 > town.getMaxFarClaims())
        throw new MyTownCommandException("mytown.cmd.err.claim.far.notAllowed");

      int price =
          (isFarClaim
                  ? Config.instance.costAmountClaimFar.get()
                  : Config.instance.costAmountClaim.get())
              + Config.instance.costAdditionClaim.get() * town.townBlocksContainer.size();

      makeBankPayment(player, town, price);

      TownBlock block =
          getUniverse()
              .newBlock(
                  player.dimension,
                  player.chunkCoordX,
                  player.chunkCoordZ,
                  isFarClaim,
                  price,
                  town);
      if (block == null) throw new MyTownCommandException("mytown.cmd.err.claim.failed");

      getDatasource().saveBlock(block);
      res.sendMessage(
          getLocal()
              .getLocalization(
                  "mytown.notification.block.added",
                  block.getX() * 16,
                  block.getZ() * 16,
                  block.getX() * 16 + 15,
                  block.getZ() * 16 + 15,
                  town.getName()));
    } else {
      if (!StringUtils.tryParseInt(args.get(0)))
        throw new MyTownCommandException("mytown.cmd.err.notPositiveInteger", args.get(0));

      int radius = Integer.parseInt(args.get(0));
      List<ChunkPos> chunks =
          WorldUtils.getChunksInBox(
              player.dimension,
              (int) (player.posX - radius * 16),
              (int) (player.posZ - radius * 16),
              (int) (player.posX + radius * 16),
              (int) (player.posZ + radius * 16));
      isFarClaim = true;

      for (Iterator<ChunkPos> it = chunks.iterator(); it.hasNext(); ) {
        ChunkPos chunk = it.next();
        if (checkNearby(player.dimension, chunk.getX(), chunk.getZ(), town)) {
          isFarClaim = false;
        }
        if (getUniverse().blocks.contains(player.dimension, chunk.getX(), chunk.getZ()))
          it.remove();

        for (int x = chunk.getX() - Config.instance.distanceBetweenTowns.get();
            x <= chunk.getX() + Config.instance.distanceBetweenTowns.get();
            x++) {
          for (int z = chunk.getZ() - Config.instance.distanceBetweenTowns.get();
              z <= chunk.getZ() + Config.instance.distanceBetweenTowns.get();
              z++) {
            Town nearbyTown = MyTownUtils.getTownAtPosition(player.dimension, x, z);
            if (nearbyTown != null
                && nearbyTown != town
                && !(Boolean) nearbyTown.flagsContainer.getValue(FlagType.NEARBY))
              throw new MyTownCommandException(
                  "mytown.cmd.err.claim.tooClose",
                  nearbyTown.getName(),
                  Config.instance.distanceBetweenTowns.get());
          }
        }
      }

      if (town.townBlocksContainer.size() + chunks.size() > town.getMaxBlocks())
        throw new MyTownCommandException("mytown.cmd.err.town.maxBlocks", chunks.size());

      if (isFarClaim && town.townBlocksContainer.getFarClaims() + 1 > town.getMaxFarClaims())
        throw new MyTownCommandException("mytown.cmd.err.claim.far.notAllowed");

      makeBankPayment(
          player,
          town,
          (isFarClaim
                  ? Config.instance.costAmountClaimFar.get()
                      + Config.instance.costAmountClaim.get() * (chunks.size() - 1)
                  : Config.instance.costAmountClaim.get() * chunks.size())
              + MathUtils.sumFromNtoM(
                      town.townBlocksContainer.size(),
                      town.townBlocksContainer.size() + chunks.size() - 1)
                  * Config.instance.costAdditionClaim.get());

      for (ChunkPos chunk : chunks) {
        int price =
            (isFarClaim
                    ? Config.instance.costAmountClaimFar.get()
                    : Config.instance.costAmountClaim.get())
                + Config.instance.costAdditionClaim.get() * town.townBlocksContainer.size();
        TownBlock block =
            getUniverse()
                .newBlock(player.dimension, chunk.getX(), chunk.getZ(), isFarClaim, price, town);
        // Only one of the block will be a farClaim, rest will be normal claim
        isFarClaim = false;
        getDatasource().saveBlock(block);
        res.sendMessage(
            getLocal()
                .getLocalization(
                    "mytown.notification.block.added",
                    block.getX() * 16,
                    block.getZ() * 16,
                    block.getX() * 16 + 15,
                    block.getZ() * 16 + 15,
                    town.getName()));
      }
    }
    return CommandResponse.DONE;
  }