Search in sources :

Example 41 with Town

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

the class PlayerCacheUtil method getPermission.

/**
	 * Test if the player has permission to perform a certain action at this
	 * WorldCoord.
	 * 
	 * @param player
	 * @param status
	 * @param pos
	 * @param id
	 * @param data
	 * @param action
	 * @return true if allowed.
	 */
private static boolean getPermission(Player player, TownBlockStatus status, WorldCoord pos, Integer blockId, byte data, TownyPermission.ActionType action) {
    if (// || plugin.isTownyAdmin(player)) // status == TownBlockStatus.ADMIN ||
    status == TownBlockStatus.OFF_WORLD || status == TownBlockStatus.WARZONE || status == TownBlockStatus.PLOT_OWNER || status == TownBlockStatus.TOWN_OWNER)
        return true;
    if (status == TownBlockStatus.NOT_REGISTERED) {
        cacheBlockErrMsg(player, TownySettings.getLangString("msg_cache_block_error"));
        return false;
    }
    if (status == TownBlockStatus.LOCKED) {
        cacheBlockErrMsg(player, TownySettings.getLangString("msg_cache_block_error_locked"));
        return false;
    }
    TownBlock townBlock = null;
    Town playersTown = null;
    Town targetTown = null;
    try {
        playersTown = TownyUniverse.getDataSource().getResident(player.getName()).getTown();
    } catch (NotRegisteredException e1) {
    }
    try {
        townBlock = pos.getTownBlock();
        targetTown = townBlock.getTown();
    } catch (NotRegisteredException e) {
        try {
            // Wilderness Permissions
            if (status == TownBlockStatus.UNCLAIMED_ZONE) {
                if (TownyUniverse.getPermissionSource().hasWildOverride(pos.getTownyWorld(), player, blockId, data, action)) {
                    return true;
                } else {
                    // Don't have permission to build/destroy/switch/item_use here
                    cacheBlockErrMsg(player, String.format(TownySettings.getLangString("msg_cache_block_error_wild"), action.toString()));
                    return false;
                }
            }
        } catch (NotRegisteredException e2) {
            TownyMessaging.sendErrorMsg(player, "Error updating " + action.toString() + " permission.");
            return false;
        }
    }
    // Allow admins to have ALL permissions over towns.
    if (TownyUniverse.getPermissionSource().isTownyAdmin(player))
        return true;
    if (townBlock.hasResident()) {
        /*
			 * Check town overrides before testing plot permissions
			 */
        if (targetTown.equals(playersTown) && (TownyUniverse.getPermissionSource().hasOwnTownOverride(player, blockId, data, action))) {
            return true;
        } else if (!targetTown.equals(playersTown) && (TownyUniverse.getPermissionSource().hasAllTownOverride(player, blockId, data, action))) {
            return true;
        } else if (status == TownBlockStatus.PLOT_FRIEND) {
            if (townBlock.getPermissions().getResidentPerm(action)) {
                if (townBlock.getType() == TownBlockType.WILDS) {
                    try {
                        if (TownyUniverse.getPermissionSource().unclaimedZoneAction(pos.getTownyWorld(), blockId, action))
                            return true;
                    } catch (NotRegisteredException e) {
                    }
                } else {
                    return true;
                }
            }
            cacheBlockErrMsg(player, String.format(TownySettings.getLangString("msg_cache_block_error_plot"), "friends", action.toString()));
            return false;
        } else if (status == TownBlockStatus.PLOT_ALLY) {
            if (townBlock.getPermissions().getAllyPerm(action)) {
                if (townBlock.getType() == TownBlockType.WILDS) {
                    try {
                        if (TownyUniverse.getPermissionSource().unclaimedZoneAction(pos.getTownyWorld(), blockId, action))
                            return true;
                    } catch (NotRegisteredException e) {
                    }
                } else {
                    return true;
                }
            }
            cacheBlockErrMsg(player, String.format(TownySettings.getLangString("msg_cache_block_error_plot"), "allies", action.toString()));
            return false;
        } else {
            if (townBlock.getPermissions().getOutsiderPerm(action)) {
                if (townBlock.getType() == TownBlockType.WILDS) {
                    try {
                        if (TownyUniverse.getPermissionSource().unclaimedZoneAction(pos.getTownyWorld(), blockId, action))
                            return true;
                    } catch (NotRegisteredException e) {
                    }
                } else {
                    return true;
                }
            }
            cacheBlockErrMsg(player, String.format(TownySettings.getLangString("msg_cache_block_error_plot"), "outsiders", action.toString()));
            return false;
        }
    }
    // Town Permissions
    if (status == TownBlockStatus.TOWN_RESIDENT) {
        /*
			 * Check town overrides before testing town permissions
			 */
        if (targetTown.equals(playersTown) && (TownyUniverse.getPermissionSource().hasTownOwnedOverride(player, blockId, data, action))) {
            return true;
        } else if (!targetTown.equals(playersTown) && (TownyUniverse.getPermissionSource().hasAllTownOverride(player, blockId, data, action))) {
            return true;
        } else if (townBlock.getPermissions().getResidentPerm(action)) {
            if (townBlock.getType() == TownBlockType.WILDS) {
                try {
                    if (TownyUniverse.getPermissionSource().unclaimedZoneAction(pos.getTownyWorld(), blockId, action))
                        return true;
                } catch (NotRegisteredException e) {
                }
            } else {
                return true;
            }
        }
        cacheBlockErrMsg(player, String.format(TownySettings.getLangString("msg_cache_block_error_town_resident"), action.toString()));
        return false;
    } else if (status == TownBlockStatus.TOWN_ALLY) {
        /*
			 * Check town overrides before testing town permissions
			 */
        if (targetTown.equals(playersTown) && (TownyUniverse.getPermissionSource().hasOwnTownOverride(player, blockId, data, action))) {
            return true;
        } else if (!targetTown.equals(playersTown) && (TownyUniverse.getPermissionSource().hasAllTownOverride(player, blockId, data, action))) {
            return true;
        } else if (townBlock.getPermissions().getAllyPerm(action)) {
            if (townBlock.getType() == TownBlockType.WILDS) {
                try {
                    if (TownyUniverse.getPermissionSource().unclaimedZoneAction(pos.getTownyWorld(), blockId, action))
                        return true;
                } catch (NotRegisteredException e) {
                }
            } else {
                return true;
            }
        }
        cacheBlockErrMsg(player, String.format(TownySettings.getLangString("msg_cache_block_error_town_allies"), action.toString()));
        return false;
    } else if (status == TownBlockStatus.OUTSIDER || status == TownBlockStatus.ENEMY) {
        /*
			 * Check town overrides before testing town permissions
			 */
        if (TownyUniverse.getPermissionSource().hasAllTownOverride(player, blockId, data, action)) {
            return true;
        } else if (townBlock.getPermissions().getOutsiderPerm(action)) {
            if (townBlock.getType() == TownBlockType.WILDS) {
                try {
                    if (TownyUniverse.getPermissionSource().unclaimedZoneAction(pos.getTownyWorld(), blockId, action))
                        return true;
                } catch (NotRegisteredException e) {
                }
            } else {
                return true;
            }
        }
        cacheBlockErrMsg(player, String.format(TownySettings.getLangString("msg_cache_block_error_town_outsider"), action.toString()));
        return false;
    }
    TownyMessaging.sendErrorMsg(player, "Error updating " + action.toString() + " permission.");
    return false;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Aggregations

Town (com.palmergames.bukkit.towny.object.Town)41 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)24 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)23 Resident (com.palmergames.bukkit.towny.object.Resident)17 ArrayList (java.util.ArrayList)15 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)14 Nation (com.palmergames.bukkit.towny.object.Nation)10 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)8 IOException (java.io.IOException)8 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)7 InvalidNameException (javax.naming.InvalidNameException)5 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)4 KeyValueFile (com.palmergames.util.KeyValueFile)4 File (java.io.File)4 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)3 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)3 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3