/**
   * Delete a resident and it's data file (if not online) Available Only to players with the
   * 'towny.admin' permission node.
   *
   * @param player
   * @param split
   */
  public void residentDelete(Player player, String[] split) {

    if (split.length == 0)
      TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_invalid_name"));
    else
      try {
        if (!TownyUniverse.getPermissionSource().isTownyAdmin(player))
          throw new TownyException(TownySettings.getLangString("msg_err_admin_only_delete"));

        for (String name : split) {
          try {
            Resident resident = TownyUniverse.getDataSource().getResident(name);
            if (!resident.isNPC() && !BukkitTools.isOnline(resident.getName())) {
              TownyUniverse.getDataSource().removeResident(resident);
              TownyUniverse.getDataSource().removeResidentList(resident);
              TownyMessaging.sendGlobalMessage(TownySettings.getDelResidentMsg(resident));
            } else
              TownyMessaging.sendErrorMsg(
                  player,
                  String.format(TownySettings.getLangString("msg_err_online_or_npc"), name));
          } catch (NotRegisteredException x) {
            // This name isn't registered as a resident
            TownyMessaging.sendErrorMsg(
                player, String.format(TownySettings.getLangString("msg_err_invalid_name"), name));
          }
        }
      } catch (TownyException x) {
        // Admin only escape
        TownyMessaging.sendErrorMsg(player, x.getMessage());
        return;
      }
  }
 public boolean loadResidents() {
   sendDebugMsg("Loading Residents");
   for (Resident resident : universe.getResidents())
     if (!loadResident(resident)) {
       System.out.println(
           "[Towny] Loading Error: Could not read resident data '" + resident.getName() + "'.");
       return false;
     }
   return true;
 }
  @Override
  public void run() {

    plugin.getLogger().info("-*****************************************************-");
    for (Resident resident : TownyUniverse.getDataSource().getResidents()) {
      plugin.getLogger().info("Resident: " + resident.getName());
      plugin.getLogger().info("Total Money: " + resident.getHoldingFormattedBalance());
    }
    plugin.getLogger().info("-*****************************************************-");
  }
Exemple #4
0
  /**
   * Get a TownyAccountHolder for the town of which player is a resident, if any.
   *
   * @param player
   * @return TownyAccountHolder for the town of which player is a resident, if any. null otherwise.
   */
  public TownyAccountHolder getTownAccountHolder(Player player) {
    try {
      Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
      Town town = resident.getTown();
      return new TownyAccountHolder(town);

    } catch (NotRegisteredException e) {
    }

    return null;
  }
Exemple #5
0
    @EventHandler(priority = EventPriority.HIGH)
    public void onPermissionEntityEvent(PermissionEntityEvent event) {

      Resident resident = null;
      Player player = null;

      try {
        if (PermissionEventEnums.PEXEntity_Action.valueOf(event.getEventName()) != null) {
          PermissionEntityEvent EntityEvent = (PermissionEntityEvent) event;
          PermissionEntity entity = EntityEvent.getEntity();
          if (entity instanceof PermissionGroup) {
            PermissionGroup group = (PermissionGroup) entity;

            // Update all players who are in this group.
            for (Player toUpdate : BukkitTools.getOnlinePlayers()) {
              if (toUpdate != null)
                if (Arrays.asList(getPlayerGroups(toUpdate)).contains(group)) {
                  // setup default modes
                  String[] modes =
                      getPlayerPermissionStringNode(
                              toUpdate.getName(), PermissionNodes.TOWNY_DEFAULT_MODES.getNode())
                          .split(",");
                  plugin.setPlayerMode(toUpdate, modes, false);
                  plugin.resetCache(toUpdate);
                }
            }

          } else if (entity instanceof PermissionUser) {

            try {
              resident =
                  TownyUniverse.getDataSource().getResident(((PermissionUser) entity).getName());
              player = BukkitTools.getPlayerExact(resident.getName());
              if (player != null) {
                // setup default modes for this player.
                String[] modes =
                    getPlayerPermissionStringNode(
                            player.getName(), PermissionNodes.TOWNY_DEFAULT_MODES.getNode())
                        .split(",");
                plugin.setPlayerMode(player, modes, false);
                plugin.resetCache(player);
              }
            } catch (NotRegisteredException x) {
            }
          }
        }
      } catch (IllegalArgumentException e) {
        // Not tracking this event type
      }
    }
Exemple #6
0
  /**
   * Is resident b an enemy of resident a?
   *
   * @param a
   * @param b
   * @return true if b is an enemy.
   */
  public static boolean isEnemy(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;
      if (residentA.getTown().getNation().hasEnemy(residentB.getTown().getNation())) return true;
    } catch (NotRegisteredException e) {
      return false;
    }
    return false;
  }
Exemple #7
0
  /**
   * Is the defending resident an ally of the attacking resident?
   *
   * @param attackingResident
   * @param defendingResident
   * @return true if the defender is an ally of the attacker.
   */
  public static boolean isAlly(String attackingResident, String defendingResident) {

    try {
      Resident residentA = TownyUniverse.getDataSource().getResident(attackingResident);
      Resident residentB = TownyUniverse.getDataSource().getResident(defendingResident);
      if (residentA.getTown() == residentB.getTown()) return true;
      if (residentA.getTown().getNation() == residentB.getTown().getNation()) return true;
      if (residentA.getTown().getNation().hasAlly(residentB.getTown().getNation())) return true;
    } catch (NotRegisteredException e) {
      return false;
    }
    return false;
  }
Exemple #8
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;
  }
Exemple #9
0
  /**
   * getPermissionNode
   *
   * <p>returns the specified prefix/suffix nodes from permissionsEX
   *
   * @param resident
   * @param node
   * @return String of the prefix or suffix
   */
  @Override
  public String getPrefixSuffix(Resident resident, String node) {

    String group = "", user = "";
    Player player = BukkitTools.getPlayer(resident.getName());

    PermissionManager pexPM = PermissionsEx.getPermissionManager();

    if (node == "prefix") {
      group = pexPM.getUser(player).getPrefix(player.getWorld().getName());
      user = pexPM.getUser(player).getOwnPrefix();
    } else if (node == "suffix") {
      group = pexPM.getUser(player).getSuffix(player.getWorld().getName());
      user = pexPM.getUser(player).getOwnSuffix();
    }
    if (group == null) group = "";
    if (user == null) user = "";

    if (!group.equals(user)) user = group + user;
    user = TownySettings.parseSingleLineString(user);

    return user;
  }
  public void parseToggleCommand(String[] split) throws TownyException {

    boolean choice;

    if (split.length == 0) {
      // command was '/townyadmin toggle'
      player.sendMessage(ChatTools.formatTitle("/townyadmin toggle"));
      player.sendMessage(ChatTools.formatCommand("", "/townyadmin toggle", "war", ""));
      player.sendMessage(ChatTools.formatCommand("", "/townyadmin toggle", "neutral", ""));
      player.sendMessage(ChatTools.formatCommand("", "/townyadmin toggle", "devmode", ""));
      player.sendMessage(ChatTools.formatCommand("", "/townyadmin toggle", "debug", ""));
      player.sendMessage(
          ChatTools.formatCommand("", "/townyadmin toggle", "townwithdraw/nationwithdraw", ""));
      player.sendMessage(ChatTools.formatCommand("", "/townyadmin toggle npc", "[resident]", ""));
      return;
    }

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

    if (split[0].equalsIgnoreCase("war")) {
      choice = TownyUniverse.isWarTime();

      if (!choice) {
        plugin.getTownyUniverse().startWarEvent();
        TownyMessaging.sendMsg(getSender(), TownySettings.getLangString("msg_war_started"));
      } else {
        plugin.getTownyUniverse().endWarEvent();
        TownyMessaging.sendMsg(getSender(), TownySettings.getLangString("msg_war_ended"));
      }
    } else if (split[0].equalsIgnoreCase("neutral")) {

      try {
        choice = !TownySettings.isDeclaringNeutral();
        TownySettings.setDeclaringNeutral(choice);
        TownyMessaging.sendMsg(
            getSender(),
            String.format(
                TownySettings.getLangString("msg_nation_allow_neutral"),
                choice ? "Enabled" : "Disabled"));

      } catch (Exception e) {
        TownyMessaging.sendErrorMsg(
            getSender(), TownySettings.getLangString("msg_err_invalid_choice"));
        return;
      }

    } else if (split[0].equalsIgnoreCase("devmode")) {
      try {
        choice = !TownySettings.isDevMode();
        TownySettings.setDevMode(choice);
        TownyMessaging.sendMsg(
            getSender(),
            "Dev Mode " + (choice ? Colors.Green + "Enabled" : Colors.Red + "Disabled"));
      } catch (Exception e) {
        TownyMessaging.sendErrorMsg(
            getSender(), TownySettings.getLangString("msg_err_invalid_choice"));
      }
    } else if (split[0].equalsIgnoreCase("debug")) {
      try {
        choice = !TownySettings.getDebug();
        TownySettings.setDebug(choice);
        TownyMessaging.sendMsg(
            getSender(),
            "Debug Mode " + (choice ? Colors.Green + "Enabled" : Colors.Red + "Disabled"));
      } catch (Exception e) {
        TownyMessaging.sendErrorMsg(
            getSender(), TownySettings.getLangString("msg_err_invalid_choice"));
      }
    } else if (split[0].equalsIgnoreCase("townwithdraw")) {
      try {
        choice = !TownySettings.getTownBankAllowWithdrawls();
        TownySettings.SetTownBankAllowWithdrawls(choice);
        TownyMessaging.sendMsg(
            getSender(),
            "Town Withdrawls " + (choice ? Colors.Green + "Enabled" : Colors.Red + "Disabled"));
      } catch (Exception e) {
        TownyMessaging.sendErrorMsg(
            getSender(), TownySettings.getLangString("msg_err_invalid_choice"));
      }
    } else if (split[0].equalsIgnoreCase("nationwithdraw")) {
      try {
        choice = !TownySettings.geNationBankAllowWithdrawls();
        TownySettings.SetNationBankAllowWithdrawls(choice);
        TownyMessaging.sendMsg(
            getSender(),
            "Nation Withdrawls " + (choice ? Colors.Green + "Enabled" : Colors.Red + "Disabled"));
      } catch (Exception e) {
        TownyMessaging.sendErrorMsg(
            getSender(), TownySettings.getLangString("msg_err_invalid_choice"));
      }

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

      if (split.length != 2)
        throw new TownyException(
            String.format(
                TownySettings.getLangString("msg_err_invalid_input"), "Eg: toggle npc [resident]"));

      try {
        Resident resident = TownyUniverse.getDataSource().getResident(split[1]);
        resident.setNPC(!resident.isNPC());

        TownyUniverse.getDataSource().saveResident(resident);

        TownyMessaging.sendMessage(
            sender,
            String.format(
                TownySettings.getLangString("msg_npc_flag"), resident.isNPC(), resident.getName()));

      } catch (NotRegisteredException x) {
        throw new TownyException(
            String.format(TownySettings.getLangString("msg_err_not_registered_1"), split[1]));
      }

    } else {
      // parameter error message
      // neutral/war/townmobs/worldmobs
      TownyMessaging.sendErrorMsg(
          getSender(), TownySettings.getLangString("msg_err_invalid_choice"));
    }
  }
  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;
    }
  }