modified fishing drop item loading

This commit is contained in:
Jules 2022-01-31 20:36:56 +01:00
parent d4f1477bea
commit 62999f1c61
2 changed files with 6 additions and 11 deletions

View File

@ -15,9 +15,7 @@ public class FishingDropItem {
private final int weight; private final int weight;
public FishingDropItem(String value) { public FishingDropItem(MMOLineConfig config) {
MMOLineConfig config = new MMOLineConfig(value);
config.validate("tugs", "experience"); config.validate("tugs", "experience");
tugs = new RandomAmount(config.getString("tugs")); tugs = new RandomAmount(config.getString("tugs"));

View File

@ -68,10 +68,10 @@ public class FishingManager extends SpecificProfessionManager {
for (String str : list) for (String str : list)
try { try {
FishingDropItem dropItem = new FishingDropItem(str); FishingDropItem dropItem = new FishingDropItem(new MMOLineConfig(str));
maxWeight += dropItem.getWeight(); maxWeight += dropItem.getWeight();
items.add(dropItem); items.add(dropItem);
} catch (IllegalArgumentException | IndexOutOfBoundsException 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,14 +91,11 @@ public class FishingManager extends SpecificProfessionManager {
} }
public FishingDropItem getRandomItem() { public FishingDropItem getRandomItem() {
int weight = random.nextInt(maxWeight); int randomCoefficient = random.nextInt(maxWeight);
for (FishingDropItem item : items) { for (FishingDropItem item : items)
weight -= item.getWeight(); if ((randomCoefficient -= item.getWeight()) <= 0)
if (weight <= 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");
} }