2023-01-13 20:34:34 +01:00
|
|
|
package net.knarcraft.blacksmith.util;
|
|
|
|
|
2024-05-05 15:37:38 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2023-01-13 20:34:34 +01:00
|
|
|
import org.bukkit.Material;
|
2023-01-14 15:10:40 +01:00
|
|
|
import org.bukkit.Server;
|
2023-11-14 16:04:48 +01:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2023-01-13 20:34:34 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.inventory.Recipe;
|
|
|
|
import org.bukkit.inventory.ShapedRecipe;
|
|
|
|
import org.bukkit.inventory.ShapelessRecipe;
|
2023-11-14 16:04:48 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
2023-01-13 20:34:34 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2023-12-30 13:56:33 +01:00
|
|
|
import java.util.Collection;
|
2023-01-13 20:34:34 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper class for deciding the salvage returned if salvaging an item
|
|
|
|
*/
|
|
|
|
public final class SalvageHelper {
|
|
|
|
|
2024-05-04 01:01:56 +02:00
|
|
|
private static final Random random = new Random();
|
|
|
|
|
2024-05-05 15:37:38 +02:00
|
|
|
/**
|
|
|
|
* Gets whether the given item has a valid crafting recipe
|
|
|
|
*
|
|
|
|
* @param item <p>The item to check</p>
|
|
|
|
* @return <p>True if the item has a valid crafting recipe</p>
|
|
|
|
*/
|
|
|
|
public static boolean hasRecipe(@NotNull ItemStack item) {
|
|
|
|
List<Recipe> recipes = Bukkit.getRecipesFor(new ItemStack(item.getType(), item.getAmount()));
|
|
|
|
for (Recipe recipe : recipes) {
|
|
|
|
if (recipe instanceof ShapedRecipe || recipe instanceof ShapelessRecipe) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:04:48 +01:00
|
|
|
/**
|
|
|
|
* Gets the sum of all enchantment levels for the given item
|
|
|
|
*
|
|
|
|
* @param item <p>The item to check</p>
|
|
|
|
* @return <p>The total amount of enchantment levels on the item</p>
|
|
|
|
*/
|
|
|
|
public static int getTotalEnchantmentLevels(@NotNull ItemStack item) {
|
|
|
|
int expLevels = 0;
|
|
|
|
for (Enchantment enchantment : item.getEnchantments().keySet()) {
|
|
|
|
expLevels += item.getEnchantmentLevel(enchantment);
|
|
|
|
}
|
|
|
|
return expLevels;
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:34:34 +01:00
|
|
|
/**
|
|
|
|
* Gets the items to return if salvaging the given item stack
|
|
|
|
*
|
|
|
|
* <p>Note: Only items craft-able in a crafting table are salvageable. Netherite gear is therefore not salvageable.</p>
|
|
|
|
*
|
2024-05-05 18:02:12 +02:00
|
|
|
* @param server <p>The server to get recipes from</p>
|
|
|
|
* @param salvagedItem <p>The item stack to salvage</p>
|
|
|
|
* @param trashSalvage <p>Any material treated as trash salvage</p>
|
|
|
|
* @param extended <p>Whether to enable extended salvage, ignoring the repairable restriction</p>
|
2023-01-13 20:34:34 +01:00
|
|
|
* @return <p>The items to return to the user, or null if not salvageable</p>
|
|
|
|
*/
|
2023-11-14 16:04:48 +01:00
|
|
|
public static @Nullable List<ItemStack> getSalvage(@NotNull Server server, @Nullable ItemStack salvagedItem,
|
2024-05-05 18:02:12 +02:00
|
|
|
@NotNull Collection<Material> trashSalvage, boolean extended) {
|
2023-01-13 20:34:34 +01:00
|
|
|
if (salvagedItem == null || salvagedItem.getAmount() < 1 ||
|
2024-05-04 01:01:56 +02:00
|
|
|
(!extended && !ItemHelper.isRepairable(salvagedItem))) {
|
2023-01-13 20:34:34 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-05-04 01:01:56 +02:00
|
|
|
for (Recipe recipe : server.getRecipesFor(new ItemStack(salvagedItem.getType(), salvagedItem.getAmount()))) {
|
|
|
|
if (recipe instanceof ShapedRecipe || recipe instanceof ShapelessRecipe) {
|
2024-05-05 18:02:12 +02:00
|
|
|
List<ItemStack> salvage = getRecipeSalvage(recipe, salvagedItem, trashSalvage);
|
2024-05-04 01:01:56 +02:00
|
|
|
if (salvage != null && !salvage.isEmpty()) {
|
|
|
|
return salvage;
|
|
|
|
}
|
2023-01-13 20:34:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the salvage resulting from the given recipe and the given item
|
|
|
|
*
|
2024-05-05 18:02:12 +02:00
|
|
|
* @param recipe <p>The recipe to get salvage for</p>
|
|
|
|
* @param salvagedItem <p>The item to be salvaged</p>
|
|
|
|
* @param trashSalvage <p>Any material treated as trash salvage</p>
|
2023-01-13 20:34:34 +01:00
|
|
|
* @return <p>A list of items, or null if not a valid type of recipe</p>
|
|
|
|
*/
|
2023-11-14 16:04:48 +01:00
|
|
|
private static @Nullable List<ItemStack> getRecipeSalvage(@NotNull Recipe recipe, @NotNull ItemStack salvagedItem,
|
2024-05-05 18:02:12 +02:00
|
|
|
@NotNull Collection<Material> trashSalvage) {
|
2023-01-13 20:34:34 +01:00
|
|
|
List<ItemStack> ingredients;
|
|
|
|
if (recipe instanceof ShapedRecipe shapedRecipe) {
|
|
|
|
ingredients = getIngredients(shapedRecipe);
|
|
|
|
} else if (recipe instanceof ShapelessRecipe shapelessRecipe) {
|
|
|
|
ingredients = shapelessRecipe.getIngredientList();
|
|
|
|
} else {
|
|
|
|
//Recipes other than crafting shouldn't be considered for salvaging
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
//Make things easier by eliminating identical stacks
|
|
|
|
ingredients = combineStacks(ingredients);
|
|
|
|
|
2024-05-05 18:02:12 +02:00
|
|
|
return combineStacks(getSalvage(copyItems(ingredients), salvagedItem, trashSalvage));
|
2023-01-14 17:46:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies a list of items
|
|
|
|
*
|
|
|
|
* <p>Note: This does not copy any metadata. It only copies the item type and the amount.</p>
|
|
|
|
*
|
|
|
|
* @param itemsToCopy <p>The items to make a copy of</p>
|
|
|
|
* @return <p>A copy of the given items</p>
|
|
|
|
*/
|
2023-11-14 16:04:48 +01:00
|
|
|
private static @NotNull List<ItemStack> copyItems(@NotNull List<ItemStack> itemsToCopy) {
|
2023-01-14 17:46:29 +01:00
|
|
|
List<ItemStack> copies = new ArrayList<>(itemsToCopy.size());
|
|
|
|
for (ItemStack itemStack : itemsToCopy) {
|
|
|
|
copies.add(new ItemStack(itemStack.getType(), itemStack.getAmount()));
|
|
|
|
}
|
|
|
|
return copies;
|
2023-01-13 20:34:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the salvage resulting from the given item, and the given recipe items
|
|
|
|
*
|
|
|
|
* @param recipeItems <p>All items required for crafting the item to salvage</p>
|
|
|
|
* @param salvagedItem <p>The item to be salvaged</p>
|
2024-05-05 18:02:12 +02:00
|
|
|
* @param trashSalvage <p>The types of materials considered trash for this recipe</p>
|
2023-01-13 20:34:34 +01:00
|
|
|
* @return <p>The items to be returned to the user as salvage</p>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
|
|
|
private static List<ItemStack> getSalvage(@NotNull List<ItemStack> recipeItems,
|
2024-05-05 18:02:12 +02:00
|
|
|
@NotNull ItemStack salvagedItem,
|
|
|
|
@NotNull Collection<Material> trashSalvage) {
|
2024-05-04 01:01:56 +02:00
|
|
|
int durability = ItemHelper.getDurability(salvagedItem);
|
|
|
|
int maxDurability = ItemHelper.getMaxDurability(salvagedItem);
|
|
|
|
|
|
|
|
// Prevent divide by zero for items that don't have a set max durability
|
|
|
|
if (maxDurability == 0) {
|
|
|
|
maxDurability = 1;
|
|
|
|
durability = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
double percentageRemaining = (double) durability / maxDurability;
|
|
|
|
|
2023-11-14 16:04:48 +01:00
|
|
|
int totalItems = totalItemAmount(recipeItems);
|
2023-01-13 20:34:34 +01:00
|
|
|
//Get the amount of recipe items to be returned
|
|
|
|
int itemsToReturn = (int) Math.floor(percentageRemaining * totalItems);
|
|
|
|
int bound = recipeItems.size();
|
|
|
|
|
2024-05-05 18:02:12 +02:00
|
|
|
List<ItemStack> goodItems = copyItems(recipeItems);
|
|
|
|
goodItems.removeIf((item) -> trashSalvage.contains(item.getType()));
|
|
|
|
int goodSalvage = totalItemAmount(goodItems);
|
|
|
|
|
2024-05-04 01:01:56 +02:00
|
|
|
List<ItemStack> salvage = new ArrayList<>();
|
2023-01-13 20:34:34 +01:00
|
|
|
for (int i = 0; i < itemsToReturn; i++) {
|
2024-05-05 18:02:12 +02:00
|
|
|
// Pick random item
|
2024-05-04 01:01:56 +02:00
|
|
|
int itemIndex = SalvageHelper.random.nextInt(bound);
|
2023-01-13 20:34:34 +01:00
|
|
|
ItemStack itemStack = recipeItems.get(itemIndex);
|
|
|
|
|
2024-05-05 18:02:12 +02:00
|
|
|
// The selected item is trash, so skip it
|
|
|
|
if (trashSalvage.contains(itemStack.getType()) && i < goodSalvage) {
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:34:34 +01:00
|
|
|
//Make sure to never give more of one item than the amount which exists in the recipe
|
|
|
|
if (itemStack.getAmount() <= 0) {
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-05 18:02:12 +02:00
|
|
|
|
2023-01-13 20:34:34 +01:00
|
|
|
itemStack.setAmount(itemStack.getAmount() - 1);
|
|
|
|
|
|
|
|
salvage.add(new ItemStack(itemStack.getType(), 1));
|
|
|
|
}
|
|
|
|
return salvage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the total sum of items in the given list of items
|
|
|
|
*
|
|
|
|
* @param items <p>The items to get the sum of</p>
|
|
|
|
* @return <p>The total number of items</p>
|
|
|
|
*/
|
2023-11-14 16:04:48 +01:00
|
|
|
private static int totalItemAmount(@NotNull List<ItemStack> items) {
|
2023-01-13 20:34:34 +01:00
|
|
|
int sum = 0;
|
|
|
|
for (ItemStack itemStack : items) {
|
|
|
|
sum += itemStack.getAmount();
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Combines all items of the same type in the given list
|
|
|
|
*
|
|
|
|
* <p>Basically, if the input is two item stacks containing one diamond each, the output will be an item stack with
|
|
|
|
* two diamonds instead.</p>
|
|
|
|
*
|
|
|
|
* @param items <p>The items to combine</p>
|
|
|
|
* @return <p>The given items, but combined</p>
|
|
|
|
*/
|
2023-11-14 16:04:48 +01:00
|
|
|
private static @NotNull List<ItemStack> combineStacks(@NotNull List<ItemStack> items) {
|
2023-01-13 20:34:34 +01:00
|
|
|
Map<Material, Integer> itemAmounts = new HashMap<>();
|
|
|
|
for (ItemStack item : items) {
|
|
|
|
Material itemType = item.getType();
|
|
|
|
itemAmounts.put(itemType, itemAmounts.getOrDefault(itemType, 0) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<ItemStack> combined = new ArrayList<>();
|
|
|
|
for (Material material : itemAmounts.keySet()) {
|
|
|
|
combined.add(new ItemStack(material, itemAmounts.get(material)));
|
|
|
|
}
|
|
|
|
return combined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all ingredients contained in the given shaped recipe
|
|
|
|
*
|
|
|
|
* @param shapedRecipe <p>The shaped recipe to get ingredients for</p>
|
|
|
|
* @return <p>The items contained in the recipe</p>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
|
|
|
private static List<ItemStack> getIngredients(@NotNull ShapedRecipe shapedRecipe) {
|
2023-01-13 20:34:34 +01:00
|
|
|
List<ItemStack> ingredients = new ArrayList<>();
|
|
|
|
Map<Character, ItemStack> ingredientMap = shapedRecipe.getIngredientMap();
|
|
|
|
//The shape is a list of the three rows' strings. Each string contains 3 characters.
|
|
|
|
String[] shape = shapedRecipe.getShape();
|
|
|
|
for (String row : shape) {
|
|
|
|
for (int column = 0; column < row.length(); column++) {
|
|
|
|
ItemStack item = ingredientMap.get(row.charAt(column));
|
|
|
|
if (item != null && item.getType() != Material.AIR && item.getAmount() > 0) {
|
|
|
|
ingredients.add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ingredients;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|