Search in sources :

Example 1 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project TownyCultures by TownyAdvanced.

the class CultureAdminCommand method getTownsStartingWith.

/**
 * Returns a List<String> containing strings of town names that match with arg.
 *
 * @return Matches
 */
static List<String> getTownsStartingWith(String arg) {
    List<String> matches = new ArrayList<>();
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    matches.addAll(townyUniverse.getTownsTrie().getStringsFromKey(arg));
    return matches;
}
Also used : ArrayList(java.util.ArrayList) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse)

Example 2 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project Towny by TownyAdvanced.

the class TownyPlayerListener method onPlayerMove.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent event) {
    // Let's ignore Citizens NPCs
    if (BukkitTools.checkCitizens(event.getPlayer()))
        return;
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    /*
		 * Abort if we havn't really moved
		 */
    if (event.getFrom().getBlockX() == event.getTo().getBlockX() && event.getFrom().getBlockZ() == event.getTo().getBlockZ() && event.getFrom().getBlockY() == event.getTo().getBlockY()) {
        return;
    }
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    Player player = event.getPlayer();
    Location to = event.getTo();
    Location from;
    PlayerCache cache = plugin.getCache(player);
    Resident resident = townyUniverse.getResident(player.getUniqueId());
    if (resident != null && TownyTimerHandler.isTeleportWarmupRunning() && TownySettings.getTeleportWarmupTime() > 0 && TownySettings.isMovementCancellingSpawnWarmup() && resident.getTeleportRequestTime() > 0 && !townyUniverse.getPermissionSource().isTownyAdmin(player)) {
        TeleportWarmupTimerTask.abortTeleportRequest(resident);
        TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_teleport_cancelled"));
    }
    try {
        from = cache.getLastLocation();
    } catch (NullPointerException e) {
        from = event.getFrom();
    }
    if (WorldCoord.cellChanged(from, to)) {
        TownyWorld fromWorld = TownyAPI.getInstance().getTownyWorld(from.getWorld().getName());
        TownyWorld toWorld = TownyAPI.getInstance().getTownyWorld(to.getWorld().getName());
        if (fromWorld == null || toWorld == null) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("not_registered"));
            cache.setLastLocation(to);
            return;
        }
        WorldCoord fromCoord = new WorldCoord(fromWorld.getName(), Coord.parseCoord(from));
        WorldCoord toCoord = new WorldCoord(toWorld.getName(), Coord.parseCoord(to));
        onPlayerMoveChunk(player, fromCoord, toCoord, from, to, event);
    }
    // Update the cached players current location
    cache.setLastLocation(to);
}
Also used : Player(org.bukkit.entity.Player) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Resident(com.palmergames.bukkit.towny.object.Resident) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 3 with TownyUniverse

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

Example 4 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project Towny by TownyAdvanced.

the class KingCheck method runCheck.

@Override
public boolean runCheck(Player player, String checkString) {
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    Resident resident = townyUniverse.getResident(player.getUniqueId());
    if (resident != null && resident.hasNation())
        return resident.getTownOrNull().getNationOrNull().isKing(resident);
    return false;
}
Also used : Resident(com.palmergames.bukkit.towny.object.Resident) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse)

Example 5 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project Towny by TownyAdvanced.

the class AllyType method getRecipients.

@Override
public Collection<Player> getRecipients(Collection<Player> recipients, Player player) {
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    Resident res = townyUniverse.getResident(player.getUniqueId());
    if (res == null)
        return recipients;
    try {
        final Nation nation = res.getTown().getNation();
        Collection<Player> newRecipients = new HashSet<>();
        for (Player p : recipients) {
            Resident playerRes = townyUniverse.getResident(p.getUniqueId());
            if (playerRes != null && playerRes.hasNation()) {
                Nation playerNation = playerRes.getTown().getNation();
                if (playerNation.getUUID().equals(nation.getUUID()) || playerNation.hasAlly(nation))
                    newRecipients.add(p);
            }
        }
        return newRecipients;
    } catch (NotRegisteredException ignore) {
    }
    return recipients;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse) HashSet(java.util.HashSet)

Aggregations

TownyUniverse (com.palmergames.bukkit.towny.TownyUniverse)27 Resident (com.palmergames.bukkit.towny.object.Resident)19 ArrayList (java.util.ArrayList)8 Player (org.bukkit.entity.Player)8 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)7 Town (com.palmergames.bukkit.towny.object.Town)6 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)4 EventHandler (org.bukkit.event.EventHandler)4 Nation (com.palmergames.bukkit.towny.object.Nation)3 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)3 HashSet (java.util.HashSet)3 Invite (com.palmergames.bukkit.towny.invites.Invite)2 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)2 Translator (com.palmergames.bukkit.towny.object.Translator)2 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)2 InvalidObjectException (java.io.InvalidObjectException)2 List (java.util.List)2 UUID (java.util.UUID)2 SiegeSide (com.gmail.goosius.siegewar.enums.SiegeSide)1 Siege (com.gmail.goosius.siegewar.objects.Siege)1