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;
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);

View File

@ -15,7 +15,7 @@ import java.util.logging.Level;
public class FishingManager extends SpecificProfessionManager {
private final Set<FishingDropTable> 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<Condition> conditions = new HashSet<>();
private final List<FishingDropItem> 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");