Search in sources :

Example 1 with SetPermissionType

use of com.palmergames.bukkit.towny.utils.PermissionGUIUtil.SetPermissionType in project Towny by TownyAdvanced.

the class PlayerCacheUtil method getPermission.

/**
 * Test if the player has permission to perform a certain action at this
 * WorldCoord.
 *
 * @param player - {@link Player}
 * @param status - {@link TownBlockStatus}
 * @param pos - {@link WorldCoord}
 * @param material - {@link Material}
 * @param action {@link ActionType}
 * @return true if allowed.
 */
private static boolean getPermission(Player player, TownBlockStatus status, WorldCoord pos, Material material, TownyPermission.ActionType action) {
    // Allow admins to have ALL permissions
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    if (townyUniverse.getPermissionSource().isTownyAdmin(player))
        return true;
    Town targetTown = pos.getTownOrNull();
    // If town is bankrupt (but not ruined), nobody can build
    if (targetTown != null && TownySettings.isTownBankruptcyEnabled() && action == ActionType.BUILD && targetTown.isBankrupt() && !targetTown.isRuined()) {
        cacheBlockErrMsg(player, Translatable.of("msg_err_bankrupt_town_cannot_build").forLocale(player));
        return false;
    }
    if (status == TownBlockStatus.OFF_WORLD || status == TownBlockStatus.PLOT_OWNER || status == TownBlockStatus.TOWN_OWNER)
        return true;
    Resident res = townyUniverse.getResident(player.getUniqueId());
    if (res == null) {
        cacheBlockErrMsg(player, Translatable.of("msg_err_not_registered").forLocale(player));
        return false;
    }
    if (status == TownBlockStatus.NOT_REGISTERED) {
        cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error").forLocale(player));
        return false;
    }
    if (status == TownBlockStatus.LOCKED) {
        cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_locked").forLocale(player));
        return false;
    }
    /*
		 * Handle the wilderness. 
		 */
    if (TownyAPI.getInstance().isWilderness(pos)) {
        /*
			 * Handle the Wilderness.
			 */
        boolean hasWildOverride = townyUniverse.getPermissionSource().hasWildOverride(pos.getTownyWorldOrNull(), player, material, action);
        if (status == TownBlockStatus.UNCLAIMED_ZONE) {
            if (hasWildOverride)
                return true;
            // Don't have permission to build/destroy/switch/item_use here
            cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_wild", Translatable.of(action.toString())).forLocale(player));
            return false;
        }
        /*
			 * Handle the possiblity that NationZones are enabled the 
			 * TownBlockStatus is NATION_ZONE instead of UNCLAIMED_ZONE.
			 * In all situations the player still has to hasWildOverride.
			 */
        if (TownySettings.getNationZonesEnabled() && status == TownBlockStatus.NATION_ZONE) {
            // Admins that also have wilderness permission can bypass the nation zone.
            if (townyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_ADMIN_NATION_ZONE.getNode()))
                return true;
            // Wasn't able to build in the wilderness, regardless.
            if (!hasWildOverride) {
                cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_wild", Translatable.of(action.toString())).forLocale(player));
                return false;
            }
            // We know that the nearest Town will have a nation because the TownBlockStatus.
            Nation nearestNation = TownyAPI.getInstance().getTownNationOrNull(pos.getTownyWorldOrNull().getClosestTownWithNationFromCoord(pos.getCoord(), null));
            // If the player has a Nation and is a member of this NationZone's nation.
            if (res.hasNation() && res.getNationOrNull().getUUID().equals(nearestNation.getUUID()))
                return true;
            // The player is not a nation member of this NationZone.
            cacheBlockErrMsg(player, Translatable.of("nation_zone_this_area_under_protection_of", pos.getTownyWorldOrNull().getUnclaimedZoneName(), nearestNation.getName()).forLocale(player));
            return false;
        }
    }
    /*
		 * Not going to be in the wilderness at this point.
		 */
    TownBlock townBlock = pos.getTownBlockOrNull();
    /*
		 * Check all-towns overrides before testing any plot permissions.
		 */
    if (townyUniverse.getPermissionSource().hasAllTownOverride(player, material, action))
        return true;
    /*
		 * Check own-town & town-owned overrides before testing plot permissions, if the resident is in their own town.
		 */
    if (targetTown.equals(TownyAPI.getInstance().getResidentTownOrNull(res))) {
        if (townyUniverse.getPermissionSource().hasOwnTownOverride(player, material, action))
            return true;
        if (!townBlock.hasResident() && townyUniverse.getPermissionSource().hasTownOwnedOverride(player, material, action))
            return true;
    }
    /*
		 * Player has a permission override set.
		 */
    if (townBlock.getPermissionOverrides().containsKey(res) && townBlock.getPermissionOverrides().get(res).getPermissionTypes()[action.getIndex()] != SetPermissionType.UNSET) {
        SetPermissionType type = townBlock.getPermissionOverrides().get(res).getPermissionTypes()[action.getIndex()];
        if (type == SetPermissionType.NEGATED)
            cacheBlockErrMsg(player, Translatable.of("msg_cache_block_err", Translatable.of(action.toString())).forLocale(player));
        return type.equals(SetPermissionType.SET);
    }
    /*
		 * Player has Trusted status here.
		 */
    if (status == TownBlockStatus.PLOT_TRUSTED || status == TownBlockStatus.TOWN_TRUSTED)
        return true;
    /*
		 * Handle personally-owned plots' friend and town permissions.
		 */
    if (status == TownBlockStatus.PLOT_FRIEND) {
        // Plot allows Friends perms and we aren't stopped by a TownBlockType overriding the allowed material and action.
        if (townBlock.getPermissions().getResidentPerm(action) && isAllowedMaterial(townBlock, material, action))
            return true;
        cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_plot", Translatable.of("msg_cache_block_error_plot_friends"), Translatable.of(action.toString())).forLocale(player));
        return false;
    }
    if (status == TownBlockStatus.PLOT_TOWN) {
        // Plot allows Town perms and we aren't stopped by a TownBlockType overriding the allowed material and action.
        if (townBlock.getPermissions().getNationPerm(action) && isAllowedMaterial(townBlock, material, action))
            return true;
        cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_plot", Translatable.of("msg_cache_block_error_plot_town_members"), Translatable.of(action.toString())).forLocale(player));
        return false;
    }
    /*
		 * Handle town-owned plots' resident and nation permissions.
		 */
    if (status == TownBlockStatus.TOWN_RESIDENT) {
        // Plot allows Resident perms and we aren't stopped by a TownBlockType overriding the allowed material and action.
        if (townBlock.getPermissions().getResidentPerm(action) && isAllowedMaterial(townBlock, material, action))
            return true;
        cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_town_resident", Translatable.of(action.toString())).forLocale(player));
        return false;
    }
    if (status == TownBlockStatus.TOWN_NATION) {
        // Plot allows Nation perms and we aren't stopped by a TownBlockType overriding the allowed material and action.
        if (townBlock.getPermissions().getNationPerm(action) && isAllowedMaterial(townBlock, material, action))
            return true;
        cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_town_nation", Translatable.of(action.toString())).forLocale(player));
        return false;
    }
    /*
		 * Handle both personally-owned and town-owned Ally permissions.
		 */
    if (status == TownBlockStatus.PLOT_ALLY || status == TownBlockStatus.TOWN_ALLY) {
        // Plot allows Ally perms and we aren't stopped by a TownBlockType overriding things.
        if (townBlock.getPermissions().getAllyPerm(action) && isAllowedMaterial(townBlock, material, action))
            return true;
        // Choose which error message will be shown.
        if (status == TownBlockStatus.PLOT_ALLY)
            cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_plot", Translatable.of("msg_cache_block_error_plot_allies"), Translatable.of(action.toString())).forLocale(player));
        else
            cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_town_allies", Translatable.of(action.toString())).forLocale(player));
        return false;
    }
    /*
		 * Handle both personally-owned and town-owned Outsider and Enemy statuses.
		 */
    if (status == TownBlockStatus.OUTSIDER || status == TownBlockStatus.ENEMY) {
        // Plot allows Outsider perms and we aren't stopped by a TownBlockType overriding the allowed material and action.
        if (townBlock.getPermissions().getOutsiderPerm(action) && isAllowedMaterial(townBlock, material, action))
            return true;
        // Choose which error message will be shown.
        if (townBlock.hasResident())
            cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_plot", Translatable.of("msg_cache_block_error_plot_outsiders"), Translatable.of(action.toString())).forLocale(player));
        else
            cacheBlockErrMsg(player, Translatable.of("msg_cache_block_error_town_outsider", Translatable.of(action.toString())).forLocale(player));
        return false;
    }
    /*
		 * Towny doesn't set a WARZONE status itself, some other plugin has used the API. 
		 */
    if (status == TownBlockStatus.WARZONE)
        return true;
    TownyMessaging.sendErrorMsg(player, "Error updating " + action.toString() + " permission.");
    return false;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) SetPermissionType(com.palmergames.bukkit.towny.utils.PermissionGUIUtil.SetPermissionType) Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Aggregations

TownyUniverse (com.palmergames.bukkit.towny.TownyUniverse)1 Nation (com.palmergames.bukkit.towny.object.Nation)1 Resident (com.palmergames.bukkit.towny.object.Resident)1 Town (com.palmergames.bukkit.towny.object.Town)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1 SetPermissionType (com.palmergames.bukkit.towny.utils.PermissionGUIUtil.SetPermissionType)1