Example #1
0
  // /town  create <town_name>
  public static void create(Player player, String townName) {
    PlayerData playerData = Players.get(player.getName());
    HashMap<String, Integer> nearbyTowns = PlayerTowns.checkForNearbyTowns(player.getLocation());
    Holdings holdings = iConomy.getAccount(player.getName()).getHoldings();
    int cost = RageConfig.townLevels.get(1).initialCost;

    // Ensure that the player is not currently a resident of a town
    if (!playerData.townName.equals("")) {
      Util.message(
          player,
          "You are already a resident of '"
              + playerData.townName
              + "'; you must use '/town leave' before you can create a new town.");
      return;
    }
    // Ensure that the town name is not taken
    if (PlayerTowns.get(townName) != null) {
      Util.message(player, "A town named " + townName + " already exists!");
      return;
    }
    // Ensure that the current zone is allowed to create towns
    if (!RageZones.checkPermission(player.getLocation(), Action.TOWN_CREATE)) {
      Util.message(player, "You cannot create a town in this zone.");
      return;
    }
    // Check for any towns that are too close to the current point - list all
    if (nearbyTowns.size() > 0) {
      String message = "You are too close to the following towns: ";
      for (String nearbyTownName : nearbyTowns.keySet()) {
        message += nearbyTownName + " (" + nearbyTowns.get(nearbyTownName) + "m) ";
      }
      Util.message(player, message);
      Util.message(
          player,
          "Towns must be a minimum distance of "
              + RageConfig.Town_MIN_DISTANCE_BETWEEN
              + "m apart.");
      return;
    }
    // Check to see if the player has enough money to join the specified faction
    if (!holdings.hasEnough(cost)) {
      Util.message(
          player,
          "You need at least "
              + iConomy.format(cost)
              + " to create a "
              + RageConfig.townLevels.get(1).name
              + ".");
      return;
    }

    // Subtract from player balance
    holdings.subtract(cost);

    // TODO: Check against NPC town names

    // Create the town if name selected, otherwise return message
    if (!townName.equals("")) {
      // Add the new town to the database
      int townID = RageMod.database.townCreate(player, townName);

      // Update PlayerTowns
      PlayerTown playerTown = new PlayerTown();
      playerTown.id_PlayerTown = townID;
      playerTown.townName = townName;
      playerTown.centerPoint =
          new Location2D((int) player.getLocation().getX(), (int) player.getLocation().getZ());
      playerTown.id_Faction = playerData.id_Faction;
      playerTown.bankruptDate = null;
      playerTown.townLevel = RageConfig.townLevels.get(1);
      playerTown.treasuryBalance = RageConfig.townLevels.get(1).minimumBalance;
      playerTown.minimumBalance = RageConfig.townLevels.get(1).minimumBalance;
      playerTown.mayor = playerData.name;
      playerTown.world = player.getWorld();

      playerTown.buildRegion();
      playerTown.createBorder();

      PlayerTowns.put(playerTown);

      // Update the playerData
      playerData.townName = townName;
      playerData.isMayor = true;
      playerData.currentTown = playerTown;
      playerData.treasuryBalance = cost;
      Players.update(playerData);

      Util.message(player, "Congratulations, you are the new mayor of " + townName + "!");
    } else {
      Util.message(
          player,
          "This location is valid for a new town - to create one, type '/town create <town_name>'");
    }
  }
Example #2
0
  // /town upgrade <confirm>
  public static void upgrade(Player player, boolean isConfirmed) {
    PlayerData playerData = Players.get(player.getName());
    PlayerTown playerTown = PlayerTowns.get(playerData.townName);

    // Ensure that the current player is the mayor
    if (!playerData.isMayor) {
      Util.message(player, "Only town mayors can use '/town upgrade'.");
      return;
    }
    // Ensure that the town is not at its maximum level
    if (playerTown.isAtMaxLevel()) {
      Util.message(player, "Your town is already at its maximum level.");
      return;
    }

    // Load the data for the target town level
    TownLevel targetLevel = RageConfig.townLevels.get(playerTown.townLevel.level + 1);

    // If the upgrade would make the current town a capitol...
    if (targetLevel.isCapitol) {
      // ...check to see if the player's faction already has a capitol...
      if (PlayerTowns.doesFactionCapitolExist(playerData.id_Faction)) {
        Util.message(
            player, "Your faction already has a capitol; your town cannot be upgraded further.");
        return;
      }
      // ...and make sure it is not too close to enemy capitols.
      if (PlayerTowns.areEnemyCapitolsTooClose(playerTown)) {
        Util.message(
            player,
            "Your town is ineligible to be your faction's capitol; it is too close to an enemy capitol.");
        return;
      }
    }
    // Check treasury balance
    if (playerTown.treasuryBalance < targetLevel.initialCost) {
      Util.message(
          player,
          "You need at least "
              + iConomy.format(targetLevel.initialCost)
              + " in your treasury to upgrade your town to a "
              + targetLevel.name
              + ".");
      return;
    }

    // Make the updates if confirm was typed
    if (isConfirmed) {
      // Update PlayerTowns; subtract balance from treasury; also add minimum balance
      playerTown.townLevel = RageConfig.townLevels.get(playerTown.townLevel.level + 1);
      playerTown.treasuryBalance =
          playerTown.treasuryBalance - targetLevel.initialCost + targetLevel.minimumBalance;
      playerTown.minimumBalance = targetLevel.minimumBalance;
      playerTown.buildRegion();
      playerTown.createBorder();
      PlayerTowns.put(playerTown);

      RageMod.database.townUpgrade(
          playerTown.townName, (targetLevel.initialCost - targetLevel.minimumBalance));

      Util.message(
          player,
          "Congratulations, "
              + playerTown.townName
              + " has been upgraded to a "
              + targetLevel.name
              + "!");
      Util.message(
          player,
          iConomy.format(targetLevel.initialCost) + " has been deducted from the town treasury.");
    } else {
      Util.message(
          player,
          "Your town is ready to be upgraded to a "
              + targetLevel.name
              + "; type '/town upgrade confirm' to complete the upgrade.");
    }
  }