Search in sources :

Example 1 with TownBlock

use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.

the class TownyEntityListener method onEntityExplode.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    TownyWorld townyWorld;
    /**
     * Perform this test outside the block loop so we only get the world
     * once per explosion.
     */
    try {
        townyWorld = TownyUniverse.getDataSource().getWorld(event.getLocation().getWorld().getName());
        if (!townyWorld.isUsingTowny())
            return;
    } catch (NotRegisteredException e) {
        // failed to get world so abort
        return;
    }
    Coord coord;
    List<Block> blocks = event.blockList();
    Entity entity = event.getEntity();
    int count = 0;
    // Sort blocks by height (lowest to highest).
    Collections.sort(blocks, ArraySort.getInstance());
    for (Block block : blocks) {
        coord = Coord.parseCoord(block.getLocation());
        count++;
        // Warzones
        if (townyWorld.isWarZone(coord)) {
            if (!TownyWarConfig.isAllowingExplosionsInWarZone()) {
                if (event.getEntity() != null)
                    TownyMessaging.sendDebugMsg("onEntityExplode: Canceled " + event.getEntity().getEntityId() + " from exploding within " + coord.toString() + ".");
                event.setCancelled(true);
                return;
            } else {
                if (TownyWarConfig.explosionsBreakBlocksInWarZone()) {
                    if (TownyWarConfig.regenBlocksAfterExplosionInWarZone()) {
                    // ***********************************
                    // TODO
                    // On completion, remove TODO from config.yml
                    // comments.
                    /*
							 * if
							 * (!plugin.getTownyUniverse().hasProtectionRegenTask
							 * (new BlockLocation(block.getLocation()))) {
							 * ProtectionRegenTask task = new
							 * ProtectionRegenTask(plugin.getTownyUniverse(),
							 * block, false);
							 * task.setTaskId(plugin.getServer().getScheduler().
							 * scheduleSyncDelayedTask(plugin, task,
							 * ((TownySettings.getPlotManagementWildRegenDelay()
							 * + count)*20)));
							 * plugin.getTownyUniverse().addProtectionRegenTask
							 * (task ); }
							 */
                    // TODO
                    // ***********************************
                    }
                // Break the block
                } else {
                    event.blockList().remove(block);
                }
            }
            return;
        }
        // TODO: expand to protect neutrals during a war
        try {
            TownBlock townBlock = townyWorld.getTownBlock(coord);
            // and the towns has no nation
            if (townyWorld.isUsingTowny() && !townyWorld.isForceExpl()) {
                if ((!townBlock.getPermissions().explosion) || (TownyUniverse.isWarTime() && TownySettings.isAllowWarBlockGriefing() && !townBlock.getTown().hasNation() && !townBlock.getTown().isBANG())) {
                    if (event.getEntity() != null)
                        TownyMessaging.sendDebugMsg("onEntityExplode: Canceled " + event.getEntity().getEntityId() + " from exploding within " + coord.toString() + ".");
                    event.setCancelled(true);
                    return;
                }
            }
        } catch (TownyException x) {
            // Wilderness explosion regeneration
            if (townyWorld.isUsingTowny())
                if (townyWorld.isExpl()) {
                    if (townyWorld.isUsingPlotManagementWildRevert() && (entity != null)) {
                        TownyMessaging.sendDebugMsg("onEntityExplode: Testing entity: " + entity.getType().getEntityClass().getSimpleName().toLowerCase() + " @ " + coord.toString() + ".");
                        if (townyWorld.isProtectingExplosionEntity(entity)) {
                            if ((!TownyRegenAPI.hasProtectionRegenTask(new BlockLocation(block.getLocation()))) && (block.getType() != Material.TNT)) {
                                ProtectionRegenTask task = new ProtectionRegenTask(plugin, block, false);
                                task.setTaskId(plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, task, ((TownySettings.getPlotManagementWildRegenDelay() + count) * 20)));
                                TownyRegenAPI.addProtectionRegenTask(task);
                                event.setYield((float) 0.0);
                                block.getDrops().clear();
                            }
                        }
                    }
                } else {
                    event.setCancelled(true);
                    return;
                }
        }
    }
}
Also used : Coord(com.palmergames.bukkit.towny.object.Coord) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) ProtectionRegenTask(com.palmergames.bukkit.towny.tasks.ProtectionRegenTask) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 2 with TownBlock

use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.

the class TownyEntityListener method onCreatureSpawn.

/**
 * @param event
 */
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    if (event.getEntity() instanceof LivingEntity) {
        LivingEntity livingEntity = (LivingEntity) event.getEntity();
        Location loc = event.getLocation();
        Coord coord = Coord.parseCoord(loc);
        TownyWorld townyWorld = null;
        try {
            townyWorld = TownyUniverse.getDataSource().getWorld(loc.getWorld().getName());
        } catch (NotRegisteredException e) {
            // Failed to fetch a world
            return;
        }
        // remove from world if set to remove mobs globally
        if (townyWorld.isUsingTowny())
            if (!townyWorld.hasWorldMobs() && ((MobRemovalTimerTask.isRemovingWorldEntity(livingEntity) || ((livingEntity instanceof Villager) && !((Villager) livingEntity).isAdult() && (TownySettings.isRemovingVillagerBabiesWorld()))))) {
                if (plugin.isCitizens2()) {
                    if (!CitizensAPI.getNPCRegistry().isNPC(livingEntity)) {
                        // TownyMessaging.sendDebugMsg("onCreatureSpawn world: Canceled "
                        // + event.getEntityType().name() +
                        // " from spawning within "+coord.toString()+".");
                        event.setCancelled(true);
                    }
                } else
                    event.setCancelled(true);
            }
        // remove from towns if in the list and set to remove
        try {
            TownBlock townBlock = townyWorld.getTownBlock(coord);
            if (townyWorld.isUsingTowny() && !townyWorld.isForceTownMobs()) {
                if (!townBlock.getTown().hasMobs() && !townBlock.getPermissions().mobs) {
                    if ((MobRemovalTimerTask.isRemovingTownEntity(livingEntity) || ((livingEntity instanceof Villager) && !((Villager) livingEntity).isAdult() && (TownySettings.isRemovingVillagerBabiesTown())))) {
                        if (plugin.isCitizens2()) {
                            if (!CitizensAPI.getNPCRegistry().isNPC(livingEntity)) {
                                // TownyMessaging.sendDebugMsg("onCreatureSpawn town: Canceled "
                                // + event.getEntityType().name() +
                                // " from spawning within "+coord.toString()+".");
                                event.setCancelled(true);
                            }
                        } else
                            event.setCancelled(true);
                    }
                }
            }
        } catch (TownyException x) {
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Coord(com.palmergames.bukkit.towny.object.Coord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Villager(org.bukkit.entity.Villager) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Location(org.bukkit.Location) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 3 with TownBlock

use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.

the class DailyTimerTask method collectTownTaxes.

/**
 * Collect taxes due to the town from it's residents.
 *
 * @param nation
 * @throws EconomyException
 */
protected void collectTownTaxes(Town town) throws EconomyException {
    // Resident Tax
    if (town.getTaxes() > 0) {
        List<Resident> residents = new ArrayList<Resident>(town.getResidents());
        ListIterator<Resident> residentItr = residents.listIterator();
        Resident resident = null;
        while (residentItr.hasNext()) {
            resident = residentItr.next();
            /*
				 * Only collect resident tax from this resident if it really
				 * still exists. We are running in an Async thread so MUST
				 * verify all objects.
				 */
            if (TownyUniverse.getDataSource().hasResident(resident.getName())) {
                if (TownyPerms.getResidentPerms(resident).containsKey("towny.tax_exempt") || resident.isNPC()) {
                    try {
                        TownyMessaging.sendResidentMessage(resident, TownySettings.getTaxExemptMsg());
                    } catch (TownyException e) {
                    // Player is not online
                    }
                    continue;
                } else if (town.isTaxPercentage()) {
                    double cost = resident.getHoldingBalance() * town.getTaxes() / 100;
                    resident.payTo(cost, town, "Town Tax (Percentage)");
                /*
						 * Don't send individual message anymore to ease up on
						 * the lag. try {
						 * TownyMessaging.sendResidentMessage(resident,
						 * TownySettings.getPayedResidentTaxMsg() + cost); }
						 * catch (TownyException e) { // Player is not online }
						 */
                } else if (!resident.payTo(town.getTaxes(), town, "Town Tax")) {
                    TownyMessaging.sendTownMessage(town, TownySettings.getCouldntPayTaxesMsg(resident, "town"));
                    try {
                        // reset this resident and remove him from the town.
                        resident.clear();
                        TownyUniverse.getDataSource().saveTown(town);
                    } catch (EmptyTownException e) {
                        // No mayor so remove the town.
                        TownyUniverse.getDataSource().removeTown(town);
                    }
                    TownyUniverse.getDataSource().saveResident(resident);
                }
            // else
            /*
					 * Don't send individual message anymore to ease up on the
					 * lag. try { TownyMessaging.sendResidentMessage(resident,
					 * TownySettings.getPayedResidentTaxMsg() +
					 * town.getTaxes()); } catch (TownyException e) { // Player
					 * is not online }
					 */
            }
        }
    }
    // Plot Tax
    if (town.getPlotTax() > 0 || town.getCommercialPlotTax() > 0 || town.getEmbassyPlotTax() > 0) {
        // Hashtable<Resident, Integer> townPlots = new Hashtable<Resident,
        // Integer>();
        // Hashtable<Resident, Double> townTaxes = new Hashtable<Resident,
        // Double>();
        List<TownBlock> townBlocks = new ArrayList<TownBlock>(town.getTownBlocks());
        ListIterator<TownBlock> townBlockItr = townBlocks.listIterator();
        TownBlock townBlock = null;
        while (townBlockItr.hasNext()) {
            townBlock = townBlockItr.next();
            if (!townBlock.hasResident())
                continue;
            try {
                Resident resident = townBlock.getResident();
                /*
					 * Only collect plot tax from this resident if it really
					 * still exists. We are running in an Async thread so MUST
					 * verify all objects.
					 */
                if (TownyUniverse.getDataSource().hasResident(resident.getName())) {
                    if (TownyPerms.getResidentPerms(resident).containsKey("towny.tax_exempt") || resident.isNPC()) {
                        continue;
                    }
                    if (!resident.payTo(townBlock.getType().getTax(town), town, String.format("Plot Tax (%s)", townBlock.getType()))) {
                        TownyMessaging.sendTownMessage(town, String.format(TownySettings.getLangString("msg_couldnt_pay_plot_taxes"), resident));
                        townBlock.setResident(null);
                        townBlock.setPlotPrice(-1);
                        // Set the plot permissions to mirror the towns.
                        townBlock.setType(townBlock.getType());
                        TownyUniverse.getDataSource().saveResident(resident);
                        TownyUniverse.getDataSource().saveTownBlock(townBlock);
                    }
                // else {
                // townPlots.put(resident,
                // (townPlots.containsKey(resident) ?
                // townPlots.get(resident) : 0) + 1);
                // townTaxes.put(resident,
                // (townTaxes.containsKey(resident) ?
                // townTaxes.get(resident) : 0) +
                // townBlock.getType().getTax(town));
                // }
                }
            } catch (NotRegisteredException e) {
            }
        }
    /*
			 * Don't send individual message anymore to ease up on the lag. for
			 * (Resident resident : townPlots.keySet()) { try { int numPlots =
			 * townPlots.get(resident); double totalCost =
			 * townTaxes.get(resident);
			 * TownyMessaging.sendResidentMessage(resident,
			 * String.format(TownySettings.getLangString("msg_payed_plot_cost"),
			 * totalCost, numPlots, town.getName())); } catch (TownyException e)
			 * { // Player is not online } }
			 */
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) ArrayList(java.util.ArrayList) Resident(com.palmergames.bukkit.towny.object.Resident) EmptyTownException(com.palmergames.bukkit.towny.exceptions.EmptyTownException) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 4 with TownBlock

use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.

the class DailyTimerTask method collectTownCosts.

/**
 * Collect or pay upkeep for all towns.
 *
 * @throws EconomyException
 * @throws TownyException
 */
public void collectTownCosts() throws EconomyException, TownyException {
    List<Town> towns = new ArrayList<Town>(TownyUniverse.getDataSource().getTowns());
    ListIterator<Town> townItr = towns.listIterator();
    Town town = null;
    while (townItr.hasNext()) {
        town = townItr.next();
        /*
			 * Only charge/pay upkeep for this town if it really still exists.
			 * We are running in an Async thread so MUST verify all objects.
			 */
        if (TownyUniverse.getDataSource().hasTown(town.getName())) {
            if (town.hasUpkeep()) {
                double upkeep = TownySettings.getTownUpkeepCost(town);
                if (upkeep > 0) {
                    // Town is paying upkeep
                    if (!town.pay(upkeep, "Town Upkeep")) {
                        TownyUniverse.getDataSource().removeTown(town);
                        TownyMessaging.sendGlobalMessage(town.getName() + TownySettings.getLangString("msg_bankrupt_town"));
                    }
                } else if (upkeep < 0) {
                    // Negative upkeep
                    if (TownySettings.isUpkeepPayingPlots()) {
                        // Pay each plot owner a share of the negative
                        // upkeep
                        List<TownBlock> plots = new ArrayList<TownBlock>(town.getTownBlocks());
                        for (TownBlock townBlock : plots) {
                            if (townBlock.hasResident())
                                townBlock.getResident().pay((upkeep / plots.size()), "Negative Town Upkeep - Plot income");
                            else
                                town.pay((upkeep / plots.size()), "Negative Town Upkeep - Plot income");
                        }
                    } else {
                        // Not paying plot owners so just pay the town
                        town.pay(upkeep, "Negative Town Upkeep");
                    }
                }
            }
        }
    }
    universe.setChangedNotify(UPKEEP_TOWN);
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Example 5 with TownBlock

use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.

the class PlotClaim method residentUnclaim.

private boolean residentUnclaim(WorldCoord worldCoord) throws TownyException {
    try {
        TownBlock townBlock = worldCoord.getTownBlock();
        townBlock.setResident(null);
        townBlock.setPlotPrice(townBlock.getTown().getPlotTypePrice(townBlock.getType()));
        // Set the plot permissions to mirror the towns.
        townBlock.setType(townBlock.getType());
        TownyUniverse.getDataSource().saveTownBlock(townBlock);
        plugin.updateCache(worldCoord);
    } catch (NotRegisteredException e) {
        throw new TownyException(TownySettings.getLangString("msg_not_own_place"));
    }
    return true;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

TownBlock (com.palmergames.bukkit.towny.object.TownBlock)36 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)30 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)17 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)13 Resident (com.palmergames.bukkit.towny.object.Resident)11 Town (com.palmergames.bukkit.towny.object.Town)9 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)9 Coord (com.palmergames.bukkit.towny.object.Coord)8 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)7 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)5 Location (org.bukkit.Location)5 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)4 World (org.bukkit.World)4 Block (org.bukkit.block.Block)4 Nation (com.palmergames.bukkit.towny.object.Nation)3 EventHandler (org.bukkit.event.EventHandler)3 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)2 KeyValueFile (com.palmergames.util.KeyValueFile)2 EOFException (java.io.EOFException)2