Getting the values in a configuration section

Discussion in 'Spigot Plugin Development' started by HarleySwtfus, Jan 7, 2020.

  1. So I have my configuration here:
    Code (Text):

    1:
    display-name: "&f&lSimple"
    size: 8
    drops:
      //chance: material
      50: stone
      20: iron_ore
      10: coal_ore
      10: lapis_ore
      5: diamond_ore
      5: emerald_ore
    Key:
    <chance%>:<material>


    I need to find a way to get a list of all the % chances in the "drops" link, along with all the materials that the chances are assigned to. For example, I need to make it a 50% chance of returning "stone", 20% chance of returning "iron_ore", etc...
    How would I do this? I've done some research about this but I'm not sure exactly what I'd be searching for.
     
  2. isnt this the third time this exact question has been asked today? have u asked this question today? :thinking:
    you cant use numbers as keys. keys have to be unique. reverse them. after that, compile them into a map (or a list of objects), and iterate through them after youve calculated a random number, cumulatively subtracting each block's chance from your calculated random number until you reach 0 or a negative number. return whatever material is at that index.
     
    • Like Like x 1
    • Informative Informative x 1
  3. Could you give me a kickstart on how I would do this please?
     
    • Winner Winner x 1
  4. You could have it the other way around:
    Code (YAML):
    drops:
      stone
    : 50
      iron_ore
    : 20
     
  5. Good idea, so now how would I get every value in the "drops" list? Would I get the configurationsection? And if I did how would I create a hash to store each values?
     
    • Winner Winner x 1
  6. Code (Java):
    FileConfiguration configuration = getConfig();
    List<?> list = configuration.getList("drops");

    // Make sure list is not null.
    if (list == null)
        list = new ArrayList<>();

    // We'll gather the keys here.
    List<String> keys = new ArrayList<String>();
    list.forEach((element) -> {
        if (element instanceof String)
            keys.add((String)element);
    });

    // Create map of key:percentages.
    Map<String, Integer> percentageMap = new HashMap<>();
    keys.forEach((key) -> percentageMap.put(key, configuration.getInt("drops." + key, 0)));

    // The map is created.
     
    • Useful Useful x 1
  7. drops in that example is a configurationsection object, not a list.
     
    • Agree Agree x 1