From 0b07bd9da276d8b9a16ec14ee7fcc37a57b2082b Mon Sep 17 00:00:00 2001 From: Paldiu Date: Wed, 15 Jun 2022 08:28:53 -0500 Subject: [PATCH] Minor Update 1.2.1 Adjusted the Luck class to reflect: - SplittableRandom has been replaced with SecureRandom in favor of entropy-based pseudorandom calculations compared to pseudorandom calculations based off the system time. - Adjusted the quickRNG to factor in whether the user has the luck potion effect, and to just apply the multiplier regardless of whether it is the default value. - Also adjusted the values, as the original value still remained at 1024, whereas the randomized number criteria was a percentage of 100. Both the input value and the randomized number criteria now are percentages of 100, based off a total of 1024 possible points. --- .../io/github/simplex/luck/player/Luck.java | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/simplex/luck/player/Luck.java b/src/main/java/io/github/simplex/luck/player/Luck.java index 44988d1..030b9b6 100644 --- a/src/main/java/io/github/simplex/luck/player/Luck.java +++ b/src/main/java/io/github/simplex/luck/player/Luck.java @@ -4,10 +4,16 @@ import io.github.simplex.api.LuckContainer; import io.github.simplex.luck.FeelingLucky; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffectType; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; -import java.util.*; +import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("all") public class Luck implements LuckContainer { @@ -34,10 +40,16 @@ public class Luck implements LuckContainer { event = new PlayerLuckChangeEvent(this); } + /** + * This creates a new instance of a pseudorandom number generator based off entropy provided by the operating system. + * This will allow for a much purer randomization, due to entropy being different for each call. + * + * @return A new instance of SecureRandom. Each time this method is called a new instance is created to provide maximum variation with entropic calculations. + */ @Contract(pure = true, value = "-> new") - public static @NotNull SplittableRandom RNG() { - return new SplittableRandom(); + public static @NotNull SecureRandom RNG() { + return new SecureRandom(SecureRandom.getSeed(20)); } public static boolean quickRNGnoMultiplier(double value) { @@ -53,6 +65,10 @@ public class Luck implements LuckContainer { return (value >= actual); } + public boolean playerHasLuckPE() { + return player.hasPotionEffect(PotionEffectType.LUCK); + } + public FeelingLucky getPlugin() { return plugin; } @@ -89,6 +105,12 @@ public class Luck implements LuckContainer { return player; } + /** + * Quickly calculate whether or not the player has enough luck to trigger the condition. + * + * @param value The players luck value. + * @return True if the player meets the criteria, false if they do not. + */ public boolean quickRNG(double value) { double rng; if (value >= 1024.0) { @@ -97,13 +119,19 @@ public class Luck implements LuckContainer { rng = RNG().nextDouble(0.0, 1024.0); } + AtomicReference multiplier = new AtomicReference<>(multiplier()); double actual = Math.round((rng / 1024) * 100); + double newVal = Math.round((value / 1024) * 100); - if (multiplier() > 1.0) { - return ((value * multiplier()) >= actual); + if (playerHasLuckPE()) { + player.getActivePotionEffects() + .stream() + .filter(p -> p.getType().equals(PotionEffectType.LUCK)) + .findFirst() + .ifPresent(p -> multiplier.updateAndGet(v -> new Double((double) (v + p.getAmplifier())))); } - return (value >= actual); + return ((newVal * multiplier.get()) >= actual); } public void reset() {