Search in sources :

Example 11 with TownBlock

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;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) KeyValueFile(com.palmergames.util.KeyValueFile) World(org.bukkit.World) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) 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) Resident(com.palmergames.bukkit.towny.object.Resident) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) File(java.io.File) KeyValueFile(com.palmergames.util.KeyValueFile) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Location(org.bukkit.Location)

Example 12 with TownBlock

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;
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Coord(com.palmergames.bukkit.towny.object.Coord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Location(org.bukkit.Location) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation)

Example 13 with TownBlock

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;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 14 with TownBlock

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;
                }
        }
    }
}
Also used : Coord(com.palmergames.bukkit.towny.object.Coord) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) ProtectionRegenTask(com.palmergames.bukkit.towny.tasks.ProtectionRegenTask) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 15 with TownBlock

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) {
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Coord(com.palmergames.bukkit.towny.object.Coord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Villager(org.bukkit.entity.Villager) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Location(org.bukkit.Location) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Aggregations

TownBlock (com.palmergames.bukkit.towny.object.TownBlock)34 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)28 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)16 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)12 Resident (com.palmergames.bukkit.towny.object.Resident)11 Town (com.palmergames.bukkit.towny.object.Town)8 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)7 Coord (com.palmergames.bukkit.towny.object.Coord)7 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)7 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)5 Location (org.bukkit.Location)5 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)4 World (org.bukkit.World)4 Block (org.bukkit.block.Block)4 Nation (com.palmergames.bukkit.towny.object.Nation)3 EventHandler (org.bukkit.event.EventHandler)3 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)2 KeyValueFile (com.palmergames.util.KeyValueFile)2 EOFException (java.io.EOFException)2