use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.
the class TownySQLSource method loadTownBlocks.
@Override
public boolean loadTownBlocks() {
String line = "";
Boolean result = false;
// Load town blocks
if (!getContext())
return false;
ResultSet rs;
for (TownBlock townBlock : getAllTownBlocks()) {
try {
Statement s = cntx.createStatement();
rs = s.executeQuery("SELECT * FROM " + tb_prefix + "TOWNBLOCKS" + " WHERE world='" + townBlock.getWorld().getName() + "' AND x='" + townBlock.getX() + "' AND z='" + townBlock.getZ() + "'");
while (rs.next()) {
line = rs.getString("name");
if (line != null)
try {
townBlock.setName(line.trim());
} catch (Exception e) {
}
line = rs.getString("price");
if (line != null)
try {
townBlock.setPlotPrice(Float.parseFloat(line.trim()));
} catch (Exception e) {
}
line = rs.getString("town");
if (line != null)
try {
Town town = getTown(line.trim());
townBlock.setTown(town);
} catch (Exception e) {
}
line = rs.getString("resident");
if (line != null && !line.isEmpty())
try {
Resident res = getResident(line.trim());
townBlock.setResident(res);
} catch (Exception e) {
}
line = rs.getString("type");
if (line != null)
try {
townBlock.setType(Integer.parseInt(line));
} catch (Exception e) {
}
line = rs.getString("outpost");
if (line != null)
try {
townBlock.setOutpost(Boolean.parseBoolean(line));
} catch (Exception e) {
}
line = rs.getString("permissions");
if ((line != null) && !line.isEmpty())
try {
townBlock.setPermissions(line.trim().replaceAll("#", ","));
//set = true;
} catch (Exception e) {
}
result = rs.getBoolean("changed");
if (result != null)
try {
townBlock.setChanged(result);
} catch (Exception e) {
}
result = rs.getBoolean("locked");
if (result != null)
try {
townBlock.setLocked(result);
} catch (Exception e) {
}
}
// 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
// }
// }
s.close();
} catch (SQLException e) {
TownyMessaging.sendErrorMsg("Loading Error: Exception while reading TownBlocks ");
e.printStackTrace();
return false;
}
}
return true;
}
use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.
the class TownySQLSource method loadTown.
@SuppressWarnings("deprecation")
@Override
public boolean loadTown(Town town) {
String line;
String[] tokens;
TownyMessaging.sendDebugMsg("Loading town " + town.getName());
if (!getContext())
return false;
try {
Statement s = cntx.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM " + tb_prefix + "TOWNS " + " WHERE name='" + town.getName() + "'");
String search;
while (rs.next()) {
line = rs.getString("residents");
if (line != null) {
search = (line.contains("#")) ? "#" : ",";
tokens = line.split(search);
for (String token : tokens) {
if (!token.isEmpty()) {
Resident resident = getResident(token);
if (resident != null)
town.addResident(resident);
}
}
}
town.setMayor(getResident(rs.getString("mayor")));
// line = rs.getString("assistants");
// if (line != null) {
// tokens = line.split(",");
// for (String token : tokens) {
// if (!token.isEmpty()) {
// Resident assistant = getResident(token);
// if ((assistant != null) && (town.hasResident(assistant)))
// town.addAssistant(assistant);
// }
// }
// }
town.setTownBoard(rs.getString("townBoard"));
line = rs.getString("tag");
if (line != null)
try {
town.setTag(line);
} catch (TownyException e) {
town.setTag("");
}
town.setPermissions(rs.getString("protectionStatus").replaceAll("#", ","));
town.setBonusBlocks(rs.getInt("bonus"));
town.setTaxPercentage(rs.getBoolean("taxpercent"));
town.setTaxes(rs.getFloat("taxes"));
town.setHasUpkeep(rs.getBoolean("hasUpkeep"));
town.setPlotPrice(rs.getFloat("plotPrice"));
town.setPlotTax(rs.getFloat("plotTax"));
town.setEmbassyPlotPrice(rs.getFloat("embassyPlotPrice"));
town.setEmbassyPlotTax(rs.getFloat("embassyPlotTax"));
town.setCommercialPlotPrice(rs.getFloat("commercialPlotPrice"));
town.setCommercialPlotTax(rs.getFloat("commercialPlotTax"));
town.setOpen(rs.getBoolean("open"));
town.setPublic(rs.getBoolean("public"));
town.setAdminDisabledPVP(rs.getBoolean("admindisabledpvp"));
town.setPurchasedBlocks(rs.getInt("purchased"));
line = rs.getString("homeBlock");
if (line != null) {
search = (line.contains("#")) ? "#" : ",";
tokens = line.split(search);
if (tokens.length == 3)
try {
TownyWorld world = getWorld(tokens[0]);
try {
int x = Integer.parseInt(tokens[1]);
int z = Integer.parseInt(tokens[2]);
TownBlock homeBlock = world.getTownBlock(x, z);
town.forceSetHomeBlock(homeBlock);
} catch (NumberFormatException e) {
TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid location.");
} catch (NotRegisteredException e) {
TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid TownBlock.");
} catch (TownyException e) {
TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " does not have a home block.");
}
} catch (NotRegisteredException e) {
TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid world.");
}
}
line = rs.getString("spawn");
if (line != null) {
search = (line.contains("#")) ? "#" : ",";
tokens = line.split(search);
if (tokens.length >= 4)
try {
World world = plugin.getServerWorld(tokens[0]);
double x = Double.parseDouble(tokens[1]);
double y = Double.parseDouble(tokens[2]);
double z = Double.parseDouble(tokens[3]);
Location loc = new Location(world, x, y, z);
if (tokens.length == 6) {
loc.setPitch(Float.parseFloat(tokens[4]));
loc.setYaw(Float.parseFloat(tokens[5]));
}
town.forceSetSpawn(loc);
} catch (NumberFormatException e) {
} catch (NotRegisteredException e) {
} catch (NullPointerException e) {
}
// Load outpost spawns
line = rs.getString("outpostSpawns");
if (line != null) {
String[] outposts = line.split(";");
for (String spawn : outposts) {
search = (line.contains("#")) ? "#" : ",";
tokens = spawn.split(search);
if (tokens.length >= 4)
try {
World world = plugin.getServerWorld(tokens[0]);
double x = Double.parseDouble(tokens[1]);
double y = Double.parseDouble(tokens[2]);
double z = Double.parseDouble(tokens[3]);
Location loc = new Location(world, x, y, z);
if (tokens.length == 6) {
loc.setPitch(Float.parseFloat(tokens[4]));
loc.setYaw(Float.parseFloat(tokens[5]));
}
town.forceAddOutpostSpawn(loc);
} catch (NumberFormatException e) {
} catch (NotRegisteredException e) {
} catch (NullPointerException e) {
}
}
}
}
/*
* Attempt these for older databases.
*/
try {
line = rs.getString("townBlocks");
if (line != null)
utilLoadTownBlocks(line, town, null);
} catch (SQLException e) {
}
s.close();
return true;
}
s.close();
return false;
} catch (SQLException e) {
TownyMessaging.sendErrorMsg("SQL: Load Town sql Error - " + e.getMessage());
} catch (Exception e) {
TownyMessaging.sendErrorMsg("SQL: Load Town unknown Error - ");
e.printStackTrace();
}
return false;
}
use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.
the class TownyFlatFileSource method loadTown.
@Override
public boolean loadTown(Town town) {
String line;
String[] tokens;
String path = getTownFilename(town);
File fileTown = new File(path);
if (fileTown.exists() && fileTown.isFile()) {
try {
KeyValueFile kvFile = new KeyValueFile(path);
line = kvFile.get("residents");
if (line != null) {
tokens = line.split(",");
for (String token : tokens) {
if (!token.isEmpty()) {
TownyMessaging.sendDebugMsg("Town Fetching Resident: " + token);
Resident resident = getResident(token);
if (resident != null) {
try {
town.addResident(resident);
} catch (AlreadyRegisteredException e) {
TownyMessaging.sendErrorMsg("Loading Error: " + resident.getName() + " is already a member of a town (" + resident.getTown().getName() + ").");
}
}
}
}
}
line = kvFile.get("mayor");
if (line != null)
town.setMayor(getResident(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) && (town.hasResident(assistant)))
// town.addAssistant(assistant);
// }
// }
// }
town.setTownBoard(kvFile.get("townBoard"));
line = kvFile.get("tag");
if (line != null)
try {
town.setTag(line);
} catch (TownyException e) {
town.setTag("");
}
line = kvFile.get("protectionStatus");
if (line != null)
town.setPermissions(line);
line = kvFile.get("bonusBlocks");
if (line != null)
try {
town.setBonusBlocks(Integer.parseInt(line));
} catch (Exception e) {
town.setBonusBlocks(0);
}
line = kvFile.get("purchasedBlocks");
if (line != null)
try {
town.setPurchasedBlocks(Integer.parseInt(line));
} catch (Exception e) {
town.setPurchasedBlocks(0);
}
line = kvFile.get("plotPrice");
if (line != null)
try {
town.setPlotPrice(Double.parseDouble(line));
} catch (Exception e) {
town.setPlotPrice(0);
}
line = kvFile.get("hasUpkeep");
if (line != null)
try {
town.setHasUpkeep(Boolean.parseBoolean(line));
} catch (NumberFormatException nfe) {
} catch (Exception e) {
}
line = kvFile.get("taxpercent");
if (line != null)
try {
town.setTaxPercentage(Boolean.parseBoolean(line));
} catch (NumberFormatException nfe) {
} catch (Exception e) {
}
line = kvFile.get("taxes");
if (line != null)
try {
town.setTaxes(Double.parseDouble(line));
} catch (Exception e) {
town.setTaxes(0);
}
line = kvFile.get("plotTax");
if (line != null)
try {
town.setPlotTax(Double.parseDouble(line));
} catch (Exception e) {
town.setPlotTax(0);
}
line = kvFile.get("commercialPlotPrice");
if (line != null)
try {
town.setCommercialPlotPrice(Double.parseDouble(line));
} catch (Exception e) {
town.setCommercialPlotPrice(0);
}
line = kvFile.get("commercialPlotTax");
if (line != null)
try {
town.setCommercialPlotTax(Double.parseDouble(line));
} catch (Exception e) {
town.setCommercialPlotTax(0);
}
line = kvFile.get("embassyPlotPrice");
if (line != null)
try {
town.setEmbassyPlotPrice(Double.parseDouble(line));
} catch (Exception e) {
town.setEmbassyPlotPrice(0);
}
line = kvFile.get("embassyPlotTax");
if (line != null)
try {
town.setEmbassyPlotTax(Double.parseDouble(line));
} catch (Exception e) {
town.setEmbassyPlotTax(0);
}
line = kvFile.get("adminDisabledPvP");
if (line != null)
try {
town.setAdminDisabledPVP(Boolean.parseBoolean(line));
} catch (NumberFormatException nfe) {
} catch (Exception e) {
}
/*
* line = kvFile.get("mobs");
* if (line != null)
* try {
* town.setHasMobs(Boolean.parseBoolean(line));
* } catch (NumberFormatException nfe) {
* } catch (Exception e) {
* }
*/
line = kvFile.get("open");
if (line != null)
try {
town.setOpen(Boolean.parseBoolean(line));
} catch (NumberFormatException nfe) {
} catch (Exception e) {
}
line = kvFile.get("public");
if (line != null)
try {
town.setPublic(Boolean.parseBoolean(line));
} catch (NumberFormatException nfe) {
} catch (Exception e) {
}
/*
* line = kvFile.get("explosion");
* if (line != null)
* try {
* town.setBANG(Boolean.parseBoolean(line));
* } catch (NumberFormatException nfe) {
* } catch (Exception e) {
* }
*
* line = kvFile.get("fire");
* if (line != null)
* try {
* town.setFire(Boolean.parseBoolean(line));
* } catch (NumberFormatException nfe) {
* } catch (Exception e) {
* }
*/
line = kvFile.get("townBlocks");
if (line != null)
utilLoadTownBlocks(line, town, null);
line = kvFile.get("homeBlock");
if (line != null) {
tokens = line.split(",");
if (tokens.length == 3)
try {
TownyWorld world = getWorld(tokens[0]);
try {
int x = Integer.parseInt(tokens[1]);
int z = Integer.parseInt(tokens[2]);
TownBlock homeBlock = world.getTownBlock(x, z);
town.forceSetHomeBlock(homeBlock);
} catch (NumberFormatException e) {
TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid location.");
} catch (NotRegisteredException e) {
TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid TownBlock.");
} catch (TownyException e) {
TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " does not have a home block.");
}
} catch (NotRegisteredException e) {
TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid world.");
}
}
line = kvFile.get("spawn");
if (line != null) {
tokens = line.split(",");
if (tokens.length >= 4)
try {
World world = plugin.getServerWorld(tokens[0]);
double x = Double.parseDouble(tokens[1]);
double y = Double.parseDouble(tokens[2]);
double z = Double.parseDouble(tokens[3]);
Location loc = new Location(world, x, y, z);
if (tokens.length == 6) {
loc.setPitch(Float.parseFloat(tokens[4]));
loc.setYaw(Float.parseFloat(tokens[5]));
}
town.forceSetSpawn(loc);
} catch (NumberFormatException e) {
} catch (NotRegisteredException e) {
} catch (NullPointerException e) {
}
}
// Load outpost spawns
line = kvFile.get("outpostspawns");
if (line != null) {
String[] outposts = line.split(";");
for (String spawn : outposts) {
tokens = spawn.split(",");
if (tokens.length >= 4)
try {
World world = plugin.getServerWorld(tokens[0]);
double x = Double.parseDouble(tokens[1]);
double y = Double.parseDouble(tokens[2]);
double z = Double.parseDouble(tokens[3]);
Location loc = new Location(world, x, y, z);
if (tokens.length == 6) {
loc.setPitch(Float.parseFloat(tokens[4]));
loc.setYaw(Float.parseFloat(tokens[5]));
}
town.forceAddOutpostSpawn(loc);
} catch (NumberFormatException e) {
} catch (NotRegisteredException e) {
} catch (NullPointerException e) {
}
}
}
} catch (Exception e) {
TownyMessaging.sendErrorMsg("Loading Error: Exception while reading town file " + town.getName());
return false;
}
return true;
} else
return false;
}
use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.
the class TownyBlockListener method testBlockMove.
private boolean testBlockMove(Block block, BlockFace direction) {
Block blockTo = block.getRelative(direction);
Location loc = block.getLocation();
Location locTo = blockTo.getLocation();
Coord coord = Coord.parseCoord(loc);
Coord coordTo = Coord.parseCoord(locTo);
TownyWorld townyWorld = null;
TownBlock CurrentTownBlock = null, destinationTownBlock = null;
try {
townyWorld = TownyUniverse.getDataSource().getWorld(loc.getWorld().getName());
CurrentTownBlock = townyWorld.getTownBlock(coord);
} catch (NotRegisteredException e) {
//System.out.print("Failed to fetch TownBlock");
}
try {
destinationTownBlock = townyWorld.getTownBlock(coordTo);
} catch (NotRegisteredException e1) {
//System.out.print("Failed to fetch TownBlockTo");
}
if (CurrentTownBlock != destinationTownBlock) {
// Cancel if either is not null, but other is (wild to town).
if (((CurrentTownBlock == null) && (destinationTownBlock != null)) || ((CurrentTownBlock != null) && (destinationTownBlock == null))) {
//event.setCancelled(true);
return true;
}
// If both blocks are owned by the town.
if (!CurrentTownBlock.hasResident() && !destinationTownBlock.hasResident())
return false;
try {
if ((!CurrentTownBlock.hasResident() && destinationTownBlock.hasResident()) || (CurrentTownBlock.hasResident() && !destinationTownBlock.hasResident()) || (CurrentTownBlock.getResident() != destinationTownBlock.getResident()) || (CurrentTownBlock.getPlotPrice() != -1) || (destinationTownBlock.getPlotPrice() != -1)) {
return true;
}
} catch (NotRegisteredException e) {
// Failed to fetch a resident
return true;
}
}
return false;
}
use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.
the class PlotClaim method residentUnclaim.
private boolean residentUnclaim(WorldCoord worldCoord) throws TownyException {
try {
TownBlock townBlock = worldCoord.getTownBlock();
townBlock.setResident(null);
townBlock.setPlotPrice(townBlock.getTown().getPlotTypePrice(townBlock.getType()));
// Set the plot permissions to mirror the towns.
townBlock.setType(townBlock.getType());
TownyUniverse.getDataSource().saveTownBlock(townBlock);
plugin.updateCache(worldCoord);
} catch (NotRegisteredException e) {
throw new TownyException(TownySettings.getLangString("msg_not_own_place"));
}
return true;
}
Aggregations