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;
}
use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.
the class TownyEntityListener method onEntityExplode.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
TownyWorld townyWorld;
/**
* Perform this test outside the block loop so we only get the world
* once per explosion.
*/
try {
townyWorld = TownyUniverse.getDataSource().getWorld(event.getLocation().getWorld().getName());
if (!townyWorld.isUsingTowny())
return;
} catch (NotRegisteredException e) {
// failed to get world so abort
return;
}
Coord coord;
List<Block> blocks = event.blockList();
Entity entity = event.getEntity();
int count = 0;
// Sort blocks by height (lowest to highest).
Collections.sort(blocks, ArraySort.getInstance());
for (Block block : blocks) {
coord = Coord.parseCoord(block.getLocation());
count++;
// Warzones
if (townyWorld.isWarZone(coord)) {
if (!TownyWarConfig.isAllowingExplosionsInWarZone()) {
if (event.getEntity() != null)
TownyMessaging.sendDebugMsg("onEntityExplode: Canceled " + event.getEntity().getEntityId() + " from exploding within " + coord.toString() + ".");
event.setCancelled(true);
return;
} else {
if (TownyWarConfig.explosionsBreakBlocksInWarZone()) {
if (TownyWarConfig.regenBlocksAfterExplosionInWarZone()) {
// ***********************************
// TODO
// On completion, remove TODO from config.yml
// comments.
/*
* if
* (!plugin.getTownyUniverse().hasProtectionRegenTask
* (new BlockLocation(block.getLocation()))) {
* ProtectionRegenTask task = new
* ProtectionRegenTask(plugin.getTownyUniverse(),
* block, false);
* task.setTaskId(plugin.getServer().getScheduler().
* scheduleSyncDelayedTask(plugin, task,
* ((TownySettings.getPlotManagementWildRegenDelay()
* + count)*20)));
* plugin.getTownyUniverse().addProtectionRegenTask
* (task ); }
*/
// TODO
// ***********************************
}
// Break the block
} else {
event.blockList().remove(block);
}
}
return;
}
// TODO: expand to protect neutrals during a war
try {
TownBlock townBlock = townyWorld.getTownBlock(coord);
// and the towns has no nation
if (townyWorld.isUsingTowny() && !townyWorld.isForceExpl()) {
if ((!townBlock.getPermissions().explosion) || (TownyUniverse.isWarTime() && TownySettings.isAllowWarBlockGriefing() && !townBlock.getTown().hasNation() && !townBlock.getTown().isBANG())) {
if (event.getEntity() != null)
TownyMessaging.sendDebugMsg("onEntityExplode: Canceled " + event.getEntity().getEntityId() + " from exploding within " + coord.toString() + ".");
event.setCancelled(true);
return;
}
}
} catch (TownyException x) {
// Wilderness explosion regeneration
if (townyWorld.isUsingTowny())
if (townyWorld.isExpl()) {
if (townyWorld.isUsingPlotManagementWildRevert() && (entity != null)) {
TownyMessaging.sendDebugMsg("onEntityExplode: Testing entity: " + entity.getType().getEntityClass().getSimpleName().toLowerCase() + " @ " + coord.toString() + ".");
if (townyWorld.isProtectingExplosionEntity(entity)) {
if ((!TownyRegenAPI.hasProtectionRegenTask(new BlockLocation(block.getLocation()))) && (block.getType() != Material.TNT)) {
ProtectionRegenTask task = new ProtectionRegenTask(plugin, block, false);
task.setTaskId(plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, task, ((TownySettings.getPlotManagementWildRegenDelay() + count) * 20)));
TownyRegenAPI.addProtectionRegenTask(task);
event.setYield((float) 0.0);
block.getDrops().clear();
}
}
}
} else {
event.setCancelled(true);
return;
}
}
}
}
use of com.palmergames.bukkit.towny.object.TownBlock in project Towny by ElgarL.
the class TownyEntityListener method onCreatureSpawn.
/**
*
* @param event
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
if (event.getEntity() instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) event.getEntity();
Location loc = event.getLocation();
Coord coord = Coord.parseCoord(loc);
TownyWorld townyWorld = null;
try {
townyWorld = TownyUniverse.getDataSource().getWorld(loc.getWorld().getName());
} catch (NotRegisteredException e) {
// Failed to fetch a world
return;
}
// remove from world if set to remove mobs globally
if (townyWorld.isUsingTowny())
if (!townyWorld.hasWorldMobs() && ((MobRemovalTimerTask.isRemovingWorldEntity(livingEntity) || ((livingEntity instanceof Villager) && !((Villager) livingEntity).isAdult() && (TownySettings.isRemovingVillagerBabiesWorld()))))) {
if (plugin.isCitizens2()) {
if (!CitizensAPI.getNPCRegistry().isNPC(livingEntity)) {
// TownyMessaging.sendDebugMsg("onCreatureSpawn world: Canceled "
// + event.getEntityType().name() +
// " from spawning within "+coord.toString()+".");
event.setCancelled(true);
}
} else
event.setCancelled(true);
}
// remove from towns if in the list and set to remove
try {
TownBlock townBlock = townyWorld.getTownBlock(coord);
if (townyWorld.isUsingTowny() && !townyWorld.isForceTownMobs()) {
if (!townBlock.getTown().hasMobs() && !townBlock.getPermissions().mobs) {
if ((MobRemovalTimerTask.isRemovingTownEntity(livingEntity) || ((livingEntity instanceof Villager) && !((Villager) livingEntity).isAdult() && (TownySettings.isRemovingVillagerBabiesTown())))) {
if (plugin.isCitizens2()) {
if (!CitizensAPI.getNPCRegistry().isNPC(livingEntity)) {
// TownyMessaging.sendDebugMsg("onCreatureSpawn town: Canceled "
// + event.getEntityType().name() +
// " from spawning within "+coord.toString()+".");
event.setCancelled(true);
}
} else
event.setCancelled(true);
}
}
}
} catch (TownyException x) {
}
}
}
Aggregations