Search in sources :

Example 6 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 7 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 8 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 9 with PlayerCache

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

the class TownyPlayerListener method onPlayerInteract.

public boolean onPlayerInteract(Player player, Block block, ItemStack item) {
    boolean cancelState = false;
    WorldCoord worldCoord;
    try {
        String worldName = player.getWorld().getName();
        if (block != null)
            worldCoord = new WorldCoord(worldName, Coord.parseCoord(block));
        else
            worldCoord = new WorldCoord(worldName, Coord.parseCoord(player));
        // Get itemUse permissions (updates if none exist)
        boolean bItemUse;
        if (block != null)
            bItemUse = PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(item), BukkitTools.getDataData(item), TownyPermission.ActionType.ITEM_USE);
        else
            bItemUse = PlayerCacheUtil.getCachePermission(player, player.getLocation(), BukkitTools.getTypeId(item), BukkitTools.getDataData(item), TownyPermission.ActionType.ITEM_USE);
        boolean wildOverride = TownyUniverse.getPermissionSource().hasWildOverride(worldCoord.getTownyWorld(), player, BukkitTools.getTypeId(item), BukkitTools.getDataData(item), TownyPermission.ActionType.ITEM_USE);
        PlayerCache cache = plugin.getCache(player);
        // cache.updateCoord(worldCoord);
        try {
            TownBlockStatus status = cache.getStatus();
            if (status == TownBlockStatus.UNCLAIMED_ZONE && wildOverride)
                return cancelState;
            // Allow item_use if we have an override
            if (((status == TownBlockStatus.TOWN_RESIDENT) && (TownyUniverse.getPermissionSource().hasOwnTownOverride(player, BukkitTools.getTypeId(item), BukkitTools.getDataData(item), TownyPermission.ActionType.ITEM_USE))) || (((status == TownBlockStatus.OUTSIDER) || (status == TownBlockStatus.TOWN_ALLY) || (status == TownBlockStatus.ENEMY)) && (TownyUniverse.getPermissionSource().hasAllTownOverride(player, BukkitTools.getTypeId(item), BukkitTools.getDataData(item), TownyPermission.ActionType.ITEM_USE))))
                return cancelState;
            if (status == TownBlockStatus.WARZONE) {
                if (!TownyWarConfig.isAllowingItemUseInWarZone()) {
                    cancelState = true;
                    TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_warzone_cannot_use_item"));
                }
                return cancelState;
            }
            if (((status == TownBlockStatus.UNCLAIMED_ZONE) && (!wildOverride)) || ((!bItemUse) && (status != TownBlockStatus.UNCLAIMED_ZONE))) {
                // if (status == TownBlockStatus.UNCLAIMED_ZONE)
                // TownyMessaging.sendErrorMsg(player,
                // String.format(TownySettings.getLangString("msg_err_cannot_perform_action"),
                // world.getUnclaimedZoneName()));
                cancelState = true;
            }
            if (// && (status !=
            (cache.hasBlockErrMsg()))
                // TownBlockStatus.UNCLAIMED_ZONE))
                TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
        } catch (NullPointerException e) {
            System.out.print("NPE generated!");
            System.out.print("Player: " + player.getName());
            System.out.print("Item: " + item.getData().getItemType().name());
        // System.out.print("Block: " + block.getType().toString());
        }
    } catch (NotRegisteredException e1) {
        TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_not_configured"));
        cancelState = true;
        return cancelState;
    }
    return cancelState;
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownBlockStatus(com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus)

Example 10 with PlayerCache

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

the class TownyVehicleListener method onVehicleDestroy.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onVehicleDestroy(VehicleDestroyEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    if (event.getAttacker() instanceof Player) {
        Player player = (Player) event.getAttacker();
        boolean bBreak = true;
        int blockID = 0;
        switch(event.getVehicle().getType()) {
            case MINECART:
                if (event.getVehicle() instanceof org.bukkit.entity.minecart.StorageMinecart) {
                    blockID = 342;
                } else if (event.getVehicle() instanceof org.bukkit.entity.minecart.RideableMinecart) {
                    blockID = 328;
                } else if (event.getVehicle() instanceof org.bukkit.entity.minecart.PoweredMinecart) {
                    blockID = 343;
                } else if (event.getVehicle() instanceof org.bukkit.entity.minecart.HopperMinecart) {
                    blockID = 408;
                } else {
                    blockID = 321;
                }
                if ((blockID != 0) && (!TownySettings.isItemUseMaterial(BukkitTools.getMaterial(blockID).name())))
                    return;
                // Get permissions (updates if none exist)
                bBreak = PlayerCacheUtil.getCachePermission(player, event.getVehicle().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.ITEM_USE);
                break;
            default:
                break;
        }
        if (blockID != 0) {
            // Allow the removal if we are permitted
            if (bBreak)
                return;
            event.setCancelled(true);
            /*
				 * Fetch the players cache
				 */
            PlayerCache cache = plugin.getCache(player);
            if (cache.hasBlockErrMsg())
                TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
            return;
        }
    }
}
Also used : Player(org.bukkit.entity.Player) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) EventHandler(org.bukkit.event.EventHandler)

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