Search in sources :

Example 1 with TownRuinedEvent

use of com.palmergames.bukkit.towny.event.town.TownRuinedEvent in project Towny by TownyAdvanced.

the class TownRuinUtil method putTownIntoRuinedState.

/**
 * Put town into ruined state:
 * 1. Remove town from nation
 * 2. Set mayor to NPC
 * 3. Enable all perms
 * 4. Now, the residents cannot run /plot commands, and some /t commands
 * 5. Town will later be deleted full, unless it is reclaimed
 * @param town The town to put into a "ruined" state.
 */
public static void putTownIntoRuinedState(Town town) {
    // Town already ruined.
    if (town.isRuined())
        return;
    // Remove town from nation, otherwise after we change the mayor to NPC and if the nation falls, the npc would receive nation refund.
    if (town.hasNation())
        town.removeNation();
    String oldMayorName = town.hasMayor() ? town.getMayor().getName() : "none";
    // Set NPC mayor, otherwise mayor of ruined town cannot leave until full deletion
    Resident resident = ResidentUtil.createAndGetNPCResident();
    try {
        resident.setTown(town);
    } catch (AlreadyRegisteredException ignored) {
    }
    resident.save();
    setMayor(town, resident);
    town.setHasUpkeep(false);
    // Call the TownRuinEvent.
    TownRuinedEvent event = new TownRuinedEvent(town, oldMayorName);
    Bukkit.getPluginManager().callEvent(event);
    // Set Town settings.
    town.setRuined(true);
    town.setRuinedTime(System.currentTimeMillis());
    town.setPublic(TownySettings.areRuinsMadePublic());
    town.setOpen(TownySettings.areRuinsMadeOpen());
    town.getPermissions().setAll(true);
    // Return town blocks to the basic, unowned, type
    for (TownBlock townBlock : town.getTownBlocks()) {
        if (townBlock.hasResident())
            // Removes any personal ownership.
            townBlock.setResident(null);
        // Sets the townblock's perm line to the Town's perm line set above.
        townBlock.setType(TownBlockType.RESIDENTIAL);
        // Makes the plot not for sale.
        townBlock.setPlotPrice(-1);
        // Removes plotgroup if it were present.
        townBlock.removePlotObjectGroup();
        // Removes all permission overrides from the plot.
        townBlock.getPermissionOverrides().clear();
        // Removes all trusted residents.
        townBlock.getTrustedResidents().clear();
        townBlock.save();
    }
    // Unregister the now empty plotgroups.
    if (town.getPlotGroups() != null)
        for (PlotGroup group : new ArrayList<>(town.getPlotGroups())) TownyUniverse.getInstance().getDataSource().removePlotGroup(group);
    // Check if Town has more residents than it should be allowed (if it were the capital of a nation.)
    if (TownySettings.getMaxResidentsPerTown() > 0)
        ResidentUtil.reduceResidentCountToFitTownMaxPop(town);
    town.save();
    Towny.getPlugin().resetCache();
    TownyMessaging.sendGlobalMessage(Translatable.of("msg_ruin_town", town.getName()));
}
Also used : AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownRuinedEvent(com.palmergames.bukkit.towny.event.town.TownRuinedEvent) PlotGroup(com.palmergames.bukkit.towny.object.PlotGroup) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Aggregations

TownRuinedEvent (com.palmergames.bukkit.towny.event.town.TownRuinedEvent)1 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)1 PlotGroup (com.palmergames.bukkit.towny.object.PlotGroup)1 Resident (com.palmergames.bukkit.towny.object.Resident)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1