Search in sources :

Example 11 with Resident

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

the class ResidentPurge method run.

@Override
public void run() {
    int count = 0;
    message("Scanning for old residents...");
    for (Resident resident : new ArrayList<Resident>(TownyUniverse.getDataSource().getResidents())) {
        if (!resident.isNPC() && (System.currentTimeMillis() - resident.getLastOnline() > (this.deleteTime)) && !BukkitTools.isOnline(resident.getName())) {
            count++;
            message("Deleting resident: " + resident.getName());
            TownyUniverse.getDataSource().removeResident(resident);
            TownyUniverse.getDataSource().removeResidentList(resident);
        }
    }
    message("Resident purge complete: " + count + " deleted.");
}
Also used : ArrayList(java.util.ArrayList) Resident(com.palmergames.bukkit.towny.object.Resident)

Example 12 with Resident

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

the class AreaSelectionUtil method selectWorldCoordAreaCircle.

public static List<WorldCoord> selectWorldCoordAreaCircle(TownBlockOwner owner, WorldCoord pos, String[] args) throws TownyException {
    List<WorldCoord> out = new ArrayList<WorldCoord>();
    if (pos.getTownyWorld().isClaimable()) {
        if (args.length > 0) {
            int r = 0, available = 0;
            if (owner instanceof Town) {
                Town town = (Town) owner;
                available = TownySettings.getMaxTownBlocks(town) - town.getTownBlocks().size();
            } else if (owner instanceof Resident) {
                available = TownySettings.getMaxResidentPlots((Resident) owner);
            }
            if (args[0].equalsIgnoreCase("auto")) {
                if (// Since: 0 - ceil(Pi * 0^2) >= 0 is a true statement.
                available > 0)
                    while (available - Math.ceil(Math.PI * r * r) >= 0) r += 1;
            } else {
                try {
                    r = Integer.parseInt(args[0]);
                } catch (NumberFormatException e) {
                    throw new TownyException(TownySettings.getLangString("msg_err_invalid_radius"));
                }
            }
            if (r > 1000)
                r = 1000;
            for (int z = -r; z <= r; z++) for (int x = -r; x <= r; x++) if ((x * x + z * z <= r * r) && (out.size() < available))
                out.add(new WorldCoord(pos.getWorldName(), pos.getX() + x, pos.getZ() + z));
        } else {
            throw new TownyException(TownySettings.getLangString("msg_err_invalid_radius"));
        }
    }
    return out;
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 13 with Resident

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

the class AreaSelectionUtil method selectWorldCoordAreaRect.

public static List<WorldCoord> selectWorldCoordAreaRect(TownBlockOwner owner, WorldCoord pos, String[] args) throws TownyException {
    List<WorldCoord> out = new ArrayList<WorldCoord>();
    if (pos.getTownyWorld().isClaimable()) {
        if (args.length > 0) {
            int r = 0, available = 1000;
            if (owner instanceof Town) {
                Town town = (Town) owner;
                available = TownySettings.getMaxTownBlocks(town) - town.getTownBlocks().size();
            } else if (owner instanceof Resident) {
                available = TownySettings.getMaxResidentPlots((Resident) owner);
            }
            if (args[0].equalsIgnoreCase("auto")) {
                while (available - Math.pow((r + 1) * 2 - 1, 2) >= 0) r += 1;
            } else {
                try {
                    r = Integer.parseInt(args[0]);
                } catch (NumberFormatException e) {
                    throw new TownyException(TownySettings.getLangString("msg_err_invalid_radius"));
                }
            }
            if (r > 1000)
                r = 1000;
            for (int z = -r; z <= r; z++) for (int x = -r; x <= r; x++) if (out.size() < available)
                out.add(new WorldCoord(pos.getWorldName(), pos.getX() + x, pos.getZ() + z));
        } else {
            throw new TownyException(TownySettings.getLangString("msg_err_invalid_radius"));
        }
    }
    return out;
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 14 with Resident

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

the class DailyTimerTask method collectTownTaxes.

/**
	 * Collect taxes due to the town from it's residents.
	 * 
	 * @param nation
	 * @throws EconomyException
	 */
protected void collectTownTaxes(Town town) throws EconomyException {
    // Resident Tax
    if (town.getTaxes() > 0) {
        List<Resident> residents = new ArrayList<Resident>(town.getResidents());
        ListIterator<Resident> residentItr = residents.listIterator();
        Resident resident = null;
        while (residentItr.hasNext()) {
            resident = residentItr.next();
            /*
				 * Only collect resident tax from this resident if it really
				 * still exists. We are running in an Async thread so MUST
				 * verify all objects.
				 */
            if (TownyUniverse.getDataSource().hasResident(resident.getName())) {
                if (TownyPerms.getResidentPerms(resident).containsKey("towny.tax_exempt") || resident.isNPC()) {
                    try {
                        TownyMessaging.sendResidentMessage(resident, TownySettings.getTaxExemptMsg());
                    } catch (TownyException e) {
                    // Player is not online
                    }
                    continue;
                } else if (town.isTaxPercentage()) {
                    double cost = resident.getHoldingBalance() * town.getTaxes() / 100;
                    resident.payTo(cost, town, "Town Tax (Percentage)");
                /*
						 * Don't send individual message anymore to ease up on
						 * the lag. try {
						 * TownyMessaging.sendResidentMessage(resident,
						 * TownySettings.getPayedResidentTaxMsg() + cost); }
						 * catch (TownyException e) { // Player is not online }
						 */
                } else if (!resident.payTo(town.getTaxes(), town, "Town Tax")) {
                    TownyMessaging.sendTownMessage(town, TownySettings.getCouldntPayTaxesMsg(resident, "town"));
                    try {
                        // reset this resident and remove him from the town.
                        resident.clear();
                        TownyUniverse.getDataSource().saveTown(town);
                    } catch (EmptyTownException e) {
                        // No mayor so remove the town.
                        TownyUniverse.getDataSource().removeTown(town);
                    }
                    TownyUniverse.getDataSource().saveResident(resident);
                }
            // else
            /*
					 * Don't send individual message anymore to ease up on the
					 * lag. try { TownyMessaging.sendResidentMessage(resident,
					 * TownySettings.getPayedResidentTaxMsg() +
					 * town.getTaxes()); } catch (TownyException e) { // Player
					 * is not online }
					 */
            }
        }
    }
    // Plot Tax
    if (town.getPlotTax() > 0 || town.getCommercialPlotTax() > 0 || town.getEmbassyPlotTax() > 0) {
        // Hashtable<Resident, Integer> townPlots = new Hashtable<Resident,
        // Integer>();
        // Hashtable<Resident, Double> townTaxes = new Hashtable<Resident,
        // Double>();
        List<TownBlock> townBlocks = new ArrayList<TownBlock>(town.getTownBlocks());
        ListIterator<TownBlock> townBlockItr = townBlocks.listIterator();
        TownBlock townBlock = null;
        while (townBlockItr.hasNext()) {
            townBlock = townBlockItr.next();
            if (!townBlock.hasResident())
                continue;
            try {
                Resident resident = townBlock.getResident();
                /*
					 * Only collect plot tax from this resident if it really
					 * still exists. We are running in an Async thread so MUST
					 * verify all objects.
					 */
                if (TownyUniverse.getDataSource().hasResident(resident.getName())) {
                    if (TownyPerms.getResidentPerms(resident).containsKey("towny.tax_exempt") || resident.isNPC()) {
                        continue;
                    }
                    if (!resident.payTo(townBlock.getType().getTax(town), town, String.format("Plot Tax (%s)", townBlock.getType()))) {
                        TownyMessaging.sendTownMessage(town, String.format(TownySettings.getLangString("msg_couldnt_pay_plot_taxes"), resident));
                        townBlock.setResident(null);
                        townBlock.setPlotPrice(-1);
                        // Set the plot permissions to mirror the towns.
                        townBlock.setType(townBlock.getType());
                        TownyUniverse.getDataSource().saveResident(resident);
                        TownyUniverse.getDataSource().saveTownBlock(townBlock);
                    }
                // else {
                // townPlots.put(resident,
                // (townPlots.containsKey(resident) ?
                // townPlots.get(resident) : 0) + 1);
                // townTaxes.put(resident,
                // (townTaxes.containsKey(resident) ?
                // townTaxes.get(resident) : 0) +
                // townBlock.getType().getTax(town));
                // }
                }
            } catch (NotRegisteredException e) {
            }
        }
    /*
			 * Don't send individual message anymore to ease up on the lag. for
			 * (Resident resident : townPlots.keySet()) { try { int numPlots =
			 * townPlots.get(resident); double totalCost =
			 * townTaxes.get(resident);
			 * TownyMessaging.sendResidentMessage(resident,
			 * String.format(TownySettings.getLangString("msg_payed_plot_cost"),
			 * totalCost, numPlots, town.getName())); } catch (TownyException e)
			 * { // Player is not online } }
			 */
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) ArrayList(java.util.ArrayList) Resident(com.palmergames.bukkit.towny.object.Resident) EmptyTownException(com.palmergames.bukkit.towny.exceptions.EmptyTownException) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 15 with Resident

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

the class TownyPlayerListener method onPlayerBedEnter.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
    if (!TownySettings.getBedUse())
        return;
    boolean isOwner = false;
    boolean isInnPlot = false;
    try {
        Resident resident = TownyUniverse.getDataSource().getResident(event.getPlayer().getName());
        WorldCoord worldCoord = new WorldCoord(event.getPlayer().getWorld().getName(), Coord.parseCoord(event.getBed().getLocation()));
        TownBlock townblock = worldCoord.getTownBlock();
        isOwner = townblock.isOwner(resident);
        isInnPlot = townblock.getType() == TownBlockType.INN;
        if (resident.hasNation() && townblock.getTown().hasNation()) {
            Nation residentNation = resident.getTown().getNation();
            Nation townblockNation = townblock.getTown().getNation();
            if (townblockNation.hasEnemy(residentNation)) {
                event.setCancelled(true);
                TownyMessaging.sendErrorMsg(event.getPlayer(), "You cannot sleep in an enemy's Inn.");
                return;
            }
        }
    } catch (NotRegisteredException e) {
    // Wilderness as it error'd getting a townblock.
    }
    if (!isOwner && !isInnPlot) {
        event.setCancelled(true);
        TownyMessaging.sendErrorMsg(event.getPlayer(), "You do not own the land this bed occupies and it is not an Inn plot.");
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) EventHandler(org.bukkit.event.EventHandler)

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