public boolean loadNations() {
   sendDebugMsg("Loading Nations");
   for (Nation nation : universe.getNations())
     if (!loadNation(nation)) {
       System.out.println(
           "[Towny] Loading Error: Could not read nation data '" + nation.getName() + "'.");
       return false;
     }
   return true;
 }
Esempio n. 2
0
  /**
   * Can resident a attack resident b?
   *
   * @param a
   * @param b
   * @return true if they can attack.
   */
  public static boolean canAttackEnemy(String a, String b) {

    try {
      Resident residentA = TownyUniverse.getDataSource().getResident(a);
      Resident residentB = TownyUniverse.getDataSource().getResident(b);
      if (residentA.getTown() == residentB.getTown()) return false;
      if (residentA.getTown().getNation() == residentB.getTown().getNation()) return false;
      Nation nationA = residentA.getTown().getNation();
      Nation nationB = residentB.getTown().getNation();
      if (nationA.isNeutral() || nationB.isNeutral()) return false;
      if (nationA.hasEnemy(nationB)) return true;
    } catch (NotRegisteredException e) {
      return false;
    }
    return false;
  }
Esempio n. 3
0
  public void start() {
    warTime = true;

    // Announce

    // Seed spoils of war
    try {
      warSpoils.pay(TownySettings.getBaseSpoilsOfWar(), "Start of War - Base Spoils");
      TownyMessaging.sendMsg(
          "[War] Seeding spoils of war with " + TownySettings.getBaseSpoilsOfWar());
    } catch (EconomyException e) {
      TownyMessaging.sendErrorMsg("[War] Could not seed spoils of war.");
    }

    // Gather all nations at war
    for (Nation nation : TownyUniverse.getDataSource().getNations()) {
      if (!nation.isNeutral()) {
        add(nation);
        TownyMessaging.sendGlobalMessage(
            String.format(TownySettings.getLangString("msg_war_join_nation"), nation.getName()));
      } else if (!TownySettings.isDeclaringNeutral()) {
        try {
          nation.setNeutral(false);
          add(nation);
          TownyMessaging.sendGlobalMessage(
              String.format(TownySettings.getLangString("msg_war_join_forced"), nation.getName()));
        } catch (TownyException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
    // warTimer.scheduleAtFixedRate(new WarTimerTask(this), 0, 1000);
    int id =
        plugin
            .getServer()
            .getScheduler()
            .scheduleAsyncRepeatingTask(
                getPlugin(), new WarTimerTask(this), 0, TimeTools.convertToTicks(5));
    if (id == -1) {
      TownyMessaging.sendErrorMsg("Could not schedule war event loop.");
      end();
    } else addTaskId(id);
    checkEnd();
  }
Esempio n. 4
0
  @Override
  protected String[] get(Event arg0) {
    String t = this.nation.getSingle(arg0);
    Nation tw = null;
    try {
      tw = TownyUniverse.getDataSource().getNation(t);
    } catch (NotRegisteredException e) {
      e.printStackTrace();
    }

    if (tw == null) {
      return null;
    }

    String tr = null;
    tr = tw.getCapital().toString();

    return new String[] {tr};
  }
Esempio n. 5
0
  public void end() {
    for (Player player : TownyUniverse.getOnlinePlayers()) sendStats(player);
    double halfWinnings;
    try {
      // Transactions might leave 1 coin. (OH noez!)
      halfWinnings = getWarSpoils().getHoldingBalance() / 2.0;

      try {
        double nationWinnings = halfWinnings / warringNations.size(); // Again, might leave residue.
        for (Nation winningNation : warringNations) {
          getWarSpoils().payTo(nationWinnings, winningNation, "War - Nation Winnings");
          TownyMessaging.sendGlobalMessage(
              winningNation.getName()
                  + " won "
                  + nationWinnings
                  + " "
                  + TownyEconomyObject.getEconomyCurrency()
                  + ".");
        }
      } catch (ArithmeticException e) {
        // A war ended with 0 nations.
      }

      try {
        KeyValue<Town, Integer> winningTownScore = getWinningTownScore();
        getWarSpoils().payTo(halfWinnings, winningTownScore.key, "War - Nation Winnings");
        TownyMessaging.sendGlobalMessage(
            winningTownScore.key.getName()
                + " won "
                + halfWinnings
                + " "
                + TownyEconomyObject.getEconomyCurrency()
                + " with the score "
                + winningTownScore.value
                + ".");
      } catch (TownyException e) {
      }
    } catch (EconomyException e1) {
    }
  }
Esempio n. 6
0
  public void adminSet(String[] split) throws TownyException {

    if (split.length == 0) {
      sender.sendMessage(ChatTools.formatTitle("/townyadmin set"));
      // TODO: player.sendMessage(ChatTools.formatCommand("",
      // "/townyadmin set", "king [nation] [king]", ""));
      sender.sendMessage(
          ChatTools.formatCommand(
              "",
              "/townyadmin set",
              "mayor [town] " + TownySettings.getLangString("town_help_2"),
              ""));
      sender.sendMessage(ChatTools.formatCommand("", "/townyadmin set", "mayor [town] npc", ""));
      // player.sendMessage(ChatTools.formatCommand("", "/townyadmin set",
      // "debugmode [on/off]", ""));
      // player.sendMessage(ChatTools.formatCommand("", "/townyadmin set",
      // "devmode [on/off]", ""));

      return;
    }

    if (!TownyUniverse.getPermissionSource()
        .testPermission(
            player, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_SET.getNode(split[0].toLowerCase())))
      throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));

    if (split[0].equalsIgnoreCase("mayor")) {
      if (split.length < 3) {

        sender.sendMessage(ChatTools.formatTitle("/townyadmin set mayor"));
        sender.sendMessage(
            ChatTools.formatCommand(
                "Eg",
                "/townyadmin set mayor",
                "[town] " + TownySettings.getLangString("town_help_2"),
                ""));
        sender.sendMessage(
            ChatTools.formatCommand("Eg", "/townyadmin set mayor", "[town] npc", ""));

      } else
        try {
          Resident newMayor = null;
          Town town = TownyUniverse.getDataSource().getTown(split[1]);

          if (split[2].equalsIgnoreCase("npc")) {
            String name = nextNpcName();
            TownyUniverse.getDataSource().newResident(name);

            newMayor = TownyUniverse.getDataSource().getResident(name);

            newMayor.setRegistered(System.currentTimeMillis());
            newMayor.setLastOnline(0);
            newMayor.setNPC(true);

            TownyUniverse.getDataSource().saveResident(newMayor);
            TownyUniverse.getDataSource().saveResidentList();

            // set for no upkeep as an NPC mayor is assigned
            town.setHasUpkeep(false);

          } else {
            newMayor = TownyUniverse.getDataSource().getResident(split[2]);

            // set upkeep again
            town.setHasUpkeep(true);
          }

          if (!town.hasResident(newMayor)) TownCommand.townAddResident(town, newMayor);
          // Delete the resident if the old mayor was an NPC.
          Resident oldMayor = town.getMayor();

          town.setMayor(newMayor);

          if (oldMayor.isNPC()) {
            try {
              town.removeResident(oldMayor);
              TownyUniverse.getDataSource().removeResident(oldMayor);

              TownyUniverse.getDataSource().removeResidentList(oldMayor);

            } catch (EmptyTownException e) {
              // Should never reach here as we are setting a new
              // mayor before removing the old one.
              e.printStackTrace();
            }
          }
          TownyUniverse.getDataSource().saveTown(town);
          String[] msg = TownySettings.getNewMayorMsg(newMayor.getName());
          TownyMessaging.sendTownMessage(town, msg);
          // TownyMessaging.sendMessage(player, msg);
        } catch (TownyException e) {
          TownyMessaging.sendErrorMsg(getSender(), e.getMessage());
        }

    } else if (split[0].equalsIgnoreCase("capital")) {

      if (split.length < 2) {

        sender.sendMessage(ChatTools.formatTitle("/townyadmin set capital"));
        sender.sendMessage(ChatTools.formatCommand("Eg", "/ta set capital", "[town name]", ""));

      } else {

        try {
          Town newCapital = TownyUniverse.getDataSource().getTown(split[1]);
          Nation nation = newCapital.getNation();

          nation.setCapital(newCapital);
          plugin.resetCache();

          TownyMessaging.sendNationMessage(
              nation,
              TownySettings.getNewKingMsg(newCapital.getMayor().getName(), nation.getName()));

          TownyUniverse.getDataSource().saveNation(nation);
          TownyUniverse.getDataSource().saveNationList();

        } catch (TownyException e) {
          TownyMessaging.sendErrorMsg(player, e.getMessage());
        }
      }

    } else {
      TownyMessaging.sendErrorMsg(
          getSender(),
          String.format(TownySettings.getLangString("msg_err_invalid_property"), "administrative"));
      return;
    }
  }
Esempio n. 7
0
  public void parseAdminNationCommand(String[] split) throws TownyException {

    if (split.length == 0 || split[0].equalsIgnoreCase("?")) {

      sender.sendMessage(ChatTools.formatTitle("/townyadmin nation"));
      sender.sendMessage(
          ChatTools.formatCommand(
              TownySettings.getLangString("admin_sing"), "/townyadmin nation", "[nation]", ""));
      sender.sendMessage(
          ChatTools.formatCommand(
              TownySettings.getLangString("admin_sing"),
              "/townyadmin nation",
              "[nation] add [] .. []",
              ""));
      sender.sendMessage(
          ChatTools.formatCommand(
              TownySettings.getLangString("admin_sing"),
              "/townyadmin nation",
              "[nation] rename [newname]",
              ""));
      sender.sendMessage(
          ChatTools.formatCommand(
              TownySettings.getLangString("admin_sing"),
              "/townyadmin nation",
              "[nation] delete",
              ""));

      return;
    }
    try {
      Nation nation = TownyUniverse.getDataSource().getNation(split[0]);
      if (split.length == 1) {
        TownyMessaging.sendMessage(getSender(), TownyFormatter.getStatus(nation));
        return;
      }

      if (!TownyUniverse.getPermissionSource()
          .testPermission(
              player,
              PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_NATION.getNode(split[1].toLowerCase())))
        throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));

      if (split[1].equalsIgnoreCase("add")) {
        /*
         * if (isConsole) { sender.sendMessage(
         * "[Towny] InputError: This command was designed for use in game only."
         * ); return; }
         */
        NationCommand.nationAdd(
            nation, TownyUniverse.getDataSource().getTowns(StringMgmt.remArgs(split, 2)));

      } else if (split[1].equalsIgnoreCase("delete")) {

        TownyUniverse.getDataSource().removeNation(nation);

      } else if (split[1].equalsIgnoreCase("rename")) {

        if (!NameValidation.isBlacklistName(split[2])) {
          TownyUniverse.getDataSource().renameNation(nation, split[2]);
          TownyMessaging.sendNationMessage(
              nation,
              String.format(
                  TownySettings.getLangString("msg_nation_set_name"),
                  ((getSender() instanceof Player) ? player.getName() : "CONSOLE"),
                  nation.getName()));
        } else
          TownyMessaging.sendErrorMsg(getSender(), TownySettings.getLangString("msg_invalid_name"));
      }

    } catch (NotRegisteredException e) {
      TownyMessaging.sendErrorMsg(getSender(), e.getMessage());
    } catch (AlreadyRegisteredException e) {
      TownyMessaging.sendErrorMsg(getSender(), e.getMessage());
    }
  }
Esempio n. 8
0
 public int countActiveTowns(Nation nation) {
   int n = 0;
   for (Town town : nation.getTowns()) if (warringTowns.contains(town)) n++;
   return n;
 }
Esempio n. 9
0
 public boolean townsLeft(Nation nation) {
   return warringTowns.containsAll(nation.getTowns());
 }
Esempio n. 10
0
 public void nationLeave(Nation nation) {
   remove(nation);
   for (Town town : nation.getTowns()) remove(town);
   TownyMessaging.sendGlobalMessage(TownySettings.getWarTimeForfeitMsg(nation.getName()));
   checkEnd();
 }
Esempio n. 11
0
 public void eliminate(Nation nation) {
   remove(nation);
   TownyMessaging.sendGlobalMessage(TownySettings.getWarTimeEliminatedMsg(nation.getName()));
   checkEnd();
 }
Esempio n. 12
0
 public void add(Nation nation) {
   for (Town town : nation.getTowns()) add(town);
   warringNations.add(nation);
 }