Search in sources :

Example 11 with PlayerCache

use of com.palmergames.bukkit.towny.object.PlayerCache 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)

Example 12 with PlayerCache

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

the class TownyEntityListener method onHangingPlace.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onHangingPlace(HangingPlaceEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    long start = System.currentTimeMillis();
    Player player = event.getPlayer();
    Entity hanging = event.getEntity();
    try {
        TownyWorld townyWorld = TownyUniverse.getDataSource().getWorld(hanging.getWorld().getName());
        if (!townyWorld.isUsingTowny())
            return;
        // Get build permissions (updates if none exist)
        boolean bBuild = PlayerCacheUtil.getCachePermission(player, hanging.getLocation(), 321, (byte) 0, TownyPermission.ActionType.BUILD);
        // Allow placing if we are permitted
        if (bBuild)
            return;
        /*
			 * Fetch the players cache
			 */
        PlayerCache cache = plugin.getCache(player);
        event.setCancelled(true);
        if (cache.hasBlockErrMsg())
            TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
    } catch (NotRegisteredException e1) {
        TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_not_configured"));
        event.setCancelled(true);
        return;
    }
    TownyMessaging.sendDebugMsg("onHangingBreak took " + (System.currentTimeMillis() - start) + "ms (" + event.getEventName() + ", " + event.isCancelled() + ")");
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) EventHandler(org.bukkit.event.EventHandler)

Example 13 with PlayerCache

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

the class TownyEntityListener method onHangingBreak.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onHangingBreak(HangingBreakEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    TownyWorld townyWorld = null;
    String worldName = null;
    Entity hanging = event.getEntity();
    try {
        worldName = hanging.getWorld().getName();
        townyWorld = TownyUniverse.getDataSource().getWorld(worldName);
        if (!townyWorld.isUsingTowny())
            return;
    } catch (NotRegisteredException e1) {
        // event.setCancelled(true);
        return;
    }
    if (event instanceof HangingBreakByEntityEvent) {
        HangingBreakByEntityEvent evt = (HangingBreakByEntityEvent) event;
        Object remover = evt.getRemover();
        /*
			 * Check if this has a shooter.
			 */
        if (remover instanceof Projectile) {
            remover = ((Projectile) remover).getShooter();
        }
        if (remover instanceof Player) {
            Player player = (Player) remover;
            // Get destroy permissions (updates if none exist)
            boolean bDestroy = PlayerCacheUtil.getCachePermission(player, hanging.getLocation(), 321, (byte) 0, TownyPermission.ActionType.DESTROY);
            // Allow the removal if we are permitted
            if (bDestroy)
                return;
            /*
				 * Fetch the players cache
				 */
            PlayerCache cache = plugin.getCache(player);
            event.setCancelled(true);
            if (cache.hasBlockErrMsg())
                TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
        } else {
            // Explosions are blocked in this plot
            if (!locationCanExplode(townyWorld, hanging.getLocation()))
                event.setCancelled(true);
        }
    } else {
        switch(event.getCause()) {
            case EXPLOSION:
                if (!locationCanExplode(townyWorld, event.getEntity().getLocation()))
                    event.setCancelled(true);
                break;
            default:
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) HangingBreakByEntityEvent(org.bukkit.event.hanging.HangingBreakByEntityEvent) Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 14 with PlayerCache

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

the class TownyPlayerListener method onPlayerInteractEntity.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractAtEntityEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    if (event.getRightClicked() != null) {
        TownyWorld World = null;
        try {
            World = TownyUniverse.getDataSource().getWorld(event.getPlayer().getWorld().getName());
            if (!World.isUsingTowny())
                return;
        } catch (NotRegisteredException e) {
            // World not registered with Towny.
            e.printStackTrace();
            return;
        }
        Player player = event.getPlayer();
        boolean bBuild = true;
        int blockID = 0;
        /*
			 * Protect specific entity interactions.
			 */
        switch(event.getRightClicked().getType()) {
            case ARMOR_STAND:
                TownyMessaging.sendDebugMsg("ArmorStand Right Clicked");
                blockID = 416;
                // Get permissions (updates if none exist)
                bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
                break;
            default:
                break;
        }
        if (blockID != 0) {
            // Allow the removal if we are permitted
            if (bBuild)
                return;
            event.setCancelled(true);
            /*
				 * Fetch the players cache
				 */
            PlayerCache cache = plugin.getCache(player);
            if (cache.hasBlockErrMsg())
                TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
            return;
        }
        /*
			 * Item_use protection.
			 */
        if (event.getPlayer().getItemInHand() != null) {
            /*
				 * Info Tool
				 */
            if (event.getPlayer().getItemInHand().getType() == Material.getMaterial(TownySettings.getTool())) {
                Entity entity = event.getRightClicked();
                TownyMessaging.sendMessage(player, Arrays.asList(ChatTools.formatTitle("Entity Info"), ChatTools.formatCommand("", "Entity Class", "", entity.getType().getEntityClass().getSimpleName())));
                event.setCancelled(true);
            }
            if (TownySettings.isItemUseMaterial(event.getPlayer().getItemInHand().getType().name())) {
                event.setCancelled(onPlayerInteract(event.getPlayer(), null, event.getPlayer().getItemInHand()));
                return;
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) EventHandler(org.bukkit.event.EventHandler)

Example 15 with PlayerCache

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

the class TownyPlayerListener method onPlayerSwitchEvent.

public boolean onPlayerSwitchEvent(Player player, Block block, String errMsg, TownyWorld world) {
    if (!TownySettings.isSwitchMaterial(block.getType().name()))
        return false;
    // Get switch permissions (updates if none exist)
    boolean bSwitch = PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(block), BukkitTools.getData(block), TownyPermission.ActionType.SWITCH);
    // Allow switch if we are permitted
    if (bSwitch)
        return false;
    /*
		 * Fetch the players cache
		 */
    PlayerCache cache = plugin.getCache(player);
    TownBlockStatus status = cache.getStatus();
    /*
		 * display any error recorded for this plot
		 */
    if (cache.hasBlockErrMsg())
        TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
    /*
		 * Flag war
		 */
    if (status == TownBlockStatus.WARZONE) {
        if (!TownyWarConfig.isAllowingSwitchesInWarZone()) {
            TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_warzone_cannot_use_switches"));
            return true;
        }
        return false;
    } else {
        return true;
    }
}
Also used : PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownBlockStatus(com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus)

Aggregations

PlayerCache (com.palmergames.bukkit.towny.object.PlayerCache)19 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)9 Player (org.bukkit.entity.Player)9 EventHandler (org.bukkit.event.EventHandler)9 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)7 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)5 Entity (org.bukkit.entity.Entity)5 TownBlockStatus (com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus)4 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)3 LivingEntity (org.bukkit.entity.LivingEntity)3 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)2 Block (org.bukkit.block.Block)2 Projectile (org.bukkit.entity.Projectile)2 DisallowedPVPEvent (com.palmergames.bukkit.towny.event.DisallowedPVPEvent)1 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)1 Coord (com.palmergames.bukkit.towny.object.Coord)1 ProtectionRegenTask (com.palmergames.bukkit.towny.tasks.ProtectionRegenTask)1 Location (org.bukkit.Location)1 ArmorStand (org.bukkit.entity.ArmorStand)1 Monster (org.bukkit.entity.Monster)1