diff --git a/src/main/java/net/Indyuce/mmocore/loot/droptable/dropitem/fishing/FishingDropItem.java b/src/main/java/net/Indyuce/mmocore/loot/droptable/dropitem/fishing/FishingDropItem.java index b1754716..7762aa7b 100644 --- a/src/main/java/net/Indyuce/mmocore/loot/droptable/dropitem/fishing/FishingDropItem.java +++ b/src/main/java/net/Indyuce/mmocore/loot/droptable/dropitem/fishing/FishingDropItem.java @@ -1,34 +1,31 @@ package net.Indyuce.mmocore.loot.droptable.dropitem.fishing; +import io.lumine.mythic.lib.api.MMOLineConfig; +import net.Indyuce.mmocore.MMOCore; +import net.Indyuce.mmocore.api.util.math.formula.RandomAmount; +import net.Indyuce.mmocore.loot.LootBuilder; +import net.Indyuce.mmocore.loot.droptable.dropitem.DropItem; import org.apache.commons.lang.Validate; import org.bukkit.inventory.ItemStack; - -import net.Indyuce.mmocore.MMOCore; -import net.Indyuce.mmocore.loot.droptable.dropitem.DropItem; -import net.Indyuce.mmocore.loot.LootBuilder; -import net.Indyuce.mmocore.api.util.math.formula.RandomAmount; -import io.lumine.mythic.lib.api.MMOLineConfig; +import org.jetbrains.annotations.Nullable; public class FishingDropItem { private final RandomAmount experience, tugs; private final DropItem dropItem; - private final int weight; - public FishingDropItem(MMOLineConfig config) { config.validate("tugs", "experience"); tugs = new RandomAmount(config.getString("tugs")); experience = new RandomAmount(config.getString("experience")); - weight = config.getInt("weight", 1); - Validate.isTrue(weight > 0, "A fishing drop table item cannot have 0 weight"); - dropItem = MMOCore.plugin.loadManager.loadDropItem(config); + Validate.isTrue(dropItem.getWeight() > 0, "A fishing drop table item must have a strictly positive weight"); } + @Deprecated public int getWeight() { - return weight; + return (int) Math.floor(getItem().getWeight()); } public DropItem getItem() { @@ -55,6 +52,7 @@ public class FishingDropItem { return dropItem; } + @Nullable public ItemStack collect(LootBuilder builder) { dropItem.collect(builder); return builder.getLoot().stream().findAny().orElse(null); diff --git a/src/main/java/net/Indyuce/mmocore/manager/profession/FishingManager.java b/src/main/java/net/Indyuce/mmocore/manager/profession/FishingManager.java index 4c400de1..aa9e1738 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/profession/FishingManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/profession/FishingManager.java @@ -15,7 +15,7 @@ import java.util.logging.Level; public class FishingManager extends SpecificProfessionManager { private final Set tables = new LinkedHashSet<>(); - private static final Random random = new Random(); + private static final Random RANDOM = new Random(); public FishingManager() { super("on-fish"); @@ -44,7 +44,7 @@ public class FishingManager extends SpecificProfessionManager { public static class FishingDropTable { private final Set conditions = new HashSet<>(); private final List items = new ArrayList<>(); - private int maxWeight = 0; + private double maxWeight = 0; public FishingDropTable(ConfigurationSection section) { Validate.notNull(section, "Could not load config"); @@ -69,8 +69,8 @@ public class FishingManager extends SpecificProfessionManager { for (String str : list) try { FishingDropItem dropItem = new FishingDropItem(new MMOLineConfig(str)); - maxWeight += dropItem.getWeight(); items.add(dropItem); + maxWeight += dropItem.getItem().getWeight(); } catch (RuntimeException exception) { MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load item '" + str + "' from fishing drop table '" + id + "': " + exception.getMessage()); @@ -91,10 +91,10 @@ public class FishingManager extends SpecificProfessionManager { } public FishingDropItem getRandomItem() { - int randomCoefficient = random.nextInt(maxWeight); + double randomCoefficient = RANDOM.nextDouble() * maxWeight; for (FishingDropItem item : items) - if ((randomCoefficient -= item.getWeight()) <= 0) + if ((randomCoefficient -= item.getItem().getWeight()) <= 0) return item; throw new NullPointerException("Could not find item in drop table");