Search in sources :

Example 1 with PlayerCache

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

the class TownyEntityListener method onEntityDamageByEntityEvent.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
    if (plugin.isError()) {
        return;
    }
    TownyWorld townyWorld = null;
    Entity entity = event.getEntity();
    if (entity instanceof ArmorStand) {
        String damager = event.getDamager().getType().name();
        if (damager == "PRIMED_TNT" || damager == "WITHER_SKULL" || damager == "FIREBALL" || damager == "SMALL_FIREBALL" || damager == "LARGE_FIREBALL" || damager == "WITHER") {
            try {
                townyWorld = TownyUniverse.getDataSource().getWorld(entity.getWorld().getName());
            } catch (NotRegisteredException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (!locationCanExplode(townyWorld, entity.getLocation())) {
                event.setCancelled(true);
                return;
            }
        }
        if (event.getDamager() instanceof Projectile) {
            try {
                townyWorld = TownyUniverse.getDataSource().getWorld(entity.getWorld().getName());
            } catch (NotRegisteredException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Object remover = event.getDamager();
            remover = ((Projectile) remover).getShooter();
            if (remover instanceof Monster) {
                event.setCancelled(true);
            } else if (remover instanceof Player) {
                Player player = (Player) remover;
                // Get destroy permissions (updates if none exist)
                boolean bDestroy = PlayerCacheUtil.getCachePermission(player, entity.getLocation(), 416, (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);
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) ArmorStand(org.bukkit.entity.ArmorStand) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Monster(org.bukkit.entity.Monster) 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 2 with PlayerCache

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

the class CombatUtil method preventDamageCall.

/**
 * Tests the attacker against defender to see if we need to cancel
 * the damage event due to world PvP, Plot PvP or Friendly Fire settings.
 * Only allow a Wolves owner to cause it damage, and residents with destroy
 * permissions to damage passive animals and villagers while in a town.
 *
 * @param world
 * @param attackingEntity
 * @param defendingEntity
 * @param attackingPlayer
 * @param defendingPlayer
 * @return true if we should cancel.
 */
public static boolean preventDamageCall(Towny plugin, TownyWorld world, Entity attackingEntity, Entity defendingEntity, Player attackingPlayer, Player defendingPlayer) {
    // World using Towny
    if (!world.isUsingTowny())
        return false;
    /*
		 * We have an attacking player
		 */
    if (attackingPlayer != null) {
        Coord coord = Coord.parseCoord(defendingEntity);
        TownBlock defenderTB = null;
        TownBlock attackerTB = null;
        try {
            attackerTB = world.getTownBlock(Coord.parseCoord(attackingEntity));
        } catch (NotRegisteredException ex) {
        }
        try {
            defenderTB = world.getTownBlock(coord);
        } catch (NotRegisteredException ex) {
        }
        /*
			 * If another player is the target
			 * or
			 * The target is in a TownBlock and...
			 * the target is a tame wolf and we are not it's owner
			 */
        if ((defendingPlayer != null) || ((defenderTB != null) && ((defendingEntity instanceof Wolf) && ((Wolf) defendingEntity).isTamed() && !((Wolf) defendingEntity).getOwner().equals((AnimalTamer) attackingEntity)))) {
            /*
				 * Defending player is in a warzone
				 */
            if (world.isWarZone(coord))
                return false;
            /*
				 * Check for special pvp plots (arena)
				 */
            if (isPvPPlot(attackingPlayer, defendingPlayer))
                return false;
            /*
				 * Check if we are preventing friendly fire between allies
				 * Check the attackers TownBlock and it's Town for their PvP
				 * status, else the world.
				 * Check the defenders TownBlock and it's Town for their PvP
				 * status, else the world.
				 */
            if (preventFriendlyFire(attackingPlayer, defendingPlayer) || preventPvP(world, attackerTB) || preventPvP(world, defenderTB)) {
                DisallowedPVPEvent event = new DisallowedPVPEvent(attackingPlayer, defendingPlayer);
                plugin.getServer().getPluginManager().callEvent(event);
                return !event.isCancelled();
            }
        } else {
            /*
				 * Defender is not a player so check for PvM
				 */
            if (defenderTB != null) {
                List<Class<?>> prots = EntityTypeUtil.parseLivingEntityClassNames(TownySettings.getEntityTypes(), "TownMobPVM:");
                if (EntityTypeUtil.isInstanceOfAny(prots, defendingEntity)) {
                    /*
						 * Only allow the player to kill protected entities etc,
						 * if they are from the same town
						 * and have destroy permissions (grass) in the defending
						 * TownBlock
						 */
                    if (!PlayerCacheUtil.getCachePermission(attackingPlayer, attackingPlayer.getLocation(), 3, (byte) 0, ActionType.DESTROY))
                        return true;
                }
            }
            /*
				 * Remove prevention end
				 */
            /*
				 * Protect specific entity interactions (faked with block ID's).
				 */
            int blockID = 0;
            switch(defendingEntity.getType()) {
                case ITEM_FRAME:
                    blockID = 389;
                    break;
                case PAINTING:
                    blockID = 321;
                    break;
                case MINECART:
                    if (defendingEntity instanceof org.bukkit.entity.minecart.StorageMinecart) {
                        blockID = 342;
                    } else if (defendingEntity instanceof org.bukkit.entity.minecart.RideableMinecart) {
                        blockID = 328;
                    } else if (defendingEntity instanceof org.bukkit.entity.minecart.PoweredMinecart) {
                        blockID = 343;
                    } else if (defendingEntity instanceof org.bukkit.entity.minecart.HopperMinecart) {
                        blockID = 408;
                    } else {
                        blockID = 321;
                    }
                default:
                    break;
            }
            if (blockID != 0) {
                // Get permissions (updates if none exist)
                boolean bDestroy = PlayerCacheUtil.getCachePermission(attackingPlayer, defendingEntity.getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
                if (!bDestroy) {
                    /*
						 * Fetch the players cache
						 */
                    PlayerCache cache = plugin.getCache(attackingPlayer);
                    if (cache.hasBlockErrMsg())
                        TownyMessaging.sendErrorMsg(attackingPlayer, cache.getBlockErrMsg());
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Coord(com.palmergames.bukkit.towny.object.Coord) Wolf(org.bukkit.entity.Wolf) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) DisallowedPVPEvent(com.palmergames.bukkit.towny.event.DisallowedPVPEvent)

Example 3 with PlayerCache

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

the class TownyPlayerListener method onPlayerMove.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    /*
		 * Abort if we havn't really moved
		 */
    if (event.getFrom().getBlockX() == event.getTo().getBlockX() && event.getFrom().getBlockZ() == event.getTo().getBlockZ() && event.getFrom().getBlockY() == event.getTo().getBlockY()) {
        return;
    }
    Player player = event.getPlayer();
    Location to = event.getTo();
    Location from;
    PlayerCache cache = plugin.getCache(player);
    try {
        from = cache.getLastLocation();
    } catch (NullPointerException e) {
        from = event.getFrom();
    }
    // Prevent fly/double jump cheats
    if (!(event instanceof PlayerTeleportEvent)) {
        if (TownySettings.isUsingCheatProtection() && (player.getGameMode() != GameMode.CREATIVE) && !TownyUniverse.getPermissionSource().has(player, PermissionNodes.CHEAT_BYPASS.getNode())) {
            try {
                if (TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).isUsingTowny())
                    if ((from.getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR) && (player.getFallDistance() == 0) && (player.getVelocity().getY() <= -0.6) && (player.getLocation().getY() > 0)) {
                        // plugin.sendErrorMsg(player, "Cheat Detected!");
                        Location blockLocation = from;
                        // find the first non air block below us
                        while ((blockLocation.getBlock().getType() == Material.AIR) && (blockLocation.getY() > 0)) blockLocation.setY(blockLocation.getY() - 1);
                        // set to 1 block up so we are not sunk in the
                        // ground
                        blockLocation.setY(blockLocation.getY() + 1);
                        // Update the cache for this location (same
                        // WorldCoord).
                        cache.setLastLocation(blockLocation);
                        player.teleport(blockLocation);
                        return;
                    }
            } catch (NotRegisteredException e1) {
                TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_not_configured"));
                return;
            }
        }
    }
    try {
        TownyWorld fromWorld = TownyUniverse.getDataSource().getWorld(from.getWorld().getName());
        WorldCoord fromCoord = new WorldCoord(fromWorld.getName(), Coord.parseCoord(from));
        TownyWorld toWorld = TownyUniverse.getDataSource().getWorld(to.getWorld().getName());
        WorldCoord toCoord = new WorldCoord(toWorld.getName(), Coord.parseCoord(to));
        if (!fromCoord.equals(toCoord))
            onPlayerMoveChunk(player, fromCoord, toCoord, from, to, event);
        else {
        // plugin.sendDebugMsg("    From: " + fromCoord);
        // plugin.sendDebugMsg("    To:   " + toCoord);
        // plugin.sendDebugMsg("        " + from.toString());
        // plugin.sendDebugMsg("        " + to.toString());
        }
    } catch (NotRegisteredException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    }
    // Update the cached players current location
    cache.setLastLocation(to);
// plugin.updateCache(player);
// plugin.sendDebugMsg("onBlockMove: " + player.getName() + ": ");
// plugin.sendDebugMsg("        " + from.toString());
// plugin.sendDebugMsg("        " + to.toString());
}
Also used : Player(org.bukkit.entity.Player) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerTeleportEvent(org.bukkit.event.player.PlayerTeleportEvent) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Location(org.bukkit.Location) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) EventHandler(org.bukkit.event.EventHandler)

Example 4 with PlayerCache

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

the class TownyBlockListener method onBlockPlace.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    Player player = event.getPlayer();
    Block block = event.getBlock();
    WorldCoord worldCoord;
    try {
        TownyWorld world = TownyUniverse.getDataSource().getWorld(block.getWorld().getName());
        worldCoord = new WorldCoord(world.getName(), Coord.parseCoord(block));
        // Get build permissions (updates if none exist)
        boolean bBuild = PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(block), BukkitTools.getData(block), TownyPermission.ActionType.BUILD);
        // Allow build if we are permitted
        if (bBuild)
            return;
        /*
			 * Fetch the players cache
			 */
        PlayerCache cache = plugin.getCache(player);
        TownBlockStatus status = cache.getStatus();
        /*
			 * Flag war
			 */
        if (((status == TownBlockStatus.ENEMY) && TownyWarConfig.isAllowingAttacks()) && (event.getBlock().getType() == TownyWarConfig.getFlagBaseMaterial())) {
            try {
                if (TownyWar.callAttackCellEvent(plugin, player, block, worldCoord))
                    return;
            } catch (TownyException e) {
                TownyMessaging.sendErrorMsg(player, e.getMessage());
            }
            event.setBuild(false);
            event.setCancelled(true);
        } else if (status == TownBlockStatus.WARZONE) {
            if (!TownyWarConfig.isEditableMaterialInWarZone(block.getType())) {
                event.setBuild(false);
                event.setCancelled(true);
                TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_warzone_cannot_edit_material"), "build", block.getType().toString().toLowerCase()));
            }
            return;
        } else {
            event.setBuild(false);
            event.setCancelled(true);
        }
        /* 
			 * display any error recorded for this plot
			 */
        if ((cache.hasBlockErrMsg()) && (event.isCancelled()))
            TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
    } catch (NotRegisteredException e1) {
        TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_not_configured"));
        event.setCancelled(true);
    }
}
Also used : Player(org.bukkit.entity.Player) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownBlockStatus(com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 5 with PlayerCache

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

the class PlayerCacheUtil method cacheSwitch.

/**
 * Update the player cache for Switch rights at this WorldCoord.
 *
 * @param player
 * @param worldCoord
 * @param id
 * @param data
 * @param switchRight
 */
private static void cacheSwitch(Player player, WorldCoord worldCoord, Integer id, byte data, Boolean switchRight) {
    PlayerCache cache = plugin.getCache(player);
    cache.updateCoord(worldCoord);
    cache.setSwitchPermission(id, data, switchRight);
    TownyMessaging.sendDebugMsg(player.getName() + " (" + worldCoord.toString() + ") Cached Switch: " + switchRight);
}
Also used : PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache)

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