Blacksmith/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java
EpicKnarvik97 42ca42c571
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
Adds increased scrapper cost when used excessively
2024-12-21 19:48:39 +01:00

308 lines
10 KiB
Java

package net.knarcraft.blacksmith.config.scrapper;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* A class which keeps track of all default scrapper NPC settings and all global scrapper settings
*/
public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
private final Map<ScrapperSetting, Object> settings = new HashMap<>();
private final List<Material> defaultSalvageableMaterials = new ArrayList<>();
private final Map<Material, Set<Material>> trashSalvage = new HashMap<>();
private final BlacksmithPlugin instance;
/**
* Instantiates a new "Settings"
*
* @param instance <p>A reference to the blacksmith plugin</p>
*/
public GlobalScrapperSettings(@NotNull BlacksmithPlugin instance) {
this.instance = instance;
}
/**
* Loads all configuration values from the config file
*/
public void load() {
//Just in case, clear existing values
this.settings.clear();
//Load/Save global settings
loadSettings();
//Save any modified values to disk
instance.saveConfig();
}
/**
* Changes the value of the given setting
*
* @param scrapperSetting <p>The default NPC setting to change</p>
* @param newValue <p>The new value for the setting</p>
*/
public void changeValue(@NotNull ScrapperSetting scrapperSetting, @Nullable Object newValue) {
if (scrapperSetting.getValueType() == SettingValueType.STRING_LIST ||
scrapperSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) {
//Workaround to make sure it's treated as the correct type
this.settings.put(scrapperSetting, newValue == null ? null : ConfigHelper.asStringList(newValue));
} else {
this.settings.put(scrapperSetting, newValue);
}
if (scrapperSetting == ScrapperSetting.SALVAGE_ABLE_ITEMS) {
loadSalvageAbleItems();
}
save();
}
/**
* Gets the current raw value of the given global setting
*
* @param scrapperSetting <p>The setting to get</p>
* @return <p>The current raw setting value</p>
*/
@NotNull
public Object getRawValue(@NotNull ScrapperSetting scrapperSetting) {
return this.settings.get(scrapperSetting);
}
/**
* Gets the current value of the default NPC settings
*
* @return <p>The current value of the default NPC settings</p>
*/
@NotNull
public Map<ScrapperSetting, Object> getDefaultNPCSettings() {
return new HashMap<>(this.settings);
}
/**
* Gets whether to show exact time for reforging wait-time, and for wait-time between sessions
*
* @return <p>Whether to show exact time</p>
*/
public boolean showExactTime() {
return asBoolean(ScrapperSetting.SHOW_EXACT_TIME);
}
/**
* Gets the given value as a boolean
*
* <p>This will throw an exception if used for a non-boolean value</p>
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a boolean</p>
*/
public boolean asBoolean(@NotNull ScrapperSetting setting) {
return ConfigHelper.asBoolean(getValue(setting));
}
/**
* Gets the given value as a double
*
* <p>This will throw an exception if used for a non-double setting</p>
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a double</p>
*/
public double asDouble(@NotNull ScrapperSetting setting) {
return ConfigHelper.asDouble(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>
*/
private Object getValue(ScrapperSetting setting) {
Object value = this.settings.get(setting);
//If not set in config.yml, use the default value from the enum
if (value == null) {
value = setting.getDefaultValue();
}
return value;
}
/**
* Loads all global settings
*/
private void loadSettings() {
instance.reloadConfig();
FileConfiguration configuration = instance.getConfiguration();
for (ScrapperSetting setting : ScrapperSetting.values()) {
if (!configuration.contains(setting.getPath())) {
//If the setting does not exist in the config file, add it
configuration.set(setting.getPath(), setting.getDefaultValue());
} else {
//Set the setting to the value found in the path
this.settings.put(setting, configuration.get(setting.getPath()));
}
}
loadSalvageAbleItems();
loadTrashSalvage();
}
/**
* Saves all current settings to the config file
*/
private void save() {
FileConfiguration fileConfiguration = instance.getConfiguration();
//Save all default settings
for (ScrapperSetting setting : ScrapperSetting.values()) {
fileConfiguration.set(setting.getPath(), this.settings.get(setting));
}
//Perform the actual save to disk
instance.saveConfig();
}
/**
* Gets the value of salvageAbleItems
*
* @return <p>The value of salvageAbleItems</p>
*/
@NotNull
public List<Material> getSalvageAbleItems() {
return this.defaultSalvageableMaterials;
}
/**
* Gets the cost of using a scrapper to salvage an item
*
* @return <p>The cost of using a scrapper to salvage an item</p>
*/
public double getSalvageCost() {
return asDouble(ScrapperSetting.SALVAGE_COST);
}
/**
* Gets the cost of using a scrapper to remove armor trim
*
* @return <p>The cost of using a scrapper to remove armor trim</p>
*/
public double getArmorTrimSalvageCost() {
return asDouble(ScrapperSetting.ARMOR_TRIM_SALVAGE_COST);
}
/**
* Gets the cost of using a scrapper to remove netherite from an item
*
* @return <p>The cost of using a scrapper to remove netherite from an item</p>
*/
public double getNetheriteSalvageCost() {
return asDouble(ScrapperSetting.NETHERITE_SALVAGE_COST);
}
/**
* Gets the math formula for the increase in salvage cost
*
* @return <p>The salvage cost increase formula</p>
*/
public String getSalvageCostIncrease() {
return asString(ScrapperSetting.SALVAGE_COST_INCREASE);
}
/**
* Gets the math formula for the increase in salvage cooldown
*
* @return <p>The salvage cooldown increase formula</p>
*/
public String getSalvageCooldownIncrease() {
return asString(ScrapperSetting.SALVAGE_COOLDOWN_INCREASE);
}
/**
* Gets trash salvage for the given material
*
* <p>The trash salvage should be deferred when calculating item salvage, in order to increase the probability of
* getting the more expensive materials back.</p>
*
* @param material <p>The material to get trash salvage for</p>
* @return <p>The trash salvage</p>
*/
@Nullable
public Set<Material> getTrashSalvage(@NotNull Material material) {
return this.trashSalvage.get(material);
}
/**
* Loads reforgeAble items from the current value
*/
private void loadSalvageAbleItems() {
this.defaultSalvageableMaterials.clear();
List<String> materialNames = ConfigHelper.asStringList(this.settings.get(ScrapperSetting.SALVAGE_ABLE_ITEMS));
if (materialNames != null) {
this.defaultSalvageableMaterials.addAll(ItemHelper.getItems(materialNames, true));
}
}
/**
* Loads all trash salvage from the configuration file
*/
private void loadTrashSalvage() {
this.trashSalvage.clear();
List<String> allTrashSalvage = ConfigHelper.asStringList(this.settings.get(ScrapperSetting.IGNORED_SALVAGE));
if (allTrashSalvage == null) {
return;
}
for (String trashSalvageInfo : allTrashSalvage) {
// Ignore invalid lines
if (!trashSalvageInfo.contains(":")) {
BlacksmithPlugin.warn(String.format("The trash salvage configuration line %s is invalid", trashSalvageInfo));
continue;
}
// Parse all material names
String[] data = trashSalvageInfo.split(":");
String[] materialStrings = data[0].split(";");
List<Material> materials = new ArrayList<>();
for (String materialString : materialStrings) {
materials.addAll(ItemHelper.getWildcardMatch(materialString, true));
}
String[] trashSalvageStrings = data[1].split(";");
List<Material> ignored = new ArrayList<>();
for (String trashSalvageString : trashSalvageStrings) {
ignored.addAll(ItemHelper.getWildcardMatch(trashSalvageString, true));
}
// Add the trash salvage to all the matched materials
for (Material material : materials) {
trashSalvage.computeIfAbsent(material, k -> new HashSet<>());
trashSalvage.get(material).addAll(ignored);
}
}
}
/**
* 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();
}
}