Skip to content

Commit

Permalink
Minor AnvilListener & SmithingListener readability changes
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyScript committed Apr 9, 2022
1 parent 41df9d6 commit 3a5d922
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -68,14 +71,17 @@ public void onCheck(PrepareAnvilEvent event) {
for (CustomRecipeAnvil recipe : customCrafting.getRegistries().getRecipes().getAvailable(RecipeType.ANVIL, player)) {
Optional<CustomItem> finalInputLeft = Optional.empty();
Optional<CustomItem> 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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 3a5d922

Please sign in to comment.