Solved How i create a probability in bukkit?

Discussion in 'Plugin Development' started by Gonmarte, Aug 29, 2015.

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

    Gonmarte

    Hi guys
    When i hit a player i would like that he has a chance to be poisonous (25%) and 75% of nothing happens.
    In my code when i hit a plyer he will be poisonous.
    So the question is how i create a probability?

    Code

    Code:
        @EventHandler
        public void  onHit(EntityDamageByEntityEvent event) {
           
            if (event.getDamager() instanceof Player) {
               
                Player damager = (Player) event.getDamager();
                   
                   player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 20*5, 1 );
               
            }
           
        }
    
    Thank you!
     
  2. Offline

    meguy26

    @Gonmarte
    Although not technically a bukkit question, as probability is more Java oriented, but the following method should work.
    Code:
        public static boolean percentChance(double percent){
            if(percent > 100 || percent < 0){
                throw new IllegalArgumentException("Percentage cannot be greater than 100 or less than 0!");
            }
            double result = new Random().nextDouble() * 100;
            return result <= percent;
        }
    Just input the percent (a number between 0 and 100), and receive a Boolean decider.
     
  3. Offline

    Gonmarte

    @meguy26
    I didnt understand at all...
    First we are check if the percent if more then 100 or less then 0 and we are saying then its impossible it happens.
    Then i didnt understand the variable double result = newRandom().nextDouble() * 100;
    return result <= result;
    What is the mean for this variable? why the number 100?
    Thank you!
     
  4. Offline

    MOMOTHEREAL

    @Gonmarte

    @meguy26's reply is a method that will return "true" based on the percentage given in the argument.
    For example, if you put 75.0 as the argument:
    - the method "new Random().nextDouble()" returns a double value between 0.0 and 1.0, hence the need to multiply the value by 100. "result" will have a random value between 0.0 and 100.0.
    - then check if the result (random value) is lower or equal than the given percentage of probability.

    That sums it up.
     
  5. Offline

    meguy26

    @Gonmarte
    Activating commented code operation:
    Code:
       public static boolean percentChance(double percent){
            if(percent > 100 || percent < 0){ //If the percent is greater than 100, than its always true, if its less than 0, than its always false, this check can be removed if you wish
                throw new IllegalArgumentException("Percentage cannot be greater than 100 or less than 0!");
            }
            double result = new Random().nextDouble() * 100; //Random#nextDouble() generates a double number between 0 and 1, then multiply that by 100 to get a nice percentage
                                //because percentages can be converted from decimals.
            return result <= percent; //You're inputed percent chance (75 for example), will return true when the 'result' is less than or equal to it (75).
                    //This may not look like a probability calculation, but it is. Because the random number generated can be ANYWHERE between 0 and 100,
                    //it will be less than or equal to 75, 75% of the time.
        }
    EDIT:
    Yeah what @MOMOTHEREAL said
     
    MOMOTHEREAL likes this.
  6. Offline

    Gonmarte

    @meguy26
    @meguy26 @MOMOTHEREAL
    Thank you both =)
    I tested the "test plugin" just to see if work and i doesnt work ;/
    What is the error?
    Code:
    public static boolean percentChance(double percent) {
           
            if(percent > 100 || percent < 100) {
               
                throw new IllegalArgumentException("A percentagem nao pode ser maior do que 100 nem menor do que 0");
               
               
               
            }
            double result = new Random().nextDouble() * 100;
           
            return result <= percent;
           
        }
       
        @EventHandler
       
        public void onPlayerHit(EntityDamageByEntityEvent event) {
           
            if(event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
               
                Player damager = (Player) event.getDamager();
               
                if(percentChance(75)) {
                   
                    damager.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20*5, 2));
                   
                }
               
            }
           
        }
    
    
     
  7. Offline

    meguy26

    @Gonmarte
    What exactly doesn't work? Can we see your main class?
     
  8. Offline

    MOMOTHEREAL

    @Gonmarte
    Are you sure you have registered your Listener class? Try adding some debug lines to see what's not working right.
     
  9. Offline

    Gonmarte

    @meguy26 Its just that but here we go :
    Code:
    package me.gonmarte;
    
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Main extends JavaPlugin implements Listener{
      
        @Override
        public void onEnable() {
          
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
      
        @Override
        public void onDisable() {
          
          
        }
      
      
        public static boolean percentChance(double percent) {
          
            if(percent > 100 || percent < 100) {
              
                throw new IllegalArgumentException("A percentagem nao pode ser maior do que 100 nem menor do que 0");
              
              
              
            }
            double result = new Random().nextDouble() * 100;
          
            return result <= percent;
          
        }
      
        @EventHandler
      
        public void onPlayerHit(EntityDamageByEntityEvent event) {
          
            if(event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
              
                Player damager = (Player) event.getDamager();
              
                if(percentChance(75)) {
                  
                    damager.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20*5, 2));
                  
                }
              
            }
          
        }
    }
    
    
    
    @MOMOTHEREAL Look u.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  10. Offline

    MOMOTHEREAL

    @Gonmarte
    Try adding some debug just in case you are checking something wrong:
    Code:
    @EventHandler
     
        public void onPlayerHit(EntityDamageByEntityEvent event) {
          getLogger().info("An entity was damaged by another entity...");
            if(event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
                getLogger().info("A player was damaged by another player...");
                Player damager = (Player) event.getDamager();
                boolean speed = percentChance(75.0);           
    
                if(speed) {
                    getLogger().info("The probability was met (75%)");
                    damager.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20*5, 2));
                 
                }
            }
         
        }
     
  11. Offline

    Gonmarte

    @MOMOTHEREAL I tried ur code and ran a HUGEEEEEEEEE error in the console, but i cant copy it. There is anyway to get it coding?
     
  12. Offline

    Zombie_Striker

    @Gonmarte
    What do you mean "can't copy it". Just highlight over the text in the output log, right click, and click "copy". Should be as simple as that.
     
  13. Offline

    meguy26

    @Gonmarte
    You can copy the error by going into the 'logs' folder and finding the one with the error
     
    Zombie_Striker likes this.
  14. Offline

    mythbusterma

    @MOMOTHEREAL @Zombie_Striker

    *slams my face into the desk*
     
    Gonmarte and teej107 like this.
  15. Offline

    Gonmarte

  16. Offline

    mythbusterma

    Let's think about our code before we write it.
     
  17. Offline

    Gonmarte

    Last edited by a moderator: Aug 29, 2015
  18. Offline

    MOMOTHEREAL

    @mythbusterma Darnit, I assumed he copy-pasted the code he was spoonfed... you know, people seem to do that here, usually... ;)
     
Thread Status:
Not open for further replies.

Share This Page