Example #1
0
 public static void check(TileEntity te) {
   for (SegmentTileEntity segment : segmentsTile.get(te.getClass())) {
     if (!segment.shouldExist(te)) {
       ItemStack itemStack = new ItemStack(te.getBlockType(), 1, te.getBlockMetadata());
       NBTTagCompound nbt = new NBTTagCompound();
       te.writeToNBT(nbt);
       itemStack.setTagCompound(nbt);
       WorldUtils.dropAsEntity(te.getWorldObj(), te.xCoord, te.yCoord, te.zCoord, itemStack);
       te.getWorldObj().setBlock(te.xCoord, te.yCoord, te.zCoord, Blocks.air);
       te.invalidate();
       MyTown.instance.LOG.info("TileEntity {} was ATOMICALLY DISINTEGRATED!", te.toString());
       return;
     }
   }
 }
  @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;
  }