[1.10] Percentage to drop items from blocks

Discussion in 'Plugin Development' started by Blackwing_Forged, Aug 21, 2016.

Thread Status:
Not open for further replies.
  1. I want to have it so when you break any block you have a 30% chance to get one item, 20% for another, 10% then 6% and last 2%.

    I know how to detect if a block is broken and how to give the player the item, but im just not sure how to add the percentages, if anyone could help i'd really appreciate it.
     
  2. Offline

    Zombie_Striker

    @Blackwing_Forged
    1. Use ThreadLocalRandom.current to get a Random instance.
    2. Create a new int between 0 and 100.
    3. If the int is greater than 30, don't do anything.
    4. If is is equal to 30, give one item
    5. If it's equal to 20, give them to.
    6. continue for all other percentages.
     
  3. I've never used ThreadLocalRandom.current before, could you maybe give a quick example please?
     
  4. Offline

    Zombie_Striker

    @Blackwing_Forged
    Random random = ThreadLocalRandom.current;
    int randomInt = random.nextInt(100);
     
  5. @Zombie_Striker
    Ok, sorry for the late response, i had trouble with the server, anyways, it works but it seems like they're getting the rarest item quite common, here's what ive done.


    Code:
    Random rand = ThreadLocalRandom.current();
            int num = rand.nextInt(100);
            if(num > 30)
            {
              
            }
            else if(num == 20)
            {
                //adds item to inventory
            }
            else if(num == 10)
            {          
                  //adds item to inventory
            }
            else if(num == 5)
            {          
                  //adds item to inventory
            }
            else if(num == 3)
            {          
                  //adds item to inventory
            }
            else if(num == 1)
            {          
                  //adds item to inventory
            }
        }
     
    Last edited: Aug 22, 2016
  6. Offline

    Zombie_Striker

    @Blackwing_Forged
    That is because you're not doing the checks correctly. You are checking if the number is exactly equal to 10,20,ect. This means there is only a %1 chance of getting each item. You need to do bounding checks instead of int checks. Heres an example
    Code:
    if(num ==1){
     //only 1% chance
    }else if(num > 1 && num <= 3){
     //only 2% chance
    }else if(num > 3 && num <= 5){
     //only 2% chance
    }else if(num > 5 && num <= 10){
     //only 5% chance
    
    //ect.
     
  7. @Zombie_Striker
    Alright, so like this, starting from the top is common then as it goes down its less common.


    Code:
    if(num == 30)
            {
                    //Do nothing
            }
            else if(num > 20 && num <= 30)
            {
                   //give common
            }
            else if(num > 10 && num <= 20)
            {
                  //give uncommon
            }
            else if(num > 5 && num <= 10)
            {
                  //give rare
            }
            else if(num > 3 && num <= 5)
            {
                  //give epic
            }
            else if(num > 1 && num <= 3)
            {
                  //give legendary
            }
    Edit: just tried it and it gives everything more frequently then what i had before
     
  8. Offline

    Zombie_Striker

  9. @Zombie_Striker
    It doesn't work as i want, i want them to be less common, i get an item basically every other block break. I want everything rare, and the less percent the rarer it is. Like i want common to be common but not every other block break common, if that makes sense.
     
  10. Offline

    SantaClawz69

    Here is how I do my random percentages, might work better for you.

    Code:
    Rough code
    int aRandom=RandomUtils.nextInt(101);
    If(aRandom<=PERCENTAGE-VALUE){
    // drop block or whatever
    }
    OR you can do this
    
    private static Random random = new Random();
    double r = random.nextDouble();
    
    if(r <= 0.3){
    //Drop rare block
    } else if(r <=0.7) {
    drop common
    }
    
     
  11. @SantaClawz69
    Ive tried them both, messed around with the number and its still to common, im not sure really what to do.

    I'll make it more clear of what im trying to do.
    Everytime a block is broken i want to have it have a 1% chance to drop an item, a 3% to drop another, etc.
     
    Last edited: Aug 23, 2016
  12. Offline

    Zombie_Striker

    @Blackwing_Forged
    How do you know its too common? 30% means that there should be 1/3 successes.

    Here is a test. Use the following bit of code and add the command to show you the percentages:
    Code:
    public boolean onCommand(......){
    if(cmd.getName().equalsIgnoreCase("test")){
       double average = 0;
       int successes = 0;
      for(int i = 0; i < 100; i++){
        int test = random.nextInt(100);
        ave +=test;
        if(test <= 30) successes++;
      }
       sender.sendMessage("Average = "+(average/100)+" Number of successes = "+successes);
    }
    }
    What this should print out is that there should be roughly 30 successes, and that the average should be 30. If you want things to be rarer, than you have to edit the percentages.
     
  13. @Zombie_Striker
    I know its to common because every block i break i get an item, that's not supposed to happen. I want so if someone breaks a block there's a 1% chance to get an item and another item should be 5% to get, etc.

    and the average is never below 44.(random numbers here) and never above 54.(random numbers here)
     
  14. Offline

    kameronn

    lol maybe the problem is because its not adding up to 100%. If you have multiple percents make sure they add up to a certain "100" number for example they can add up to 700 but you'd have to check if them out of 700 instead of 100. If they add up to 200 so on and so on. Well to sum it up look no further because heres some code.
    Code:
                            double[] percentages = {80,80,80,80,80,80,20};
                            boolean chosen = false;
                            Random random = new Random();
                            double luck = 100.0;
                            while(!chosen){
                                for(double percent : percentages){
                                    luck = (random.nextDouble()) * 500;
                                    if(luck <= percent)
                                        chosen = true;
                                }
                            }
     
Thread Status:
Not open for further replies.

Share This Page