How to get random reward from config?

Discussion in 'Plugin Development' started by AppleMen, Nov 29, 2015.

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

    AppleMen

    Hello,

    I am making a system which should choose a random reward from my config.yml:
    Code:
    Rewards:
      Normal:
        1:
          name: 'Messaged'
          chance: 100 #percentage
          command: 'msg %player% well done!'
        2:
          name: 'Money'
          chance: 10 #percentage
          command: 'eco give %player% 20000'
    
    It contains the Rewards section with type of rewards as child. Each type contains a unique id to get it with an for loop.

    What I try to do is getting one of those unique id's. But I want to look at the chance (percentage) of getting it. How can I accomplish this? (For example: Randomly choose 1 or 2, but look at the chance of getting it.
     
  2. Offline

    mcdorli

    getConfig().getConfigurationSection("Rewards").getKeys(false), it will return all of the keys after the rewards (1; 2; 3; etc.), then just rand.nextInt(list.size);
     
  3. Offline

    AppleMen

    How does this look to the type of reward (like normal) and the chance?
     
  4. Offline

    mcdorli

    go trough every of the rewards, every time, create a new random number, and see if the chance is smaller than that, if it is, then the player won the prize. I think I did this in my LotteryTicket plugin.
     
Thread Status:
Not open for further replies.

Share This Page