Rewrites a lot of scrapper code to fix actual and potential bugs
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
2024-07-29 17:53:07 +02:00
parent cf702c0e48
commit f3372a13a5
7 changed files with 158 additions and 86 deletions

View File

@ -1,6 +1,7 @@
package net.knarcraft.blacksmith.util;
import org.bukkit.Bukkit;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.container.RecipeResult;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.enchantments.Enchantment;
@ -20,6 +21,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
/**
* A helper class for deciding the salvage returned if salvaging an item
@ -123,23 +125,6 @@ public final class SalvageHelper {
return false;
}
/**
* Gets the amount of an item that's required to salvage that item
*
* @param server <p>The server to get recipes from</p>
* @param item <p>The item to check</p>
* @return <p>The number of items required for salvage, or -1 if the recipe was not found</p>
*/
public static int getRequiredAmountForSalvage(@NotNull Server server, @NotNull ItemStack item) {
for (Recipe recipe : server.getRecipesFor(new ItemStack(item.getType(), item.getAmount()))) {
// Only crafting recipes are allowed.
if (recipe instanceof ShapedRecipe || recipe instanceof ShapelessRecipe) {
return recipe.getResult().getAmount();
}
}
return 1;
}
/**
* Gets the sum of all enchantment levels for the given item
*
@ -165,28 +150,34 @@ public final class SalvageHelper {
* @param extended <p>Whether to enable extended salvage, ignoring the repairable restriction</p>
* @return <p>The items to return to the user, or null if not salvageable</p>
*/
public static @Nullable List<ItemStack> getSalvage(@NotNull Server server, @Nullable ItemStack salvagedItem,
@NotNull Collection<Material> trashSalvage, boolean extended) {
public static @Nullable RecipeResult getSalvage(@NotNull Server server, @Nullable ItemStack salvagedItem,
@NotNull Collection<Material> trashSalvage, boolean extended) {
if (salvagedItem == null || salvagedItem.getAmount() < 1 ||
(!extended && !ItemHelper.isRepairable(salvagedItem))) {
return null;
}
for (Recipe recipe : server.getRecipesFor(new ItemStack(salvagedItem.getType(), salvagedItem.getAmount()))) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Considering recipe: " + recipe.getResult() + " -> " + getRawRecipeSalvage(recipe));
// Only consider crafting table recipes
if (!(recipe instanceof ShapedRecipe) && !(recipe instanceof ShapelessRecipe)) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Recipe had invalid type");
continue;
}
// Make sure the player has enough items
if (salvagedItem.getAmount() < getRequiredAmountForSalvage(Bukkit.getServer(), salvagedItem)) {
if (salvagedItem.getAmount() < recipe.getResult().getAmount()) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Too few items for recipe");
continue;
}
// Get actual salvage, as long as any can be produced
List<ItemStack> salvage = getRecipeSalvage(recipe, salvagedItem, trashSalvage);
if (salvage != null && !salvage.isEmpty()) {
return salvage;
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Valid recipe: " + recipe.getResult() + " -> " + getRawRecipeSalvage(recipe));
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Actual salvage: " + salvage);
return new RecipeResult(recipe, salvage);
}
}
@ -207,7 +198,13 @@ public final class SalvageHelper {
if (ingredients == null) {
return null;
}
return combineStacks(getSalvage(copyItems(ingredients), salvagedItem, trashSalvage));
List<ItemStack> copy = copyItems(ingredients);
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Copied salvage: " + copy);
List<ItemStack> salvage = getSalvage(copy, salvagedItem, trashSalvage);
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Combining salvage: " + salvage);
List<ItemStack> combined = combineStacks(salvage);
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Combined : " + combined);
return combined;
}
/**
@ -247,7 +244,10 @@ public final class SalvageHelper {
durability = 1;
}
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Durability: " + durability + "/" + maxDurability);
double percentageRemaining = (double) durability / maxDurability;
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Remaining: " + percentageRemaining);
return pickRandomSalvage(recipeItems, trashSalvage, percentageRemaining);
}
@ -268,36 +268,47 @@ public final class SalvageHelper {
percentageRemaining = 1;
}
int totalItems = totalItemAmount(itemsToChooseFrom);
//Get the amount of recipe items to be returned
int itemsToReturn = (int) Math.floor(percentageRemaining * totalItems);
int bound = itemsToChooseFrom.size();
// If not damaged, just give everything
if (percentageRemaining == 1) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "100% Remaining. Copying " + itemsToChooseFrom);
return copyItems(itemsToChooseFrom);
}
// Split into good items and trash items
List<ItemStack> goodItems = copyItems(itemsToChooseFrom);
goodItems.removeIf((item) -> trashSalvage.contains(item.getType()));
int goodSalvage = totalItemAmount(goodItems);
List<ItemStack> trashItems = copyItems(itemsToChooseFrom);
trashItems.removeIf((item) -> !trashSalvage.contains(item.getType()));
List<ItemStack> salvage = new ArrayList<>();
for (int i = 0; i < itemsToReturn; i++) {
// Pick random item
int itemIndex = SalvageHelper.random.nextInt(bound);
ItemStack itemStack = itemsToChooseFrom.get(itemIndex);
int itemsToReturn = (int) Math.floor(totalItemAmount(itemsToChooseFrom) * percentageRemaining);
int goodItemAmount = totalItemAmount(goodItems);
int pickedItems = 0;
List<ItemStack> salvage = new ArrayList<>(itemsToReturn);
// The selected item is trash, so skip it
if (trashSalvage.contains(itemStack.getType()) && i < goodSalvage) {
i--;
while (pickedItems < itemsToReturn && pickedItems < goodItemAmount) {
int index = SalvageHelper.random.nextInt(goodItems.size());
ItemStack itemStack = goodItems.get(index);
if (itemStack.getType().isAir() || itemStack.getAmount() == 0) {
continue;
}
//Make sure to never give more of one item than the amount which exists in the recipe
if (itemStack.getAmount() <= 0) {
i--;
continue;
}
itemStack.setAmount(itemStack.getAmount() - 1);
salvage.add(new ItemStack(itemStack.getType(), 1));
itemStack.setAmount(itemStack.getAmount() - 1);
pickedItems++;
}
while (pickedItems < itemsToReturn) {
int index = SalvageHelper.random.nextInt(trashItems.size());
ItemStack itemStack = trashItems.get(index);
if (itemStack.getType().isAir() || itemStack.getAmount() == 0) {
continue;
}
salvage.add(new ItemStack(itemStack.getType(), 1));
itemStack.setAmount(itemStack.getAmount() - 1);
pickedItems++;
}
return salvage;
}
@ -328,7 +339,7 @@ public final class SalvageHelper {
Map<Material, Integer> itemAmounts = new HashMap<>();
for (ItemStack item : items) {
Material itemType = item.getType();
itemAmounts.put(itemType, itemAmounts.getOrDefault(itemType, 0) + 1);
itemAmounts.put(itemType, itemAmounts.getOrDefault(itemType, 0) + item.getAmount());
}
List<ItemStack> combined = new ArrayList<>();