Grabbing a Random ConfigurationSection

Discussion in 'Spigot Plugin Development' started by Ian0526, May 16, 2016.

  1. Hello, I am making a NPC Quest plugin for my server, and I am having trouble trying to grab a random configuration from this configurationsection. In other words, I just want to have it so that it chooses a random quest from the "Challenges" ConfigurationSection.

    This is how my config would look:
    Code (Text):
        needed: COOKED_PORK
        amount: 5
        message-before-complete: '&bTrader Joe &8> &aI need 5 Cooked Pork.'
        message-after-complete: '&bTrader Joe &8> &aThanks, enjoy your diamond.'
        command-upon-completion: 'give {player} diamond 1'
        needed: Wheat
        amount: 1
        message-before-complete: '&bTrader Joe &8> &aI need wheat.'
        message-after-complete: '&bTrader Joe &8> &aThanks, enjoy your diamond.'
        command-upon-completion: 'give {player} diamond 1'
  2. Get the keys of Challenges and change it to an array. Then get a random value between 0 and your array size and get the challenge assigned to that number (so if the random would return 0, than you would get the 0th element of the array).
    • Agree Agree x 2
  3. Thank you!
  4. Code (Text):
                Random random = new Random();
                ArrayList<String> challenges = new ArrayList<String>();
                String currentchallenge = challenges.get(random.nextInt(challenges.size()));
    Does this look correct?
  5. Besides that there is no reason to create a new ArrayList, it should work
  6. getKeys(true) should probably be changed to getKeys(false) as you only want the challenge and not the info in the challenge. For the other part, you could also do this with an array. This would make it this:
    Code (Text):
    Random random = new Random();
    Strnig[] array = getConfig().getConfigurationSection("Challenges").getKeys(false).toArray();
    String challenge = array[random.nextInt(array.length)];
    I don't know what's the best. I would be guessing the array, because it only takes the size it's set to, however I'm not an expert with that.