YAML help

Discussion in 'Plugin Development' started by pie_flavor, Feb 4, 2015.

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

    pie_flavor

    As most of you are aware, this is valid YAML:
    Code:
    special-drops:
    - block: IRON_ORE
      drops:
      - tool: GOLD_PICKAXE
        drop: IRON_INGOT
    - block: WEB
      drops:
      - tool: SHEARS
        drop: WEB
      - tool: DIAMOND_SWORD
        drop: STRING^2
      - tool: GOLD_SWORD
        drop: STRING^3
    which should result in a list of ConfigurationSections.
    Problem: I don't see getConfigurationSectionList() anywhere in the API. The config is really annoying to do otherwise, and I need this feature. I get errors when I try to just getList() and try to cast it to List<ConfigurationSection>.
    Help pls?
     
  2. Offline

    caderape

    @pie_flavor
    Are you trying to get a configuration section from a string list ?

    Here a StringList:
    1. special-drops:
    2. - block: IRON_ORE
    The plugin cannot read the drops


    1. - block: IRON_ORE
    2. drops:

    You should try something like this.
    1. special-drops: // You should use '_' and not '-' Im not sure the plugin can read, correct me if i'm wrong.
    2. block:
    3. drops:
    4. tool:
    5. drop:
     
  3. Offline

    Abs0rbed

    @caderape you can use I believe you can use "-", I usually just use underscores for readability. @pie_flavor , You need to stick to correct spacing if you want sub-sections of your configuration.
     
  4. Offline

    1Rogue

    You have to do it yourself via .getList() and casting.
     
  5. Offline

    pie_flavor

    @1Rogue Problem is, I'm not sure if casting the list to type ConfigurationSection will actually work. I don't know if it actually stores it as a List<ConfigurationSection> or not, and the API doesn't seem like it implements lists of sections.
     
  6. Offline

    1Rogue

    So then you can iterate the list of unknowns that is returned and manually check each value if you want, but in my opinion it'd be appropriate to simply let it throw an error if it isn't in the correct format, as that is the result of a user fault.
     
  7. Offline

    pie_flavor

    @1Rogue Not really what I'm talking about. I'm asking if the list of unknowns is even a ConfigurationSection list at all. Does it know that the list is of ConfigurationSections?

    @Abs0rbed @caderape My syntax and indentation is perfectly correct. If you put it into a YAML parser into, say, JSON, you will see that it creates a list of compound tags.
     
  8. Offline

    1Rogue

    Generic types are erased at runtime, nothing "knows" what type it is when running. As for the source stage, no you do not, as .getList() returns a List<?>. You can either cast the whole collection to a List<ConfigurationSection>, or manually go through each value.

    Keep in mind the internal implementation of an object mapping differs between bukkit implementations, as some don't back with ConfigurationSections but rather with a Map<String, Object>.
     
  9. Offline

    pie_flavor

    @1Rogue This will require a lot of testing, but thanks.
    I'm right about the syntax, right?
     
  10. Offline

    1Rogue

    Which syntax would that be?
     
  11. Offline

    pie_flavor

    @Abs0rbed and @caderape said in earlier posts that I did my indentation wrong and I was clearly trying to do something else. It's correct indentation and syntax, right?
     
  12. Offline

    Abs0rbed

    @pie_flavor nope, definitely not correct ConfigurationSections (at least from what I've seen in my configs, I don't write them myself though, mainly generated through code). Try making a little test plugin where you create a bunch of sections in the config and see how it indents them.

    Code:
    special:
        IRON_ORE:
           tool: GOLD_PICKAXE
           drop: IRON_INGOT
        WEB:
           tool: SHEARS
           drop: WEB
    Something like that would be valid, with proper spacing (no tabs, I always use 4 spaces between sections)
    That should be valid, though I just wrote it on the fly.
     
  13. Offline

    1Rogue

    People probably don't understand what it is you're doing, the syntax is correct for a list of object mappings. It's not seen very often, but it's completely legal.
     
  14. Offline

    Abs0rbed

    @1Rogue interesting, I've never seen that before. I just went through all of my configs to see what my plugins generated :p, it's really different.
    @pie_flavor I retract that then xD but why not iterate over the paths in your config and create ConfigurationSections based on those paths to get the lowest level sections you want?

    Not too much code, just a couple for loops
    Code:
    for(String s : "topSection".getValues(false).keySet()){
        //...
    }
    and then create a configurationsection using "topSection".getConfigurationSection(s), add a null check and you're golden. Might be a bit more work than you were hoping for, but if nothing else works, this is pretty reliable.
     
  15. Offline

    pie_flavor

    @Abs0rbed Lists are easier for me, or would be if the Bukkit API supported them.
     
  16. Offline

    Abs0rbed

    @pie_flavor Lists generally are, but I've never had this fail on me, and I've never even heard of what you're doing (though it's not surprising, considering my configs are code-generated ;))
     
  17. Offline

    pie_flavor

    @Abs0rbed Why? I find that it's easier to make the file yourself and call saveDefaultConfig() in onEnable(). Saves space, provides a default config for someone to look at if they screwed up theirs, and is easier to make/edit.

    Also, it's easy to do what I was doing: Just make a normal list, but use tags instead of strings or whatever. Only put the hyphen on the first tag. Those tags can contain tag-lists of their own, too.
     
  18. Offline

    pie_flavor

    @1Rogue Did some testing and figured out that it saves as type LinkedHashMap<String, Object>.
     
  19. Offline

    1Rogue

    You should still account for both possibilities, for instance: https://github.com/CodeLanx/Codelan.../codelanx/codelanxlib/config/Config.java#L111

    This lets me convert whole lists of sections like so:

    Code:java
    1. List<Map<String, Object>> sections = getConfig().getList("example-of-sections").stream().map(Config::getConfigSectionValue).collect(Collectors.toList());
     
  20. Offline

    pie_flavor

    @1Rogue What does '::' mean? Never worked with anything involving streams before.
     
  21. Offline

    coasterman10

    This is actually a map list. Use getMapList. Then you can read each key/value pair in the map.
     
  22. Offline

    pie_flavor

  23. Offline

    1Rogue

    It's a member reference, introduced in java 8. It's equivalent to me doing:

    Code:java
    1. .stream().map((o) -> Config.getConfigSectionValue(o))
     
  24. Offline

    pie_flavor

    @1Rogue ....right. and what's -> mean?
     
  25. Offline

    Gater12

  26. Offline

    pie_flavor

    @Gater12 @1Rogue Where has this been all my life? I can say goodbye to 20 frickin' CommandExecutors in one plugin..
     
Thread Status:
Not open for further replies.

Share This Page