Minor anvil changes. I may regret this

This commit is contained in:
Auxilor 2021-08-12 22:50:47 +01:00
parent 79e42715e3
commit c3a8fea3fd
3 changed files with 37 additions and 27 deletions

View File

@ -3,7 +3,6 @@ package com.willfp.ecoenchants.enchantments.support.merging.anvil;
import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.PluginDependent; import com.willfp.eco.core.PluginDependent;
import com.willfp.eco.core.proxy.ProxyConstants; import com.willfp.eco.core.proxy.ProxyConstants;
import com.willfp.eco.core.tuples.Pair;
import com.willfp.eco.util.NumberUtils; import com.willfp.eco.util.NumberUtils;
import com.willfp.ecoenchants.proxy.proxies.OpenInventoryProxy; import com.willfp.ecoenchants.proxy.proxies.OpenInventoryProxy;
import com.willfp.ecoenchants.proxy.proxies.RepairCostProxy; import com.willfp.ecoenchants.proxy.proxies.RepairCostProxy;
@ -83,19 +82,20 @@ public class AnvilListeners extends PluginDependent<EcoPlugin> implements Listen
name = ""; name = "";
} }
Pair<ItemStack, Integer> newOut = AnvilMerge.doMerge(left, right, out, name, player); AnvilResult newOut = AnvilMerge.doMerge(left, right, out, name, player);
if (newOut.getFirst() == null) { if (newOut.result() == null) {
newOut.setFirst(new ItemStack(Material.AIR, 0)); newOut = new AnvilResult(new ItemStack(Material.AIR, 0), newOut.xp());
} }
int modCost; int modCost;
if (newOut.getSecond() == null) { if (newOut.xp() == null) {
modCost = 0; modCost = 0;
} else { } else {
modCost = newOut.getSecond(); modCost = newOut.xp();
} }
AnvilResult finalNewOut = newOut;
this.getPlugin().getScheduler().run(() -> { this.getPlugin().getScheduler().run(() -> {
// This is a disgusting bodge // This is a disgusting bodge
@ -112,7 +112,7 @@ public class AnvilListeners extends PluginDependent<EcoPlugin> implements Listen
// End pain // End pain
int preCost = event.getInventory().getRepairCost(); int preCost = event.getInventory().getRepairCost();
ItemStack item = newOut.getFirst(); ItemStack item = finalNewOut.result();
if (event.getInventory().getItem(0) == null) { if (event.getInventory().getItem(0) == null) {
return; return;
@ -151,7 +151,6 @@ public class AnvilListeners extends PluginDependent<EcoPlugin> implements Listen
event.getInventory().setRepairCost(cost); event.getInventory().setRepairCost(cost);
event.setResult(item); event.setResult(item);
event.getInventory().setItem(2, item); event.getInventory().setItem(2, item);
player.updateInventory();
}); });
} }
} }

View File

@ -1,7 +1,6 @@
package com.willfp.ecoenchants.enchantments.support.merging.anvil; package com.willfp.ecoenchants.enchantments.support.merging.anvil;
import com.willfp.eco.core.fast.FastItemStack; import com.willfp.eco.core.fast.FastItemStack;
import com.willfp.eco.core.tuples.Pair;
import com.willfp.eco.util.StringUtils; import com.willfp.eco.util.StringUtils;
import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchant;
@ -43,13 +42,13 @@ public class AnvilMerge {
* @param old The previous {@link ItemStack} result. * @param old The previous {@link ItemStack} result.
* @param itemName The anvil display name. * @param itemName The anvil display name.
* @param player The player merging (for permissions). * @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<ItemStack, Integer> doMerge(@Nullable final ItemStack left, public AnvilResult doMerge(@Nullable final ItemStack left,
@Nullable final ItemStack right, @Nullable final ItemStack right,
@Nullable final ItemStack old, @Nullable final ItemStack old,
@NotNull final String itemName, @NotNull final String itemName,
@NotNull final Player player) { @NotNull final Player player) {
/* /*
If you're currently looking at this code, If you're currently looking at this code,
pray to whatever god you have that any changes pray to whatever god you have that any changes
@ -68,11 +67,11 @@ public class AnvilMerge {
} }
if (left == null) { if (left == null) {
return new Pair<>(null, null); return AnvilResult.FAIL;
} }
if (left.getEnchantments().containsKey(EcoEnchants.PERMANENCE_CURSE)) { if (left.getEnchantments().containsKey(EcoEnchants.PERMANENCE_CURSE)) {
return new Pair<>(null, null); return AnvilResult.FAIL;
} }
name = name.replace("§", "&"); name = name.replace("§", "&");
@ -96,24 +95,24 @@ public class AnvilMerge {
int leftDamage = ((Damageable) meta).getDamage(); int leftDamage = ((Damageable) meta).getDamage();
if (outDamage >= leftDamage || outDamage == -1) { if (outDamage >= leftDamage || outDamage == -1) {
return new Pair<>(null, null); return AnvilResult.FAIL;
} else { } else {
((Damageable) outMeta).setDamage(outDamage); ((Damageable) outMeta).setDamage(outDamage);
} }
} else { } else {
return new Pair<>(null, null); return AnvilResult.FAIL;
} }
if (right == null) { if (right == null) {
return new Pair<>(null, null); return AnvilResult.FAIL;
} }
} }
out.setItemMeta(outMeta); out.setItemMeta(outMeta);
if (out.equals(left)) { 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) { 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)) { if (!left.getType().equals(right.getType()) && !(right.getItemMeta() instanceof EnchantmentStorageMeta)) {
return new Pair<>(null, null); return AnvilResult.FAIL;
} }
if (left.getAmount() != right.getAmount()) { if (left.getAmount() != right.getAmount()) {
return new Pair<>(null, null); return AnvilResult.FAIL;
} }
Map<Enchantment, Integer> outEnchants = new HashMap<>(); Map<Enchantment, Integer> outEnchants = new HashMap<>();
@ -219,7 +218,7 @@ public class AnvilMerge {
int leftDamage = ((Damageable) left.getItemMeta()).getDamage(); int leftDamage = ((Damageable) left.getItemMeta()).getDamage();
if (outDamage == leftDamage) { 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()); totalEnchantLevelDelta = Math.abs(outEnchantLevels.intValue() - inEnchantLevels.intValue());
if (output.equals(left)) { if (output.equals(left)) {
return new Pair<>(null, null); return AnvilResult.FAIL;
} }
if (PLUGIN.getConfigYml().getBool("anvil.cost-exponent.enabled")) { 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);
} }
} }

View File

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