Prevents an exception caused by enchantment start level being higher than enchantment max level

This commit is contained in:
Kristian Knarvik 2022-07-11 17:35:47 +02:00
parent 348a404066
commit b16bb933e2

View File

@ -93,7 +93,12 @@ class ReforgeSession implements Runnable {
if (roll < config.getExtraEnchantmentChance() && itemToReforge.getEnchantments().keySet().size() < config.getMaxEnchantments()) {
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.fromString(enchantments[random.nextInt(enchantments.length)]));
if (Objects.requireNonNull(enchantment).canEnchantItem(itemToReforge)) {
itemToReforge.addEnchantment(enchantment, random.nextInt(enchantment.getMaxLevel() - enchantment.getStartLevel()) + enchantment.getStartLevel());
int randomBound = enchantment.getMaxLevel() - enchantment.getStartLevel();
//A workaround for the random method's bound sometimes being negative
if (randomBound >= 0) {
itemToReforge.addEnchantment(enchantment, random.nextInt(randomBound) +
enchantment.getStartLevel());
}
}
}