Writes a lot of code necessary for the scrapper
Adds the classes necessary for the new scrapper Partly implements some scrapper functionality Restructures some classes to reduce code duplication Moves some classes to make some classes easier to find Adds a bunch of TODOs where things have an unfinished implementation
This commit is contained in:
@ -2,10 +2,13 @@ package net.knarcraft.blacksmith.util;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
import org.bukkit.inventory.ShapelessRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -18,6 +21,20 @@ import java.util.Random;
|
||||
*/
|
||||
public final class SalvageHelper {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the items to return if salvaging the given item stack
|
||||
*
|
||||
@ -28,7 +45,8 @@ public final class SalvageHelper {
|
||||
* @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(Server server, ItemStack salvagedItem, List<Material> ignoredSalvage) {
|
||||
public static @Nullable List<ItemStack> getSalvage(@NotNull Server server, @Nullable ItemStack salvagedItem,
|
||||
@NotNull List<Material> ignoredSalvage) {
|
||||
if (salvagedItem == null || salvagedItem.getAmount() < 1 ||
|
||||
!ItemHelper.isRepairable(salvagedItem)) {
|
||||
return null;
|
||||
@ -52,8 +70,8 @@ public final class SalvageHelper {
|
||||
* @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,
|
||||
List<Material> ignoredSalvage) {
|
||||
private static @Nullable List<ItemStack> getRecipeSalvage(@NotNull Recipe recipe, @NotNull ItemStack salvagedItem,
|
||||
@NotNull List<Material> ignoredSalvage) {
|
||||
List<ItemStack> ingredients;
|
||||
if (recipe instanceof ShapedRecipe shapedRecipe) {
|
||||
ingredients = getIngredients(shapedRecipe);
|
||||
@ -67,9 +85,7 @@ public final class SalvageHelper {
|
||||
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()));
|
||||
}
|
||||
ingredients.removeIf((item) -> ignoredSalvage.contains(item.getType()));
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
@ -90,7 +106,7 @@ public final class SalvageHelper {
|
||||
* @param itemsToCopy <p>The items to make a copy of</p>
|
||||
* @return <p>A copy of the given items</p>
|
||||
*/
|
||||
private static List<ItemStack> copyItems(List<ItemStack> itemsToCopy) {
|
||||
private static @NotNull List<ItemStack> copyItems(@NotNull List<ItemStack> itemsToCopy) {
|
||||
List<ItemStack> copies = new ArrayList<>(itemsToCopy.size());
|
||||
for (ItemStack itemStack : itemsToCopy) {
|
||||
copies.add(new ItemStack(itemStack.getType(), itemStack.getAmount()));
|
||||
@ -106,10 +122,11 @@ public final class SalvageHelper {
|
||||
* @param random <p>The randomness generator to use</p>
|
||||
* @return <p>The items to be returned to the user as salvage</p>
|
||||
*/
|
||||
private static List<ItemStack> getSalvage(List<ItemStack> recipeItems, ItemStack salvagedItem, Random random) {
|
||||
private static @NotNull List<ItemStack> getSalvage(@NotNull List<ItemStack> recipeItems,
|
||||
@NotNull ItemStack salvagedItem, @NotNull Random random) {
|
||||
double percentageRemaining = (ItemHelper.getDurability(salvagedItem) /
|
||||
(double) ItemHelper.getMaxDurability(salvagedItem));
|
||||
int totalItems = totalItems(recipeItems);
|
||||
int totalItems = totalItemAmount(recipeItems);
|
||||
//Get the amount of recipe items to be returned
|
||||
int itemsToReturn = (int) Math.floor(percentageRemaining * totalItems);
|
||||
int bound = recipeItems.size();
|
||||
@ -137,7 +154,7 @@ public final class SalvageHelper {
|
||||
* @param items <p>The items to get the sum of</p>
|
||||
* @return <p>The total number of items</p>
|
||||
*/
|
||||
private static int totalItems(List<ItemStack> items) {
|
||||
private static int totalItemAmount(@NotNull List<ItemStack> items) {
|
||||
int sum = 0;
|
||||
for (ItemStack itemStack : items) {
|
||||
sum += itemStack.getAmount();
|
||||
@ -154,7 +171,7 @@ public final class SalvageHelper {
|
||||
* @param items <p>The items to combine</p>
|
||||
* @return <p>The given items, but combined</p>
|
||||
*/
|
||||
private static List<ItemStack> combineStacks(List<ItemStack> items) {
|
||||
private static @NotNull List<ItemStack> combineStacks(@NotNull List<ItemStack> items) {
|
||||
Map<Material, Integer> itemAmounts = new HashMap<>();
|
||||
for (ItemStack item : items) {
|
||||
Material itemType = item.getType();
|
||||
@ -174,7 +191,7 @@ public final class SalvageHelper {
|
||||
* @param shapedRecipe <p>The shaped recipe to get ingredients for</p>
|
||||
* @return <p>The items contained in the recipe</p>
|
||||
*/
|
||||
private static List<ItemStack> getIngredients(ShapedRecipe shapedRecipe) {
|
||||
private static @NotNull List<ItemStack> getIngredients(@NotNull ShapedRecipe shapedRecipe) {
|
||||
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.
|
||||
|
Reference in New Issue
Block a user