chances

Discussion in 'Spigot Plugin Development' started by TheFuzzyHead5, Apr 19, 2017.

  1. I've done a lot of research but fail to find anything proper.

    Let's say that my config is:

    Code (Text):
    1:
      command: 'say chance1'
      chance: 0.3
    2:
      command: 'say chance2'
      chance: 0.08
    3:
      command: 'say chance3'
      chance: 0.2
    How would I then make it so that upon, for example, a player toggling crouch, would trigger the command basd on the chances. It really messes with my head.
     
  2. I'd like to know first, if thats what you are trying to do:
    When the player toggles crouch, you generate a random number between 0 and 1. If the number is 0.3 or smaller, you execute the command "say chance 1", then, you generate a second number. If the number is 0.08 or smaller, you executes the command "say chance 2", and finally, you generates a third number, if the number is 0.2 or smaller, you executes the command "say chance 3".
     
  3. That's what I thought but the problem is that they are variable and could be changed as they are in the config file, therefore setting different values would mean that your idea would be inefficient
     
  4. Well, if you use the variables of your config in your code, theres no problem if they are changed. So you could do this with the help of this code snippet:
    Code (Text):
    @EventHandler
        public void onToggleSneak(PlayerToggleSneakEvent e)
        {
            double chance_1 = getConfig().getDouble("1.chance");
            double chance_2 = getConfig().getDouble("2.chance");
            double chance_3 = getConfig().getDouble("3.chance");

            Random r = new Random();
            double random_1 = r.nextDouble(); //<-- This returns a random double between 0 and 1
            double random_2 = r.nextDouble();
            double random_3 = r.nextDouble();


            if(chance_1 <= random_1)
            {
                e.getPlayer().performCommand(getConfig().getString("1.command"));
            }

            if(chance_2 <= random_2)
            {
                e.getPlayer().performCommand(getConfig().getString("2.command"));
            }

            if(chance_3 <= random_3)
            {
                e.getPlayer().performCommand(.getConfig().getString("3.command"));
            }
        }
    If that's what you meant it's not too difficult.
     
  5. Load the config into a Map<String, Double>.The string can be the command to be executed and the double would be the chance. Then you could iterate over keyset and check it's chance with the random.