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.
This commit is contained in:
Paldiu 2022-06-15 08:28:53 -05:00
parent 400687733f
commit 0b07bd9da2

View File

@ -4,10 +4,16 @@ import io.github.simplex.api.LuckContainer;
import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.FeelingLucky;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; 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") @SuppressWarnings("all")
public class Luck implements LuckContainer { public class Luck implements LuckContainer {
@ -34,10 +40,16 @@ public class Luck implements LuckContainer {
event = new PlayerLuckChangeEvent(this); 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, @Contract(pure = true,
value = "-> new") value = "-> new")
public static @NotNull SplittableRandom RNG() { public static @NotNull SecureRandom RNG() {
return new SplittableRandom(); return new SecureRandom(SecureRandom.getSeed(20));
} }
public static boolean quickRNGnoMultiplier(double value) { public static boolean quickRNGnoMultiplier(double value) {
@ -53,6 +65,10 @@ public class Luck implements LuckContainer {
return (value >= actual); return (value >= actual);
} }
public boolean playerHasLuckPE() {
return player.hasPotionEffect(PotionEffectType.LUCK);
}
public FeelingLucky getPlugin() { public FeelingLucky getPlugin() {
return plugin; return plugin;
} }
@ -89,6 +105,12 @@ public class Luck implements LuckContainer {
return player; 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) { public boolean quickRNG(double value) {
double rng; double rng;
if (value >= 1024.0) { if (value >= 1024.0) {
@ -97,13 +119,19 @@ public class Luck implements LuckContainer {
rng = RNG().nextDouble(0.0, 1024.0); rng = RNG().nextDouble(0.0, 1024.0);
} }
AtomicReference<Double> multiplier = new AtomicReference<>(multiplier());
double actual = Math.round((rng / 1024) * 100); double actual = Math.round((rng / 1024) * 100);
double newVal = Math.round((value / 1024) * 100);
if (multiplier() > 1.0) { if (playerHasLuckPE()) {
return ((value * multiplier()) >= actual); 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() { public void reset() {