Search in sources :

Example 11 with Town

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

the class TownyFlatFileSource method loadNation.

@Override
public boolean loadNation(Nation nation) {
    String line = "";
    String[] tokens;
    String path = getNationFilename(nation);
    File fileResident = new File(path);
    if (fileResident.exists() && fileResident.isFile()) {
        try {
            KeyValueFile kvFile = new KeyValueFile(path);
            line = kvFile.get("towns");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        TownyMessaging.sendDebugMsg("Nation Fetching Town: " + token);
                        Town town = getTown(token);
                        if (town != null)
                            nation.addTown(town);
                    }
                }
            }
            line = kvFile.get("capital");
            if (line != null)
                nation.setCapital(getTown(line));
            //				line = kvFile.get("assistants");
            //				if (line != null) {
            //					tokens = line.split(",");
            //					for (String token : tokens) {
            //						if (!token.isEmpty()) {
            //							Resident assistant = getResident(token);
            //							if (assistant != null)
            //								nation.addAssistant(assistant);
            //						}
            //					}
            //				}
            line = kvFile.get("tag");
            if (line != null)
                try {
                    nation.setTag(line);
                } catch (TownyException e) {
                    nation.setTag("");
                }
            line = kvFile.get("allies");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Nation friend = getNation(token);
                        if (friend != null)
                            //("ally", friend);
                            nation.addAlly(friend);
                    }
                }
            }
            line = kvFile.get("enemies");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Nation enemy = getNation(token);
                        if (enemy != null)
                            //("enemy", enemy);
                            nation.addEnemy(enemy);
                    }
                }
            }
            line = kvFile.get("taxes");
            if (line != null)
                try {
                    nation.setTaxes(Double.parseDouble(line));
                } catch (Exception e) {
                    nation.setTaxes(0.0);
                }
            line = kvFile.get("neutral");
            if (line != null)
                try {
                    nation.setNeutral(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
        } catch (Exception e) {
            TownyMessaging.sendErrorMsg("Loading Error: Exception while reading nation file " + nation.getName());
            return false;
        }
        return true;
    } else
        return false;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Town(com.palmergames.bukkit.towny.object.Town) KeyValueFile(com.palmergames.util.KeyValueFile) File(java.io.File) KeyValueFile(com.palmergames.util.KeyValueFile) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) IOException(java.io.IOException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) InvalidNameException(javax.naming.InvalidNameException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 12 with Town

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

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

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

the class DailyTimerTask method collectTownCosts.

/**
	 * Collect or pay upkeep for all towns.
	 * 
	 * @throws EconomyException
	 * @throws TownyException
	 */
public void collectTownCosts() throws EconomyException, TownyException {
    List<Town> towns = new ArrayList<Town>(TownyUniverse.getDataSource().getTowns());
    ListIterator<Town> townItr = towns.listIterator();
    Town town = null;
    while (townItr.hasNext()) {
        town = townItr.next();
        /*
			 * Only charge/pay upkeep for this town if it really still exists.
			 * We are running in an Async thread so MUST verify all objects.
			 */
        if (TownyUniverse.getDataSource().hasTown(town.getName())) {
            if (town.hasUpkeep()) {
                double upkeep = TownySettings.getTownUpkeepCost(town);
                if (upkeep > 0) {
                    // Town is paying upkeep
                    if (!town.pay(upkeep, "Town Upkeep")) {
                        TownyUniverse.getDataSource().removeTown(town);
                        TownyMessaging.sendGlobalMessage(town.getName() + TownySettings.getLangString("msg_bankrupt_town"));
                    }
                } else if (upkeep < 0) {
                    // Negative upkeep
                    if (TownySettings.isUpkeepPayingPlots()) {
                        // Pay each plot owner a share of the negative
                        // upkeep
                        List<TownBlock> plots = new ArrayList<TownBlock>(town.getTownBlocks());
                        for (TownBlock townBlock : plots) {
                            if (townBlock.hasResident())
                                townBlock.getResident().pay((upkeep / plots.size()), "Negative Town Upkeep - Plot income");
                            else
                                town.pay((upkeep / plots.size()), "Negative Town Upkeep - Plot income");
                        }
                    } else {
                        // Not paying plot owners so just pay the town
                        town.pay(upkeep, "Negative Town Upkeep");
                    }
                }
            }
        }
    }
    universe.setChangedNotify(UPKEEP_TOWN);
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Example 15 with Town

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

the class DailyTimerTask method collectTownTaxes.

/**
	 * Collect taxes for all towns due from their residents.
	 * 
	 * @throws EconomyException
	 */
public void collectTownTaxes() throws EconomyException {
    List<Town> towns = new ArrayList<Town>(TownyUniverse.getDataSource().getTowns());
    ListIterator<Town> townItr = towns.listIterator();
    Town town = null;
    while (townItr.hasNext()) {
        town = townItr.next();
        /*
			 * Only collect resident tax for this town if it really still
			 * exists.
			 * We are running in an Async thread so MUST verify all objects.
			 */
        if (TownyUniverse.getDataSource().hasTown(town.getName()))
            collectTownTaxes(town);
    }
    universe.setChangedNotify(COLLECTED_TONW_TAX);
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList)

Aggregations

Town (com.palmergames.bukkit.towny.object.Town)41 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)24 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)23 Resident (com.palmergames.bukkit.towny.object.Resident)17 ArrayList (java.util.ArrayList)15 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)14 Nation (com.palmergames.bukkit.towny.object.Nation)10 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)8 IOException (java.io.IOException)8 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)7 InvalidNameException (javax.naming.InvalidNameException)5 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)4 KeyValueFile (com.palmergames.util.KeyValueFile)4 File (java.io.File)4 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)3 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)3 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3