Solved Percentages from a config file help.

Discussion in 'Plugin Development' started by ItsComits, Mar 27, 2017.

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

    ItsComits

    Hello, The title pretty much explains my problem.
    Here is my config file:
    Code:
    Prizes:
      List:
        - 'Name:Percent'
    
    Here is the code I have done so far. I want to be able to get the item based on percentages and then give them the item.

    Code:
        List<String> prizeList = MasksConfigYML.getConfigYML().getStringList("Prizes.List");
        String[] prizes = prizeList.toArray(new String[0]);
        private void activate(Player p) {
            Random r = new Random();
            //Percenatges
          
          
          
          
            //give Item
            p.getInventory().addItem(Item);
        }
     
  2. Offline

    Zombie_Striker

    @ItsComits
    1. Generate a random number between 0 and 100
    2. For loop through all the prizes
    3. if the percent for the prize is greater than the value from the config, subtract the config value from the percent and continue.
    4. Else, add the item to the player inventory and break
    Remember, this will only work if the sum of all config values equals 100. If it is less, there is a chance the player gets nothing. If it is over 100, then the percentages will be off.
     
  3. Offline

    ItsComits

    @Zombie_Striker I am having some trouble with step 3.
    Here is what I have so far:
    Code:
        List<String> prizeList = MasksConfigYML.getConfigYML().getStringList("Prizes.List");
        String[] prizes = prizeList.toArray(new String[0]);
        Integer[] percentages = prizeList.toArray(new Integer[1]);
    
        @EventHandler
        public void interactEvent(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            activate(p);
        }
        public void activate(Player p) {
            int percent = getRandom(0, 100);
            for (String s : prizes) {
                if (percent > percentages) {
                    //percentages is an array so this does not work.
                }
            }
        }
     
  4. Offline

    Zombie_Striker

    @ItsComits
    Your is that prizes is empty. The prizes has to contain a value in order for the for loop to work. Also, you can compare a integer to an array. You have to provide an index for percentages.
     
  5. Offline

    ItsComits

    @Zombie_Striker Thanks, But now I feel that I have done something wrong. I have not tested it as I do not have access to my main computer.
    Code:
        List<String> prizeList = MasksConfigYML.getConfigYML().getStringList("Masks.Prizes");
        String[] prizes = prizeList.toArray(new String[0]);
        Integer[] percentages = prizeList.toArray(new Integer[1]);
    
        @EventHandler
        public void interactEvent(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            activate(p);
        }
        public void activate1(Player p) {
            int percent = getRandom(0, 100);
            for (String s : prizes) {
                if (percent > percentages[0]) {
                    int i = percentages[0]-percent;
                } else {
                    //add Item
                    break;
                }
            }
        }
        public int getRandom(int lower, int upper) {
            return undiscoveredMask.random.nextInt((upper - lower) + 1) + lower;
        }
     
  6. Offline

    Zombie_Striker

    @ItsComits
    The if statement for checking the percents is not entirely accurate. What you are doing is checking if the percent is equal to a constant, static value. That means that the percents for each prize is exactly the same.

    Instead, do the following:
    1. For loop through all the prizes in the config. Do this by getting the keys for each prize.
    2. For each key, check if the chance for that prize is greater than the "percent". If so, subtract the chance from percent and continiue;
    3. Else, if it is less than or equal to percent, add the item and break out of the loop.
     
  7. Offline

    ItsComits

    @Zombie_Striker I think I have done it.
    Code:
        public void activate(Player p) {
            int percent = getRandom(0, 100);
            for (String loop : Config.getConfigYML().getStringList("Prizes.List")) {
                String[] key = loop.split("\\:");
                int percentage = Integer.parseInt(key[1]);
                if (percentage > percent) {
                    int i = percentage - percent;
                    continue;
                } else if (percentage <= percent) {
                    // add Item
                    break;
                }
            }
        }
     
  8. Offline

    Zombie_Striker

  9. Offline

    ItsComits

    I cannot try it right now. I will try it in a couple of hours when I do get my main computer back. Your help is greatly appreciated. Thanks :)
     
Thread Status:
Not open for further replies.

Share This Page