Search in sources :

Example 1 with PreDeleteNationEvent

use of com.palmergames.bukkit.towny.event.PreDeleteNationEvent 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));
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) NationRemoveTownEvent(com.palmergames.bukkit.towny.event.NationRemoveTownEvent) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) ArrayList(java.util.ArrayList) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) Town(com.palmergames.bukkit.towny.object.Town) DeleteNationEvent(com.palmergames.bukkit.towny.event.DeleteNationEvent) PreDeleteNationEvent(com.palmergames.bukkit.towny.event.PreDeleteNationEvent) Resident(com.palmergames.bukkit.towny.object.Resident) PreDeleteNationEvent(com.palmergames.bukkit.towny.event.PreDeleteNationEvent) UUID(java.util.UUID) Invite(com.palmergames.bukkit.towny.invites.Invite)

Aggregations

DeleteNationEvent (com.palmergames.bukkit.towny.event.DeleteNationEvent)1 NationRemoveTownEvent (com.palmergames.bukkit.towny.event.NationRemoveTownEvent)1 PreDeleteNationEvent (com.palmergames.bukkit.towny.event.PreDeleteNationEvent)1 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)1 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)1 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)1 Invite (com.palmergames.bukkit.towny.invites.Invite)1 Nation (com.palmergames.bukkit.towny.object.Nation)1 Resident (com.palmergames.bukkit.towny.object.Resident)1 Town (com.palmergames.bukkit.towny.object.Town)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1