From e6047f38661436a1a5082fdeb87af13414f8bd94 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 6 May 2024 14:48:48 +0200 Subject: [PATCH] Implements armor trim salvage #24 --- README.md | 87 ++++++++++--------- .../config/scrapper/ScrapperNPCSettings.java | 44 ++++++++++ .../config/scrapper/ScrapperSetting.java | 32 ++++++- .../blacksmith/trait/ScrapperTrait.java | 31 ++++++- .../blacksmith/util/SalvageHelper.java | 56 ++++++++++++ src/main/resources/config.yml | 14 ++- src/main/resources/plugin.yml | 18 ++-- 7 files changed, 226 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index b5fe98c..1bfa895 100644 --- a/README.md +++ b/README.md @@ -149,11 +149,11 @@ All currently supported presets, and available filters for each preset: ## Permissions -| Permission node | Description | -|------------------|----------------------------------------------------------| -| blacksmith.admin | Allows overall blacksmith configuration | -| blacksmith.edit | Allows changing settings for the selected blacksmith NPC | -| blacksmith.use | Allows the player to repair items using blacksmiths | +| Permission node | Description | +|------------------|----------------------------------------------------------------------------------------| +| blacksmith.admin | Allows overall blacksmith and scrapper configuration. | +| blacksmith.edit | Allows changing settings for the selected blacksmith or scrapper NPC. | +| blacksmith.use | Allows the player to repair items using blacksmiths and salvage items using scrappers. | ## Configuration options @@ -171,7 +171,7 @@ All currently supported presets, and available filters for each preset: | pricePerDurabilityPoint | positive decimal number | \[default: 0.005] | The price added for each durability point present/missing (depends on whether natural cost is set to true or false). Setting this without specifying a material sets the pricePerDurabilityPoint for any item the pricePerDurabilityPoint has not been set for. You can use for example "netherite*: 10" to set the value for any material beginning with "netherite". | | enchantmentCost | positive decimal number | \[default: 5] | The added cost for each level of an enchantment present on the item. The cost can be set for specific enchantments. Not specifying an enchantment sets the value for all enchantments without a set value. | | useNaturalCost | true/false | true | If true, each missing durability will add to the cost (price = basePrice + missingDurability * pricePerDurabilityPoint + enchantmentCost). If false, durability will be used to calculate the cost instead of missingDurability (this was the behavior before natural cost was added). | -| showExactTime | true/false | false | If true, blacksmiths will display exact time remaining in minutes and seconds, instead of vague expressions | +| showExactTime | true/false | false | If true, blacksmiths will display exact time remaining in minutes and seconds, instead of vague expressions. | | chippedAnvilReforgingCost | positive decimal number | 10.0 | The price for reforging a chipped anvil (slightly damaged). No other costs apply! | | damagedAnvilReforgingCost | positive decimal number | 20.0 | The price for reforging a damaged anvil (very damaged). No other costs apply! | @@ -196,28 +196,28 @@ All currently supported presets, and available filters for each preset: #### Messages -| Message Key | Default | Explanation | -|--------------------------|-------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------| -| busyPlayerMessage | &cI'm busy at the moment. Come back later! | The message displayed when the blacksmith is serving another player | -| busyReforgeMessage | &cI'm working on it. Be patient! I'll finish {time}! | The message displayed when the blacksmith is busy reforging an item | -| coolDownUnexpiredMessage | &cYou've already had your chance! Give me a break! I'll be ready {time}! | The message displayed when the player has to wait for the cool-down to expire before using the blacksmith again | -| costMessage | &eIt will cost &a{cost}&e to reforge that &a{item}&e! Click again to reforge! | The message displayed when telling a player about the cost of repairing an item | -| failReforgeMessage | &cWhoops! Didn't mean to do that! Maybe next time? | The message displayed when a blacksmith fails to reforge an item | -| insufficientFundsMessage | &cYou don't have enough money to reforge that item! | The message displayed when a player is unable to pay for reforging an item | -| invalidItemMessage | &cI'm sorry, but I'm a/an {title}, I don't know how to reforge that! | The message displayed when a blacksmith is presented an item which it cannot repair | -| itemChangedMessage | &cThat's not the item you wanted to reforge before! | The message displayed when a player changes their item after being shown the repair cost | -| startReforgeMessage | &eOk, let's see what I can do... | The message displayed when a blacksmith starts reforging an item | -| successMessage | There you go! All better! | The message displayed when a blacksmith successfully repairs an item | -| notDamagedMessage | &cThat item is not in need of repair | The message displayed if a player tries to reforge an item with full durability | +| Message Key | Default | Explanation | +|--------------------------|-------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------| +| busyPlayerMessage | &cI'm busy at the moment. Come back later! | The message displayed when the blacksmith is serving another player. | +| busyReforgeMessage | &cI'm working on it. Be patient! I'll finish {time}! | The message displayed when the blacksmith is busy reforging an item. | +| coolDownUnexpiredMessage | &cYou've already had your chance! Give me a break! I'll be ready {time}! | The message displayed when the player has to wait for the cool-down to expire before using the blacksmith again. | +| costMessage | &eIt will cost &a{cost}&e to reforge that &a{item}&e! Click again to reforge! | The message displayed when telling a player about the cost of repairing an item. | +| failReforgeMessage | &cWhoops! Didn't mean to do that! Maybe next time? | The message displayed when a blacksmith fails to reforge an item. | +| insufficientFundsMessage | &cYou don't have enough money to reforge that item! | The message displayed when a player is unable to pay for reforging an item. | +| invalidItemMessage | &cI'm sorry, but I'm a/an {title}, I don't know how to reforge that! | The message displayed when a blacksmith is presented an item which it cannot repair. | +| itemChangedMessage | &cThat's not the item you wanted to reforge before! | The message displayed when a player changes their item after being shown the repair cost. | +| startReforgeMessage | &eOk, let's see what I can do... | The message displayed when a blacksmith starts reforging an item. | +| successMessage | There you go! All better! | The message displayed when a blacksmith successfully repairs an item. | +| notDamagedMessage | &cThat item is not in need of repair | The message displayed if a player tries to reforge an item with full durability. | ### Scrapper global-only options -| Key | Value type | Default | Description | -|----------------|------------------------------------------------------------|---------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| basePrice | positive decimal number | 0 | The cost of using a scrapper | -| showExactTime | true/false | false | If true, scrappers will display exact time remaining in minutes and seconds, instead of vague expressions | -| giveExperience | true/false | true | If true, each enchantment level on the salvaged item will give one EXP level as salvage | -| trashSalvage | TARGET_MATERIAL:IGNORED_MATERIAL\[,\*_TARGET2:\*_IGNORED2] | `"*_SHOVEL;*_PICKAXE;*_AXE;*_HOE;*_SWORD;SHIELD;*_BOW:STICK"` | The items that should be deferred when calculating partial salvage. Because receiving just the sticks when salvaging a diamond pickaxe is kind of sad, this allows specifying for example: `*_SHOVEL;*_PICKAXE;*_AXE;*_HOE;*_SWORD;SHIELD;*_BOW:STICK` (the default) for deferring sticks in salvage for shovels, pickaxes, axes, hoes and swords. A `:` character splits selected items and the trash salvage. Different item specifications are split by a `;` character. Use `,` to split separate trash salvages when using commands, like: `SHIELD:STICK,BOW_STRING` | +| Key | Value type | Default | Description | +|----------------|------------------------------------------------------------|---------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| basePrice | positive decimal number | 0 | The cost of using a scrapper. | +| showExactTime | true/false | false | If true, scrappers will display exact time remaining in minutes and seconds, instead of vague expressions. | +| giveExperience | true/false | true | If true, each enchantment level on the salvaged item will give one EXP level as salvage. | +| trashSalvage | TARGET_MATERIAL:IGNORED_MATERIAL\[,\*_TARGET2:\*_IGNORED2] | `"*_SHOVEL;*_PICKAXE;*_AXE;*_HOE;*_SWORD;SHIELD;*_BOW:STICK"` | The items that should be deferred when calculating partial salvage. Because receiving just the sticks when salvaging a diamond pickaxe is kind of sad, this allows specifying for example: `*_SHOVEL;*_PICKAXE;*_AXE;*_HOE;*_SWORD;SHIELD;*_BOW:STICK` (the default) for deferring sticks in salvage for shovels, pickaxes, axes, hoes and swords. A `:` character splits selected items and the trash salvage. Different item specifications are split by a `;` character. Use `,` to split separate trash salvages when using commands, like: `SHIELD:STICK,BOW_STRING`. | ### Scrapper per-npc (with default values set in config.yml) @@ -231,28 +231,31 @@ All currently supported presets, and available filters for each preset: | maxSalvageWaitTimeSeconds | 0-3600 | 5 | The maximum number of seconds a player needs to wait for an item to be salvaged. | | minSalvageWaitTimeSeconds | 0-3600 | 30 | The minimum number of seconds a player needs to wait for an item to be salvaged. | | salvageCoolDownSeconds | 0-3600 | 60 | The cool-down, in seconds, a player has to wait between each time they use one specific scrapper. | -| scrapperTitle | text string | scrapper | The title displayed as part of the message explaining that a scrapper doesn't recognize a player's held item | +| scrapperTitle | text string | scrapper | The title displayed as part of the message explaining that a scrapper doesn't recognize a player's held item. | | extendedSalvageEnabled | true/false | false | Whether to enable the extended salvage behavior for this scrapper. As long as it is allowed by salvageAbleItems and it can be crafted in a crafting table, it can be salvaged. This includes things like four planks salvaged into wood. | | salvageEnchanted | true/false | false | Whether the scrapper is able to salvage enchanted items. This is disabled by default as it's very easy to accidentally salvage heavily enchanted items if one is not careful. | +| salvageArmorTrims | true/false | true | Whether the scrapper is able to salvage armor trims. | #### Messages -| Message Key | Default | Explanation | -|-------------------------------|-----------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------| -| busyPlayerMessage | &cI'm busy at the moment. Come back later! | The message displayed when the scrapper is serving another player | -| busySalvageMessage | &cI'm working on it. Be patient! I'll finish {time}! | The message displayed when the scrapper is busy salvaging an item | -| coolDownUnexpiredMessage | &cYou've already had your chance! Give me a break! I'll be ready {time}! | The message displayed when the player has to wait for the cool-down to expire before using the scrapper again | -| invalidItemMessage | &cI'm sorry, but I'm a/an {title}, I don't know how to salvage that! | The message displayed when a scrapper is presented an item which it cannot salvage | -| tooDamagedForSalvageMessage | &cThat item is too damaged to be salvaged into anything useful | The message displayed when a scrapper is presented with an item too damaged to produce salvage | -| successSalvagedMessage | There you go! | The message displayed when a scrapper successfully repairs an item | -| failSalvageMessage | &cWhoops! The item broke! Maybe next time? | The message displayed when a scrapper fails to salvage an item | -| itemChangedMessage | &cThat's not the item you wanted to salvage before! | The message displayed when a player changes their item after being shown the salvage cost | -| startSalvageMessage | &eOk, let's see what I can do... | The message displayed when a scrapper starts salvaging an item | -| insufficientFundsMessage | &cYou don't have enough money to salvage an item! | The message displayed when a player is unable to pay for scrapping an item | -| costMessage | &eIt will cost &a{cost}&e to salvage that item! {yield} &eClick again to salvage! | The message displayed when telling a player about the cost of scrapping an item | -| fullSalvageMessage | &aI should be able to extract all components from that pristine item.&r | The message displayed as part of costMessage's yield placeholder if all components will be returned | -| partialSalvageMessage | &cI cannot extract all components from that damaged item.&r | The message displayed as part of costMessage's yield placeholder if only some components will be returned | -| cannotSalvageEnchantedMessage | &cI'm sorry, but I'm unable to salvage enchanted items! | The message displayed when telling a player that the scrapper cannot salvage enchanted items | +| Message Key | Default | Explanation | +|---------------------------------|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------| +| busyPlayerMessage | &cI'm busy at the moment. Come back later! | The message displayed when the scrapper is serving another player. | +| busySalvageMessage | &cI'm working on it. Be patient! I'll finish {time}! | The message displayed when the scrapper is busy salvaging an item. | +| coolDownUnexpiredMessage | &cYou've already had your chance! Give me a break! I'll be ready {time}! | The message displayed when the player has to wait for the cool-down to expire before using the scrapper again. | +| invalidItemMessage | &cI'm sorry, but I'm a/an {title}, I don't know how to salvage that! | The message displayed when a scrapper is presented an item which it cannot salvage. | +| tooDamagedForSalvageMessage | &cThat item is too damaged to be salvaged into anything useful | The message displayed when a scrapper is presented with an item too damaged to produce salvage. | +| successSalvagedMessage | There you go! | The message displayed when a scrapper successfully repairs an item. | +| failSalvageMessage | &cWhoops! The item broke! Maybe next time? | The message displayed when a scrapper fails to salvage an item. | +| itemChangedMessage | &cThat's not the item you wanted to salvage before! | The message displayed when a player changes their item after being shown the salvage cost. | +| startSalvageMessage | &eOk, let's see what I can do... | The message displayed when a scrapper starts salvaging an item. | +| insufficientFundsMessage | &cYou don't have enough money to salvage an item! | The message displayed when a player is unable to pay for scrapping an item. | +| costMessage | &eIt will cost &a{cost}&e to salvage that item! {yield} &eClick again to salvage! | The message displayed when telling a player about the cost of scrapping an item. | +| fullSalvageMessage | &aI should be able to extract all components from that pristine item.&r | The message displayed as part of costMessage's yield placeholder if all components will be returned. | +| partialSalvageMessage | &cI cannot extract all components from that damaged item.&r | The message displayed as part of costMessage's yield placeholder if only some components will be returned. | +| cannotSalvageEnchantedMessage | &cI'm sorry, but I'm unable to salvage enchanted items! | The message displayed when telling a player that the scrapper cannot salvage enchanted items. | +| cannotSalvageArmorTrimMessage | &cI'm sorry, but I'm unable to salvage armor trims! | The message displayed when telling a player that the scrapper cannot salvage items with armor trim. | +| armorTrimSalvageNotFoundMessage | "&cI'm sorry, but I don't know how to salvage that armor trim!" | The message displayed when telling a player that the scrapper cannot find the correct items to return as armor trim salvage. | ## Language customization diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSettings.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSettings.java index f861b39..3d1873c 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSettings.java @@ -141,6 +141,16 @@ public class ScrapperNPCSettings implements TraitSettings { return asString(ScrapperSetting.COST_MESSAGE); } + /** + * Gets the message to use for displaying armor trim salvage cost + * + * @return

The message to use for displaying armor trim salvage cost

+ */ + @NotNull + public String getArmorTrimCostMessage() { + return asString(ScrapperSetting.COST_MESSAGE_ARMOR_TRIM); + } + @Override @NotNull public String getCoolDownUnexpiredMessage() { @@ -297,6 +307,15 @@ public class ScrapperNPCSettings implements TraitSettings { return asBoolean(ScrapperSetting.SALVAGE_ENCHANTED); } + /** + * Whether salvage of armor trims is enabled + * + * @return

True if this scrapper can salvage armor trims

+ */ + public boolean salvageArmorTrims() { + return asBoolean(ScrapperSetting.SALVAGE_ARMOR_TRIMS); + } + /** * Gets the title of this scrapper NPC * @@ -371,4 +390,29 @@ public class ScrapperNPCSettings implements TraitSettings { return asString(ScrapperSetting.CANNOT_SALVAGE_ENCHANTED_MESSAGE); } + /** + * Gets the message to display when explaining that this scrapper is unable to salvage armor trims + * + * @return

The cannot salvage armor trim message

+ */ + @NotNull + public String getCannotSalvageArmorTrimMessage() { + return asString(ScrapperSetting.CANNOT_SALVAGE_ARMOR_TRIM_MESSAGE); + } + + /** + * Gets the message to display when explaining that this scrapper is unable to find salvage for the armor trim + * + *

Because there is no direct way (that I have found) to convert TrimMaterial and TrimPattern to Material, armor + * trim salvaging relies on material string and a hard-coded map. As those are prone to breaking because of API + * changes, there is a high likelihood that cases will arise when the scrapper is unable to find the correct + * materials.

+ * + * @return

The cannot find armor trim salvage message

+ */ + @NotNull + public String getArmorTrimSalvageNotFoundMessage() { + return asString(ScrapperSetting.ARMOR_TRIM_SALVAGE_NOT_FOUND_MESSAGE); + } + } diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java index 466027f..91bf566 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java @@ -79,6 +79,12 @@ public enum ScrapperSetting implements Setting { SALVAGE_ENCHANTED("salvageEnchanted", SettingValueType.BOOLEAN, false, "Whether to enable salvaging of enchanted items. This is disabled by default because it's " + "possible to accidentally salvage items with very good enchantments.", true, false), + + /** + * The setting for whether the NPC should allow salvaging of armor trims + */ + SALVAGE_ARMOR_TRIMS("salvageArmorTrims", SettingValueType.BOOLEAN, true, + "Whether to enable salvaging of armor trims.", true, false), /*----------- | Messages | @@ -155,12 +161,19 @@ public enum ScrapperSetting implements Setting { "The message to display when a player cannot pay for the salvaging", true, true), /** - * The message displayed when displaying the cost of reforging the held item to the player + * The message displayed when displaying the cost of salvaging the held item to the player */ COST_MESSAGE("costMessage", SettingValueType.STRING, "&eIt will cost &a{cost}&e to salvage that item! {yield} Click again to salvage!", "The message to display when informing a player about the salvaging cost", true, true), + /** + * The message displayed when displaying the cost of salvaging the armor trim of the held item to the player + */ + COST_MESSAGE_ARMOR_TRIM("costMessageArmorTrim", SettingValueType.STRING, + "&eIt will cost &a{cost}&e to salvage that armor trim!", + "The message to display when explaining the shown item's armor trim's salvage cost", true, true), + /** * The message displayed when explaining that all items will be returned as salvage */ @@ -175,10 +188,27 @@ public enum ScrapperSetting implements Setting { "&cI cannot extract all components from that damaged item.&r", "The message to display when explaining expected partial yield as part of the cost message", true, true), + /** + * The message displayed when explaining that enchanted item salvage is disabled + */ CANNOT_SALVAGE_ENCHANTED_MESSAGE("cannotSalvageEnchantedMessage", SettingValueType.STRING, "&cI'm sorry, but I'm unable to salvage enchanted items!", "The message to display when asked to salvage an enchanted item, and that option is disabled", true, true), + /** + * The message displayed when explaining that armor trim salvage is disabled + */ + CANNOT_SALVAGE_ARMOR_TRIM_MESSAGE("cannotSalvageArmorTrimMessage", SettingValueType.STRING, + "&cI'm sorry, but I'm unable to salvage armor trims!", + "The message to display when asked to salvage an armor trim, and that option is disabled", true, true), + + /** + * The message displayed when explaining that the items to return as armor trim salvage are unknown + */ + ARMOR_TRIM_SALVAGE_NOT_FOUND_MESSAGE("armorTrimSalvageNotFoundMessage", SettingValueType.STRING, + "&cI'm sorry, but I don't know how to salvage that armor trim!", + "The message to display if the correct materials to return for the armor trim are unknown", true, true), + /*------------------ | Global settings | ------------------*/ diff --git a/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java b/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java index 18b2e42..068d5fe 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java @@ -13,6 +13,7 @@ import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ArmorMeta; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -87,6 +88,23 @@ public class ScrapperTrait extends CustomTrait { return; } + List salvage = null; + + // Deal with armor trim salvage + boolean salvagingArmorTrim = false; + if (itemInHand.getItemMeta() instanceof ArmorMeta armorMeta && armorMeta.hasTrim()) { + if (!getSettings().salvageArmorTrims()) { + sendNPCMessage(this.npc, player, getSettings().getCannotSalvageArmorTrimMessage()); + return; + } + salvage = SalvageHelper.getTrimSalvage(itemInHand, armorMeta); + if (salvage == null) { + sendNPCMessage(this.npc, player, getSettings().getArmorTrimSalvageNotFoundMessage()); + return; + } + salvagingArmorTrim = true; + } + // Check if the item is enchanted, and whether this blacksmith can salvage it if (!itemInHand.getEnchantments().isEmpty() && !getSettings().salvageEnchanted()) { sendNPCMessage(this.npc, player, getSettings().getCannotSalvageEnchantedMessage()); @@ -94,7 +112,9 @@ public class ScrapperTrait extends CustomTrait { } // Check if any salvage will be produced - List salvage = getSalvage(itemInHand, extended); + if (salvage == null) { + salvage = getSalvage(itemInHand, extended); + } boolean noUsefulSalvage = salvage == null || salvage.isEmpty(); if (noUsefulSalvage) { sendNPCMessage(this.npc, player, getSettings().getTooDamagedMessage()); @@ -114,8 +134,13 @@ public class ScrapperTrait extends CustomTrait { expectedYield = getSettings().getPartialSalvageMessage(); } - sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholders(getSettings().getCostMessage(), - List.of("{cost}", "{yield}"), List.of(cost, expectedYield))); + if (salvagingArmorTrim) { + sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getArmorTrimCostMessage(), + "{cost}", cost)); + } else { + sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholders(getSettings().getCostMessage(), + List.of("{cost}", "{yield}"), List.of(cost, expectedYield))); + } } @Override diff --git a/src/main/java/net/knarcraft/blacksmith/util/SalvageHelper.java b/src/main/java/net/knarcraft/blacksmith/util/SalvageHelper.java index 4c9bf5b..37eced8 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/SalvageHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/SalvageHelper.java @@ -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 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

The item to have its armor trim salvaged

+ * @param armorMeta

The armor meta of the item to salvage

+ * @return

The salvage, or null if salvage could not be calculated

+ */ + @Nullable + public static List getTrimSalvage(@NotNull ItemStack item, @NotNull ArmorMeta armorMeta) { + ArmorTrim armorTrim = armorMeta.getTrim(); + if (armorTrim == null) { + return null; + } + + List 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 diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7ea2127..b3795b0 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -167,6 +167,9 @@ scrapper: # salvage items with very good enchantments. salvageEnchanted: false + # Whether to enable salvaging of armor trims + salvageArmorTrims: true + # Default values for messages used by NPCs messages: # The message to display when another player is using the scrapper @@ -202,6 +205,9 @@ scrapper: # The message to display when explaining the shown item's salvage cost costMessage: "&eIt will cost &a{cost}&e to salvage that item! {yield} &eClick again to salvage!" + # The message to display when explaining the shown item's armor trim's salvage cost + costMessageArmorTrim: "&eIt will cost &a{cost}&e to salvage that armor trim!" + # The yield message to display if trying to salvage a non-damaged item fullSalvageMessage: "&aI should be able to extract all components from that pristine item.&r" @@ -209,4 +215,10 @@ scrapper: partialSalvageMessage: "&cI cannot extract all components from that damaged item.&r" # The message to display when asked to salvage an enchanted item, and that option is disabled - cannotSalvageEnchantedMessage: "&cI'm sorry, but I'm unable to salvage enchanted items!" \ No newline at end of file + cannotSalvageEnchantedMessage: "&cI'm sorry, but I'm unable to salvage enchanted items!" + + # The message to display when asked to salvage an armor trim, and that option is disabled + cannotSalvageArmorTrimMessage: "&cI'm sorry, but I'm unable to salvage armor trims!" + + # The message to display if the correct materials to return for the armor trim are unknown + armorTrimSalvageNotFoundMessage: "&cI'm sorry, but I don't know how to salvage that armor trim!" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 14c8892..ea914b7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,43 +6,43 @@ main: net.knarcraft.blacksmith.BlacksmithPlugin depend: [ Citizens, Vault ] prefix: "Blacksmith" -api-version: 1.19 +api-version: 1.20 commands: blacksmith: permission: blacksmith.edit usage: /