Search in sources :

Example 36 with Town

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

the class PlotClaim method residentClaim.

private boolean residentClaim(WorldCoord worldCoord) throws TownyException, EconomyException {
    //if (resident.hasTown())
    try {
        TownBlock townBlock = worldCoord.getTownBlock();
        Town town = townBlock.getTown();
        if ((resident.hasTown() && (resident.getTown() != town) && (!townBlock.getType().equals(TownBlockType.EMBASSY))) || ((!resident.hasTown()) && (!townBlock.getType().equals(TownBlockType.EMBASSY))))
            throw new TownyException(TownySettings.getLangString("msg_err_not_part_town"));
        try {
            Resident owner = townBlock.getResident();
            if (townBlock.getPlotPrice() != -1) {
                if (TownySettings.isUsingEconomy() && !resident.payTo(townBlock.getPlotPrice(), owner, "Plot - Buy From Seller"))
                    throw new TownyException(TownySettings.getLangString("msg_no_money_purchase_plot"));
                int maxPlots = TownySettings.getMaxResidentPlots(resident);
                if (maxPlots >= 0 && resident.getTownBlocks().size() + 1 > maxPlots)
                    throw new TownyException(String.format(TownySettings.getLangString("msg_max_plot_own"), maxPlots));
                TownyMessaging.sendTownMessage(town, TownySettings.getBuyResidentPlotMsg(resident.getName(), owner.getName(), townBlock.getPlotPrice()));
                townBlock.setPlotPrice(-1);
                townBlock.setResident(resident);
                // Set the plot permissions to mirror the new owners.
                townBlock.setType(townBlock.getType());
                TownyUniverse.getDataSource().saveResident(owner);
                TownyUniverse.getDataSource().saveTownBlock(townBlock);
                // Update any caches for this WorldCoord
                plugin.updateCache(worldCoord);
                return true;
            } else if (player.hasPermission(PermissionNodes.TOWNY_COMMAND_PLOT_ASMAYOR.getNode())) {
                if (TownySettings.isUsingEconomy() && !town.payTo(0.0, owner, "Plot - Buy Back"))
                    throw new TownyException(TownySettings.getLangString("msg_town_no_money_purchase_plot"));
                TownyMessaging.sendTownMessage(town, TownySettings.getBuyResidentPlotMsg(town.getName(), owner.getName(), 0.0));
                townBlock.setResident(null);
                townBlock.setPlotPrice(-1);
                // Set the plot permissions to mirror the towns.
                townBlock.setType(townBlock.getType());
                TownyUniverse.getDataSource().saveResident(owner);
                // Update the townBlock data file so it's no longer using custom settings.
                TownyUniverse.getDataSource().saveTownBlock(townBlock);
                return true;
            } else {
                //Should never reach here.
                throw new AlreadyRegisteredException(String.format(TownySettings.getLangString("msg_already_claimed"), owner.getName()));
            }
        } catch (NotRegisteredException e) {
            if (townBlock.getPlotPrice() == -1)
                throw new TownyException(TownySettings.getLangString("msg_err_plot_nfs"));
            double bankcap = TownySettings.getTownBankCap();
            if (bankcap > 0) {
                if (townBlock.getPlotPrice() + town.getHoldingBalance() > bankcap)
                    throw new TownyException(String.format(TownySettings.getLangString("msg_err_deposit_capped"), bankcap));
            }
            if (TownySettings.isUsingEconomy() && !resident.payTo(townBlock.getPlotPrice(), town, "Plot - Buy From Town"))
                throw new TownyException(TownySettings.getLangString("msg_no_money_purchase_plot"));
            townBlock.setPlotPrice(-1);
            townBlock.setResident(resident);
            // Set the plot permissions to mirror the new owners.
            townBlock.setType(townBlock.getType());
            TownyUniverse.getDataSource().saveTownBlock(townBlock);
            return true;
        }
    } catch (NotRegisteredException e) {
        throw new TownyException(TownySettings.getLangString("msg_err_not_part_town"));
    }
//else
//	throw new TownyException(TownySettings.getLangString("msg_err_not_in_town_claim"));
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 37 with Town

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

the class TownClaim method run.

@Override
public void run() {
    List<TownyWorld> worlds = new ArrayList<TownyWorld>();
    List<Town> towns = new ArrayList<Town>();
    TownyWorld world;
    if (player != null)
        TownyMessaging.sendMsg(player, "Processing " + ((claim) ? "Town Claim..." : "Town unclaim..."));
    if (selection != null) {
        for (WorldCoord worldCoord : selection) {
            try {
                world = worldCoord.getTownyWorld();
                if (!worlds.contains(world))
                    worlds.add(world);
                if (claim) {
                    // Claim
                    townClaim(town, worldCoord, outpost);
                    // Reset so we only flag the first plot as an outpost.
                    outpost = false;
                } else {
                    // Unclaim
                    this.town = worldCoord.getTownBlock().getTown();
                    townUnclaim(town, worldCoord, forced);
                }
                // Mark this town as modified for saving.
                if (!towns.contains(town))
                    towns.add(town);
            } catch (NotRegisteredException e) {
                // Invalid world
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_err_not_configured"));
            } catch (TownyException x) {
                TownyMessaging.sendErrorMsg(player, x.getMessage());
            }
        }
    } else if (!claim) {
        if (town == null) {
            TownyMessaging.sendMsg(player, "Nothing to unclaim!");
            return;
        }
        townUnclaimAll(town);
    }
    if (!towns.isEmpty())
        for (Town test : towns) TownyUniverse.getDataSource().saveTown(test);
    if (!worlds.isEmpty())
        for (TownyWorld test : worlds) TownyUniverse.getDataSource().saveWorld(test);
    plugin.resetCache();
    if (player != null) {
        if (claim) {
            TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_annexed_area"), (selection.size() > 5) ? "Total TownBlocks: " + selection.size() : Arrays.toString(selection.toArray(new WorldCoord[0]))));
            if (town.getWorld().isUsingPlotManagementRevert())
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_wait_locked"));
        } else if (forced) {
            TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_admin_unclaim_area"), (selection.size() > 5) ? "Total TownBlocks: " + selection.size() : Arrays.toString(selection.toArray(new WorldCoord[0]))));
            if ((town != null) && (town.getWorld().isUsingPlotManagementRevert()))
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_wait_locked"));
        }
    }
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 38 with Town

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

the class TownySQLSource method loadWorld.

@Override
public boolean loadWorld(TownyWorld world) {
    String line = "";
    Boolean result = false;
    Long resultLong;
    String[] tokens;
    TownyMessaging.sendDebugMsg("Loading world " + world.getName());
    if (!getContext())
        return false;
    try {
        Statement s = cntx.createStatement();
        ResultSet rs = s.executeQuery("SELECT * FROM " + tb_prefix + "WORLDS WHERE name='" + world.getName() + "'");
        String search;
        while (rs.next()) {
            line = rs.getString("towns");
            if (line != null) {
                search = (line.contains("#")) ? "#" : ",";
                tokens = line.split(search);
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Town town = getTown(token);
                        if (town != null) {
                            town.setWorld(world);
                        }
                    }
                }
            }
            result = rs.getBoolean("claimable");
            if (result != null)
                try {
                    world.setClaimable(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("pvp");
            if (result != null)
                try {
                    world.setPVP(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("forcepvp");
            if (result != null)
                try {
                    world.setForcePVP(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("forcetownmobs");
            if (result != null)
                try {
                    world.setForceTownMobs(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("worldmobs");
            if (result != null)
                try {
                    world.setWorldMobs(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("firespread");
            if (result != null)
                try {
                    world.setFire(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("forcefirespread");
            if (result != null)
                try {
                    world.setForceFire(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("explosions");
            if (result != null)
                try {
                    world.setExpl(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("forceexplosions");
            if (result != null)
                try {
                    world.setForceExpl(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("endermanprotect");
            if (result != null)
                try {
                    world.setEndermanProtect(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("disableplayertrample");
            if (result != null)
                try {
                    world.setDisablePlayerTrample(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("disablecreaturetrample");
            if (result != null)
                try {
                    world.setDisableCreatureTrample(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("unclaimedZoneBuild");
            if (result != null)
                try {
                    world.setUnclaimedZoneBuild(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("unclaimedZoneDestroy");
            if (result != null)
                try {
                    world.setUnclaimedZoneDestroy(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("unclaimedZoneSwitch");
            if (result != null)
                try {
                    world.setUnclaimedZoneSwitch(result);
                } catch (Exception e) {
                }
            result = rs.getBoolean("unclaimedZoneItemUse");
            if (result != null)
                try {
                    world.setUnclaimedZoneItemUse(result);
                } catch (Exception e) {
                }
            line = rs.getString("unclaimedZoneName");
            if (result != null)
                try {
                    world.setUnclaimedZoneName(line);
                } catch (Exception e) {
                }
            line = rs.getString("unclaimedZoneIgnoreIds");
            if (line != null)
                try {
                    List<String> mats = new ArrayList<String>();
                    search = (line.contains("#")) ? "#" : ",";
                    for (String split : line.split(search)) if (!split.isEmpty())
                        try {
                            int id = Integer.parseInt(split);
                            mats.add(BukkitTools.getMaterial(id).name());
                        } catch (NumberFormatException e) {
                            mats.add(split);
                        }
                    world.setUnclaimedZoneIgnore(mats);
                } catch (Exception e) {
                }
            result = rs.getBoolean("usingPlotManagementDelete");
            if (result != null)
                try {
                    world.setUsingPlotManagementDelete(result);
                } catch (Exception e) {
                }
            line = rs.getString("plotManagementDeleteIds");
            if (line != null)
                try {
                    List<String> mats = new ArrayList<String>();
                    search = (line.contains("#")) ? "#" : ",";
                    for (String split : line.split(search)) if (!split.isEmpty())
                        try {
                            int id = Integer.parseInt(split);
                            mats.add(BukkitTools.getMaterial(id).name());
                        } catch (NumberFormatException e) {
                            mats.add(split);
                        }
                    world.setPlotManagementDeleteIds(mats);
                } catch (Exception e) {
                }
            result = rs.getBoolean("usingPlotManagementMayorDelete");
            if (result != null)
                try {
                    world.setUsingPlotManagementMayorDelete(result);
                } catch (Exception e) {
                }
            line = rs.getString("plotManagementMayorDelete");
            if (line != null)
                try {
                    List<String> materials = new ArrayList<String>();
                    search = (line.contains("#")) ? "#" : ",";
                    for (String split : line.split(search)) if (!split.isEmpty())
                        try {
                            materials.add(split.toUpperCase().trim());
                        } catch (NumberFormatException e) {
                        }
                    world.setPlotManagementMayorDelete(materials);
                } catch (Exception e) {
                }
            result = rs.getBoolean("usingPlotManagementRevert");
            if (result != null)
                try {
                    world.setUsingPlotManagementRevert(result);
                } catch (Exception e) {
                }
            resultLong = rs.getLong("PlotManagementRevertSpeed");
            if (resultLong != null)
                try {
                    world.setPlotManagementRevertSpeed(resultLong);
                } catch (Exception e) {
                }
            line = rs.getString("plotManagementIgnoreIds");
            if (line != null)
                try {
                    List<String> mats = new ArrayList<String>();
                    search = (line.contains("#")) ? "#" : ",";
                    for (String split : line.split(search)) if (!split.isEmpty())
                        try {
                            int id = Integer.parseInt(split);
                            mats.add(BukkitTools.getMaterial(id).name());
                        } catch (NumberFormatException e) {
                            mats.add(split);
                        }
                    world.setPlotManagementIgnoreIds(mats);
                } catch (Exception e) {
                }
            result = rs.getBoolean("usingPlotManagementWildRegen");
            if (result != null)
                try {
                    world.setUsingPlotManagementWildRevert(result);
                } catch (Exception e) {
                }
            line = rs.getString("plotManagementWildRegenEntities");
            if (line != null)
                try {
                    List<String> entities = new ArrayList<String>();
                    search = (line.contains("#")) ? "#" : ",";
                    for (String split : line.split(search)) if (!split.isEmpty())
                        try {
                            entities.add(split.trim());
                        } catch (NumberFormatException e) {
                        }
                    world.setPlotManagementWildRevertEntities(entities);
                } catch (Exception e) {
                }
            resultLong = rs.getLong("plotManagementWildRegenSpeed");
            if (resultLong != null)
                try {
                    world.setPlotManagementWildRevertDelay(resultLong);
                } catch (Exception e) {
                }
            result = rs.getBoolean("usingTowny");
            if (result != null)
                try {
                    world.setUsingTowny(result);
                } catch (Exception e) {
                }
        }
        s.close();
        return true;
    } catch (SQLException e) {
        TownyMessaging.sendErrorMsg("SQL: Load world sql error (" + world.getName() + ")" + e.getMessage());
    } catch (Exception e) {
        TownyMessaging.sendErrorMsg("SQL: Load world unknown error - ");
        e.printStackTrace();
    }
    return false;
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) SQLException(java.sql.SQLException) IOException(java.io.IOException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 39 with Town

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

the class TownyFlatFileSource method loadTownBlocks.

@Override
public boolean loadTownBlocks() {
    String line = "";
    String path;
    for (TownBlock townBlock : getAllTownBlocks()) {
        path = getTownBlockFilename(townBlock);
        //boolean set = false;
        File fileTownBlock = new File(path);
        if (fileTownBlock.exists() && fileTownBlock.isFile()) {
            try {
                KeyValueFile kvFile = new KeyValueFile(path);
                line = kvFile.get("name");
                if (line != null)
                    try {
                        townBlock.setName(line.trim());
                    } catch (Exception e) {
                    }
                line = kvFile.getString("price");
                if (line != null)
                    try {
                        townBlock.setPlotPrice(Double.parseDouble(line.trim()));
                    } catch (Exception e) {
                    }
                line = kvFile.getString("town");
                if (line != null)
                    try {
                        Town town = getTown(line.trim());
                        townBlock.setTown(town);
                    } catch (Exception e) {
                    }
                line = kvFile.getString("resident");
                if (line != null && !line.isEmpty())
                    try {
                        Resident res = getResident(line.trim());
                        townBlock.setResident(res);
                    } catch (Exception e) {
                    }
                line = kvFile.getString("type");
                if (line != null)
                    try {
                        townBlock.setType(Integer.parseInt(line));
                    } catch (Exception e) {
                    }
                line = kvFile.getString("outpost");
                if (line != null)
                    try {
                        townBlock.setOutpost(Boolean.parseBoolean(line));
                    } catch (Exception e) {
                    }
                line = kvFile.get("permissions");
                if ((line != null) && !line.isEmpty())
                    try {
                        townBlock.setPermissions(line.trim());
                    //set = true;
                    } catch (Exception e) {
                    }
                line = kvFile.get("changed");
                if (line != null)
                    try {
                        townBlock.setChanged(Boolean.parseBoolean(line.trim()));
                    } catch (Exception e) {
                    }
                line = kvFile.get("locked");
                if (line != null)
                    try {
                        townBlock.setLocked(Boolean.parseBoolean(line.trim()));
                    } catch (Exception e) {
                    }
            } catch (Exception e) {
                TownyMessaging.sendErrorMsg("Loading Error: Exception while reading TownBlock file " + path);
                return false;
            }
        //				if (!set) {
        //					// no permissions found so set in relation to it's owners perms.
        //					try {
        //						if (townBlock.hasResident()) {
        //							townBlock.setPermissions(townBlock.getResident().getPermissions().toString());
        //						} else {
        //							townBlock.setPermissions(townBlock.getTown().getPermissions().toString());
        //						}
        //					} catch (NotRegisteredException e) {
        //						// Will never reach here
        //					}
        //				}
        }
    }
    return true;
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) KeyValueFile(com.palmergames.util.KeyValueFile) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) 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)

Example 40 with Town

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

the class War method nationLeave.

public void nationLeave(Nation nation) {
    remove(nation);
    for (Town town : nation.getTowns()) remove(town);
    TownyMessaging.sendGlobalMessage(TownySettings.getWarTimeForfeitMsg(nation.getName()));
    checkEnd();
}
Also used : Town(com.palmergames.bukkit.towny.object.Town)

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