Search in sources :

Example 1 with DeleteNationEvent

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);
}
Also used : DeleteNationEvent(com.palmergames.bukkit.towny.event.DeleteNationEvent) ArrayList(java.util.ArrayList) WarSpoils(com.palmergames.bukkit.towny.war.eventwar.WarSpoils) InvalidNameException(javax.naming.InvalidNameException)

Example 2 with DeleteNationEvent

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));
}
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)2 ArrayList (java.util.ArrayList)2 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 WarSpoils (com.palmergames.bukkit.towny.war.eventwar.WarSpoils)1 UUID (java.util.UUID)1 InvalidNameException (javax.naming.InvalidNameException)1