diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilListeners.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilListeners.java index cce81754..1d1fa03d 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilListeners.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilListeners.java @@ -3,7 +3,6 @@ package com.willfp.ecoenchants.enchantments.support.merging.anvil; import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.PluginDependent; import com.willfp.eco.core.proxy.ProxyConstants; -import com.willfp.eco.core.tuples.Pair; import com.willfp.eco.util.NumberUtils; import com.willfp.ecoenchants.proxy.proxies.OpenInventoryProxy; import com.willfp.ecoenchants.proxy.proxies.RepairCostProxy; @@ -83,19 +82,20 @@ public class AnvilListeners extends PluginDependent implements Listen name = ""; } - Pair newOut = AnvilMerge.doMerge(left, right, out, name, player); + AnvilResult newOut = AnvilMerge.doMerge(left, right, out, name, player); - if (newOut.getFirst() == null) { - newOut.setFirst(new ItemStack(Material.AIR, 0)); + if (newOut.result() == null) { + newOut = new AnvilResult(new ItemStack(Material.AIR, 0), newOut.xp()); } int modCost; - if (newOut.getSecond() == null) { + if (newOut.xp() == null) { modCost = 0; } else { - modCost = newOut.getSecond(); + modCost = newOut.xp(); } + AnvilResult finalNewOut = newOut; this.getPlugin().getScheduler().run(() -> { // This is a disgusting bodge @@ -112,7 +112,7 @@ public class AnvilListeners extends PluginDependent implements Listen // End pain int preCost = event.getInventory().getRepairCost(); - ItemStack item = newOut.getFirst(); + ItemStack item = finalNewOut.result(); if (event.getInventory().getItem(0) == null) { return; @@ -151,7 +151,6 @@ public class AnvilListeners extends PluginDependent implements Listen event.getInventory().setRepairCost(cost); event.setResult(item); event.getInventory().setItem(2, item); - player.updateInventory(); }); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilMerge.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilMerge.java index c181d032..bd0ef1ba 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilMerge.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilMerge.java @@ -1,7 +1,6 @@ package com.willfp.ecoenchants.enchantments.support.merging.anvil; import com.willfp.eco.core.fast.FastItemStack; -import com.willfp.eco.core.tuples.Pair; import com.willfp.eco.util.StringUtils; import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.enchantments.EcoEnchant; @@ -43,13 +42,13 @@ public class AnvilMerge { * @param old The previous {@link ItemStack} result. * @param itemName The anvil display name. * @param player The player merging (for permissions). - * @return The result, stored as a {@link Pair} of {@link ItemStack} and {@link Integer}. + * @return The result. */ - public Pair doMerge(@Nullable final ItemStack left, - @Nullable final ItemStack right, - @Nullable final ItemStack old, - @NotNull final String itemName, - @NotNull final Player player) { + public AnvilResult doMerge(@Nullable final ItemStack left, + @Nullable final ItemStack right, + @Nullable final ItemStack old, + @NotNull final String itemName, + @NotNull final Player player) { /* If you're currently looking at this code, pray to whatever god you have that any changes @@ -68,11 +67,11 @@ public class AnvilMerge { } if (left == null) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } if (left.getEnchantments().containsKey(EcoEnchants.PERMANENCE_CURSE)) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } name = name.replace("ยง", "&"); @@ -96,24 +95,24 @@ public class AnvilMerge { int leftDamage = ((Damageable) meta).getDamage(); if (outDamage >= leftDamage || outDamage == -1) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } else { ((Damageable) outMeta).setDamage(outDamage); } } else { - return new Pair<>(null, null); + return AnvilResult.FAIL; } if (right == null) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } } out.setItemMeta(outMeta); if (out.equals(left)) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } - return new Pair<>(out, 0); + return new AnvilResult(out, 0); } if (left.getItemMeta() instanceof Damageable && right.getItemMeta() instanceof EnchantmentStorageMeta) { @@ -121,11 +120,11 @@ public class AnvilMerge { } if (!left.getType().equals(right.getType()) && !(right.getItemMeta() instanceof EnchantmentStorageMeta)) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } if (left.getAmount() != right.getAmount()) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } Map outEnchants = new HashMap<>(); @@ -219,7 +218,7 @@ public class AnvilMerge { int leftDamage = ((Damageable) left.getItemMeta()).getDamage(); if (outDamage == leftDamage) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } } @@ -276,7 +275,7 @@ public class AnvilMerge { totalEnchantLevelDelta = Math.abs(outEnchantLevels.intValue() - inEnchantLevels.intValue()); if (output.equals(left)) { - return new Pair<>(null, null); + return AnvilResult.FAIL; } if (PLUGIN.getConfigYml().getBool("anvil.cost-exponent.enabled")) { @@ -292,6 +291,6 @@ public class AnvilMerge { } } - return new Pair<>(output, totalEnchantLevelDelta); + return new AnvilResult(output, totalEnchantLevelDelta); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilResult.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilResult.java new file mode 100644 index 00000000..c7f1a7a8 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/merging/anvil/AnvilResult.java @@ -0,0 +1,12 @@ +package com.willfp.ecoenchants.enchantments.support.merging.anvil; + +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.Nullable; + +public record AnvilResult(@Nullable ItemStack result, + @Nullable Integer xp) { + /** + * Fail result. + */ + public static final AnvilResult FAIL = new AnvilResult(null, null); +}