From 93377b6027b56a50038e2d67e19b7f54b1d2caea Mon Sep 17 00:00:00 2001 From: Indyuce Date: Mon, 25 Apr 2022 21:09:01 +0200 Subject: [PATCH] Fixed chest tier chance rolling --- .../Indyuce/mmocore/loot/chest/ChestTier.java | 7 +++++-- .../mmocore/loot/chest/LootChestRegion.java | 19 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/loot/chest/ChestTier.java b/src/main/java/net/Indyuce/mmocore/loot/chest/ChestTier.java index 770c7986..de9ea2a3 100644 --- a/src/main/java/net/Indyuce/mmocore/loot/chest/ChestTier.java +++ b/src/main/java/net/Indyuce/mmocore/loot/chest/ChestTier.java @@ -10,8 +10,7 @@ public class ChestTier { private final TierEffect effect; private final ScalingFormula capacity; private final DropTable table; - - public final double chance; + private final double chance; public ChestTier(ConfigurationSection config) { effect = config.isConfigurationSection("effect") ? new TierEffect(config.getConfigurationSection("effect")) : null; @@ -24,6 +23,10 @@ public class ChestTier { return capacity.calculate(player.getLevel()); } + public double getChance() { + return chance; + } + public DropTable getDropTable() { return table; } diff --git a/src/main/java/net/Indyuce/mmocore/loot/chest/LootChestRegion.java b/src/main/java/net/Indyuce/mmocore/loot/chest/LootChestRegion.java index ef7e8217..c234d155 100644 --- a/src/main/java/net/Indyuce/mmocore/loot/chest/LootChestRegion.java +++ b/src/main/java/net/Indyuce/mmocore/loot/chest/LootChestRegion.java @@ -119,14 +119,19 @@ public class LootChestRegion { // TODO stat to increase chance to get higher tiers? public ChestTier rollTier() { - double s = 0; - for (ChestTier tier : tiers) { - if (random.nextDouble() < tier.chance / (1 - s)) - return tier; - s += tier.chance; - } + // Calculate sum of all chances and then normalize + double norm = 0; + for (ChestTier tier : tiers) + norm += tier.getChance(); - return tiers.stream().findAny().orElse(null); + Validate.isTrue(norm > 0, "No tier was found"); + + double sum = 0; + for (ChestTier tier : tiers) + if (random.nextDouble() < (sum += tier.getChance()) / norm) + return tier; + + throw new RuntimeException("Could not roll random chest tier"); } public Location getRandomLocation(Location center) {