Search in sources :

Example 36 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class TownyDataSource method loadResidents.

public boolean loadResidents() {
    TownyMessaging.sendDebugMsg("Loading Residents");
    List<Resident> toRemove = new ArrayList<Resident>();
    for (Resident resident : new ArrayList<Resident>(getResidents())) if (!loadResident(resident)) {
        System.out.println("[Towny] Loading Error: Could not read resident data '" + resident.getName() + "'.");
        toRemove.add(resident);
    //return false;
    }
    // Remove any resident which failed to load.
    for (Resident resident : toRemove) {
        System.out.println("[Towny] Loading Error: Removing resident data for '" + resident.getName() + "'.");
        removeResidentList(resident);
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Resident(com.palmergames.bukkit.towny.object.Resident)

Example 37 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class NationCommand method nationToggle.

public void nationToggle(Player player, String[] split) throws TownyException {
    if (split.length == 0) {
        player.sendMessage(ChatTools.formatTitle("/nation toggle"));
        player.sendMessage(ChatTools.formatCommand("", "/nation toggle", "neutral", ""));
    } else {
        Resident resident;
        Nation nation;
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
            nation = resident.getTown().getNation();
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
            return;
        }
        if (split[0].equalsIgnoreCase("neutral")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_NATION_TOGGLE_NEUTRAL.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            try {
                if (!TownySettings.isDeclaringNeutral())
                    throw new TownyException(TownySettings.getLangString("msg_neutral_disabled"));
                boolean choice = !nation.isNeutral();
                Double cost = TownySettings.getNationNeutralityCost();
                if (choice && TownySettings.isUsingEconomy() && !nation.pay(cost, "Nation Neutrality Cost"))
                    throw new TownyException(TownySettings.getLangString("msg_nation_cant_neutral"));
                nation.setNeutral(choice);
                // for neutral
                if (TownySettings.isUsingEconomy() && cost > 0)
                    TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_you_paid"), TownyEconomyHandler.getFormattedBalance(cost)));
                else
                    TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_nation_set_neutral"));
                TownyMessaging.sendNationMessage(nation, TownySettings.getLangString("msg_nation_neutral") + (nation.isNeutral() ? Colors.Green : Colors.Red + " not") + " neutral.");
            } catch (EconomyException e) {
                TownyMessaging.sendErrorMsg(player, e.getMessage());
            } catch (TownyException e) {
                try {
                    nation.setNeutral(false);
                } catch (TownyException e1) {
                    e1.printStackTrace();
                }
                TownyMessaging.sendErrorMsg(player, e.getMessage());
            } catch (Exception e) {
                TownyMessaging.sendErrorMsg(player, e.getMessage());
            }
        } else {
            TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_invalid_property"), "nation"));
            return;
        }
        plugin.getTownyUniverse();
        TownyUniverse.getDataSource().saveNation(nation);
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Resident(com.palmergames.bukkit.towny.object.Resident) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) EmptyNationException(com.palmergames.bukkit.towny.exceptions.EmptyNationException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) InvalidNameException(javax.naming.InvalidNameException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 38 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class NationCommand method nationRank.

public void nationRank(Player player, String[] split) {
    if (split.length == 0) {
        // Help output.
        player.sendMessage(ChatTools.formatTitle("/nation rank"));
        player.sendMessage(ChatTools.formatCommand("", "/nation rank", "add/remove [resident] rank", ""));
    } else {
        Resident resident, target;
        Town town = null;
        Town targetTown = null;
        String rank;
        /*
			 * Does the command have enough arguments?
			 */
        if (split.length < 3) {
            TownyMessaging.sendErrorMsg(player, "Eg: /town rank add/remove [resident] [rank]");
            return;
        }
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
            target = TownyUniverse.getDataSource().getResident(split[1]);
            town = resident.getTown();
            targetTown = target.getTown();
            if (town.getNation() != targetTown.getNation())
                throw new TownyException("This resident is not a member of your Town!");
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
            return;
        }
        rank = split[2].toLowerCase();
        /*
			 * Is this a known rank?
			 */
        if (!TownyPerms.getNationRanks().contains(rank)) {
            TownyMessaging.sendErrorMsg(player, "Unknown rank '" + rank + "'. Permissible ranks are :- " + StringMgmt.join(TownyPerms.getNationRanks(), ",") + ".");
            return;
        }
        /*
			 * Only allow the player to assign ranks if they have the grant perm
			 * for it.
			 */
        if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_NATION_RANK.getNode(rank))) {
            TownyMessaging.sendErrorMsg(player, "You do not have permission to modify this rank.");
            return;
        }
        if (split[0].equalsIgnoreCase("add")) {
            try {
                if (target.addNationRank(rank)) {
                    TownyMessaging.sendMsg(target, "You have been granted the Nation rank of '" + rank + "'.");
                    TownyMessaging.sendMsg(player, "You have granted the Nation rank of '" + rank + "' to " + target.getName() + ".");
                } else {
                    // Not in a nation or Rank doesn't exist
                    TownyMessaging.sendErrorMsg(player, "That resident isn't a member of a town!");
                    return;
                }
            } catch (AlreadyRegisteredException e) {
                // Must already have this rank
                TownyMessaging.sendMsg(player, target.getName() + " already holds this Nation rank.");
                return;
            }
        } else if (split[0].equalsIgnoreCase("remove")) {
            try {
                if (target.removeNationRank(rank)) {
                    TownyMessaging.sendMsg(target, "You have been demoted from the Nation rank of '" + rank + "'.");
                    TownyMessaging.sendMsg(player, "You have removed the Nation rank of '" + rank + "' from " + target.getName() + ".");
                }
            } catch (NotRegisteredException e) {
                // Must already have this rank
                TownyMessaging.sendMsg(player, target.getName() + " doesn't hold this Nation rank.");
                return;
            }
        } else {
            TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_invalid_property"), split[0]));
            return;
        }
        /*
			 * If we got here we have made a change Save the altered resident
			 * data.
			 */
        TownyUniverse.getDataSource().saveResident(target);
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 39 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class NationCommand method nationLeave.

public void nationLeave(Player player) {
    Town town = null;
    Nation nation = null;
    try {
        Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
        town = resident.getTown();
        nation = town.getNation();
        nation.removeTown(town);
        /*
			 * Remove all resident titles before saving the town itself.
			 */
        List<Resident> titleRemove = new ArrayList<Resident>(town.getResidents());
        for (Resident res : titleRemove) {
            if (res.hasTitle() || res.hasSurname()) {
                res.setTitle("");
                res.setSurname("");
                TownyUniverse.getDataSource().saveResident(res);
            }
        }
        TownyUniverse.getDataSource().saveNation(nation);
        TownyUniverse.getDataSource().saveNationList();
        TownyMessaging.sendNationMessage(nation, ChatTools.color(String.format(TownySettings.getLangString("msg_nation_town_left"), town.getName())));
        TownyMessaging.sendTownMessage(town, ChatTools.color(String.format(TownySettings.getLangString("msg_town_left_nation"), nation.getName())));
    } catch (TownyException x) {
        TownyMessaging.sendErrorMsg(player, x.getMessage());
        return;
    } catch (EmptyNationException en) {
        TownyUniverse.getDataSource().removeNation(en.getNation());
        TownyUniverse.getDataSource().saveNationList();
        TownyMessaging.sendGlobalMessage(ChatTools.color(String.format(TownySettings.getLangString("msg_del_nation"), en.getNation().getName())));
    } finally {
        TownyUniverse.getDataSource().saveTown(town);
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) Resident(com.palmergames.bukkit.towny.object.Resident) EmptyNationException(com.palmergames.bukkit.towny.exceptions.EmptyNationException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 40 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class NationCommand method nationAdd.

public void nationAdd(Player player, String[] names) {
    if (names.length < 1) {
        TownyMessaging.sendErrorMsg(player, "Eg: /nation add [names]");
        return;
    }
    Resident resident;
    Nation nation;
    try {
        resident = TownyUniverse.getDataSource().getResident(player.getName());
        nation = resident.getTown().getNation();
    } catch (TownyException x) {
        TownyMessaging.sendErrorMsg(player, x.getMessage());
        return;
    }
    nationAdd(player, nation, TownyUniverse.getDataSource().getTowns(names));
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

Resident (com.palmergames.bukkit.towny.object.Resident)54 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)35 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)32 ArrayList (java.util.ArrayList)18 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)17 Town (com.palmergames.bukkit.towny.object.Town)17 Nation (com.palmergames.bukkit.towny.object.Nation)16 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)11 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)9 IOException (java.io.IOException)8 InvalidNameException (javax.naming.InvalidNameException)7 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)5 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)5 KeyValueFile (com.palmergames.util.KeyValueFile)5 File (java.io.File)5 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)4 LinkedList (java.util.LinkedList)4 Player (org.bukkit.entity.Player)4 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3