use of com.palmergames.bukkit.towny.event.DeleteNationEvent in project Towny by ElgarL.
the class TownyDatabaseHandler method removeNation.
@Override
public void removeNation(Nation nation) {
// search and remove from all ally/enemy lists
List<Nation> toSaveNation = new ArrayList<Nation>();
for (Nation toCheck : new ArrayList<Nation>(universe.getNationsMap().values())) if (toCheck.hasAlly(nation) || toCheck.hasEnemy(nation)) {
try {
if (toCheck.hasAlly(nation))
toCheck.removeAlly(nation);
else
toCheck.removeEnemy(nation);
toSaveNation.add(toCheck);
} catch (NotRegisteredException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (Nation toCheck : toSaveNation) saveNation(toCheck);
// Transfer any money to the warchest.
if (TownyEconomyHandler.isActive())
try {
nation.payTo(nation.getHoldingBalance(), new WarSpoils(), "Remove Nation");
nation.removeAccount();
} catch (Exception e) {
}
// Delete nation and save towns
deleteNation(nation);
List<Town> toSave = new ArrayList<Town>(nation.getTowns());
nation.clear();
universe.getNationsMap().remove(nation.getName().toLowerCase());
for (Town town : toSave) {
/*
* 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("");
saveResident(res);
}
}
saveTown(town);
}
plugin.resetCache();
saveNationList();
BukkitTools.getPluginManager().callEvent(new DeleteNationEvent(nation.getName()));
universe.setChangedNotify(REMOVE_NATION);
}
use of com.palmergames.bukkit.towny.event.DeleteNationEvent in project Towny by TownyAdvanced.
the class TownyDatabaseHandler method removeNation.
@Override
public void removeNation(Nation nation) {
PreDeleteNationEvent preEvent = new PreDeleteNationEvent(nation);
BukkitTools.getPluginManager().callEvent(preEvent);
if (preEvent.isCancelled())
return;
Resident king = null;
if (nation.hasKing())
king = nation.getKing();
// Remove the Nation's spawn particle.
if (nation.hasSpawn()) {
try {
universe.removeSpawnPoint(nation.getSpawn());
} catch (TownyException ignored) {
}
}
// search and remove from all ally/enemy lists
List<Nation> toSaveNation = new ArrayList<>();
for (Nation toCheck : new ArrayList<>(universe.getNations())) if (toCheck.hasAlly(nation) || toCheck.hasEnemy(nation)) {
try {
if (toCheck.hasAlly(nation))
toCheck.removeAlly(nation);
else
toCheck.removeEnemy(nation);
toSaveNation.add(toCheck);
} catch (NotRegisteredException e) {
e.printStackTrace();
}
}
for (Nation toCheck : toSaveNation) saveNation(toCheck);
// Search and remove any ally invites sent to this nation.
for (Nation toCheck : new ArrayList<>(universe.getNations())) for (Invite invite : new ArrayList<>(toCheck.getSentAllyInvites())) if (invite.getReceiver().getName().equalsIgnoreCase(nation.getName())) {
toCheck.deleteSentAllyInvite(invite);
InviteHandler.removeInvite(invite);
}
// Search and remove any sent ally invites sent by this nation.
for (Invite invite : new ArrayList<>(nation.getSentAllyInvites())) {
nation.deleteSentAllyInvite(invite);
InviteHandler.removeInvite(invite);
}
// Transfer any money to the warchest.
if (TownyEconomyHandler.isActive())
nation.getAccount().removeAccount();
// Delete nation and save towns
deleteNation(nation);
List<Town> toSave = new ArrayList<>(nation.getTowns());
nation.clear();
try {
universe.unregisterNation(nation);
} catch (NotRegisteredException e) {
// Just print out the exception. Very unlikely to happen.
e.printStackTrace();
}
for (Town town : toSave) {
for (Resident res : town.getResidents()) {
if (res.hasTitle() || res.hasSurname()) {
res.setTitle("");
res.setSurname("");
}
res.updatePermsForNationRemoval();
res.save();
}
try {
town.setNation(null);
} catch (AlreadyRegisteredException ignored) {
// Cannot reach AlreadyRegisteredException
}
town.save();
BukkitTools.getPluginManager().callEvent(new NationRemoveTownEvent(town, nation));
}
plugin.resetCache();
UUID kingUUID = null;
if (king != null)
kingUUID = king.getUUID();
BukkitTools.getPluginManager().callEvent(new DeleteNationEvent(nation, kingUUID));
}
Aggregations