Search in sources :

Example 1 with Teleport

use of com.earth2me.essentials.Teleport in project Essentials by drtshock.

the class Commandtpaccept method run.

@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    final User requester;
    try {
        requester = ess.getUser(user.getTeleportRequest());
    } catch (Exception ex) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (!requester.getBase().isOnline()) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (user.isTpRequestHere() && ((!requester.isAuthorized("essentials.tpahere") && !requester.isAuthorized("essentials.tpaall")) || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + user.getWorld().getName())))) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (!user.isTpRequestHere() && (!requester.isAuthorized("essentials.tpa") || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + requester.getWorld().getName())))) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (args.length > 0 && !requester.getName().contains(args[0])) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (!user.hasOutstandingTeleportRequest()) {
        user.requestTeleport(null, false);
        throw new Exception(tl("requestTimedOut"));
    }
    final Trade charge = new Trade(this.getName(), ess);
    user.sendMessage(tl("requestAccepted"));
    requester.sendMessage(tl("requestAcceptedFrom", user.getDisplayName()));
    try {
        if (user.isTpRequestHere()) {
            final Location loc = user.getTpRequestLocation();
            Teleport teleport = requester.getTeleport();
            teleport.setTpType(Teleport.TeleportType.TPA);
            teleport.teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND);
            requester.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
        } else {
            Teleport teleport = requester.getTeleport();
            teleport.setTpType(Teleport.TeleportType.TPA);
            teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND);
        }
    } catch (Exception ex) {
        user.sendMessage(tl("pendingTeleportCancelled"));
        ess.showError(requester.getSource(), ex, commandLabel);
    }
    user.requestTeleport(null, false);
    throw new NoChargeException();
}
Also used : Trade(com.earth2me.essentials.Trade) User(com.earth2me.essentials.User) Teleport(com.earth2me.essentials.Teleport) Location(org.bukkit.Location)

Example 2 with Teleport

use of com.earth2me.essentials.Teleport in project Towny by ElgarL.

the class TownCommand method townSpawn.

/**
 * Core spawn function to allow admin use.
 *
 * @param player
 * @param split
 * @param town
 * @param notAffordMSG
 * @param outpost
 */
public static void townSpawn(Player player, String[] split, Town town, String notAffordMSG, Boolean outpost) {
    try {
        boolean isTownyAdmin = TownyUniverse.getPermissionSource().isTownyAdmin(player);
        Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
        Location spawnLoc;
        TownSpawnLevel townSpawnPermission;
        if (outpost) {
            if (!town.hasOutpostSpawn())
                throw new TownyException(TownySettings.getLangString("msg_err_outpost_spawn"));
            Integer index;
            try {
                index = Integer.parseInt(split[split.length - 1]);
            } catch (NumberFormatException e) {
                // invalid entry so assume the first outpost
                index = 1;
            } catch (ArrayIndexOutOfBoundsException i) {
                // Number not present so assume the first outpost.
                index = 1;
            }
            spawnLoc = town.getOutpostSpawn(Math.max(1, index));
        } else
            spawnLoc = town.getSpawn();
        // Determine conditions
        if (isTownyAdmin) {
            townSpawnPermission = TownSpawnLevel.ADMIN;
        } else if ((split.length == 0) && (!outpost)) {
            townSpawnPermission = TownSpawnLevel.TOWN_RESIDENT;
        } else {
            // split.length > 1
            if (!resident.hasTown()) {
                townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
            } else if (resident.getTown() == town) {
                townSpawnPermission = outpost ? TownSpawnLevel.TOWN_RESIDENT_OUTPOST : TownSpawnLevel.TOWN_RESIDENT;
            } else if (resident.hasNation() && town.hasNation()) {
                Nation playerNation = resident.getTown().getNation();
                Nation targetNation = town.getNation();
                if (playerNation == targetNation) {
                    townSpawnPermission = TownSpawnLevel.PART_OF_NATION;
                } else if (targetNation.hasEnemy(playerNation)) {
                    // Prevent enemies from using spawn travel.
                    throw new TownyException(TownySettings.getLangString("msg_err_public_spawn_enemy"));
                } else if (targetNation.hasAlly(playerNation)) {
                    townSpawnPermission = TownSpawnLevel.NATION_ALLY;
                } else {
                    townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
                }
            } else {
                townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
            }
        }
        TownyMessaging.sendDebugMsg(townSpawnPermission.toString() + " " + townSpawnPermission.isAllowed());
        townSpawnPermission.checkIfAllowed(plugin, player);
        // Check the permissions
        if (!(isTownyAdmin || ((townSpawnPermission == TownSpawnLevel.UNAFFILIATED) ? town.isPublic() : townSpawnPermission.hasPermissionNode(plugin, player)))) {
            throw new TownyException(TownySettings.getLangString("msg_err_not_public"));
        }
        if (!isTownyAdmin) {
            // Prevent spawn travel while in disallowed zones (if
            // configured)
            List<String> disallowedZones = TownySettings.getDisallowedTownSpawnZones();
            if (!disallowedZones.isEmpty()) {
                String inTown = null;
                try {
                    Location loc = plugin.getCache(player).getLastLocation();
                    inTown = TownyUniverse.getTownName(loc);
                } catch (NullPointerException e) {
                    inTown = TownyUniverse.getTownName(player.getLocation());
                }
                if (inTown == null && disallowedZones.contains("unclaimed"))
                    throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "the Wilderness"));
                if (inTown != null && resident.hasNation() && TownyUniverse.getDataSource().getTown(inTown).hasNation()) {
                    Nation inNation = TownyUniverse.getDataSource().getTown(inTown).getNation();
                    Nation playerNation = resident.getTown().getNation();
                    if (inNation.hasEnemy(playerNation) && disallowedZones.contains("enemy"))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Enemy areas"));
                    if (!inNation.hasAlly(playerNation) && !inNation.hasEnemy(playerNation) && disallowedZones.contains("neutral"))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Neutral towns"));
                }
            }
        }
        double travelCost = townSpawnPermission.getCost();
        // Check if need/can pay
        if (travelCost > 0 && TownySettings.isUsingEconomy() && (resident.getHoldingBalance() < travelCost))
            throw new TownyException(notAffordMSG);
        // Used later to make sure the chunk we teleport to is loaded.
        Chunk chunk = spawnLoc.getChunk();
        // Essentials tests
        boolean UsingESS = plugin.isEssentials();
        if (UsingESS && !isTownyAdmin) {
            try {
                User user = plugin.getEssentials().getUser(player);
                if (!user.isJailed()) {
                    Teleport teleport = user.getTeleport();
                    if (!chunk.isLoaded())
                        chunk.load();
                    // Cause an essentials exception if in cooldown.
                    teleport.cooldown(true);
                    teleport.teleport(spawnLoc, null);
                }
            } catch (Exception e) {
                TownyMessaging.sendErrorMsg(player, "Error: " + e.getMessage());
                // cooldown?
                return;
            }
        }
        // travel.
        if (travelCost > 0 && TownySettings.isUsingEconomy() && resident.payTo(travelCost, town, String.format("Town Spawn (%s)", townSpawnPermission))) {
            // +
            TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_cost_spawn"), TownyEconomyHandler.getFormattedBalance(travelCost)));
        // TownyEconomyObject.getEconomyCurrency()));
        }
        // If an Admin or Essentials teleport isn't being used, use our own.
        if (isTownyAdmin) {
            if (player.getVehicle() != null)
                player.getVehicle().eject();
            if (!chunk.isLoaded())
                chunk.load();
            player.teleport(spawnLoc, TeleportCause.COMMAND);
            return;
        }
        if (!UsingESS) {
            if (TownyTimerHandler.isTeleportWarmupRunning()) {
                // Use teleport warmup
                player.sendMessage(String.format(TownySettings.getLangString("msg_town_spawn_warmup"), TownySettings.getTeleportWarmupTime()));
                plugin.getTownyUniverse().requestTeleport(player, spawnLoc, travelCost);
            } else {
                // Don't use teleport warmup
                if (player.getVehicle() != null)
                    player.getVehicle().eject();
                if (!chunk.isLoaded())
                    chunk.load();
                player.teleport(spawnLoc, TeleportCause.COMMAND);
            }
        }
    } catch (TownyException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    } catch (EconomyException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    }
}
Also used : User(com.earth2me.essentials.User) Chunk(org.bukkit.Chunk) Teleport(com.earth2me.essentials.Teleport) InvalidNameException(javax.naming.InvalidNameException) Location(org.bukkit.Location)

Example 3 with Teleport

use of com.earth2me.essentials.Teleport in project Essentials by EssentialsX.

the class Commandtpaccept method run.

@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    final User requester;
    try {
        requester = ess.getUser(user.getTeleportRequest());
    } catch (Exception ex) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (!requester.getBase().isOnline()) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (user.isTpRequestHere() && ((!requester.isAuthorized("essentials.tpahere") && !requester.isAuthorized("essentials.tpaall")) || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + user.getWorld().getName())))) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (!user.isTpRequestHere() && (!requester.isAuthorized("essentials.tpa") || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + requester.getWorld().getName())))) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (args.length > 0 && !requester.getName().contains(args[0])) {
        throw new Exception(tl("noPendingRequest"));
    }
    if (!user.hasOutstandingTeleportRequest()) {
        user.requestTeleport(null, false);
        throw new Exception(tl("requestTimedOut"));
    }
    final Trade charge = new Trade(this.getName(), ess);
    user.sendMessage(tl("requestAccepted"));
    requester.sendMessage(tl("requestAcceptedFrom", user.getDisplayName()));
    try {
        if (user.isTpRequestHere()) {
            final Location loc = user.getTpRequestLocation();
            Teleport teleport = requester.getTeleport();
            teleport.setTpType(Teleport.TeleportType.TPA);
            teleport.teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND);
            requester.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
        } else {
            Teleport teleport = requester.getTeleport();
            teleport.setTpType(Teleport.TeleportType.TPA);
            teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND);
        }
    } catch (Exception ex) {
        user.sendMessage(tl("pendingTeleportCancelled"));
        ess.showError(requester.getSource(), ex, commandLabel);
    }
    user.requestTeleport(null, false);
    throw new NoChargeException();
}
Also used : Trade(com.earth2me.essentials.Trade) User(com.earth2me.essentials.User) Teleport(com.earth2me.essentials.Teleport) Location(org.bukkit.Location)

Example 4 with Teleport

use of com.earth2me.essentials.Teleport in project Towny by ElgarL.

the class ResidentCommand method residentSpawn.

/**
 * Attempt to send player to bed spawn.
 *
 * @param player
 */
public void residentSpawn(Player player) {
    boolean isTownyAdmin = TownyUniverse.getPermissionSource().isTownyAdmin(player);
    Resident resident;
    try {
        resident = TownyUniverse.getDataSource().getResident(player.getName());
        Town town;
        Location spawnLoc;
        String notAffordMSG;
        TownSpawnLevel townSpawnPermission;
        // Set target town and affiliated messages.
        town = resident.getTown();
        notAffordMSG = TownySettings.getLangString("msg_err_cant_afford_tp");
        if (TownySettings.getBedUse() && player.getBedSpawnLocation() != null) {
            spawnLoc = player.getBedSpawnLocation();
        } else {
            spawnLoc = town.getSpawn();
        }
        if (isTownyAdmin) {
            townSpawnPermission = TownSpawnLevel.ADMIN;
        } else {
            townSpawnPermission = TownSpawnLevel.TOWN_RESIDENT;
        }
        if (!isTownyAdmin) {
            // Prevent spawn travel while in disallowed zones (if
            // configured)
            List<String> disallowedZones = TownySettings.getDisallowedTownSpawnZones();
            if (!disallowedZones.isEmpty()) {
                String inTown = null;
                try {
                    Location loc = plugin.getCache(player).getLastLocation();
                    inTown = TownyUniverse.getTownName(loc);
                } catch (NullPointerException e) {
                    inTown = TownyUniverse.getTownName(player.getLocation());
                }
                if (inTown == null && disallowedZones.contains("unclaimed"))
                    throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "the Wilderness"));
                if (inTown != null && resident.hasNation() && TownyUniverse.getDataSource().getTown(inTown).hasNation()) {
                    Nation inNation = TownyUniverse.getDataSource().getTown(inTown).getNation();
                    Nation playerNation = resident.getTown().getNation();
                    if (inNation.hasEnemy(playerNation) && disallowedZones.contains("enemy"))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Enemy areas"));
                    if (!inNation.hasAlly(playerNation) && !inNation.hasEnemy(playerNation) && disallowedZones.contains("neutral"))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Neutral towns"));
                }
            }
        }
        double travelCost = townSpawnPermission.getCost();
        // Check if need/can pay
        if (travelCost > 0 && TownySettings.isUsingEconomy() && (resident.getHoldingBalance() < travelCost))
            throw new TownyException(notAffordMSG);
        // Used later to make sure the chunk we teleport to is loaded.
        Chunk chunk = spawnLoc.getChunk();
        // Essentials tests
        boolean UsingESS = plugin.isEssentials();
        if (UsingESS && !isTownyAdmin) {
            try {
                User user = plugin.getEssentials().getUser(player);
                if (!user.isJailed()) {
                    Teleport teleport = user.getTeleport();
                    if (!chunk.isLoaded())
                        chunk.load();
                    // Cause an essentials exception if in cooldown.
                    teleport.cooldown(true);
                    teleport.teleport(spawnLoc, null);
                }
            } catch (Exception e) {
                TownyMessaging.sendErrorMsg(player, "Error: " + e.getMessage());
                // cooldown?
                return;
            }
        }
        // travel.
        if (travelCost > 0 && TownySettings.isUsingEconomy() && resident.payTo(travelCost, town, String.format("Resident Spawn (%s)", townSpawnPermission))) {
            // +
            TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_cost_spawn"), TownyEconomyHandler.getFormattedBalance(travelCost)));
        // TownyEconomyObject.getEconomyCurrency()));
        }
        // If an Admin or Essentials teleport isn't being used, use our own.
        if (isTownyAdmin) {
            if (player.getVehicle() != null)
                player.getVehicle().eject();
            if (!chunk.isLoaded())
                chunk.load();
            player.teleport(spawnLoc, TeleportCause.COMMAND);
            return;
        }
        if (!UsingESS) {
            if (TownyTimerHandler.isTeleportWarmupRunning()) {
                // Use teleport warmup
                player.sendMessage(String.format(TownySettings.getLangString("msg_town_spawn_warmup"), TownySettings.getTeleportWarmupTime()));
                plugin.getTownyUniverse().requestTeleport(player, spawnLoc, travelCost);
            } else {
                // Don't use teleport warmup
                if (player.getVehicle() != null)
                    player.getVehicle().eject();
                if (!chunk.isLoaded())
                    chunk.load();
                player.teleport(spawnLoc, TeleportCause.COMMAND);
            }
        }
    } catch (TownyException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    } catch (EconomyException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) TownSpawnLevel(com.palmergames.bukkit.towny.object.TownSpawnLevel) User(com.earth2me.essentials.User) Chunk(org.bukkit.Chunk) Teleport(com.earth2me.essentials.Teleport) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) Location(org.bukkit.Location)

Aggregations

Teleport (com.earth2me.essentials.Teleport)4 User (com.earth2me.essentials.User)4 Location (org.bukkit.Location)4 Trade (com.earth2me.essentials.Trade)2 Chunk (org.bukkit.Chunk)2 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)1 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)1 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)1 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)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 TownSpawnLevel (com.palmergames.bukkit.towny.object.TownSpawnLevel)1 InvalidNameException (javax.naming.InvalidNameException)1