Search in sources :

Example 1 with EmptyTownException

use of com.palmergames.bukkit.towny.exceptions.EmptyTownException 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 2 with EmptyTownException

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

the class TownyAdminCommand method adminSet.

public void adminSet(String[] split) throws TownyException {
    if (split.length == 0) {
        sender.sendMessage(ChatTools.formatTitle("/townyadmin set"));
        // TODO: player.sendMessage(ChatTools.formatCommand("",
        // "/townyadmin set", "king [nation] [king]", ""));
        sender.sendMessage(ChatTools.formatCommand("", "/townyadmin set", "mayor [town] " + TownySettings.getLangString("town_help_2"), ""));
        sender.sendMessage(ChatTools.formatCommand("", "/townyadmin set", "mayor [town] npc", ""));
        return;
    }
    if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_SET.getNode(split[0].toLowerCase())))
        throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
    if (split[0].equalsIgnoreCase("mayor")) {
        if (split.length < 3) {
            sender.sendMessage(ChatTools.formatTitle("/townyadmin set mayor"));
            sender.sendMessage(ChatTools.formatCommand("Eg", "/townyadmin set mayor", "[town] " + TownySettings.getLangString("town_help_2"), ""));
            sender.sendMessage(ChatTools.formatCommand("Eg", "/townyadmin set mayor", "[town] npc", ""));
        } else
            try {
                Resident newMayor = null;
                Town town = TownyUniverse.getDataSource().getTown(split[1]);
                if (split[2].equalsIgnoreCase("npc")) {
                    String name = nextNpcName();
                    TownyUniverse.getDataSource().newResident(name);
                    newMayor = TownyUniverse.getDataSource().getResident(name);
                    newMayor.setRegistered(System.currentTimeMillis());
                    newMayor.setLastOnline(0);
                    newMayor.setNPC(true);
                    TownyUniverse.getDataSource().saveResident(newMayor);
                    TownyUniverse.getDataSource().saveResidentList();
                    // set for no upkeep as an NPC mayor is assigned
                    town.setHasUpkeep(false);
                } else {
                    newMayor = TownyUniverse.getDataSource().getResident(split[2]);
                    // set upkeep again
                    town.setHasUpkeep(true);
                }
                if (!town.hasResident(newMayor))
                    TownCommand.townAddResident(town, newMayor);
                // Delete the resident if the old mayor was an NPC.
                Resident oldMayor = town.getMayor();
                town.setMayor(newMayor);
                if (oldMayor.isNPC()) {
                    try {
                        town.removeResident(oldMayor);
                        TownyUniverse.getDataSource().removeResident(oldMayor);
                        TownyUniverse.getDataSource().removeResidentList(oldMayor);
                    } catch (EmptyTownException e) {
                        // Should never reach here as we are setting a new
                        // mayor before removing the old one.
                        e.printStackTrace();
                    }
                }
                TownyUniverse.getDataSource().saveTown(town);
                String[] msg = TownySettings.getNewMayorMsg(newMayor.getName());
                TownyMessaging.sendTownMessage(town, msg);
            // TownyMessaging.sendMessage(player, msg);
            } catch (TownyException e) {
                TownyMessaging.sendErrorMsg(getSender(), e.getMessage());
            }
    } else if (split[0].equalsIgnoreCase("capital")) {
        if (split.length < 2) {
            sender.sendMessage(ChatTools.formatTitle("/townyadmin set capital"));
            sender.sendMessage(ChatTools.formatCommand("Eg", "/ta set capital", "[town name]", ""));
        } else {
            try {
                Town newCapital = TownyUniverse.getDataSource().getTown(split[1]);
                Nation nation = newCapital.getNation();
                nation.setCapital(newCapital);
                plugin.resetCache();
                TownyMessaging.sendNationMessage(nation, TownySettings.getNewKingMsg(newCapital.getMayor().getName(), nation.getName()));
                TownyUniverse.getDataSource().saveNation(nation);
                TownyUniverse.getDataSource().saveNationList();
            } catch (TownyException e) {
                TownyMessaging.sendErrorMsg(player, e.getMessage());
            }
        }
    } else {
        TownyMessaging.sendErrorMsg(getSender(), String.format(TownySettings.getLangString("msg_err_invalid_property"), "administrative"));
        return;
    }
}
Also used : EmptyTownException(com.palmergames.bukkit.towny.exceptions.EmptyTownException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

EmptyTownException (com.palmergames.bukkit.towny.exceptions.EmptyTownException)2 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)2 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)1 Resident (com.palmergames.bukkit.towny.object.Resident)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1 ArrayList (java.util.ArrayList)1