Search in sources :

Example 1 with RenameNationEvent

use of com.palmergames.bukkit.towny.event.RenameNationEvent in project Towny by ElgarL.

the class TownyDatabaseHandler method renameNation.

@Override
public void renameNation(Nation nation, String newName) throws AlreadyRegisteredException, NotRegisteredException {
    lock.lock();
    String oldName;
    try {
        String filteredName;
        try {
            filteredName = NameValidation.checkAndFilterName(newName);
        } catch (InvalidNameException e) {
            throw new NotRegisteredException(e.getMessage());
        }
        if (hasNation(filteredName))
            throw new AlreadyRegisteredException("The nation " + filteredName + " is already in use.");
        // TODO: Delete/rename any invites.
        List<Town> toSave = new ArrayList<Town>(nation.getTowns());
        Double nationBalance = 0.0;
        // Clear accounts
        if (TownySettings.isUsingEconomy())
            try {
                nationBalance = nation.getHoldingBalance();
                nation.removeAccount();
            } catch (EconomyException e) {
            }
        //Tidy up old files
        deleteNation(nation);
        /*
			 * Remove the old nation from the nationsMap
			 * and rename to the new name
			 */
        oldName = nation.getName();
        universe.getNationsMap().remove(oldName.toLowerCase());
        nation.setName(filteredName);
        universe.getNationsMap().put(filteredName.toLowerCase(), nation);
        if (TownyEconomyHandler.isActive()) {
            //TODO
            try {
                nation.setBalance(nationBalance, "Rename Nation - Transfer to new account");
            } catch (EconomyException e) {
                e.printStackTrace();
            }
        }
        for (Town town : toSave) {
            saveTown(town);
        }
        saveNation(nation);
        saveNationList();
        //search and update all ally/enemy lists
        Nation oldNation = new Nation(oldName);
        List<Nation> toSaveNation = new ArrayList<Nation>(getNations());
        for (Nation toCheck : toSaveNation) if (toCheck.hasAlly(oldNation) || toCheck.hasEnemy(oldNation)) {
            try {
                if (toCheck.hasAlly(oldNation)) {
                    toCheck.removeAlly(oldNation);
                    toCheck.addAlly(nation);
                } else {
                    toCheck.removeEnemy(oldNation);
                    toCheck.addEnemy(nation);
                }
            } catch (NotRegisteredException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else
            toSave.remove(toCheck);
        for (Nation toCheck : toSaveNation) saveNation(toCheck);
    } finally {
        lock.unlock();
    }
    BukkitTools.getPluginManager().callEvent(new RenameNationEvent(oldName, nation));
    universe.setChangedNotify(RENAME_NATION);
}
Also used : InvalidNameException(javax.naming.InvalidNameException) RenameNationEvent(com.palmergames.bukkit.towny.event.RenameNationEvent) ArrayList(java.util.ArrayList)

Aggregations

RenameNationEvent (com.palmergames.bukkit.towny.event.RenameNationEvent)1 ArrayList (java.util.ArrayList)1 InvalidNameException (javax.naming.InvalidNameException)1