Implements armor trim salvage #24
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
2024-05-06 14:48:48 +02:00
parent 3ed3c99c15
commit e6047f3866
7 changed files with 226 additions and 56 deletions

View File

@ -8,6 +8,9 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ArmorMeta;
import org.bukkit.inventory.meta.trim.ArmorTrim;
import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -24,6 +27,59 @@ import java.util.Random;
public final class SalvageHelper {
private static final Random random = new Random();
private static final Map<TrimMaterial, Material> trimMaterialToMaterial;
static {
trimMaterialToMaterial = new HashMap<>();
trimMaterialToMaterial.put(TrimMaterial.AMETHYST, Material.AMETHYST_SHARD);
trimMaterialToMaterial.put(TrimMaterial.COPPER, Material.COPPER_INGOT);
trimMaterialToMaterial.put(TrimMaterial.DIAMOND, Material.DIAMOND);
trimMaterialToMaterial.put(TrimMaterial.EMERALD, Material.EMERALD);
trimMaterialToMaterial.put(TrimMaterial.GOLD, Material.GOLD_INGOT);
trimMaterialToMaterial.put(TrimMaterial.IRON, Material.IRON_INGOT);
trimMaterialToMaterial.put(TrimMaterial.LAPIS, Material.LAPIS_LAZULI);
trimMaterialToMaterial.put(TrimMaterial.NETHERITE, Material.NETHERITE_INGOT);
trimMaterialToMaterial.put(TrimMaterial.QUARTZ, Material.QUARTZ);
trimMaterialToMaterial.put(TrimMaterial.REDSTONE, Material.REDSTONE);
}
/**
* Gets salvage for the given armor trim
*
* @param item <p>The item to have its armor trim salvaged</p>
* @param armorMeta <p>The armor meta of the item to salvage</p>
* @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) {
ArmorTrim armorTrim = armorMeta.getTrim();
if (armorTrim == null) {
return null;
}
List<ItemStack> result = new ArrayList<>();
Material trimMaterial = trimMaterialToMaterial.get(armorTrim.getMaterial());
if (trimMaterial != null) {
result.add(new ItemStack(trimMaterial, 1));
} else {
return null;
}
Material patternMaterial = Material.matchMaterial(armorTrim.getPattern().getKey().getKey() +
"_ARMOR_TRIM_SMITHING_TEMPLATE");
if (patternMaterial != null) {
result.add(new ItemStack(patternMaterial, 1));
} else {
return null;
}
// Return a clone of the input item, with the armor trim removed
ItemStack strippedItem = item.clone();
armorMeta.setTrim(null);
strippedItem.setItemMeta(armorMeta);
result.add(strippedItem);
return result;
}
/**
* Gets whether the given item has a valid crafting recipe