Search in sources :

Example 46 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class ResidentCommand method residentSet.

/**
	 * 
	 * Command: /resident set [] ... []
	 * 
	 * @param player
	 * @param split
	 * @throws TownyException
	 */
/*
	 * perm [resident/outsider] [build/destroy] [on/off]
	 */
public void residentSet(Player player, String[] split) throws TownyException {
    if (split.length == 0) {
        player.sendMessage(ChatTools.formatCommand("", "/resident set", "perm ...", "'/resident set perm' " + TownySettings.getLangString("res_5")));
        player.sendMessage(ChatTools.formatCommand("", "/resident set", "mode ...", "'/resident set mode' " + TownySettings.getLangString("res_5")));
    } else {
        Resident resident;
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
            return;
        }
        if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_SET.getNode(split[0].toLowerCase())))
            throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
        if (split[0].equalsIgnoreCase("perm")) {
            String[] newSplit = StringMgmt.remFirstArg(split);
            TownCommand.setTownBlockPermissions(player, resident, resident.getPermissions(), newSplit, true);
        } else if (split[0].equalsIgnoreCase("mode")) {
            String[] newSplit = StringMgmt.remFirstArg(split);
            setMode(player, newSplit);
        } else {
            TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_invalid_property"), "town"));
            return;
        }
        TownyUniverse.getDataSource().saveResident(resident);
    }
}
Also used : Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 47 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class ResidentCommand method residentFriend.

public void residentFriend(Player player, String[] split) {
    if (split.length == 0) {
        player.sendMessage(ChatTools.formatCommand("", "/resident friend", "add " + TownySettings.getLangString("res_2"), ""));
        player.sendMessage(ChatTools.formatCommand("", "/resident friend", "remove " + TownySettings.getLangString("res_2"), ""));
        player.sendMessage(ChatTools.formatCommand("", "/resident friend", "clear", ""));
    } else {
        Resident resident;
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
            return;
        }
        // TODO: Let admin's call a subfunction of this.
        if (split[0].equalsIgnoreCase("add")) {
            String[] names = StringMgmt.remFirstArg(split);
            residentFriendAdd(player, resident, TownyUniverse.getDataSource().getResidents(player, names));
        } else if (split[0].equalsIgnoreCase("remove")) {
            String[] names = StringMgmt.remFirstArg(split);
            residentFriendRemove(player, resident, TownyUniverse.getDataSource().getResidents(player, names));
        } else if (split[0].equalsIgnoreCase("clearlist") || split[0].equalsIgnoreCase("clear")) {
            residentFriendRemove(player, resident, resident.getFriends());
        }
    }
}
Also used : Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 48 with TownyException

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

Example 49 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class ResidentCommand method parseResidentCommand.

public void parseResidentCommand(Player player, String[] split) {
    try {
        if (split.length == 0) {
            try {
                Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
                TownyMessaging.sendMessage(player, TownyFormatter.getStatus(resident, player));
            } catch (NotRegisteredException x) {
                throw new TownyException(TownySettings.getLangString("msg_err_not_registered"));
            }
        } else if (split[0].equalsIgnoreCase("?") || split[0].equalsIgnoreCase("help")) {
            for (String line : output) player.sendMessage(line);
        } else if (split[0].equalsIgnoreCase("list")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_LIST.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            listResidents(player);
        } else if (split[0].equalsIgnoreCase("tax")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_TAX.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            try {
                Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
                TownyMessaging.sendMessage(player, TownyFormatter.getTaxStatus(resident));
            } catch (NotRegisteredException x) {
                throw new TownyException(TownySettings.getLangString("msg_err_not_registered"));
            }
        } else if (split[0].equalsIgnoreCase("set")) {
            /*
				 * perms checked in method.
				 */
            String[] newSplit = StringMgmt.remFirstArg(split);
            residentSet(player, newSplit);
        } else if (split[0].equalsIgnoreCase("toggle")) {
            /*
				 * 
				 */
            String[] newSplit = StringMgmt.remFirstArg(split);
            residentToggle(player, newSplit);
        } else if (split[0].equalsIgnoreCase("friend")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_FRIEND.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            String[] newSplit = StringMgmt.remFirstArg(split);
            residentFriend(player, newSplit);
        } else if (split[0].equalsIgnoreCase("spawn")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_SPAWN.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            residentSpawn(player);
        } else {
            try {
                Resident resident = TownyUniverse.getDataSource().getResident(split[0]);
                TownyMessaging.sendMessage(player, TownyFormatter.getStatus(resident, player));
            } catch (NotRegisteredException x) {
                throw new TownyException(String.format(TownySettings.getLangString("msg_err_not_registered_1"), split[0]));
            }
        }
    } catch (Exception x) {
        TownyMessaging.sendErrorMsg(player, x.getMessage());
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) 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)

Example 50 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class TownyAdminCommand method parseAdminResidentCommand.

public void parseAdminResidentCommand(String[] split) throws TownyException {
    if (split.length == 0 || split[0].equalsIgnoreCase("?")) {
        sender.sendMessage(ChatTools.formatTitle("/townyadmin resident"));
        sender.sendMessage(ChatTools.formatCommand(TownySettings.getLangString("admin_sing"), "/townyadmin resident", "[resident]", ""));
        sender.sendMessage(ChatTools.formatCommand(TownySettings.getLangString("admin_sing"), "/townyadmin resident", "[resident] rename [newname]", ""));
        return;
    }
    try {
        Resident resident = TownyUniverse.getDataSource().getResident(split[0]);
        if (split.length == 1) {
            TownyMessaging.sendMessage(getSender(), TownyFormatter.getStatus(resident, player));
            return;
        }
        if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_RESIDENT.getNode(split[1].toLowerCase())))
            throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
        if (split[1].equalsIgnoreCase("rename")) {
            if (!NameValidation.isBlacklistName(split[2])) {
                TownyUniverse.getDataSource().renamePlayer(resident, split[2]);
            } else
                TownyMessaging.sendErrorMsg(getSender(), TownySettings.getLangString("msg_invalid_name"));
        }
    } catch (NotRegisteredException e) {
        TownyMessaging.sendErrorMsg(getSender(), e.getMessage());
    } catch (TownyException e) {
        TownyMessaging.sendErrorMsg(getSender(), e.getMessage());
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)70 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)37 Resident (com.palmergames.bukkit.towny.object.Resident)29 Town (com.palmergames.bukkit.towny.object.Town)17 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)16 Nation (com.palmergames.bukkit.towny.object.Nation)16 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)15 ArrayList (java.util.ArrayList)12 Player (org.bukkit.entity.Player)12 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)11 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)11 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)7 Location (org.bukkit.Location)7 IOException (java.io.IOException)6 EventHandler (org.bukkit.event.EventHandler)6 Coord (com.palmergames.bukkit.towny.object.Coord)5 InvalidNameException (javax.naming.InvalidNameException)5 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)4 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)3 EmptyTownException (com.palmergames.bukkit.towny.exceptions.EmptyTownException)3