Search in sources :

Example 1 with TownBlockStatus

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

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

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

Example 4 with TownBlockStatus

use of com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus 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)4 TownBlockStatus (com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus)4 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)3 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)2 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1