2023-11-12 19:02:11 +01:00
|
|
|
package net.knarcraft.blacksmith.config.scrapper;
|
|
|
|
|
2023-11-14 16:04:48 +01:00
|
|
|
import net.citizensnpcs.api.util.DataKey;
|
|
|
|
import net.knarcraft.blacksmith.config.SettingValueType;
|
|
|
|
import net.knarcraft.blacksmith.config.TraitSettings;
|
|
|
|
import net.knarcraft.blacksmith.util.ConfigHelper;
|
2023-12-30 13:56:33 +01:00
|
|
|
import net.knarcraft.blacksmith.util.ItemHelper;
|
|
|
|
import org.bukkit.Material;
|
2024-05-04 01:01:56 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
2023-11-14 16:04:48 +01:00
|
|
|
|
2023-12-30 13:56:33 +01:00
|
|
|
import java.util.ArrayList;
|
2023-11-14 16:04:48 +01:00
|
|
|
import java.util.HashMap;
|
2023-12-30 13:56:33 +01:00
|
|
|
import java.util.List;
|
2023-11-14 16:04:48 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
2023-11-16 13:06:24 +01:00
|
|
|
public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
|
2023-12-30 13:56:33 +01:00
|
|
|
|
2023-11-16 01:17:27 +01:00
|
|
|
private final Map<ScrapperSetting, Object> currentValues = new HashMap<>();
|
2023-11-14 16:04:48 +01:00
|
|
|
private final GlobalScrapperSettings globalScrapperSettings;
|
2023-12-30 13:56:33 +01:00
|
|
|
private final List<Material> salvageAbleItems = new ArrayList<>();
|
2023-11-14 16:04:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2023-11-16 01:17:27 +01:00
|
|
|
for (ScrapperSetting setting : ScrapperSetting.values()) {
|
2023-11-14 16:04:48 +01:00
|
|
|
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) {
|
2023-11-16 01:17:27 +01:00
|
|
|
for (ScrapperSetting setting : ScrapperSetting.values()) {
|
2023-11-14 16:04:48 +01:00
|
|
|
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>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
public void changeValue(@NotNull ScrapperSetting setting, @Nullable Object newValue) {
|
2023-11-14 16:04:48 +01:00
|
|
|
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);
|
|
|
|
}
|
2023-12-30 13:56:33 +01:00
|
|
|
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()));
|
|
|
|
}
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
public @NotNull Object getRawValue(@NotNull ScrapperSetting setting) {
|
2023-11-14 16:04:48 +01:00
|
|
|
return currentValues.get(setting);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-11-14 16:04:48 +01:00
|
|
|
public String getBusyWithPlayerMessage() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asString(ScrapperSetting.BUSY_WITH_PLAYER_MESSAGE);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-11-14 16:04:48 +01:00
|
|
|
public String getBusyWorkingMessage() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asString(ScrapperSetting.BUSY_WITH_SALVAGE_MESSAGE);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-11-14 16:04:48 +01:00
|
|
|
public String getStartWorkingMessage() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asString(ScrapperSetting.START_SALVAGE_MESSAGE);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-30 13:56:33 +01:00
|
|
|
* Gets the message to display when a scrapper has successfully scrapped an item
|
2023-11-14 16:04:48 +01:00
|
|
|
*
|
2023-12-30 13:56:33 +01:00
|
|
|
* @return <p>The salvage success message</p>
|
2023-11-14 16:04:48 +01:00
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-11-14 16:04:48 +01:00
|
|
|
public String getSuccessMessage() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asString(ScrapperSetting.SUCCESS_SALVAGE_MESSAGE);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
2023-12-30 13:56:33 +01:00
|
|
|
/**
|
|
|
|
* Gets the message to display when a scrapper fails to salvage an item
|
|
|
|
*
|
|
|
|
* @return <p>The salvage fail message</p>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-12-30 13:56:33 +01:00
|
|
|
public String getFailMessage() {
|
|
|
|
return asString(ScrapperSetting.FAIL_SALVAGE_MESSAGE);
|
|
|
|
}
|
|
|
|
|
2024-05-04 01:01:56 +02:00
|
|
|
/**
|
|
|
|
* Gets the message to use for displaying salvage cost
|
|
|
|
*
|
|
|
|
* @return <p>The message to use for displaying salvage cost</p>
|
|
|
|
*/
|
2024-05-05 15:37:38 +02:00
|
|
|
@NotNull
|
2024-05-04 01:01:56 +02:00
|
|
|
public String getCostMessage() {
|
|
|
|
return asString(ScrapperSetting.COST_MESSAGE);
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:04:48 +01:00
|
|
|
@Override
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-11-14 16:04:48 +01:00
|
|
|
public String getCoolDownUnexpiredMessage() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asString(ScrapperSetting.COOL_DOWN_UNEXPIRED_MESSAGE);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The message displayed if a player presents a different item after seeing the price to salvage an item
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-11-14 16:04:48 +01:00
|
|
|
public String getItemChangedMessage() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asString(ScrapperSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the minimum delay used to wait for a salvaging to finish.
|
|
|
|
*
|
|
|
|
* @return <p>The minimum salvage delay</p>
|
|
|
|
*/
|
|
|
|
public int getMinSalvageDelay() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asInt(ScrapperSetting.MIN_SALVAGE_DELAY);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum delay used to wait for a salvaging to finish
|
|
|
|
*
|
|
|
|
* @return <p>The maximum salvage delay</p>
|
|
|
|
*/
|
|
|
|
public int getMaxSalvageDelay() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asInt(ScrapperSetting.MAX_SALVAGE_DELAY);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the cool-down between each salvaging
|
|
|
|
*
|
|
|
|
* @return <p>The salvage cool-down</p>
|
|
|
|
*/
|
|
|
|
public int getSalvageCoolDown() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asInt(ScrapperSetting.SALVAGE_COOL_DOWN);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
2023-11-20 13:20:11 +01:00
|
|
|
return ConfigHelper.asBoolean(getValue(ScrapperSetting.DROP_ITEM));
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean getDisableCoolDown() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asInt(ScrapperSetting.SALVAGE_COOL_DOWN) <= 0;
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
2023-12-30 13:56:33 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:04:48 +01:00
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
private int asInt(@NotNull ScrapperSetting setting) {
|
2023-11-14 16:04:48 +01:00
|
|
|
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>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
|
|
|
private String asString(@NotNull ScrapperSetting setting) {
|
2023-11-14 16:04:48 +01:00
|
|
|
return getValue(setting).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
|
|
|
private Object getValue(@NotNull ScrapperSetting setting) {
|
2023-11-14 16:04:48 +01:00
|
|
|
Object value = currentValues.get(setting);
|
|
|
|
//If not set, use the default value from the config.yml file
|
|
|
|
if (value == null) {
|
2023-11-16 01:17:27 +01:00
|
|
|
Map<ScrapperSetting, Object> defaultNPCSettings = globalScrapperSettings.getDefaultNPCSettings();
|
2023-11-14 16:04:48 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-30 13:56:33 +01:00
|
|
|
/**
|
|
|
|
* Gets all item types this NPC is able to salvage
|
|
|
|
*
|
|
|
|
* @return <p>All salvageable items</p>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-12-30 13:56:33 +01:00
|
|
|
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
|
2024-05-04 01:01:56 +02:00
|
|
|
* recipe can be salvaged. This does not include smelting or similar.</p>
|
2023-12-30 13:56:33 +01:00
|
|
|
*
|
|
|
|
* @return <p>True if extended salvaging is enabled</p>
|
|
|
|
*/
|
|
|
|
public boolean extendedSalvageEnabled() {
|
|
|
|
return ConfigHelper.asBoolean(getValue(ScrapperSetting.EXTENDED_SALVAGE_ENABLED));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-12-30 13:56:33 +01:00
|
|
|
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>
|
|
|
|
*/
|
2024-05-04 01:01:56 +02:00
|
|
|
@NotNull
|
2023-12-30 13:56:33 +01:00
|
|
|
public String getInvalidItemMessage() {
|
|
|
|
return asString(ScrapperSetting.INVALID_ITEM_MESSAGE);
|
|
|
|
}
|
|
|
|
|
2024-05-04 01:01:56 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2024-05-05 15:37:38 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2023-11-12 19:02:11 +01:00
|
|
|
}
|