Solved Reading value from list (config).

Discussion in 'Spigot Plugin Development' started by stelioshah, Jul 7, 2021.

  1. Here is my list:
    Code (Text):
    #ClassMeUp config file

    #Pirate section:

    Piniata-Parrot-Drops:

      - '1':
        material: DIAMOND
        chance: 20.0
       
      - '2':
        material: EMERALD
        chance: 30.0
       
      - '3':
        material: DIRT
        chance: 30.0
       
      - '4':
        material: GOLDEN_APPLE
        chance: 10.0
       
      - '5':
        material: LAPIS_LAZULI
        chance: 5.0
       
      - '6':
        material: OBSIDIAN
        chance: 5.0
    Code:
    Why does this return as chance of 70? I have written this as a List because I need to know the size of the list.

    What is the proper way to do this?
     
  2. Don't know what causes it to return 70 but for instance I wouldn't set it as a list but as configuration sections, like this:

    Code (YAML):
    Piniata-Parrot-Drops:
        1
    :
            material
    : DIAMOND
            chance
    : 20.0
        2
    :
            material
    : EMERALD
            chance
    : 30.0
     
     
    • Agree Agree x 1
  3. I know that it works this way, but can I get the size of Piniata-Parrot-Drops using this? (in your example the size is 2)
     
  4. You can count the keys of the section by running getKeys(false).size()
     
    • Winner Winner x 1
  5. Sure you can, but using a different method:
    Code (Java):

    //A ConfigurationSection is like a sub-FileConfiguration (a section insde a config)
    ConfigurationSection parrotDrops = file.getConfigurationSection("your path here")

    //File would be 0 if secion = null (does not exist)
    int size = 0;

    // getKeys() returns a set of every path under the section, in this case: "1", "2", "3" or as many numbers you decide to add
    if(section != null) size = section.getKeys(false).size()
    EDIT: ConfigurationSection.
     
    • Agree Agree x 1
  6. Yes I just found this out. And by the way this was totally my bad. I had changed the config and forgot to reload it which resulted in a false chance. Thus I could not understand in any way what was causing this. Your ways above work fine. Thank you.
     
    • Friendly Friendly x 1