Skip to content

Commit

Permalink
Add config option to use different random
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Feb 23, 2024
1 parent 024bef3 commit ba02fba
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
Expand Up @@ -45,6 +45,9 @@ public void registerComments(CommentsConfiguration conf) {
@Comment("Whether you want CrazyCrates to shut up or not, This option is ignored by errors.")
public static final Property<Boolean> verbose_logging = newProperty("root.verbose_logging", true);

@Comment("This option will let you test a different way of picking random numbers. If you have any issues, You can set it back to false.")
public static final Property<Boolean> use_different_random = newProperty("root.use-different-random", false);

@Comment({
"Sends anonymous statistics about how the plugin is used to bstats.org.",
"bstats is a service for plugin developers to find out how the plugin being used,",
Expand Down
@@ -1,6 +1,7 @@
package com.badbones69.crazycrates.api;

import com.badbones69.crazycrates.api.objects.Tier;
import com.badbones69.crazycrates.common.config.types.ConfigKeys;
import org.apache.commons.lang.WordUtils;
import com.badbones69.crazycrates.CrazyCrates;
import com.badbones69.crazycrates.api.events.PlayerPrizeEvent;
Expand All @@ -18,6 +19,8 @@
import com.badbones69.crazycrates.api.utils.MsgUtils;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import static java.util.regex.Matcher.quoteReplacement;

public class PrizeManager {
Expand Down Expand Up @@ -177,7 +180,8 @@ public static Tier getTier(Crate crate) {
for (int stopLoop = 0; stopLoop <= 100; stopLoop++) {
for (Tier tier : crate.getTiers()) {
int chance = tier.getChance();
int num = new Random().nextInt(tier.getMaxRange());

int num = MiscUtils.useOtherRandom() ? ThreadLocalRandom.current().nextInt(tier.getMaxRange()) : new Random().nextInt(tier.getMaxRange());

if (num >= 1 && num <= chance) {
return tier;
Expand Down
Expand Up @@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;

public class Crate {
Expand Down Expand Up @@ -263,7 +264,7 @@ public Prize pickPrize(Player player) {
chanceCheck(prizes, usablePrizes);

try {
return prizes.get(new Random().nextInt(prizes.size()));
return prizes.get(MiscUtils.useOtherRandom() ? ThreadLocalRandom.current().nextInt(prizes.size()) : new Random().nextInt(prizes.size()));
} catch (IllegalArgumentException exception) {
this.plugin.getLogger().log(Level.WARNING, "Failed to find prize from the " + name + " crate for player " + player.getName() + ".", exception);
return null;
Expand All @@ -284,7 +285,7 @@ private void chanceCheck(List<Prize> prizes, List<Prize> usablePrizes) {
int num;

for (int counter = 1; counter <= 1; counter++) {
num = 1 + new Random().nextInt(max);
num = MiscUtils.useOtherRandom() ? 1 + ThreadLocalRandom.current().nextInt(max) : 1 + new Random().nextInt(max);

if (num <= chance) prizes.add(prize);
}
Expand Down Expand Up @@ -347,7 +348,7 @@ public Prize pickPrize(Player player, Tier tier) {
// ================= Chance Check ================= //
chanceCheck(prizes, usablePrizes);

return prizes.get(new Random().nextInt(prizes.size()));
return prizes.get(MiscUtils.useOtherRandom() ? ThreadLocalRandom.current().nextInt(prizes.size()) : new Random().nextInt(prizes.size()));
}

/**
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.objects.other.ItemBuilder;
import com.badbones69.crazycrates.common.config.types.ConfigKeys;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Location;
Expand All @@ -10,7 +11,6 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -182,7 +182,7 @@ public static long pickNumber(long min, long max) {
}

public static int randomNumber(int min, int max) {
return min + new Random().nextInt(max - min);
return MiscUtils.useOtherRandom() ? min + ThreadLocalRandom.current().nextInt(max - min) : min + new Random().nextInt(max - min);
}

public static Enchantment getEnchantment(String enchantmentName) {
Expand Down Expand Up @@ -300,4 +300,8 @@ public static List<Integer> slowSpin(int full, int cut) {

return slow;
}

public static boolean useOtherRandom() {
return plugin.getConfigManager().getConfig().getProperty(ConfigKeys.use_different_random);
}
}

0 comments on commit ba02fba

Please sign in to comment.