From 3a5d92287f7552a170f0ae93080413ac5feb3fe9 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Sat, 9 Apr 2022 20:01:49 +0200 Subject: [PATCH] Minor AnvilListener & SmithingListener readability changes --- .../listeners/AnvilListener.java | 16 +++++++---- .../listeners/SmithingListener.java | 28 ++++++++++--------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/me/wolfyscript/customcrafting/listeners/AnvilListener.java b/src/main/java/me/wolfyscript/customcrafting/listeners/AnvilListener.java index 601b87bc1..7fc900183 100644 --- a/src/main/java/me/wolfyscript/customcrafting/listeners/AnvilListener.java +++ b/src/main/java/me/wolfyscript/customcrafting/listeners/AnvilListener.java @@ -42,7 +42,10 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Repairable; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; public class AnvilListener implements Listener { @@ -68,14 +71,17 @@ public void onCheck(PrepareAnvilEvent event) { for (CustomRecipeAnvil recipe : customCrafting.getRegistries().getRecipes().getAvailable(RecipeType.ANVIL, player)) { Optional finalInputLeft = Optional.empty(); Optional finalInputRight = Optional.empty(); - if ((recipe.hasInputLeft() && (inputLeft == null || (finalInputLeft = recipe.getInputLeft().check(inputLeft, recipe.isCheckNBT())).isEmpty())) || (recipe.hasInputRight() && (inputRight == null || (finalInputRight = recipe.getInputRight().check(inputRight, recipe.isCheckNBT())).isEmpty()))) { + if ((recipe.hasInputLeft() && (inputLeft == null || (finalInputLeft = recipe.getInputLeft().check(inputLeft, recipe.isCheckNBT())).isEmpty())) + || (recipe.hasInputRight() && (inputRight == null || (finalInputRight = recipe.getInputRight().check(inputRight, recipe.isCheckNBT())).isEmpty()))) { continue; } //Recipe is valid at this point! - AnvilData anvilData = new AnvilData(recipe, Map.of(0, new IngredientData(0, recipe.getInputLeft(), finalInputLeft.orElse(null), inputLeft), 1, new IngredientData(1, recipe.getInputRight(), finalInputRight.orElse(null), inputRight))); + AnvilData anvilData = new AnvilData(recipe, Map.of( + 0, new IngredientData(0, recipe.getInputLeft(), finalInputLeft.orElse(null), inputLeft), + 1, new IngredientData(1, recipe.getInputRight(), finalInputRight.orElse(null), inputRight)) + ); //Set the result depending on what is configured! final CustomItem resultItem = recipe.getRepairTask().computeResult(recipe, event, anvilData, player, inputLeft, inputRight); - int repairCost = Math.max(1, recipe.getRepairCost()); if (inputLeft != null) { var inputMeta = inputLeft.getItemMeta(); @@ -97,11 +103,9 @@ public void onCheck(PrepareAnvilEvent event) { } } } - //Save current active recipe to consume correct item inputs! preCraftedRecipes.put(player.getUniqueId(), anvilData); final ItemStack finalResult = recipe.getResult().getItem(anvilData, resultItem, player, null); - inventory.setRepairCost(repairCost); event.setResult(repairCost > 0 ? finalResult : null); player.updateInventory(); diff --git a/src/main/java/me/wolfyscript/customcrafting/listeners/SmithingListener.java b/src/main/java/me/wolfyscript/customcrafting/listeners/SmithingListener.java index 027e2df03..19ac6eba1 100644 --- a/src/main/java/me/wolfyscript/customcrafting/listeners/SmithingListener.java +++ b/src/main/java/me/wolfyscript/customcrafting/listeners/SmithingListener.java @@ -112,19 +112,7 @@ public void onPrepare(PrepareSmithingEvent event) { //Copy damage from base item to result if (base.hasItemMeta() && base.getItemMeta() instanceof Damageable damageable) { int damage = damageable.getDamage(); - apply_damage: { - ItemsAdderIntegration iAIntegration = WolfyUtilCore.getInstance().getCompatibilityManager().getPlugins().getIntegration("ItemsAdder", ItemsAdderIntegration.class); - if (iAIntegration != null) { - CustomStack customStack = iAIntegration.getByItemStack(endResult); - if (customStack != null) { - final int maxDur = customStack.getMaxDurability(); - customStack.setDurability(maxDur - damage); - break apply_damage; - } - } - resultDamageable.setDamage(damage); - endResult.setItemMeta(resultDamageable); - } + applyDamageToItem(damage, endResult, resultDamageable); } } event.setResult(endResult); @@ -136,6 +124,20 @@ public void onPrepare(PrepareSmithingEvent event) { } } + private void applyDamageToItem(int damage, ItemStack endResult, Damageable resultDamageable) { + ItemsAdderIntegration iAIntegration = WolfyUtilCore.getInstance().getCompatibilityManager().getPlugins().getIntegration("ItemsAdder", ItemsAdderIntegration.class); + if (iAIntegration != null) { + CustomStack customStack = iAIntegration.getByItemStack(endResult); + if (customStack != null) { + final int maxDur = customStack.getMaxDurability(); + customStack.setDurability(maxDur - damage); + return; + } + } + resultDamageable.setDamage(damage); + endResult.setItemMeta(resultDamageable); + } + @EventHandler(priority = EventPriority.HIGHEST) public void onTakeOutItem(InventoryClickEvent event) { if (event.getClickedInventory() == null) return;