Need help accessing a list of objects in YAML

Discussion in 'Plugin Development' started by Wh1rledPeas, Jan 7, 2014.

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

    Wh1rledPeas

    Hello All,

    I have to admit, my experience with YAML is minimal, but I have a properly formatted YAML file that holds the information I need and I seem to be having a problem using the bukkit FileConfiguration class to gain access to the data stored in my plugin's config.

    The data contains a list of complex data items identified by IP Address. Considering the "dot nature" of the IP address and a potential conflict with the "dot nature" of the getConfig() method, I chose to store the list in the following way...

    Show Spoiler
    Code:
    servers:
        - server:
            ip: 192.168.1.101
            port: 25566
            name: test
            clients:
                - 192.168.1.105
        - server:
            ip: 192.168.1.101
            port: 25567
            name: qa
            clients:
                - 192.168.1.104
        - server:
            ip: 192.168.1.101
            port: 25568
            name: production
            clients:
                - 192.168.1.106
        - server:
            ip: 192.168.1.101
            port: 25569
            name: dev
            clients:
                - 192.168.1.105
        - server:
            ip: 192.168.1.101
            port: 25570
            name: dev2
            clients:
                - 192.168.1.105
    

    ...where the servers node contains a list of server objects. I have verified that this is valid YAML and I can get close to the data that I need using...
    Code:java
    1. Plugin.getConfig().getList("servers")

    ...but I'm having a bit of trouble getting past that initial list of servers.

    Can someone help me figure out how to use the built-in bukkit FileConfiguration objects to get and set information that is part of this server list, and to add and remove server nodes from the list? This would be really simple using XML and xpath to query the data that I require, but I'm trying to stay true to the YAML nature of a bukkit plugin's configuration information. Assuming that this might be consumed by others who have more experience with YAML.

    Thanks.
    -Peas

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. Parsing it the way you have it right now is not really elegant, I think.
    I would rather go with a format like this (also, this looks closer to how you'd have the same thing in json):
    Code:
    servers:
        - ip: 192.168.1.101
          port: 25566
          name: test
          clients:
              - 192.168.1.105
        - ip: 192.168.1.101
          port: 25567
          name: qa
          clients:
              - 192.168.1.104
        - ip: 192.168.1.101
          port: 25568
          name: production
          clients:
              - 192.168.1.106
        - ip: 192.168.1.101
          port: 25569
          name: dev
          clients:
              - 192.168.1.105
        - ip: 192.168.1.101
          port: 25570
          name: dev2
          clients:
              - 192.168.1.105
    When you then call getList for 'servers' you would be provided with a list of hashmaps which have 'ip', 'post', 'name' and 'clients' as their keys and their respective values.
    currently you'd have a list of hashmaps with only one key being 'server' and it's value being another hashmap with it's respective content.
     
    TheKingElessar likes this.
  3. Offline

    Wh1rledPeas

    @kumpelblase2: Good call on the format change. That does simplify things a bit.

    I was able to retrieve a list of clients for a given server (ip+port), using the following...
    Code:java
    1.  
    2. List list = Plugin.getConfig().getList("servers");
    3.  
    4. for (int i = 0; i < list.size(); i++) {
    5. LinkedHashMap map = (LinkedHashMap) list.get(i);
    6. String ip = (String) map.get("ip");
    7. if (ip.equals(targetIP)) {
    8. Integer port = (Integer) map.get("port");
    9. if (port.equals(targetPort)) {
    10. ret = new StringList((ArrayList) map.get("clients"));
    11. }
    12. }
    13. }
    14.  
    15. return ret;
    16.  

    However... It uses "unchecked or unsafe operations", which I'm not really cool with. I was really hoping that the FileConfiguration class within bukkit would have provided a way to directly interact with the list, by way of it's internal objects.

    I am toying with the idea of creating a custom object/collection, containing the settings and using snakeyaml to serialize and deserialize the yaml directly from and back into the object. But this circumvents Bukkit's config file handling. Which I'm not completely okay with either.

    Is there no good way of interacting directly with this list data from within the FileConfiguration class?
     
    RastaLulz likes this.
  4. Offline

    Wh1rledPeas

    @kumpelblase2: Yes. Thanks for cluing me in on the built-in object serialization. I think I will try to make that work before I go to an external library. I'd rather play IN the sandbox, than go make my own.;)
     
Thread Status:
Not open for further replies.

Share This Page