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"));
}
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"));
}
}
}
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;
}
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;
}
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();
}
Aggregations