change the way iteration over trades works

This commit is contained in:
2024-08-18 11:41:21 +02:00
parent 90c678f760
commit 2564740f97
@@ -93,23 +93,22 @@ public final class MendingBeGone extends JavaPlugin implements Listener {
private void removeMendingTrade(Merchant merchant) { private void removeMendingTrade(Merchant merchant) {
List<MerchantRecipe> trades = new ArrayList<>(merchant.getRecipes()); List<MerchantRecipe> trades = new ArrayList<>(merchant.getRecipes());
List<Integer> toReplace = new ArrayList<>(); List<Integer> toReplace = new ArrayList<>();
int i = 0;
for (MerchantRecipe recipe : trades) { for (int i = 0; i < trades.size(); i++) {
MerchantRecipe recipe = trades.get(i);
if (!recipe.getResult().getType().equals(Material.ENCHANTED_BOOK)) continue; if (!recipe.getResult().getType().equals(Material.ENCHANTED_BOOK)) continue;
if (!(recipe.getResult().getItemMeta() instanceof EnchantmentStorageMeta)) continue;
EnchantmentStorageMeta storage = (EnchantmentStorageMeta) recipe.getResult().getItemMeta(); EnchantmentStorageMeta storage = (EnchantmentStorageMeta) recipe.getResult().getItemMeta();
if (storage.hasStoredEnchant(Enchantment.MENDING)) { if (!storage.hasStoredEnchant(Enchantment.MENDING)) continue;
toReplace.add(i); toReplace.add(i);
}
} }
if (toReplace.isEmpty()) return; if (toReplace.isEmpty()) return;
for (int index : toReplace) { for (int index : toReplace) {
MerchantRecipe oldTrade = trades.get(index); MerchantRecipe oldTrade = trades.get(index);
ItemStack result = oldTrade.getResult().clone(); ItemStack result = oldTrade.getResult();
EnchantmentStorageMeta storage = (EnchantmentStorageMeta) result.getItemMeta(); replaceMendingOnItem(result);
storage.removeStoredEnchant(Enchantment.MENDING);
storage.addStoredEnchant(Enchantment.DURABILITY, 3, true);
result.setItemMeta(storage);
MerchantRecipe newTrade = new MerchantRecipe( MerchantRecipe newTrade = new MerchantRecipe(
result, result,
oldTrade.getUses(), oldTrade.getUses(),