Search in sources :

Example 16 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(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)

Example 17 with PlayerCache

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

the class PlayerCacheUtil method cacheBuild.

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

Example 18 with PlayerCache

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

the class PlayerCacheUtil method cacheItemUse.

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

Example 19 with PlayerCache

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

the class PlayerCacheUtil method getCachePermission.

/**
	 * Returns player cached permission for BUILD, DESTROY, SWITCH or ITEM_USE
	 * at this location for the specified item id.
	 * 
	 * Generates the cache if it doesn't exist.
	 * 
	 * @param player
	 * @param location
	 * @param blockId
	 * @param data
	 * @param action
	 * @return true if the player has permission.
	 */
public static boolean getCachePermission(Player player, Location location, Integer blockId, byte data, ActionType action) {
    WorldCoord worldCoord;
    try {
        worldCoord = new WorldCoord(player.getWorld().getName(), Coord.parseCoord(location));
        PlayerCache cache = plugin.getCache(player);
        cache.updateCoord(worldCoord);
        TownyMessaging.sendDebugMsg("Cache permissions for " + action.toString() + " : " + cache.getCachePermission(blockId, data, action));
        // Throws NullPointerException if the cache is empty
        return cache.getCachePermission(blockId, data, action);
    } catch (NullPointerException e) {
        // New or old cache permission was null, update it
        worldCoord = new WorldCoord(player.getWorld().getName(), Coord.parseCoord(location));
        TownBlockStatus status = cacheStatus(player, worldCoord, getTownBlockStatus(player, worldCoord));
        triggerCacheCreate(player, location, worldCoord, status, blockId, data, action);
        PlayerCache cache = plugin.getCache(player);
        cache.updateCoord(worldCoord);
        TownyMessaging.sendDebugMsg("New Cache Created and updated!");
        TownyMessaging.sendDebugMsg("New Cache permissions for " + blockId + ":" + action.toString() + ":" + status.name() + " = " + cache.getCachePermission(blockId, data, action));
        return cache.getCachePermission(blockId, data, action);
    }
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) 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