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 { private final Map currentValues = new HashMap<>(); private final GlobalScrapperSettings globalScrapperSettings; private final List salvageAbleItems = new ArrayList<>(); /** * Instantiates a new scrapper NPC settings object * * @param globalScrapperSettings

The global settings object to get default values from

*/ public ScrapperNPCSettings(GlobalScrapperSettings globalScrapperSettings) { this.globalScrapperSettings = globalScrapperSettings; } /** * Loads variables from the given data key * * @param key

The data key to load variables from

*/ 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

The data key to save variables to

*/ 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

The setting to change

* @param newValue

The new value of the setting

*/ 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 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

The setting to get the value of

* @return

The current value of the setting

*/ 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

The salvage success message

*/ @NotNull public String getSuccessMessage() { return asString(ScrapperSetting.SUCCESS_SALVAGE_MESSAGE); } /** * Gets the message to display when a scrapper fails to salvage an item * * @return

The salvage fail message

*/ @NotNull public String getFailMessage() { return asString(ScrapperSetting.FAIL_SALVAGE_MESSAGE); } /** * Gets the message to use for displaying salvage cost * * @return

The message to use for displaying salvage cost

*/ @NotNull public String getCostMessage() { return asString(ScrapperSetting.COST_MESSAGE); } @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

The minimum salvage delay

*/ public int getMinSalvageDelay() { return asInt(ScrapperSetting.MIN_SALVAGE_DELAY); } /** * Gets the maximum delay used to wait for a salvaging to finish * * @return

The maximum salvage delay

*/ public int getMaxSalvageDelay() { return asInt(ScrapperSetting.MAX_SALVAGE_DELAY); } /** * Gets the cool-down between each salvaging * * @return

The salvage cool-down

*/ 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

Whether to drop reforged items on the ground

*/ public boolean getDropItem() { return ConfigHelper.asBoolean(getValue(ScrapperSetting.DROP_ITEM)); } @Override public boolean getDisableCoolDown() { return asInt(ScrapperSetting.SALVAGE_COOL_DOWN) <= 0; } /** * Gets the chance of a salvaging to fail * * @return

The chance of a salvaging to fail

*/ public int getFailChance() { return asInt(ScrapperSetting.FAIL_SALVAGE_CHANCE); } /** * Gets the given value as an integer * *

This will throw an exception if used for a non-integer setting

* * @param setting

The setting to get the value of

* @return

The value of the given setting as an integer

*/ private int asInt(@NotNull ScrapperSetting setting) { return ConfigHelper.asInt(getValue(setting)); } /** * Gets the string value of the given setting * * @param setting

The setting to get the value of

* @return

The value of the given setting as a string

*/ @NotNull private String asString(@NotNull ScrapperSetting setting) { return getValue(setting).toString(); } /** * Gets the value of a setting, using the default if not set * * @param setting

The setting to get the value of

* @return

The current value

*/ @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 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

All salvageable items

*/ @NotNull public List 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 * *

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.

* * @return

True if extended salvaging is enabled

*/ public boolean extendedSalvageEnabled() { return ConfigHelper.asBoolean(getValue(ScrapperSetting.EXTENDED_SALVAGE_ENABLED)); } /** * Gets the title of this scrapper NPC * *

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.

* * @return

The title of this scrapper NPC

*/ @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

The message to display

*/ @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

The message to display

*/ @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

The no salvage message

*/ @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

The full salvage message

*/ @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

The partial salvage message

*/ @NotNull public String getPartialSalvageMessage() { return asString(ScrapperSetting.PARTIAL_SALVAGE_MESSAGE); } }