RandomChanceUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.gmail.nossr50.util.random;
  2. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  3. import com.gmail.nossr50.util.skills.SkillUtils;
  4. import org.bukkit.entity.Player;
  5. import org.jetbrains.annotations.NotNull;
  6. import java.text.DecimalFormat;
  7. import java.util.concurrent.ThreadLocalRandom;
  8. //TODO: Normalize chance values
  9. //TODO: Update calls to this class and its members
  10. public class RandomChanceUtil {
  11. public static final @NotNull DecimalFormat percent = new DecimalFormat("##0.00%");
  12. public static final double LUCKY_MODIFIER = 1.333D;
  13. /**
  14. * Simulate an outcome on a probability and return true or false for the result of that outcome
  15. *
  16. * @param probability target probability
  17. * @return true if the probability succeeded, false if it failed
  18. */
  19. public static boolean processProbability(@NotNull Probability probability) {
  20. return isSuccessfulRoll(probability.getValue());
  21. }
  22. /**
  23. * Modify and then Simulate an outcome on a probability and return true or false for the result of that outcome
  24. *
  25. * @param probability target probability
  26. * @param probabilityMultiplier probability will be multiplied by this before success is checked
  27. * @return true if the probability succeeded, false if it failed
  28. */
  29. public static boolean processProbability(@NotNull Probability probability, double probabilityMultiplier) {
  30. double probabilityValue = probability.getValue() * probabilityMultiplier;
  31. return isSuccessfulRoll(probabilityValue);
  32. }
  33. /**
  34. * Simulates a "roll of the dice"
  35. * If the value passed is higher than the "random" value, than it is a successful roll
  36. *
  37. * @param probabilityValue probability value
  38. * @return true for succeeding, false for failing
  39. */
  40. private static boolean isSuccessfulRoll(double probabilityValue) {
  41. return probabilityValue >= ThreadLocalRandom.current().nextDouble(1.0D);
  42. }
  43. /**
  44. * Return a chance of success in "percentage" format, show to the player in UI elements
  45. *
  46. * @param player target player
  47. * @param subSkillType target subskill
  48. * @param isLucky whether or not to apply luck modifiers
  49. *
  50. * @return "percentage" representation of success
  51. */
  52. public static double chanceOfSuccessPercentage(@NotNull Player player, @NotNull SubSkillType subSkillType, boolean isLucky) {
  53. Probability probability = SkillUtils.getSubSkillProbability(subSkillType, player);
  54. //Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
  55. double percentageValue = probability.getValue() * 100;
  56. //Apply lucky modifier
  57. if(isLucky) {
  58. percentageValue *= LUCKY_MODIFIER;
  59. }
  60. return percentageValue;
  61. }
  62. public static double chanceOfSuccessPercentage(@NotNull Probability probability, boolean isLucky) {
  63. //Probability values are on a 0-1 scale and need to be "transformed" into a 1-100 scale
  64. double percentageValue = probability.getValue() * 100;
  65. //Apply lucky modifier
  66. if(isLucky) {
  67. percentageValue *= LUCKY_MODIFIER;
  68. }
  69. return percentageValue;
  70. }
  71. }