Adds some tests for the salvage helper
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
2023-01-14 15:10:40 +01:00
parent 8e5d4c7a61
commit 7cc2aef9d4
8 changed files with 173 additions and 25 deletions

View File

@ -1,8 +1,7 @@
package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
@ -24,17 +23,19 @@ public final class SalvageHelper {
*
* <p>Note: Only items craft-able in a crafting table are salvageable. Netherite gear is therefore not salvageable.</p>
*
* @param salvagedItem <p>The item stack to salvage</p>
* @param server <p>The server to get recipes from</p>
* @param salvagedItem <p>The item stack to salvage</p>
* @param ignoredSalvage <p>Any material which should not be returned as part of the salvage.</p>
* @return <p>The items to return to the user, or null if not salvageable</p>
*/
public static List<ItemStack> getSalvage(ItemStack salvagedItem) {
public static List<ItemStack> getSalvage(Server server, ItemStack salvagedItem, List<Material> ignoredSalvage) {
if (salvagedItem == null || salvagedItem.getAmount() < 1 ||
!BlacksmithTrait.isRepairable(salvagedItem)) {
!ItemHelper.isRepairable(salvagedItem)) {
return null;
}
for (Recipe recipe : Bukkit.getServer().getRecipesFor(salvagedItem)) {
List<ItemStack> salvage = getRecipeSalvage(recipe, salvagedItem);
for (Recipe recipe : server.getRecipesFor(salvagedItem)) {
List<ItemStack> salvage = getRecipeSalvage(recipe, salvagedItem, ignoredSalvage);
if (salvage != null) {
return salvage;
}
@ -46,11 +47,13 @@ public final class SalvageHelper {
/**
* Gets the salvage resulting from the given recipe and the given item
*
* @param recipe <p>The recipe to get salvage for</p>
* @param salvagedItem <p>The item to be salvaged</p>
* @param recipe <p>The recipe to get salvage for</p>
* @param salvagedItem <p>The item to be salvaged</p>
* @param ignoredSalvage <p>Any material which should not be returned as part of the salvage.</p>
* @return <p>A list of items, or null if not a valid type of recipe</p>
*/
private static List<ItemStack> getRecipeSalvage(Recipe recipe, ItemStack salvagedItem) {
private static List<ItemStack> getRecipeSalvage(Recipe recipe, ItemStack salvagedItem,
List<Material> ignoredSalvage) {
List<ItemStack> ingredients;
if (recipe instanceof ShapedRecipe shapedRecipe) {
ingredients = getIngredients(shapedRecipe);
@ -63,6 +66,11 @@ public final class SalvageHelper {
//Make things easier by eliminating identical stacks
ingredients = combineStacks(ingredients);
//Purge any ignored salvage to only calculate salvage using the remaining items
if (ignoredSalvage != null) {
ingredients.removeIf((item) -> ignoredSalvage.contains(item.getType()));
}
//Make sure to give salvage for all items if a stack > 1 is provided
List<ItemStack> salvage = new ArrayList<>();
for (int i = 0; i < salvagedItem.getAmount(); i++) {