Fixed an issue with fishing weights

This commit is contained in:
Indyuce 2022-03-05 12:04:11 +01:00
parent dca18c993d
commit c3f51a3520
2 changed files with 15 additions and 17 deletions

View File

@ -1,34 +1,31 @@
package net.Indyuce.mmocore.loot.droptable.dropitem.fishing; 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.apache.commons.lang.Validate;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
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;
public class FishingDropItem { public class FishingDropItem {
private final RandomAmount experience, tugs; private final RandomAmount experience, tugs;
private final DropItem dropItem; private final DropItem dropItem;
private final int weight;
public FishingDropItem(MMOLineConfig config) { public FishingDropItem(MMOLineConfig config) {
config.validate("tugs", "experience"); config.validate("tugs", "experience");
tugs = new RandomAmount(config.getString("tugs")); tugs = new RandomAmount(config.getString("tugs"));
experience = new RandomAmount(config.getString("experience")); 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); 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() { public int getWeight() {
return weight; return (int) Math.floor(getItem().getWeight());
} }
public DropItem getItem() { public DropItem getItem() {
@ -55,6 +52,7 @@ public class FishingDropItem {
return dropItem; return dropItem;
} }
@Nullable
public ItemStack collect(LootBuilder builder) { public ItemStack collect(LootBuilder builder) {
dropItem.collect(builder); dropItem.collect(builder);
return builder.getLoot().stream().findAny().orElse(null); return builder.getLoot().stream().findAny().orElse(null);

View File

@ -15,7 +15,7 @@ import java.util.logging.Level;
public class FishingManager extends SpecificProfessionManager { public class FishingManager extends SpecificProfessionManager {
private final Set<FishingDropTable> tables = new LinkedHashSet<>(); private final Set<FishingDropTable> tables = new LinkedHashSet<>();
private static final Random random = new Random(); private static final Random RANDOM = new Random();
public FishingManager() { public FishingManager() {
super("on-fish"); super("on-fish");
@ -44,7 +44,7 @@ public class FishingManager extends SpecificProfessionManager {
public static class FishingDropTable { public static class FishingDropTable {
private final Set<Condition> conditions = new HashSet<>(); private final Set<Condition> conditions = new HashSet<>();
private final List<FishingDropItem> items = new ArrayList<>(); private final List<FishingDropItem> items = new ArrayList<>();
private int maxWeight = 0; private double maxWeight = 0;
public FishingDropTable(ConfigurationSection section) { public FishingDropTable(ConfigurationSection section) {
Validate.notNull(section, "Could not load config"); Validate.notNull(section, "Could not load config");
@ -69,8 +69,8 @@ public class FishingManager extends SpecificProfessionManager {
for (String str : list) for (String str : list)
try { try {
FishingDropItem dropItem = new FishingDropItem(new MMOLineConfig(str)); FishingDropItem dropItem = new FishingDropItem(new MMOLineConfig(str));
maxWeight += dropItem.getWeight();
items.add(dropItem); items.add(dropItem);
maxWeight += dropItem.getItem().getWeight();
} catch (RuntimeException exception) { } catch (RuntimeException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING, MMOCore.plugin.getLogger().log(Level.WARNING,
"Could not load item '" + str + "' from fishing drop table '" + id + "': " + exception.getMessage()); "Could not load item '" + str + "' from fishing drop table '" + id + "': " + exception.getMessage());
@ -91,10 +91,10 @@ public class FishingManager extends SpecificProfessionManager {
} }
public FishingDropItem getRandomItem() { public FishingDropItem getRandomItem() {
int randomCoefficient = random.nextInt(maxWeight); double randomCoefficient = RANDOM.nextDouble() * maxWeight;
for (FishingDropItem item : items) for (FishingDropItem item : items)
if ((randomCoefficient -= item.getWeight()) <= 0) if ((randomCoefficient -= item.getItem().getWeight()) <= 0)
return item; return item;
throw new NullPointerException("Could not find item in drop table"); throw new NullPointerException("Could not find item in drop table");