Load/Save Problem

Discussion in 'Plugin Development' started by Uniclaw, Dec 30, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Uniclaw

    Hi!

    I would save a specific Block, and load it on restart - but there is a problem.
    It's saved correctly, but not loaded correctly :(
    My Code:
    Code:
    public void save(){
    for(Location l : plugin.getB()){
    Data dd = plugin.getData(l);
     
    try {
    BufferedWriter out = new BufferedWriter(new FileWriter(new File(plugin.getDataFolder(), "data.dat")));
    out.write(
    new StringBuilder()
    .append(l.getWorld().getName()).append(",").append(l.getX()).append(",").append(l.getY()).append(",").append(l.getZ())
    .append(";")
    .append(dd.getID()).append("#").append(dd.getAmount()).append("#").append(dd.getData()).toString());
    out.newLine();
    out.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
     
    public void load(){
    try{
    BufferedReader br = new BufferedReader(new FileReader(new File(plugin.getDataFolder(), "data.dat")));
    String line = "";
    while((line = br.readLine()) != null){
    World w = Bukkit.getWorld(line.split(",")[0]);
    double x = Double.valueOf(line.split(",")[1]);
    double y = Double.valueOf(line.split(",")[2]);
    double z = Double.valueOf(line.split(",")[3]);
    Location loc = new Location(w, x,y,z);
    Data dd = new Data(Integer.valueOf(line.split(";")[1].split("#")[0]), Integer.valueOf(line.split(";")[1].split("#")[1]), Byte.valueOf(line.split(";")[1].split("#")[2]));
    plugin.blocks.put(loc, dd);
    }
    br.close();
    }catch(Exception e){
     
    }
    }
    load is called in the enable method, save in the disable method.
     
  2. Offline

    fireblast709

    YamlConfiguration maybe ;3? The conversion String-Location would be like
    Code:java
    1. public Location strToLoc(String loc) throws IllegalArgumentException
    2. {
    3. String parts = loc.split(",", 4);
    4. if(parts.length != 4)
    5. {
    6. throw new IllegalArgumentException("String representation was not valid");
    7. }
    8. World w = Bukkit.getWorld(parts[0]);
    9. if(w == null)
    10. {
    11. throw new IllegalArgumentException("World not found");
    12. }
    13. try
    14. {
    15. int x = Integer.parseInt(parts[1]);
    16. int y = Integer.parseInt(parts[2]);
    17. int z = Integer.parseInt(parts[3]);
    18. return new Location(w,x,y,z);
    19. }
    20. {
    21. throw new IllegalArgumentException("x, y or z was not a valid integer");
    22. }
    23. }
    And Location to String
    Code:java
    1. public String locToStr(Location loc)
    2. {
    3. StringBuilder sb = new StringBuilder(loc.getWorld().getName());
    4. sb.append(",").append(loc.getBlockX());
    5. sb.append(",").append(loc.getBlockY());
    6. sb.append(",").append(loc.getBlockZ());
    7. return sb.toString();
    8. }
     
  3. Offline

    Uniclaw

    fireblast709
    Thanks! But the Problem is, that its not just an location - its a location wirh 3other values, and i dont know why its not working :( Anyway, i try it :)
     
Thread Status:
Not open for further replies.

Share This Page