Solved Zombie Chances (Solved Code Inside)

Discussion in 'Plugin Development' started by badboysteee98, Jun 1, 2014.

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

    badboysteee98

    Hello Bukkit,

    I need some help with my Custom Plugin, the thing I need it to do is when a Zombie dies it has a 2% chance of dropping a Diamond and 98% chance of dropping Rotten flesh. I'm not to sure how I would make the 100% and then check if it is equal to 2 and drop a Diamond and then else drop Rotten flesh

    Code:
    Code:
        boolean random(final float percent) {
            return Math.random() <= (100/100f);
        }
       
        @EventHandler
        public void onDeath(EntityDeathEvent e) {
            if(e.getEntity() instanceof LivingEntity) {
                if(random(1)) {
                    e.getDrops().set(1, new ItemStack(Material.DIAMOND, 1));
                }
            }
        }
    That's what I have at the moment but doesn't seem to work :/ Any help I'm grateful for :)
     
  2. Offline

    Tomasko

    Maybe random 100
    Code:java
    1. Random r = new Random();
    2. int num = r.nextInt(100+1);
    3. if(num == 1 || num == 2) {//give diamond}
    4. else {//give flesh}
    5.  
     
  3. Offline

    fireblast709

    Tomasko your code has 2/101 chance that it drops :p. It should be like the following:
    Code:java
    1. Random r = new Random();
    2. if(r.nextInt(100) < 2) // (2 out of 100 numbers, 0 and 1, will yield true -> 2%)
    3. {
    4. // Drop diamond
    5. }
     
  4. Offline

    AdamQpzm

    badboysteee98 Here is my version that I use for random chances, you could probably use this.

    PHP:
    public static boolean getRandomChance(int chance) {
        return new 
    Random().nextInt(100) + <= chance;
    }
     
  5. Offline

    badboysteee98

    fireblast709 Thanks for the help :0 Worked using your method
    AdamQpzm Never tired your method but I'm sure it works as your a good developer
    Tomasko Yours is good just does it out of 101 :p

    Solved Code:
    Code:java
    1. Random ran = new Random();
    2.  
    3. @EventHandler
    4. public void onDeath(EntityDeathEvent e) {
    5.  
    6. if(e.getEntity() instanceof Zombie) {
    7. if(ran.nextInt(100) <= 100) {
    8. e.getDrops().set(0, new ItemStack(Material.DIAMOND, 1));
    9. } else {
    10. e.getDrops().set(0, new ItemStack(Material.ROTTEN_FLESH, 2));
    11. }
    12. }
    13.  
    14. }

     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page