use of com.palmergames.bukkit.towny.event.RenameResidentEvent in project Towny by ElgarL.
the class TownyDatabaseHandler method renamePlayer.
@Override
public void renamePlayer(Resident resident, String newName) throws AlreadyRegisteredException, NotRegisteredException {
lock.lock();
String oldName = resident.getName();
try {
String filteredName;
try {
filteredName = NameValidation.checkAndFilterName(newName);
} catch (InvalidNameException e) {
throw new NotRegisteredException(e.getMessage());
}
// data needed for a new resident
double balance = 0.0D;
Town town = null;
long registered = 0L;
long lastOnline = 0L;
boolean isMayor = false;
// get data needed for resident
if (TownySettings.isUsingEconomy()) {
try {
balance = resident.getHoldingBalance();
resident.removeAccount();
} catch (EconomyException e) {
}
}
List<Resident> friends = resident.getFriends();
List<String> nationRanks = resident.getNationRanks();
TownyPermission permissions = resident.getPermissions();
String surname = resident.getSurname();
String title = resident.getTitle();
if (resident.hasTown()) {
town = resident.getTown();
}
List<TownBlock> townBlocks = resident.getTownBlocks();
List<String> townRanks = resident.getTownRanks();
registered = resident.getRegistered();
lastOnline = resident.getLastOnline();
isMayor = resident.isMayor();
// delete the resident and tidy up files
deleteResident(resident);
// remove old resident from residentsMap
// rename the resident
universe.getResidentMap().remove(oldName.toLowerCase());
resident.setName(filteredName);
universe.getResidentMap().put(filteredName.toLowerCase(), resident);
// add everything back to the resident
if (TownySettings.isUsingEconomy()) {
// TODO
try {
resident.setBalance(balance, "Rename Player - Transfer to new account");
} catch (EconomyException e) {
e.printStackTrace();
}
}
resident.setFriends(friends);
resident.setNationRanks(nationRanks);
// not sure if this will work
resident.setPermissions(permissions.toString());
resident.setSurname(surname);
resident.setTitle(title);
resident.setTown(town);
resident.setTownblocks(townBlocks);
resident.setTownRanks(townRanks);
resident.setRegistered(registered);
resident.setLastOnline(lastOnline);
if (isMayor) {
try {
town.setMayor(resident);
} catch (TownyException e) {
}
}
// save stuff
saveResidentList();
saveResident(resident);
if (town != null) {
saveTown(town);
}
for (TownBlock tb : townBlocks) {
saveTownBlock(tb);
}
// search and update all friends lists
Resident oldResident = new Resident(oldName);
List<Resident> toSaveResident = new ArrayList<Resident>(getResidents());
for (Resident toCheck : toSaveResident) {
if (toCheck.hasFriend(oldResident)) {
try {
toCheck.removeFriend(oldResident);
toCheck.addFriend(resident);
} catch (NotRegisteredException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
for (Resident toCheck : toSaveResident) saveResident(toCheck);
} finally {
lock.unlock();
}
BukkitTools.getPluginManager().callEvent(new RenameResidentEvent(oldName, resident));
universe.setChangedNotify(RENAME_RESIDENT);
}
Aggregations