Implements enchantment salvage toggle #23
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-05-05 23:45:59 +02:00
parent d7bac6d08f
commit d405c0dcff
4 changed files with 70 additions and 17 deletions

View File

@ -188,7 +188,7 @@ public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
* @return <p>Whether to drop reforged items on the ground</p> * @return <p>Whether to drop reforged items on the ground</p>
*/ */
public boolean getDropItem() { public boolean getDropItem() {
return ConfigHelper.asBoolean(getValue(ScrapperSetting.DROP_ITEM)); return asBoolean(ScrapperSetting.DROP_ITEM);
} }
@Override @Override
@ -228,6 +228,16 @@ public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
return getValue(setting).toString(); return getValue(setting).toString();
} }
/**
* Gets the boolean value of the given setting
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a boolean</p>
*/
private boolean asBoolean(@NotNull ScrapperSetting setting) {
return ConfigHelper.asBoolean(getValue(setting));
}
/** /**
* Gets the value of a setting, using the default if not set * Gets the value of a setting, using the default if not set
* *
@ -275,7 +285,16 @@ public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
* @return <p>True if extended salvaging is enabled</p> * @return <p>True if extended salvaging is enabled</p>
*/ */
public boolean extendedSalvageEnabled() { public boolean extendedSalvageEnabled() {
return ConfigHelper.asBoolean(getValue(ScrapperSetting.EXTENDED_SALVAGE_ENABLED)); return asBoolean(ScrapperSetting.EXTENDED_SALVAGE_ENABLED);
}
/**
* Whether salvage of enchanted items is enabled
*
* @return <p>True of this scrapper can salvage enchanted items</p>
*/
public boolean salvageEnchanted() {
return asBoolean(ScrapperSetting.SALVAGE_ENCHANTED);
} }
/** /**
@ -341,4 +360,15 @@ public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
return asString(ScrapperSetting.PARTIAL_SALVAGE_MESSAGE); return asString(ScrapperSetting.PARTIAL_SALVAGE_MESSAGE);
} }
/**
* Gets the message to display when explaining that this scrapper is unable to salvage enchanted items
*
* @return <p>The cannot salvage enchanted message</p>
*/
@NotNull
public String getCannotSalvageEnchantedMessage() {
return asString(ScrapperSetting.CANNOT_SALVAGE_ENCHANTED_MESSAGE);
}
} }

View File

@ -73,6 +73,13 @@ public enum ScrapperSetting implements Setting {
EXTENDED_SALVAGE_ENABLED("extendedSalvageEnabled", SettingValueType.BOOLEAN, false, EXTENDED_SALVAGE_ENABLED("extendedSalvageEnabled", SettingValueType.BOOLEAN, false,
"Whether to enable salvaging of non-repairable items, such as planks", true, false), "Whether to enable salvaging of non-repairable items, such as planks", true, false),
/**
* The setting for whether the NPC should allow salvaging of enchanted items
*/
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),
/*----------- /*-----------
| Messages | | Messages |
-----------*/ -----------*/
@ -168,6 +175,10 @@ public enum ScrapperSetting implements Setting {
"&cI cannot extract all components from that damaged item.&r", "&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 to display when explaining expected partial yield as part of the cost message", true, true),
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),
/*------------------ /*------------------
| Global settings | | Global settings |
------------------*/ ------------------*/

View File

@ -45,7 +45,6 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
* *
* @return <p>The current settings for this NPC</p> * @return <p>The current settings for this NPC</p>
*/ */
@SuppressWarnings("unused")
@NotNull @NotNull
public ScrapperNPCSettings getSettings() { public ScrapperNPCSettings getSettings() {
return config; return config;
@ -58,7 +57,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
*/ */
@Override @Override
public void load(@NotNull DataKey key) { public void load(@NotNull DataKey key) {
config.loadVariables(key); getSettings().loadVariables(key);
} }
/** /**
@ -68,7 +67,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
*/ */
@Override @Override
public void save(@NotNull DataKey key) { public void save(@NotNull DataKey key) {
config.saveVariables(key); getSettings().saveVariables(key);
} }
/** /**
@ -78,38 +77,44 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
*/ */
public void startSession(@NotNull Player player) { public void startSession(@NotNull Player player) {
ItemStack itemInHand = player.getInventory().getItemInMainHand(); ItemStack itemInHand = player.getInventory().getItemInMainHand();
List<Material> salvageAbleItems = this.config.getSalvageAbleItems(); List<Material> salvageAbleItems = getSettings().getSalvageAbleItems();
boolean extended = this.config.extendedSalvageEnabled(); boolean extended = getSettings().extendedSalvageEnabled();
// Check if the item can be salvaged
if (!canBeSalvaged(player.getServer(), itemInHand, salvageAbleItems, extended)) { if (!canBeSalvaged(player.getServer(), itemInHand, salvageAbleItems, extended)) {
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getInvalidItemMessage(), sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(),
"{title}", config.getScrapperTitle())); "{title}", getSettings().getScrapperTitle()));
return; return;
} }
List<ItemStack> salvage = getSalvage(itemInHand, extended); // 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());
return;
}
// If extended mode is disabled, only allow repairable items to be salvaged // Check if any salvage will be produced
List<ItemStack> salvage = getSalvage(itemInHand, extended);
boolean noUsefulSalvage = salvage == null || salvage.isEmpty(); boolean noUsefulSalvage = salvage == null || salvage.isEmpty();
if (noUsefulSalvage) { if (noUsefulSalvage) {
sendNPCMessage(this.npc, player, config.getTooDamagedMessage()); sendNPCMessage(this.npc, player, getSettings().getTooDamagedMessage());
return; return;
} }
//Start a new scrapper session for the player //Start a new scrapper session for the player
currentSessionStartTime = System.currentTimeMillis(); currentSessionStartTime = System.currentTimeMillis();
session = new SalvageSession(this, player, npc, config, salvage); session = new SalvageSession(this, player, npc, getSettings(), salvage);
//Tell the player the cost of repairing the item //Tell the player the cost of repairing the item
String cost = EconomyManager.formatScrapperCost(); String cost = EconomyManager.formatScrapperCost();
String expectedYield; String expectedYield;
if (ItemHelper.getDamage(itemInHand) <= 0) { if (ItemHelper.getDamage(itemInHand) <= 0) {
expectedYield = config.getFullSalvageMessage(); expectedYield = getSettings().getFullSalvageMessage();
} else { } else {
expectedYield = config.getPartialSalvageMessage(); expectedYield = getSettings().getPartialSalvageMessage();
} }
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholders(config.getCostMessage(), sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholders(getSettings().getCostMessage(),
List.of("{cost}", "{yield}"), List.of(cost, expectedYield))); List.of("{cost}", "{yield}"), List.of(cost, expectedYield)));
} }

View File

@ -163,6 +163,10 @@ scrapper:
# Whether to enable salvaging of non-repairable items, such as planks # Whether to enable salvaging of non-repairable items, such as planks
extendedSalvageEnabled: false extendedSalvageEnabled: 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.
salvageEnchanted: false
# Default values for messages used by NPCs # Default values for messages used by NPCs
messages: messages:
# The message to display when another player is using the scrapper # The message to display when another player is using the scrapper
@ -203,3 +207,6 @@ scrapper:
# The yield message to display if trying to salvage a damaged item # The yield message to display if trying to salvage a damaged item
partialSalvageMessage: "&cI cannot extract all components from that damaged item.&r" 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!"