Problem Saving to Config File

Discussion in 'Plugin Development' started by Maurdekye, Mar 25, 2014.

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

    Maurdekye

    I have some inventories and locations that I want to save to a config file, but I keep coming to an impass. When I save the information, it comes out like this;
    Code:
        location: !!org.bukkit.Location
          pitch: 0.0
          world: !!org.bukkit.craftbukkit.v1_7_R1.CraftWorld
            PVP: true
            ambientSpawnLimit: 15
            animalSpawnLimit: 15
            autoSave: true
            difficulty: EASY
            environment: NORMAL
            fullTime: 144084
            keepSpawnInMemory: true
            monsterSpawnLimit: 70
            thunderDuration: 10150
            thundering: false
            time: 84
            waterAnimalSpawnLimit: 5
            weatherDuration: 65089
          x: 29.0
          y: 82.0
          yaw: 0.0
          z: 12.0
    And when my program tries to read it back again, it fails with an error on the lines with two exclamation marks. The object seems to be serializing fine, but it's not saving the type correctly for some reason.
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    Maurdekye Objects that are not ConfiguerationSerlizable do not round trip when set through the Configuration API. Only objects which are ConfigurationSerializable, Strings, or Collections behave correctly.
     
  3. Offline

    Maurdekye

    Sagacious_Zed I've been able to save locations to a config file before; why aren't they working now / how can I get it to work?
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    Since I need to repeat myself. Objects that are not ConfigurationSerilizable do not round trip.

    Objects that are not ConfigurationSerializable cannot be loaded by the configuration API. You must define your own object that is ConfigurationSerializable.
     
  5. Offline

    Maurdekye

    Sagacious_Zed So, just extend Location and implement ConfigurationSerializable?
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    Extending Location is not really advisable. On that note, an object is only ConfigurationSerializable if all the fields are ConfigurationSerilizable or String or Map or List or Set or a primitive.
     
  7. Offline

    Maurdekye

    Sagacious_Zed Then what is advisable? i'm not going to just walk away from this problem.
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    As I mentioned before "You must define your own object that is ConfigurationSerializable" Alternatively, you could encode location data as a String.
     
  9. Offline

    Maurdekye

    Sagacious_Zed "Location data as a string"... Think i'll try that.

    Sagacious_Zed Also, why would they serialize, but not be able to deserialize? It doesn't make sense.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    transcend_

    Try using these two methods

    Code:java
    1. public String serializeLoc(Location loc)
    2. {
    3. return loc.getWorld().getName()+";"+loc.getX()+";"+loc.getY()+";"+loc.getZ()+";"+loc.getYaw()+";"+loc.getPitch();
    4. }
    5.  
    6. public Location deserializeLoc(String loc)
    7. {
    8. String[] st = loc.split(";");
    9.  
    10. return new Location(Bukkit.getWorld(st[0]), Double.parseDouble(st[1]), Double.parseDouble(st[2]), Double.parseDouble(st[3]), Float.parseFloat(st[4]), Float.parseFloat(st[5]));
    11. }


    Than do
    Code:
    Location loc;
     
    public void onEnable()
    {
        loc = deserializeLoc(getConfig().getString("location"));
    }
     
    //Than to serialize it you do it anywhere you want
    getConfig().set("location", serializeLoc(loc));
    saveConfig();
     
Thread Status:
Not open for further replies.

Share This Page