Search in sources :

Example 6 with KeyValueFile

use of com.palmergames.util.KeyValueFile in project Towny by ElgarL.

the class TownyFlatFileSource method loadNation.

@Override
public boolean loadNation(Nation nation) {
    String line = "";
    String[] tokens;
    String path = getNationFilename(nation);
    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) {
                    if (!token.isEmpty()) {
                        TownyMessaging.sendDebugMsg("Nation Fetching Town: " + token);
                        Town town = getTown(token);
                        if (town != null)
                            nation.addTown(town);
                    }
                }
            }
            line = kvFile.get("capital");
            if (line != null)
                nation.setCapital(getTown(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)
            //								nation.addAssistant(assistant);
            //						}
            //					}
            //				}
            line = kvFile.get("tag");
            if (line != null)
                try {
                    nation.setTag(line);
                } catch (TownyException e) {
                    nation.setTag("");
                }
            line = kvFile.get("allies");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Nation friend = getNation(token);
                        if (friend != null)
                            //("ally", friend);
                            nation.addAlly(friend);
                    }
                }
            }
            line = kvFile.get("enemies");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Nation enemy = getNation(token);
                        if (enemy != null)
                            //("enemy", enemy);
                            nation.addEnemy(enemy);
                    }
                }
            }
            line = kvFile.get("taxes");
            if (line != null)
                try {
                    nation.setTaxes(Double.parseDouble(line));
                } catch (Exception e) {
                    nation.setTaxes(0.0);
                }
            line = kvFile.get("neutral");
            if (line != null)
                try {
                    nation.setNeutral(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
        } catch (Exception e) {
            TownyMessaging.sendErrorMsg("Loading Error: Exception while reading nation file " + nation.getName());
            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) 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)

Example 7 with KeyValueFile

use of com.palmergames.util.KeyValueFile in project Towny by ElgarL.

the class TownyHModFlatFileSource method loadTown.

@Override
public boolean loadTown(Town town) {
    System.out.println("[Towny] [hMod Conversion] Town: " + town.getName());
    String line;
    String[] tokens;
    String path = rootFolder + dataFolder + "/towns/" + town.getName() + ".txt";
    File fileResident = new File(path);
    if (fileResident.exists() && fileResident.isFile()) {
        try {
            KeyValueFile kvFile = new KeyValueFile(path);
            line = kvFile.get("residents");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    Resident resident = getResident(token);
                    if (resident != null)
                        town.addResident(resident);
                }
            }
            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) {
            //						Resident assistant = getResident(token);
            //						if (assistant != null)
            //							town.addAssistant(assistant);
            //					}
            //				}
            town.setTownBoard(kvFile.get("townBoard"));
            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(Integer.parseInt(line));
                } catch (Exception e) {
                    town.setPlotPrice(0);
                }
            line = kvFile.get("taxes");
            if (line != null)
                try {
                    town.setTaxes(Integer.parseInt(line));
                } catch (Exception e) {
                    town.setTaxes(0);
                }
            line = kvFile.get("plotTax");
            if (line != null)
                try {
                    town.setPlotTax(Integer.parseInt(line));
                } catch (Exception e) {
                    town.setPlotTax(0);
                }
            line = kvFile.get("pvp");
            if (line != null)
                try {
                    town.setPVP(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("taxpercent");
            if (line != null)
                try {
                    town.setTaxPercentage(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) {
                }
        } catch (Exception e) {
            System.out.println("[Towny] Loading Error: Exception while reading town file " + town.getName());
            e.printStackTrace();
            return false;
        }
        return true;
    } else
        return false;
}
Also used : Resident(com.palmergames.bukkit.towny.object.Resident) 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 KeyValueFile

use of com.palmergames.util.KeyValueFile 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;
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) KeyValueFile(com.palmergames.util.KeyValueFile) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) 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

AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)8 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)8 KeyValueFile (com.palmergames.util.KeyValueFile)8 File (java.io.File)8 IOException (java.io.IOException)8 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)5 Resident (com.palmergames.bukkit.towny.object.Resident)5 EOFException (java.io.EOFException)5 FileNotFoundException (java.io.FileNotFoundException)5 InvalidNameException (javax.naming.InvalidNameException)5 Town (com.palmergames.bukkit.towny.object.Town)4 Nation (com.palmergames.bukkit.towny.object.Nation)2 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)2 ArrayList (java.util.ArrayList)2 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)1 List (java.util.List)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1