Get chance to use an item with a listed percentage

Discussion in 'Plugin Development' started by JjPwN1, Dec 30, 2013.

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

    JjPwN1

    Currently, I have a list created in a config that has an item id and a percent chance that the item listed will be used.
    Code:
    list:
      - 1|75
      - 2|25
    The 1 in the "1|75" entry is the item id, and the 75 is the percent chance that that entry will be used.

    I am currently having trouble on doing this, as I do not know how to correctly calculate the percent.
    Code:
    for(int x = 0; x < getConfig().getList("list").size(); x++){
        String entry = (String) pc.getConfig().getList("list").get(x);
        String[] split = entry.split("|");
        int itemID = Integer.parseInt(split[0]);
        int chance = Integer.parseInt(split[1]);
    The above is the code I am currently using to get an entry its item id and chance. How would I use the percent chance listed to correctly determine which entry to use in the list?
     
  2. Offline

    Jogy34

    Code:java
    1.  
    2. Random rnd = new Random();
    3. if(rnd.nextInt(100) + 1 <= chance)
    4. {
    5. //do stuff
    6. }
     
Thread Status:
Not open for further replies.

Share This Page