Finishes the scrapper implementation
Some checks failed
EpicKnarvik97/Blacksmith/pipeline/head There was a failure building this commit

This commit is contained in:
2024-05-04 01:01:56 +02:00
parent 455db78988
commit 4012e532da
30 changed files with 501 additions and 216 deletions

View File

@ -8,7 +8,6 @@ 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;
@ -46,7 +45,11 @@ public final class ItemHelper {
* @return <p>The max durability of the item</p>
*/
public static short getMaxDurability(@NotNull ItemStack itemStack) {
return itemStack.getType().getMaxDurability();
if (itemStack.getItemMeta() instanceof Damageable) {
return itemStack.getType().getMaxDurability();
} else {
return 0;
}
}
/**
@ -56,12 +59,11 @@ public final class ItemHelper {
* @return <p>The durability of the item</p>
*/
public static short getDurability(@NotNull ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
int maxDurability = getMaxDurability(itemStack);
if (damageable != null) {
if (itemStack.getItemMeta() instanceof Damageable damageable) {
int maxDurability = getMaxDurability(itemStack);
return (short) (maxDurability - damageable.getDamage());
} else {
return (short) maxDurability;
return 0;
}
}
@ -72,8 +74,7 @@ public final class ItemHelper {
* @return <p>The damage done to the item</p>
*/
public static short getDamage(@NotNull ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
if (damageable != null) {
if (itemStack.getItemMeta() instanceof Damageable damageable) {
return (short) damageable.getDamage();
} else {
return 0;
@ -173,26 +174,49 @@ public final class ItemHelper {
* @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()) {
for (Recipe recipe : server.getRecipesFor(new ItemStack(item.getType(), item.getAmount()))) {
// Only crafting recipes are allowed.
if ((recipe instanceof CraftingRecipe) && item.getAmount() >= recipe.getResult().getAmount()) {
return true;
}
}
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 CraftingRecipe) {
return recipe.getResult().getAmount();
}
}
return -1;
}
/**
* Gets all materials matching the given material wildcard
*
* @param materialName <p>The material name or material wildcard to match</p>
* @param extended <p>Whether to use an extended match, allowing any material</p>
* @return <p>The matched material(s)</p>
*/
public static @NotNull List<Material> getWildcardMatch(@NotNull String materialName) {
public static @NotNull List<Material> getWildcardMatch(@NotNull String materialName, boolean extended) {
String search = InputParsingHelper.regExIfy(materialName);
List<Material> materials = new ArrayList<>();
for (Material material : ItemHelper.getAllReforgeAbleMaterials()) {
List<Material> all;
if (extended) {
all = List.of(Material.values());
} else {
all = ItemHelper.getAllReforgeAbleMaterials();
}
for (Material material : all) {
if (material.name().matches(search)) {
materials.add(material);
}
@ -249,7 +273,7 @@ public final class ItemHelper {
}
/**
* Replaces smoth presets in the given list of strings
* Replaces smith 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>