YAML and Configurations

Discussion in 'Plugin Development' started by Izze, Mar 9, 2011.

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

    Izze

    Hello.

    So I've created a small plugin, and it needs to save certain locations to a file and then be able to load them. I also need a configuration file. I've successfully opened a YAML file, but I have some problems...

    1. Creating a YAML file
    So, if a user wants to use my plugin, they now have to copy a whole folder from a zipped archive, because the configruation and locations file are in there. I want to automatically create a folder with those files if it doesn't already exist. I've managed to create the folders and empty files, but I have to check if the files are empty too, so that I can add the default configurations properties and an empty locations variable. How do I do that? If I try to read an unexisting property from a YAML file, the whole plugin crashes, so I need something better.

    2. Saving to the YAML file
    I've managed to save locations to the file, but the file itself looks odd. I'll show you:
    Code:
    locations:
    -   - -120 # No indentation
        - 63 # Three spaces indentation?
        - 8
        - world
    -   - -117
        - 63
        - 8
        - world
    Shouldn't it look more like this?
    Code:
    locations:
      -  - -120 # Two spaces indentation
        - 63 # Four spaces indentation
        - 8
        - world
      -  - -117
        - 63
        - 8
        - world
    Even though it looks odd, I manage to read the file. My next problem is that I have to get some kind of list out of this. If I do this:
    Code:
    List<Object> locations = file.getList("locations");
    I get a List<List<Object>>, which I have to cast a hundred times to get something useful. Is there a better way to save my locations? I think this looks kind of ugly.

    3. Saving to the YAML file
    I manages to add and remove locations from that file, but it isn't pretty. To remove locations, I loop through all saved locations in the file, and tests whether all coordinates match, and if they do, I remove that part and saves the file.
    To add locations, I first read the file into a List<List<Object>>, and then I add my locations as a List<Object> to that list, and then I save the file. I think this whole process is quite messy and overly complicated.

    Well, I think that's every problem you can have with a file; creating, loading and saving. [​IMG]
    I'll be thankful for any help I receive. Thanks in advance!
     
  2. Offline

    Nohup

    1) Look at the source of my projects for creating YAML on the fly. Snow, Drop, and Philo all do it.
    2) you are ending up with a list of lists. it would be better to have a list of associative arrays or a list of objects, even a parseable string. all of these would be easier to read
    3) Sounds like you do what you have to do right now for saving and what not, but you can manage all of this in the Configuration object without having to go through external lists. You could name your locations and get them via the key, or even just create a unique key. It is an adjustment to thinking about it, plot out what you need then create to it...
     
  3. Offline

    Izze

    Many thanks to you! This solved all my problems. I now generate an UID (hashCode for the block + currentTimeMillis()) to get a better layout in my YAML file.

    Thanks again! Problem solved! Lock or whatever! Happy day, yay! :D
     
Thread Status:
Not open for further replies.

Share This Page