Search in sources :

Example 1 with ProtectionRegenTask

use of com.palmergames.bukkit.towny.tasks.ProtectionRegenTask 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 ProtectionRegenTask

use of com.palmergames.bukkit.towny.tasks.ProtectionRegenTask in project Towny by ElgarL.

the class TownyRegenAPI method cancelProtectionRegenTasks.

/**
	 * Cancel all regenerating tasks and clear all queues.
	 */
public static void cancelProtectionRegenTasks() {
    for (ProtectionRegenTask task : protectionRegenTasks.values()) {
        BukkitTools.getServer().getScheduler().cancelTask(task.getTaskId());
        task.replaceProtections();
    }
    protectionRegenTasks.clear();
    protectionPlaceholders.clear();
}
Also used : ProtectionRegenTask(com.palmergames.bukkit.towny.tasks.ProtectionRegenTask)

Example 3 with ProtectionRegenTask

use of com.palmergames.bukkit.towny.tasks.ProtectionRegenTask in project Towny by ElgarL.

the class TownyBlockListener method onBlockBreak.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    Player player = event.getPlayer();
    Block block = event.getBlock();
    //Get build permissions (updates cache if none exist)
    boolean bDestroy = PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(block), BukkitTools.getData(block), TownyPermission.ActionType.DESTROY);
    // Allow destroy if we are permitted
    if (bDestroy)
        return;
    /*
		 * Fetch the players cache
		 */
    PlayerCache cache = plugin.getCache(player);
    /*
		 * Allow destroy in a WarZone (FlagWar) if it's an editable material.
		 */
    if (cache.getStatus() == TownBlockStatus.WARZONE) {
        if (!TownyWarConfig.isEditableMaterialInWarZone(block.getType())) {
            event.setCancelled(true);
            TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_warzone_cannot_edit_material"), "destroy", block.getType().toString().toLowerCase()));
        }
        return;
    }
    /*
		 * Queue a protectionRegenTask if we have delayed regeneration set
		 */
    long delay = TownySettings.getRegenDelay();
    if (delay > 0) {
        if (!TownyRegenAPI.isPlaceholder(block)) {
            if (!TownyRegenAPI.hasProtectionRegenTask(new BlockLocation(block.getLocation()))) {
                ProtectionRegenTask task = new ProtectionRegenTask(plugin, block, true);
                task.setTaskId(plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, task, 20 * delay));
                TownyRegenAPI.addProtectionRegenTask(task);
            }
        } else {
            TownyRegenAPI.removePlaceholder(block);
            BukkitTools.setTypeId(block, 0, false);
        }
    } else {
        event.setCancelled(true);
    }
    /* 
		 * display any error recorded for this plot
		 */
    if ((cache.hasBlockErrMsg()) && (event.isCancelled()))
        TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
}
Also used : Player(org.bukkit.entity.Player) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) ProtectionRegenTask(com.palmergames.bukkit.towny.tasks.ProtectionRegenTask) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) EventHandler(org.bukkit.event.EventHandler)

Aggregations

ProtectionRegenTask (com.palmergames.bukkit.towny.tasks.ProtectionRegenTask)3 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)2 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)2 Block (org.bukkit.block.Block)2 EventHandler (org.bukkit.event.EventHandler)2 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)1 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)1 Coord (com.palmergames.bukkit.towny.object.Coord)1 PlayerCache (com.palmergames.bukkit.towny.object.PlayerCache)1 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1