From a1c92898ea7350a2ac55da7122495c61c65e9aef Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 31 Dec 2020 13:54:08 +0000 Subject: [PATCH] Fixed random number generation --- .../support/obtaining/EnchantingListeners.java | 5 ++--- .../main/java/com/willfp/eco/util/NumberUtils.java | 11 +++-------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/obtaining/EnchantingListeners.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/obtaining/EnchantingListeners.java index 442e9b42..e5eb7e9f 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/obtaining/EnchantingListeners.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/obtaining/EnchantingListeners.java @@ -221,8 +221,7 @@ public class EnchantingListeners extends PluginDependent implements Listener { try { event.getOffers()[2].setCost(NumberUtils.equalIfOver(event.getOffers()[2].getCost(), maxLevel)); - } catch (ArrayIndexOutOfBoundsException | NullPointerException ignored) { - } + } catch (ArrayIndexOutOfBoundsException | NullPointerException ignored) { } if (!SECONDARY_ENCHANTABLE.contains(event.getItem().getType())) { return; @@ -236,7 +235,7 @@ public class EnchantingListeners extends PluginDependent implements Listener { bonus = 1; } - double baseLevel = (NumberUtils.randInt(1, 8) + Math.floor((double) bonus / 2) + NumberUtils.randInt(0, bonus)); + double baseLevel = NumberUtils.randInt(1, 8) + Math.floor((double) bonus / 2) + NumberUtils.randInt(0, bonus); int bottomEnchantLevel = (int) Math.ceil(Math.max(baseLevel / 3, 1)); int midEnchantLevel = (int) ((baseLevel * 2) / 3) + 1; diff --git a/eco-util/src/main/java/com/willfp/eco/util/NumberUtils.java b/eco-util/src/main/java/com/willfp/eco/util/NumberUtils.java index e154abec..23e57a75 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/NumberUtils.java +++ b/eco-util/src/main/java/com/willfp/eco/util/NumberUtils.java @@ -3,16 +3,11 @@ package com.willfp.eco.util; import lombok.experimental.UtilityClass; import java.text.DecimalFormat; -import java.util.Random; import java.util.TreeMap; +import java.util.concurrent.ThreadLocalRandom; @UtilityClass public class NumberUtils { - /** - * The RNG to use. - */ - private static final Random RANDOM = new Random(); - /** * Set of roman numerals to look up. */ @@ -99,7 +94,7 @@ public class NumberUtils { */ public int randInt(final int min, final int max) { - return (int) ((long) min + RANDOM.nextInt() * ((long) max - min + 1)); + return ThreadLocalRandom.current().nextInt(min, max + 1); } /** @@ -111,7 +106,7 @@ public class NumberUtils { */ public double randFloat(final double min, final double max) { - return RANDOM.nextFloat() * (max - min) + min; + return ThreadLocalRandom.current().nextDouble(min, max); } /**