use of com.palmergames.bukkit.towny.event.RenameTownEvent in project Towny by ElgarL.
the class TownyDatabaseHandler method renameTown.
@Override
public void renameTown(Town town, 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 (hasTown(filteredName))
throw new AlreadyRegisteredException("The town " + filteredName + " is already in use.");
// TODO: Delete/rename any invites.
List<Resident> toSave = new ArrayList<Resident>(town.getResidents());
Boolean isCapital = false;
Nation nation = null;
Double townBalance = 0.0;
oldName = town.getName();
// Clear accounts
if (TownySettings.isUsingEconomy())
try {
townBalance = town.getHoldingBalance();
town.removeAccount();
} catch (EconomyException e) {
}
// Store the nation in case we have to update the capitol
if (town.hasNation()) {
nation = town.getNation();
isCapital = town.isCapital();
}
/*
* Tidy up old files.
* Has to be done here else the town no longer exists
* and the file move command may fail.
*/
deleteTown(town);
/*
* Remove the old town from the townsMap
* and rename to the new name
*/
universe.getTownsMap().remove(town.getName().toLowerCase());
town.setName(filteredName);
universe.getTownsMap().put(filteredName.toLowerCase(), town);
// If this was a nation capitol
if (isCapital) {
nation.setCapital(town);
}
if (TownySettings.isUsingEconomy()) {
// TODO
try {
town.setBalance(townBalance, "Rename Town - Transfer to new account");
} catch (EconomyException e) {
e.printStackTrace();
}
}
for (Resident resident : toSave) {
saveResident(resident);
}
for (TownBlock townBlock : town.getTownBlocks()) {
// townBlock.setTown(town);
saveTownBlock(townBlock);
}
saveTown(town);
saveTownList();
saveWorld(town.getWorld());
if (nation != null) {
saveNation(nation);
}
} finally {
lock.unlock();
}
BukkitTools.getPluginManager().callEvent(new RenameTownEvent(oldName, town));
universe.setChangedNotify(RENAME_TOWN);
}
Aggregations