Implements netherite salvaging #24
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
2024-05-06 21:26:21 +02:00
parent e6047f3866
commit 1d7e8a0732
15 changed files with 430 additions and 209 deletions

View File

@@ -110,6 +110,7 @@ public final class ConfigHelper {
/**
* Changes all configuration values from the old name to the new name
*
* @param dataFolderPath <p>The path to this plugin's data</p>
* @param currentConfiguration <p>The current config to back up</p>
*/
public static void migrateConfig(@NotNull String dataFolderPath, @NotNull FileConfiguration currentConfiguration) {

View File

@@ -3,11 +3,8 @@ 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.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
@@ -168,40 +165,6 @@ 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(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
*

View File

@@ -1,6 +1,5 @@
package net.knarcraft.blacksmith.util;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.enchantments.Enchantment;
@@ -43,6 +42,29 @@ public final class SalvageHelper {
trimMaterialToMaterial.put(TrimMaterial.REDSTONE, Material.REDSTONE);
}
/**
* Gets salvage for a given netherite item
*
* @param item <p>The netherite armor/tool to salvage</p>
* @return <p></p>
*/
@Nullable
public static List<ItemStack> getNetheriteSalvage(@NotNull ItemStack item) {
Material newMaterial = Material.matchMaterial(item.getType().name().replace("NETHERITE", "DIAMOND"));
if (newMaterial == null) {
return null;
}
ItemStack clone = item.clone();
clone.setType(newMaterial);
List<ItemStack> salvage = new ArrayList<>();
salvage.add(clone);
salvage.add(new ItemStack(Material.NETHERITE_INGOT, 1));
salvage.add(new ItemStack(Material.NETHERITE_UPGRADE_SMITHING_TEMPLATE, 1));
return salvage;
}
/**
* Gets salvage for the given armor trim
*
@@ -51,7 +73,7 @@ public final class SalvageHelper {
* @return <p>The salvage, or null if salvage could not be calculated</p>
*/
@Nullable
public static List<ItemStack> getTrimSalvage(@NotNull ItemStack item, @NotNull ArmorMeta armorMeta) {
public static List<ItemStack> getArmorTrimSalvage(@NotNull ItemStack item, @NotNull ArmorMeta armorMeta) {
ArmorTrim armorTrim = armorMeta.getTrim();
if (armorTrim == null) {
return null;
@@ -82,22 +104,41 @@ public final class SalvageHelper {
}
/**
* Gets whether the given item has a valid crafting recipe
* Checks whether the given item can be salvaged, assuming no restrictions apply
*
* @param item <p>The item to check</p>
* @return <p>True if the item has a valid crafting recipe</p>
* @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 hasRecipe(@NotNull ItemStack item) {
List<Recipe> recipes = Bukkit.getRecipesFor(new ItemStack(item.getType(), item.getAmount()));
public static boolean isSalvageable(@NotNull Server server, @NotNull ItemStack item) {
List<Recipe> recipes = server.getRecipesFor(new ItemStack(item.getType(), item.getAmount()));
for (Recipe recipe : recipes) {
if (recipe instanceof ShapedRecipe || recipe instanceof ShapelessRecipe) {
// Only crafting recipes are allowed.
if ((recipe instanceof ShapedRecipe || recipe instanceof ShapelessRecipe) &&
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 ShapedRecipe || recipe instanceof ShapelessRecipe) {
return recipe.getResult().getAmount();
}
}
return 1;
}
/**
* Gets the sum of all enchantment levels for the given item
*