Search in sources :

Example 41 with Resident

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

the class PlotCommand method onTabComplete.

/**
	 * Overridden method custom for this command set.
	 * 
	 */
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
    LinkedList<String> output = new LinkedList<String>();
    String lastArg = "";
    // Get the last argument
    if (args.length > 0) {
        lastArg = args[args.length - 1].toLowerCase();
    }
    if (!lastArg.equalsIgnoreCase("")) {
        // Match residents
        for (Resident resident : TownyUniverse.getDataSource().getResidents()) {
            if (resident.getName().toLowerCase().startsWith(lastArg)) {
                output.add(resident.getName());
            }
        }
    }
    return output;
}
Also used : Resident(com.palmergames.bukkit.towny.object.Resident) LinkedList(java.util.LinkedList)

Example 42 with Resident

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

the class PlotCommand method plotTestOwner.

/**
	 * Test the townBlock to ensure we are either the plot owner, or the
	 * mayor/assistant
	 * 
	 * @param resident
	 * @param townBlock
	 * @throws TownyException
	 */
public TownBlockOwner plotTestOwner(Resident resident, TownBlock townBlock) throws TownyException {
    Player player = BukkitTools.getPlayer(resident.getName());
    boolean isAdmin = TownyUniverse.getPermissionSource().isTownyAdmin(player);
    if (townBlock.hasResident()) {
        Resident owner = townBlock.getResident();
        boolean isSameTown = (resident.hasTown()) ? resident.getTown() == owner.getTown() : false;
        if ((resident == owner) || ((isSameTown) && (player.hasPermission(PermissionNodes.TOWNY_COMMAND_PLOT_ASMAYOR.getNode()))) || isAdmin) {
            return owner;
        }
        // Not the plot owner or the towns mayor or an admin.
        throw new TownyException(TownySettings.getLangString("msg_area_not_own"));
    } else {
        Town owner = townBlock.getTown();
        boolean isSameTown = (resident.hasTown()) ? resident.getTown() == owner : false;
        if (isSameTown && !BukkitTools.getPlayer(resident.getName()).hasPermission(PermissionNodes.TOWNY_COMMAND_PLOT_ASMAYOR.getNode()))
            throw new TownyException(TownySettings.getLangString("msg_not_mayor_ass"));
        if (!isSameTown && !isAdmin)
            throw new TownyException(TownySettings.getLangString("msg_err_not_part_town"));
        return owner;
    }
}
Also used : Player(org.bukkit.entity.Player) Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 43 with Resident

use of com.palmergames.bukkit.towny.object.Resident 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 44 with Resident

use of com.palmergames.bukkit.towny.object.Resident 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 45 with Resident

use of com.palmergames.bukkit.towny.object.Resident 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

Resident (com.palmergames.bukkit.towny.object.Resident)54 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)35 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)32 ArrayList (java.util.ArrayList)18 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)17 Town (com.palmergames.bukkit.towny.object.Town)17 Nation (com.palmergames.bukkit.towny.object.Nation)16 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)11 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)9 IOException (java.io.IOException)8 InvalidNameException (javax.naming.InvalidNameException)7 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)5 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)5 KeyValueFile (com.palmergames.util.KeyValueFile)5 File (java.io.File)5 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)4 LinkedList (java.util.LinkedList)4 Player (org.bukkit.entity.Player)4 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3