Implements some necessary code for the scrapper
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

Implements ignored salvage
Implements salvage-able items
Adds some TODOs
Implements salvage fail chance
Moves several methods to ItemHelper
Adds option for allowing extended salvage
This commit is contained in:
2023-12-30 13:56:33 +01:00
parent 6872dadca8
commit dfae68050e
13 changed files with 482 additions and 165 deletions

View File

@ -1,14 +1,24 @@
package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SmithPreset;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.CraftingRecipe;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.SmithingTransformRecipe;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
/**
* A helper class for getting information about items
@ -155,4 +165,109 @@ public final class ItemHelper {
return false;
}
/**
* Checks whether the given item can be salvaged, assuming no restrictions apply
*
* @param server <p>The server to get recipes from</p>
* @param item <p>The item to check</p>
* @return <p>True if the item can be salvaged</p>
*/
public static boolean isSalvageable(@NotNull Server server, @NotNull ItemStack item) {
for (Recipe recipe : server.getRecipesFor(item)) {
// Only crafting recipes, and smithing transform recipes (diamond -> netherite) are allowed.
if (recipe instanceof CraftingRecipe || recipe instanceof SmithingTransformRecipe &&
item.getAmount() >= recipe.getResult().getAmount()) {
return true;
}
}
return false;
}
/**
* Gets all materials matching the given material wildcard
*
* @param materialName <p>The material name or material wildcard to match</p>
* @return <p>The matched material(s)</p>
*/
public static @NotNull List<Material> getWildcardMatch(@NotNull String materialName) {
String search = InputParsingHelper.regExIfy(materialName);
List<Material> materials = new ArrayList<>();
for (Material material : ItemHelper.getAllReforgeAbleMaterials()) {
if (material.name().matches(search)) {
materials.add(material);
}
}
return materials;
}
/**
* Gets a list of the items described in the given item list
*
* @param itemList <p>The list of items defined by the user</p>
* @param requireRepairable <p>Whether a material must be repairable to be valid.</p>
* @return <p>The materials contained in the item list</p>
*/
public static List<Material> getItems(@Nullable List<String> itemList, boolean requireRepairable) {
List<Material> items = new ArrayList<>();
if (itemList == null) {
return null;
}
//Convert any presets with a list of materials
itemList = replacePresets(itemList);
Set<Material> blacklisted = new HashSet<>();
//Parse every material, and add to reforgeAble items
for (String item : itemList) {
//Ignore ",,"
if (InputParsingHelper.isEmpty(item)) {
continue;
}
boolean blacklist = false;
if (item.startsWith("-")) {
blacklist = true;
item = item.substring(1);
}
Material material = InputParsingHelper.matchMaterial(item);
if (material != null && (!requireRepairable || ItemHelper.isRepairable(new ItemStack(material, 1)))) {
if (!blacklist) {
items.add(material);
} else {
blacklisted.add(material);
}
} else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item +
" as a valid repairable item");
}
}
//Remove any blacklisted materials at the end to make sure order of arguments won't matter
items.removeAll(blacklisted);
return items;
}
/**
* Replaces smoth presets in the given list of strings
*
* @param stringList <p>The value specified by a user</p>
* @return <p>The value with smith presets replaced</p>
*/
private static @NotNull List<String> replacePresets(@NotNull List<String> stringList) {
List<String> newStrings = new ArrayList<>();
for (String item : stringList) {
if (item == null) {
continue;
}
String replaced = SmithPreset.replacePreset(item);
if (!replaced.equals(item)) {
newStrings.addAll(List.of(replaced.split(",")));
} else {
newStrings.add(item);
}
}
return newStrings;
}
}