Search in sources :

Example 31 with TownyWorld

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

the class TownyPlayerListener method onPlayerInteract.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    Player player = event.getPlayer();
    Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    TownyWorld World = null;
    try {
        World = TownyUniverse.getDataSource().getWorld(block.getLocation().getWorld().getName());
        if (!World.isUsingTowny())
            return;
    } catch (NotRegisteredException e) {
        // World not registered with Towny.
        e.printStackTrace();
        return;
    }
    if ((event.getAction() == Action.PHYSICAL)) {
        if ((block.getType() == Material.SOIL) || (block.getType() == Material.CROPS))
            if (World.isDisablePlayerTrample() || !PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(block), BukkitTools.getData(block), TownyPermission.ActionType.DESTROY)) {
                event.setCancelled(true);
                return;
            }
    }
    if (event.hasItem()) {
        /*
			 * Info Tool
			 */
        if (event.getPlayer().getItemInHand().getType() == Material.getMaterial(TownySettings.getTool())) {
            if (TownyUniverse.getPermissionSource().isTownyAdmin(player)) {
                if (event.getClickedBlock() instanceof Block) {
                    block = (Block) event.getClickedBlock();
                    TownyMessaging.sendMessage(player, Arrays.asList(ChatTools.formatTitle("Block Info"), ChatTools.formatCommand("", "Block Type", "", block.getType().name()), ChatTools.formatCommand("", "Data value", "", Byte.toString(BukkitTools.getData(block)))));
                    event.setCancelled(true);
                }
            }
        }
        if (TownySettings.isItemUseMaterial(event.getItem().getType().name())) {
            event.setCancelled(onPlayerInteract(player, event.getClickedBlock(), event.getItem()));
        }
    }
    if (event.getClickedBlock() != null) {
        // Towny regen
        if (TownySettings.getRegenDelay() > 0) {
            if (event.getClickedBlock().getState().getData() instanceof Attachable) {
                Attachable attachable = (Attachable) event.getClickedBlock().getState().getData();
                BlockLocation attachedToBlock = new BlockLocation(event.getClickedBlock().getRelative(attachable.getAttachedFace()).getLocation());
                // Prevent attached blocks from falling off when interacting
                if (TownyRegenAPI.hasProtectionRegenTask(attachedToBlock)) {
                    event.setCancelled(true);
                    return;
                }
            }
        }
        if (TownySettings.isSwitchMaterial(event.getClickedBlock().getType().name()) || event.getAction() == Action.PHYSICAL) {
            onPlayerSwitchEvent(event, null, World);
            return;
        }
    }
}
Also used : Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Attachable(org.bukkit.material.Attachable) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) EventHandler(org.bukkit.event.EventHandler)

Example 32 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld 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 33 with TownyWorld

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

the class TownyPlayerListener method onPlayerInteractEntity.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent 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 ITEM_FRAME:
                TownyMessaging.sendDebugMsg("ItemFrame Right Clicked");
                blockID = 389;
                // Get permissions (updates if none exist)
                bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
                break;
            case PAINTING:
                blockID = 321;
                // Get permissions (updates if none exist)
                bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
                break;
            case MINECART:
                if (event.getRightClicked() instanceof org.bukkit.entity.minecart.StorageMinecart) {
                    blockID = 342;
                } else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.RideableMinecart) {
                    blockID = 328;
                } else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.PoweredMinecart) {
                    blockID = 343;
                } else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.HopperMinecart) {
                    blockID = 408;
                } else {
                    blockID = 321;
                }
                if ((blockID != 0) && (!TownySettings.isSwitchMaterial(BukkitTools.getMaterial(blockID).name())))
                    return;
                // Get permissions (updates if none exist)
                bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.SWITCH);
                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)

Aggregations

TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)33 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)27 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)15 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)14 EventHandler (org.bukkit.event.EventHandler)12 Player (org.bukkit.entity.Player)11 Entity (org.bukkit.entity.Entity)9 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)7 PlayerCache (com.palmergames.bukkit.towny.object.PlayerCache)7 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)7 Location (org.bukkit.Location)7 LivingEntity (org.bukkit.entity.LivingEntity)7 Coord (com.palmergames.bukkit.towny.object.Coord)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Resident (com.palmergames.bukkit.towny.object.Resident)5 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)5 Block (org.bukkit.block.Block)5 Town (com.palmergames.bukkit.towny.object.Town)3 Projectile (org.bukkit.entity.Projectile)3