Search in sources :

Example 6 with Town

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

the class TownyFlatFileSource method saveTownList.

@Override
public boolean saveTownList() {
    List<String> list = new ArrayList<String>();
    for (Town town : getTowns()) {
        list.add(town.getName());
    }
    /*
		 *  Make sure we only save in async
		 */
    this.queryQueue.add(new FlatFile_Task(list, rootFolder + dataFolder + FileMgmt.fileSeparator() + "towns.txt"));
    return true;
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList)

Example 7 with Town

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

the class TownyHModFlatFileSource method loadNation.

@Override
public boolean loadNation(Nation nation) {
    System.out.println("[Towny] [hMod Conversion] Nation: " + nation.getName());
    String line = "";
    String[] tokens;
    String path = rootFolder + dataFolder + "/nations/" + nation.getName() + ".txt";
    File fileResident = new File(path);
    if (fileResident.exists() && fileResident.isFile()) {
        try {
            KeyValueFile kvFile = new KeyValueFile(path);
            line = kvFile.get("towns");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    Town town = getTown(token);
                    if (town != null)
                        nation.addTown(town);
                }
            }
            line = kvFile.get("capital");
            nation.setCapital(getTown(line));
            //				line = kvFile.get("assistants");
            //				if (line != null) {
            //					tokens = line.split(",");
            //					for (String token : tokens) {
            //						Resident assistant = getResident(token);
            //						if (assistant != null)
            //							nation.addAssistant(assistant);
            //					}
            //				}
            line = kvFile.get("allies");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    Nation friend = getNation(token);
                    if (friend != null)
                        nation.setAllegiance("ally", friend);
                }
            }
            line = kvFile.get("enemies");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    Nation enemy = getNation(token);
                    if (enemy != null)
                        nation.setAllegiance("enemy", enemy);
                }
            }
            line = kvFile.get("taxes");
            if (line != null)
                try {
                    nation.setTaxes(Integer.parseInt(line));
                } catch (Exception e) {
                    nation.setTaxes(0);
                }
            line = kvFile.get("neutral");
            if (line != null)
                try {
                    nation.setNeutral(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
        } catch (Exception e) {
            System.out.println("[Towny] Loading Error: Exception while reading nation file " + nation.getName());
            e.printStackTrace();
            return false;
        }
        return true;
    } else
        return false;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Town(com.palmergames.bukkit.towny.object.Town) KeyValueFile(com.palmergames.util.KeyValueFile) File(java.io.File) KeyValueFile(com.palmergames.util.KeyValueFile) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) IOException(java.io.IOException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)

Example 8 with Town

use of com.palmergames.bukkit.towny.object.Town 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;
}
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) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) 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 9 with Town

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

the class TownySQLSource method loadNation.

@Override
public boolean loadNation(Nation nation) {
    String line = "";
    String[] tokens;
    TownyMessaging.sendDebugMsg("Loading nation " + nation.getName());
    if (!getContext())
        return false;
    try {
        Statement s = cntx.createStatement();
        ResultSet rs = s.executeQuery("SELECT * FROM " + tb_prefix + "NATIONS WHERE name='" + nation.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)
                            nation.addTown(town);
                    }
                }
            }
            nation.setCapital(getTown(rs.getString("capital")));
            // line = rs.getString("assistants");
            // if (line != null) {
            // tokens = line.split(",");
            // for (String token : tokens) {
            // if (!token.isEmpty()) {
            // Resident assistant = getResident(token);
            // if (assistant != null)
            // nation.addAssistant(assistant);
            // }
            // }
            // }
            nation.setTag(rs.getString("tag"));
            line = rs.getString("allies");
            if (line != null) {
                search = (line.contains("#")) ? "#" : ",";
                tokens = line.split(search);
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Nation friend = getNation(token);
                        if (friend != null)
                            // ("ally", friend);
                            nation.addAlly(friend);
                    }
                }
            }
            line = rs.getString("enemies");
            if (line != null) {
                search = (line.contains("#")) ? "#" : ",";
                tokens = line.split(search);
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Nation enemy = getNation(token);
                        if (enemy != null)
                            // ("enemy", enemy);
                            nation.addEnemy(enemy);
                    }
                }
            }
            nation.setTaxes(rs.getDouble("taxes"));
            nation.setNeutral(rs.getBoolean("neutral"));
        }
        s.close();
        return true;
    } catch (SQLException e) {
        TownyMessaging.sendErrorMsg("SQL: Load Nation sql error " + e.getMessage());
    } catch (Exception e) {
        TownyMessaging.sendErrorMsg("SQL: Load Nation unknown error - ");
        e.printStackTrace();
    }
    return false;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Town(com.palmergames.bukkit.towny.object.Town) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) 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 10 with Town

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

the class TownyFlatFileSource method loadWorld.

@Override
public boolean loadWorld(TownyWorld world) {
    String line = "";
    String[] tokens;
    String path = getWorldFilename(world);
    // create the world file if it doesn't exist
    try {
        FileMgmt.checkFiles(new String[] { path });
    } catch (IOException e1) {
        TownyMessaging.sendErrorMsg("Loading Error: Exception while reading file " + path);
    }
    File fileWorld = new File(path);
    if (fileWorld.exists() && fileWorld.isFile()) {
        try {
            KeyValueFile kvFile = new KeyValueFile(path);
            line = kvFile.get("towns");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        TownyMessaging.sendDebugMsg("World Fetching Town: " + token);
                        Town town = getTown(token);
                        if (town != null) {
                            town.setWorld(world);
                        //world.addTown(town); not needed as it's handled in the Town object
                        }
                    }
                }
            }
            line = kvFile.get("claimable");
            if (line != null)
                try {
                    world.setClaimable(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("pvp");
            if (line != null)
                try {
                    world.setPVP(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("forcepvp");
            if (line != null)
                try {
                    world.setForcePVP(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("forcetownmobs");
            if (line != null)
                try {
                    world.setForceTownMobs(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("worldmobs");
            if (line != null)
                try {
                    world.setWorldMobs(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("firespread");
            if (line != null)
                try {
                    world.setFire(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("forcefirespread");
            if (line != null)
                try {
                    world.setForceFire(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("explosions");
            if (line != null)
                try {
                    world.setExpl(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("forceexplosions");
            if (line != null)
                try {
                    world.setForceExpl(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("endermanprotect");
            if (line != null)
                try {
                    world.setEndermanProtect(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("disableplayertrample");
            if (line != null)
                try {
                    world.setDisablePlayerTrample(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("disablecreaturetrample");
            if (line != null)
                try {
                    world.setDisableCreatureTrample(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("unclaimedZoneBuild");
            if (line != null)
                try {
                    world.setUnclaimedZoneBuild(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("unclaimedZoneDestroy");
            if (line != null)
                try {
                    world.setUnclaimedZoneDestroy(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("unclaimedZoneSwitch");
            if (line != null)
                try {
                    world.setUnclaimedZoneSwitch(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("unclaimedZoneItemUse");
            if (line != null)
                try {
                    world.setUnclaimedZoneItemUse(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("unclaimedZoneName");
            if (line != null)
                try {
                    world.setUnclaimedZoneName(line);
                } catch (Exception e) {
                }
            line = kvFile.get("unclaimedZoneIgnoreIds");
            if (line != null)
                try {
                    List<String> mats = new ArrayList<String>();
                    for (String s : line.split(",")) if (!s.isEmpty())
                        try {
                            int id = Integer.parseInt(s);
                            mats.add(BukkitTools.getMaterial(id).name());
                        } catch (NumberFormatException e) {
                            mats.add(s);
                        }
                    world.setUnclaimedZoneIgnore(mats);
                } catch (Exception e) {
                }
            line = kvFile.get("usingPlotManagementDelete");
            if (line != null)
                try {
                    world.setUsingPlotManagementDelete(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("plotManagementDeleteIds");
            if (line != null)
                try {
                    //List<Integer> nums = new ArrayList<Integer>();
                    List<String> mats = new ArrayList<String>();
                    for (String s : line.split(",")) if (!s.isEmpty())
                        try {
                            int id = Integer.parseInt(s);
                            mats.add(BukkitTools.getMaterial(id).name());
                        } catch (NumberFormatException e) {
                            mats.add(s);
                        }
                    world.setPlotManagementDeleteIds(mats);
                } catch (Exception e) {
                }
            line = kvFile.get("usingPlotManagementMayorDelete");
            if (line != null)
                try {
                    world.setUsingPlotManagementMayorDelete(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("plotManagementMayorDelete");
            if (line != null)
                try {
                    List<String> materials = new ArrayList<String>();
                    for (String s : line.split(",")) if (!s.isEmpty())
                        try {
                            materials.add(s.toUpperCase().trim());
                        } catch (NumberFormatException e) {
                        }
                    world.setPlotManagementMayorDelete(materials);
                } catch (Exception e) {
                }
            line = kvFile.get("usingPlotManagementRevert");
            if (line != null)
                try {
                    world.setUsingPlotManagementRevert(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("usingPlotManagementRevertSpeed");
            if (line != null)
                try {
                    world.setPlotManagementRevertSpeed(Long.parseLong(line));
                } catch (Exception e) {
                }
            line = kvFile.get("plotManagementIgnoreIds");
            if (line != null)
                try {
                    List<String> mats = new ArrayList<String>();
                    for (String s : line.split(",")) if (!s.isEmpty())
                        try {
                            int id = Integer.parseInt(s);
                            mats.add(BukkitTools.getMaterial(id).name());
                        } catch (NumberFormatException e) {
                            mats.add(s);
                        }
                    world.setPlotManagementIgnoreIds(mats);
                } catch (Exception e) {
                }
            line = kvFile.get("usingPlotManagementWildRegen");
            if (line != null)
                try {
                    world.setUsingPlotManagementWildRevert(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
            line = kvFile.get("PlotManagementWildRegenEntities");
            if (line != null)
                try {
                    List<String> entities = new ArrayList<String>();
                    for (String s : line.split(",")) if (!s.isEmpty())
                        try {
                            entities.add(s.trim());
                        } catch (NumberFormatException e) {
                        }
                    world.setPlotManagementWildRevertEntities(entities);
                } catch (Exception e) {
                }
            line = kvFile.get("usingPlotManagementWildRegenDelay");
            if (line != null)
                try {
                    world.setPlotManagementWildRevertDelay(Long.parseLong(line));
                } catch (Exception e) {
                }
            line = kvFile.get("usingTowny");
            if (line != null)
                try {
                    world.setUsingTowny(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
        // loadTownBlocks(world);
        } catch (Exception e) {
            TownyMessaging.sendErrorMsg("Loading Error: Exception while reading world file " + path);
            return false;
        }
        return true;
    } else {
        TownyMessaging.sendErrorMsg("Loading Error: File error while reading " + world.getName());
        return false;
    }
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) KeyValueFile(com.palmergames.util.KeyValueFile) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) 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)

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