EpicKnarvik97 33ef557771
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
Fixes some savage fail problems
Fixes item not being returned when salvage fails
Removes some redundancy when giving back items
Makes extended salvage return 50% of the salvage (no items if only one item would be returned)
Adds a better message when failing to salvage extended salvage items
Fixes a bug in the caching of smith presets
2024-05-07 01:45:48 +02:00

458 lines
14 KiB
Java

package net.knarcraft.blacksmith.config.scrapper;
import net.citizensnpcs.api.util.DataKey;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.TraitSettings;
import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
private final Map<ScrapperSetting, Object> currentValues = new HashMap<>();
private final GlobalScrapperSettings globalScrapperSettings;
private final List<Material> salvageAbleItems = new ArrayList<>();
/**
* Instantiates a new scrapper NPC settings object
*
* @param globalScrapperSettings <p>The global settings object to get default values from</p>
*/
public ScrapperNPCSettings(GlobalScrapperSettings globalScrapperSettings) {
this.globalScrapperSettings = globalScrapperSettings;
}
/**
* Loads variables from the given data key
*
* @param key <p>The data key to load variables from</p>
*/
public void loadVariables(DataKey key) {
for (ScrapperSetting setting : ScrapperSetting.values()) {
if (key.keyExists(setting.getChildPath())) {
currentValues.put(setting, key.getRaw(setting.getChildPath()));
}
}
}
/**
* Saves variables to the given data key
*
* @param key <p>The data key to save variables to</p>
*/
public void saveVariables(DataKey key) {
for (ScrapperSetting setting : ScrapperSetting.values()) {
key.setRaw(setting.getChildPath(), currentValues.get(setting));
}
}
/**
* Changes one setting to the given value
*
* @param setting <p>The setting to change</p>
* @param newValue <p>The new value of the setting</p>
*/
public void changeValue(@NotNull ScrapperSetting setting, @Nullable Object newValue) {
if (setting.getValueType() == SettingValueType.STRING_LIST ||
setting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) {
//Workaround to make sure it's treated as the correct type
currentValues.put(setting, newValue == null ? null : ConfigHelper.asStringList(newValue));
} else {
currentValues.put(setting, newValue);
}
if (setting == ScrapperSetting.SALVAGE_ABLE_ITEMS) {
updateSalvageAbleItems();
}
}
/**
* Updates the reforge-able items according to the current value of the setting
*/
private void updateSalvageAbleItems() {
this.salvageAbleItems.clear();
List<String> materialStrings = ConfigHelper.asStringList(getValue(ScrapperSetting.SALVAGE_ABLE_ITEMS));
if (materialStrings != null) {
this.salvageAbleItems.addAll(ItemHelper.getItems(materialStrings, !extendedSalvageEnabled()));
}
}
/**
* Gets the raw current value of a setting
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The current value of the setting</p>
*/
public @NotNull Object getRawValue(@NotNull ScrapperSetting setting) {
return currentValues.get(setting);
}
@Override
@NotNull
public String getBusyWithPlayerMessage() {
return asString(ScrapperSetting.BUSY_WITH_PLAYER_MESSAGE);
}
@Override
@NotNull
public String getBusyWorkingMessage() {
return asString(ScrapperSetting.BUSY_WITH_SALVAGE_MESSAGE);
}
@Override
@NotNull
public String getStartWorkingMessage() {
return asString(ScrapperSetting.START_SALVAGE_MESSAGE);
}
/**
* Gets the message to display when a scrapper has successfully scrapped an item
*
* @return <p>The salvage success message</p>
*/
@NotNull
public String getSuccessMessage() {
return asString(ScrapperSetting.SUCCESS_SALVAGE_MESSAGE);
}
/**
* Gets the message to display when a scrapper fails to salvage an item
*
* @return <p>The salvage fail message</p>
*/
@NotNull
public String getFailSalvageMessage() {
return asString(ScrapperSetting.FAIL_SALVAGE_MESSAGE);
}
/**
* Gets the message to display when a scrapper fails to salvage an item without durability
*
* @return <p>The extended salvage fail message</p>
*/
@NotNull
public String getFailExtendedSalvageMessage() {
return asString(ScrapperSetting.FAIL_SALVAGE_EXTENDED_MESSAGE);
}
/**
* Gets the message to use for displaying salvage cost
*
* @return <p>The message to use for displaying salvage cost</p>
*/
@NotNull
public String getCostMessage() {
return asString(ScrapperSetting.COST_MESSAGE);
}
/**
* Gets the message to use for displaying armor trim salvage cost
*
* @return <p>The message to use for displaying armor trim salvage cost</p>
*/
@NotNull
public String getArmorTrimCostMessage() {
return asString(ScrapperSetting.COST_MESSAGE_ARMOR_TRIM);
}
/**
* Gets the message to use for displaying netherite salvage cost
*
* @return <p>The message to use for displaying netherite salvage cost</p>
*/
@NotNull
public String getNetheriteCostMessage() {
return asString(ScrapperSetting.COST_MESSAGE_NETHERITE);
}
@Override
@NotNull
public String getCoolDownUnexpiredMessage() {
return asString(ScrapperSetting.COOL_DOWN_UNEXPIRED_MESSAGE);
}
/**
* The message displayed if a player presents a different item after seeing the price to salvage an item
*/
@NotNull
public String getItemChangedMessage() {
return asString(ScrapperSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE);
}
/**
* Gets the minimum delay used to wait for a salvaging to finish.
*
* @return <p>The minimum salvage delay</p>
*/
public int getMinSalvageDelay() {
return asInt(ScrapperSetting.MIN_SALVAGE_DELAY);
}
/**
* Gets the maximum delay used to wait for a salvaging to finish
*
* @return <p>The maximum salvage delay</p>
*/
public int getMaxSalvageDelay() {
return asInt(ScrapperSetting.MAX_SALVAGE_DELAY);
}
/**
* Gets the cool-down between each salvaging
*
* @return <p>The salvage cool-down</p>
*/
public int getSalvageCoolDown() {
return asInt(ScrapperSetting.SALVAGE_COOL_DOWN);
}
/**
* Gets whether an item should be dropped on the ground instead of being given to the player
*
* @return <p>Whether to drop reforged items on the ground</p>
*/
public boolean getDropItem() {
return asBoolean(ScrapperSetting.DROP_ITEM);
}
@Override
public boolean getDisableCoolDown() {
return asInt(ScrapperSetting.SALVAGE_COOL_DOWN) <= 0;
}
/**
* Gets the chance of a salvaging to fail
*
* @return <p>The chance of a salvaging to fail</p>
*/
public int getFailChance() {
return asInt(ScrapperSetting.FAIL_SALVAGE_CHANCE);
}
/**
* Gets the given value as an integer
*
* <p>This will throw an exception if used for a non-integer setting</p>
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as an integer</p>
*/
private int asInt(@NotNull ScrapperSetting setting) {
return ConfigHelper.asInt(getValue(setting));
}
/**
* Gets the string 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 string</p>
*/
@NotNull
private String asString(@NotNull ScrapperSetting setting) {
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
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The current value</p>
*/
@NotNull
private Object getValue(@NotNull ScrapperSetting setting) {
Object value = currentValues.get(setting);
//If not set, use the default value from the config.yml file
if (value == null) {
Map<ScrapperSetting, Object> defaultNPCSettings = globalScrapperSettings.getDefaultNPCSettings();
if (defaultNPCSettings.containsKey(setting)) {
value = defaultNPCSettings.get(setting);
}
}
//If not set in config.yml, use the default value from the enum
if (value == null) {
value = setting.getDefaultValue();
}
return value;
}
/**
* Gets all item types this NPC is able to salvage
*
* @return <p>All salvageable items</p>
*/
@NotNull
public List<Material> getSalvageAbleItems() {
Object currentValue = currentValues.get(ScrapperSetting.SALVAGE_ABLE_ITEMS);
if (currentValue == null || String.valueOf(currentValue).isEmpty()) {
return globalScrapperSettings.getSalvageAbleItems();
} else {
return new ArrayList<>(this.salvageAbleItems);
}
}
/**
* Whether extended salvaging is enabled
*
* <p>When not extended, only repairable items can be salvaged. When extended, any item produced using a crafting
* recipe can be salvaged. This does not include smelting or similar.</p>
*
* @return <p>True if extended salvaging is enabled</p>
*/
public boolean extendedSalvageEnabled() {
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);
}
/**
* Whether salvage of armor trims is enabled
*
* @return <p>True if this scrapper can salvage armor trims</p>
*/
public boolean salvageArmorTrims() {
return asBoolean(ScrapperSetting.SALVAGE_ARMOR_TRIMS);
}
/**
* Whether salvage of netherite items is enabled
*
* @return <p>True if this scrapper can salvage netherite items</p>
*/
public boolean salvageNetherite() {
return asBoolean(ScrapperSetting.SALVAGE_NETHERITE);
}
/**
* Gets the title of this scrapper NPC
*
* <p>The title is used to specify scrapper sub-types in order to make it clear which items a scrapper can salvage.
* For example, an armor-scrapper would only be able to salvage armor pieces.</p>
*
* @return <p>The title of this scrapper NPC</p>
*/
@NotNull
public String getScrapperTitle() {
return asString(ScrapperSetting.SCRAPPER_TITLE);
}
/**
* Gets the message to display if this NPC is given an item it cannot salvage
*
* @return <p>The message to display</p>
*/
@NotNull
public String getInvalidItemMessage() {
return asString(ScrapperSetting.INVALID_ITEM_MESSAGE);
}
/**
* Gets the message to display if the player is unable to afford an item salvage
*
* @return <p>The message to display</p>
*/
@NotNull
public String getInsufficientFundsMessage() {
return asString(ScrapperSetting.INSUFFICIENT_FUNDS_MESSAGE);
}
/**
* Gets the message to display if an item is too damaged to produce any salvage
*
* @return <p>The no salvage message</p>
*/
@NotNull
public String getTooDamagedMessage() {
return asString(ScrapperSetting.TOO_DAMAGED_FOR_SALVAGE_MESSAGE);
}
/**
* Gets the message to display when explaining that full salvage will be given for an item
*
* @return <p>The full salvage message</p>
*/
@NotNull
public String getFullSalvageMessage() {
return asString(ScrapperSetting.FULL_SALVAGE_MESSAGE);
}
/**
* Gets the message to display when explaining that only partial salvage will be given for an item
*
* @return <p>The partial salvage message</p>
*/
@NotNull
public String getPartialSalvageMessage() {
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);
}
/**
* Gets the message to display when explaining that this scrapper is unable to salvage armor trims
*
* @return <p>The cannot salvage armor trim message</p>
*/
@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
*
* <p>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.</p>
*
* @return <p>The cannot find armor trim salvage message</p>
*/
@NotNull
public String getArmorTrimSalvageNotFoundMessage() {
return asString(ScrapperSetting.ARMOR_TRIM_SALVAGE_NOT_FOUND_MESSAGE);
}
/**
* Gets the message to display when explaining that this scrapper is unable to salvage netherite items
*
* @return <p>The message to display when explaining that this scrapper is unable to salvage netherite items</p>
*/
@NotNull
public String getCannotSalvageNetheriteMessage() {
return asString(ScrapperSetting.CANNOT_SALVAGE_NETHERITE_MESSAGE);
}
}