Solved Config - Adding items

Discussion in 'Spigot Plugin Development' started by Fluddershy, May 22, 2017.

  1. Hi there,

    I've always wanted to know how to do this, to have an infinite amount of things to add to a config. Sure, I could use a stringlist, but what if I didn't want to. What if I want to do something like:
    Code (Text):
    world:
        food:
        health:
    world_nether:
        food:
        health:
    world_factions_test1:
        food:
        health:
    Obviously, this has nothing to do with what I'm trying to achieve, but how would I do something like that? Like I said before, I want to be able to add things, and use them. Let's say a player throws an egg, and you check the name of that egg. How would I be able to add that egg to the config, and check if name: is equal to that of the eggs. Thanks in advance!
     
  2. MiniDigger

    Supporter

    keys follow a hierarchy.
    set("world.food") and set("world.health") would produce what you want.
     
  3. Code (Text):
        @EventHandler
        public void onPlayerEggThrow(PlayerEggThrowEvent e)
        {
            List<String> keys = new ArrayList<String>();
            for (String key : this.getConfig().getConfigurationSection("egg-types").getKeys(false))
                keys.add(key);
           
            if (keys.contains(e.getEgg().getName()))
                System.out.println("Works");
            else
                System.out.println("Didn't work.");
                // Do your code.
        }
    And the config.yml:
    Code (Text):
    egg-types:
      special: # Eggs name = key;
        bounces: true
      rare: # Eggs name = key;
        bounces: false
      legendary: # Eggs name = key;
        bounces: false
     
    • Like Like x 1
  4. Note if you want to have the nodes directly on the top level (not recommended) you can simply use config.getKeys(false).

    If you want it to recurse into the sub-nodes automatically (also not recommended) you can use getKeys(true)
     
    • Like Like x 1
    • Agree Agree x 1
    • Friendly Friendly x 1