Majorly changes most plugin code

Changes the entire settings structure
Splits settings into NPC settings and global settings
Adds some command classes in preparation for a new command system
Moves a lot of code away from BlacksmithPlugin
Adds a new EconomyManager class which takes care of everything economy and pricing
Removes HyperConomy softdepend
Changes the reload command to /blacksmith reload
Adds a proper configuration file to make possible to change stuff without a bloody wiki
This commit is contained in:
2022-08-07 01:21:47 +02:00
parent e2b167e020
commit c557d969b7
14 changed files with 1076 additions and 723 deletions

View File

@ -0,0 +1,79 @@
package net.knarcraft.blacksmith.config;
import java.util.Arrays;
public enum GlobalSetting {
/**
* The base price for repairing, regardless of durability
*
* <p>This allows specifying a price for each item, by setting base-price.item_name.</p>
*/
BASE_PRICE("global.base-price.default", 10.0),
/**
* The base price for each durability point
*
* <p>If natural cost, this is the cost each missing durability point will add to the cost. If not natural cost,
* this is the cost each present durability point will add to the cost. This allows specifying a price per
* durability point value for each item, by setting price-per-durability-point.item_name</p>
*/
PRICE_PER_DURABILITY_POINT("global.price-per-durability-point.default", 0.005),
/**
* The price increase for each level of each present enchantment
*
* <p>This can be specified for each possible enchantment by setting enchantment-modifiers.enchantment_name</p>
*/
ENCHANTMENT_MODIFIER("global.enchantment-cost.default", 5),
/**
* Whether the cost should increase for damage taken, as opposed to increase for durability present
*/
NATURAL_COST("global.natural-cost", true);
private final String path;
private final String parent;
private final Object value;
/**
* Instantiates a new setting
*
* @param path <p>The full config path for this setting</p>
* @param value <p>The default value of this setting</p>
*/
GlobalSetting(String path, Object value) {
this.path = path;
this.value = value;
String[] pathParts = path.split("\\.");
this.parent = String.join(".", Arrays.copyOfRange(pathParts, 0, pathParts.length - 1));
}
/**
* Gets the full config path for this setting
*
* @return <p>The full config path for this setting</p>
*/
public String getPath() {
return path;
}
/**
* Gets the parent item of the defined path
*
* @return <p>The parent node</p>
*/
public String getParent() {
return parent;
}
/**
* Gets the value of this setting
*
* @return <p>The value of this setting</p>
*/
Object getDefaultValue() {
return value;
}
}

View File

@ -0,0 +1,263 @@
package net.knarcraft.blacksmith.config;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.YamlStorage;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* A class which keeps track of all default NPC settings and all global settings
*/
public class GlobalSettings {
private final Map<Material, Double> materialBasePrices = new HashMap<>();
private final Map<Material, Double> materialPricePerDurabilityPoints = new HashMap<>();
private final Map<Enchantment, Double> enchantmentModifiers = new HashMap<>();
private final Map<NPCSetting, Object> defaultNPCSettings = new HashMap<>();
private final Map<GlobalSetting, Object> globalSettings = new HashMap<>();
private final YamlStorage defaultConfig;
/**
* Instantiates a new "Settings"
*
* @param plugin <p>A reference to the blacksmith plugin</p>
*/
public GlobalSettings(BlacksmithPlugin plugin) {
defaultConfig = new YamlStorage(new File(plugin.getDataFolder() + File.separator + "config.yml"),
"Blacksmith Configuration\nWarning: The values under defaults are the values set for a blacksmith" +
"upon creation. To change any values for existing NPCs, edit the citizens NPC file.");
}
/**
* Loads all configuration values from the config file
*/
public void load() {
//Load the config from disk
defaultConfig.load();
DataKey root = defaultConfig.getKey("");
//Just in case, clear existing values
defaultNPCSettings.clear();
globalSettings.clear();
materialBasePrices.clear();
materialPricePerDurabilityPoints.clear();
enchantmentModifiers.clear();
//Load/Save NPC default settings
loadDefaultNPCSettings(root);
//Load/Save global settings
loadGlobalSettings(root);
//Save any modified values to disk
defaultConfig.save();
}
/**
* Gets the current value of the default NPC settings
*
* @return <p>The current value of the default NPC settings</p>
*/
public Map<NPCSetting, Object> getDefaultNPCSettings() {
return new HashMap<>(this.defaultNPCSettings);
}
/**
* Gets whether to use natural cost for cost calculation
*
* <p>Natural cost makes it more costly the more damage is dealt to an item. The alternative is the legacy behavior
* where the amount of durability points remaining increases the cost.</p>
*
* @return <p>Whether to use natural cost</p>
*/
public boolean getUseNaturalCost() {
return asBoolean(GlobalSetting.NATURAL_COST);
}
/**
* Gets the base price for the given material
*
* @param material <p>The material to get the base price for</p>
* @return <p>The base price for the material</p>
*/
public double getBasePrice(Material material) {
if (materialBasePrices.containsKey(material) && materialBasePrices.get(material) != null) {
return materialBasePrices.get(material);
} else {
return asDouble(GlobalSetting.BASE_PRICE);
}
}
/**
* Gets the price per durability point for the given material
*
* @param material <p>The material to get the durability point price for</p>
* @return <p>The durability point price for the material</p>
*/
public double getPricePerDurabilityPoint(Material material) {
if (materialPricePerDurabilityPoints.containsKey(material) &&
materialPricePerDurabilityPoints.get(material) != null) {
return materialPricePerDurabilityPoints.get(material);
} else {
return asDouble(GlobalSetting.PRICE_PER_DURABILITY_POINT);
}
}
/**
* Gets the cost to be added for each level of the given enchantment
*
* @param enchantment <p>The enchantment to get the cost for</p>
* @return <p>The cost of each enchantment level</p>
*/
public double getEnchantmentModifier(Enchantment enchantment) {
if (enchantmentModifiers.containsKey(enchantment) && enchantmentModifiers.get(enchantment) != null) {
return enchantmentModifiers.get(enchantment);
} else {
return asDouble(GlobalSetting.ENCHANTMENT_MODIFIER);
}
}
/**
* 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(GlobalSetting setting) {
Object value = getValue(setting);
if (value instanceof String) {
return Boolean.parseBoolean((String) value);
} else {
return (Boolean) value;
}
}
/**
* 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(GlobalSetting setting) {
Object value = getValue(setting);
if (value instanceof String) {
return Double.parseDouble((String) value);
} else if (value instanceof Integer) {
return (Integer) value;
} else {
return (Double) value;
}
}
/**
* 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(GlobalSetting setting) {
Object value = globalSettings.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
*
* @param root <p>The root node of all global settings</p>
*/
private void loadGlobalSettings(DataKey root) {
for (GlobalSetting globalSetting : GlobalSetting.values()) {
if (!root.keyExists(globalSetting.getPath())) {
//If the setting does not exist in the config file, add it
root.setRaw(globalSetting.getPath(), globalSetting.getDefaultValue());
} else {
//Set the setting to the value found in the path
globalSettings.put(globalSetting, root.getRaw(globalSetting.getPath()));
}
}
//Load all base prices
DataKey basePriceNode = root.getRelative(GlobalSetting.BASE_PRICE.getParent());
Map<String, String> relevantKeys = getRelevantKeys(basePriceNode);
for (String key : relevantKeys.keySet()) {
Material material = Material.matchMaterial(relevantKeys.get(key));
if (material != null) {
materialBasePrices.put(material, basePriceNode.getDouble(key));
}
}
//Load all per-durability-point prices
DataKey basePerDurabilityPriceNode = root.getRelative(GlobalSetting.PRICE_PER_DURABILITY_POINT.getParent());
relevantKeys = getRelevantKeys(basePerDurabilityPriceNode);
for (String key : relevantKeys.keySet()) {
Material material = Material.matchMaterial(relevantKeys.get(key));
if (material != null) {
materialPricePerDurabilityPoints.put(material, basePerDurabilityPriceNode.getDouble(key));
}
}
//Load all enchantment prices
DataKey enchantmentModifiersNode = root.getRelative(GlobalSetting.ENCHANTMENT_MODIFIER.getParent());
relevantKeys = getRelevantKeys(basePerDurabilityPriceNode);
for (String key : relevantKeys.keySet()) {
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(relevantKeys.get(key)));
if (enchantment != null) {
enchantmentModifiers.put(enchantment, enchantmentModifiersNode.getDouble(key));
}
}
}
/**
* Gets a map between relevant keys and their normalized name
*
* @param rootKey <p>The root data key containing sub-keys</p>
* @return <p>Any sub-keys found that aren't the default</p>
*/
private Map<String, String> getRelevantKeys(DataKey rootKey) {
Map<String, String> relevant = new HashMap<>();
for (DataKey dataKey : rootKey.getSubKeys()) {
String keyName = dataKey.name();
//Skip the default value
if (keyName.equals("default")) {
continue;
}
String normalizedName = keyName.toUpperCase().replace("-", "_");
relevant.put(keyName, normalizedName);
}
return relevant;
}
/**
* Loads all default NPC settings
*
* @param root <p>The root node of all default NPC settings</p>
*/
private void loadDefaultNPCSettings(DataKey root) {
for (NPCSetting setting : NPCSetting.values()) {
if (!root.keyExists(setting.getPath())) {
//If the setting does not exist in the config file, add it
root.setRaw(setting.getPath(), setting.getDefaultValue());
} else {
//Set the setting to the value found in the path
defaultNPCSettings.put(setting, root.getRaw(setting.getPath()));
}
}
}
}

View File

@ -5,10 +5,22 @@ import java.util.Arrays;
/**
* An enum representing all of Blacksmith's settings
*/
public enum Setting {
public enum NPCSetting {
BASE_PRICE("base-prices.default", 10.0),
PRICE_PER_DURABILITY_POINT("price-per-durability-point.default", 1.0),
DROP_ITEM("defaults.drop-item", true),
DISABLE_COOL_DOWN("defaults.disable-cool-down", false),
DISABLE_DELAY("defaults.disable-delay", false),
FAIL_CHANCE("defaults.percent-chance-to-fail-reforge", 10),
EXTRA_ENCHANTMENT_CHANCE("defaults.percent-chance-for-extra-enchantment", 5),
MAX_ENCHANTMENTS("defaults.maximum-enchantments", 3),
MAX_REFORGE_DELAY("defaults.delays-in-seconds.maximum", 30),
MIN_REFORGE_DELAY("defaults.delays-in-seconds.minimum", 5),
REFORGE_COOL_DOWN("defaults.delays-in-seconds.reforge-cool-down", 60),
REFORGE_ABLE_ITEMS("defaults.reforge-able-items", new String[]{}),
/*-----------
| Messages |
-----------*/
BUSY_WITH_PLAYER_MESSAGE("defaults.messages.busy-with-player", "§cI'm busy at the moment. Come back later!"),
BUSY_WITH_REFORGE_MESSAGE("defaults.messages.busy-with-reforge", "§cI'm working on it. Be patient!"),
COOL_DOWN_UNEXPIRED_MESSAGE(
@ -17,11 +29,6 @@ public enum Setting {
COST_MESSAGE(
"defaults.messages.cost",
"§eIt will cost §a<price> §eto reforge that §a<item>§e! Click again to reforge!"),
DROP_ITEM("defaults.drop-item", true),
DISABLE_COOL_DOWN("defaults.disable-cool-down", false),
DISABLE_DELAY("defaults.disable-delay", false),
ENCHANTMENT_MODIFIER("enchantment-modifiers.default", 5),
FAIL_CHANCE("defaults.percent-chance-to-fail-reforge", 10),
FAIL_MESSAGE("defaults.messages.fail-reforge", "§cWhoops! Didn't mean to do that! Maybe next time?"),
INSUFFICIENT_FUNDS_MESSAGE(
"defaults.messages.insufficient-funds",
@ -30,18 +37,12 @@ public enum Setting {
ITEM_UNEXPECTEDLY_CHANGED_MESSAGE(
"defaults.messages.item-changed-during-reforge",
"§cThat's not the item you wanted to reforge before!"),
EXTRA_ENCHANTMENT_CHANCE("defaults.percent-chance-for-extra-enchantment", 5),
MAX_ENCHANTMENTS("defaults.maximum-enchantments", 3),
MAX_REFORGE_DELAY("defaults.delays-in-seconds.maximum", 30),
MIN_REFORGE_DELAY("defaults.delays-in-seconds.minimum", 5),
REFORGE_COOL_DOWN("defaults.delays-in-seconds.reforge-cool-down", 60),
START_REFORGE_MESSAGE("defaults.messages.start-reforge", "§eOk, let's see what I can do..."),
SUCCESS_MESSAGE("defaults.messages.successful-reforge", "There you go! All better!"),
NATURAL_COST("defaults.natural-cost", true);
SUCCESS_MESSAGE("defaults.messages.successful-reforge", "There you go! All better!");
private final String path;
private final String childPath;
private Object value;
private final Object value;
/**
* Instantiates a new setting
@ -49,7 +50,7 @@ public enum Setting {
* @param path <p>The full config path for this setting</p>
* @param value <p>The default value of this setting</p>
*/
Setting(String path, Object value) {
NPCSetting(String path, Object value) {
this.path = path;
this.value = value;
String[] pathParts = path.split("\\.");
@ -74,78 +75,13 @@ public enum Setting {
return childPath;
}
/**
* Gets this setting as a boolean
*
* <p>This will throw an exception if used for a non-boolean value</p>
*
* @return <p>This setting as a boolean</p>
*/
public boolean asBoolean() {
if (value instanceof String) {
return Boolean.parseBoolean((String) value);
} else {
return (Boolean) value;
}
}
/**
* Gets this setting as a double
*
* <p>This will throw an exception if used for a non-double setting</p>
*
* @return <p>This setting as a double</p>
*/
public double asDouble() {
if (value instanceof String) {
return Double.parseDouble((String) value);
} else if (value instanceof Integer) {
return (Integer) value;
} else {
return (Double) value;
}
}
/**
* Gets this setting as an integer
*
* <p>This will throw an exception if used for a non-integer setting</p>
*
* @return <p>This setting as an integer</p>
*/
public int asInt() {
if (value instanceof String) {
return Integer.parseInt((String) value);
} else {
return (Integer) value;
}
}
/**
* Gets this setting as a string
*
* @return <p>This setting as a string</p>
*/
public String asString() {
return value.toString();
}
/**
* Gets the value of this setting
*
* @return <p>The value of this setting</p>
*/
Object get() {
Object getDefaultValue() {
return value;
}
/**
* Sets the value of this setting
*
* @param value <p>The new value of this setting</p>
*/
void set(Object value) {
this.value = value;
}
}

View File

@ -0,0 +1,322 @@
package net.knarcraft.blacksmith.config;
import net.citizensnpcs.api.util.DataKey;
import org.bukkit.Material;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A class which keeps track of all Blacksmith settings/config values for one NPC
*/
public class NPCSettings {
private final List<Material> reforgeAbleItems = new ArrayList<>();
private final Map<NPCSetting, Object> currentValues = new HashMap<>();
private final GlobalSettings globalSettings;
/**
* Instantiates a new "Settings" object
*/
public NPCSettings(GlobalSettings globalSettings) {
this.globalSettings = globalSettings;
}
/**
* Loads variables from the given data key
*
* @param key <p>The data key to load variables from</p>
*/
public void loadVariables(DataKey key) {
for (NPCSetting setting : NPCSetting.values()) {
if (key.keyExists(setting.getChildPath())) {
currentValues.put(setting, key.getRaw(setting.getChildPath()));
}
}
//Updates the list of reforge-able items/materials
updateReforgeAbleItems();
}
/**
* Saves variables to the given data key
*
* @param key <p>The data key to save variables to</p>
*/
public void saveVariables(DataKey key) {
for (NPCSetting setting : NPCSetting.values()) {
key.setRaw(setting.getChildPath(), currentValues.get(setting));
}
}
/**
* Gets the message to display when the blacksmith is busy with another player
*
* @return <p>The busy with player message</p>
*/
public String getBusyWithPlayerMessage() {
return asString(NPCSetting.BUSY_WITH_PLAYER_MESSAGE);
}
/**
* Gets the message to display when the blacksmith is busy with reforging an item
*
* @return <p>The busy reforging message</p>
*/
public String getBusyReforgingMessage() {
return asString(NPCSetting.BUSY_WITH_REFORGE_MESSAGE);
}
/**
* Gets the message to use for displaying an item's cost
*
* @return <p>The message to use for displaying item cost</p>
*/
public String getCostMessage() {
return asString(NPCSetting.COST_MESSAGE);
}
/**
* Gets the message to display when a blacksmith has been given an invalid item
*
* @return <p>The invalid item message</p>
*/
public String getInvalidItemMessage() {
return asString(NPCSetting.INVALID_ITEM_MESSAGE);
}
/**
* Gets the message to display when a blacksmith starts reforging an item
*
* @return <p>The start reforge message</p>
*/
public String getStartReforgeMessage() {
return asString(NPCSetting.START_REFORGE_MESSAGE);
}
/**
* Gets the message to display when a blacksmith has successfully repaired an item
*
* @return <p>The reforge success message</p>
*/
public String getSuccessMessage() {
return asString(NPCSetting.SUCCESS_MESSAGE);
}
/**
* Gets the message to display when a blacksmith has failed to repair an item
*
* @return <p>The reforge fail message</p>
*/
public String getFailMessage() {
return asString(NPCSetting.FAIL_MESSAGE);
}
/**
* Gets the message to display when a player cannot afford re-forging an item
*
* @return <p>The insufficient funds message</p>
*/
public String getInsufficientFundsMessage() {
return asString(NPCSetting.INSUFFICIENT_FUNDS_MESSAGE);
}
/**
* Gets the message to display when a blacksmith is still affected by a cool-down
*
* @return <p>The cool down unexpired message</p>
*/
public String getCoolDownUnexpiredMessage() {
return asString(NPCSetting.COOL_DOWN_UNEXPIRED_MESSAGE);
}
/**
* Gets the message to display when a player has changed the item they're trying to reforge
*
* @return <p>The item changed message</p>
*/
public String getItemChangedMessage() {
return asString(NPCSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE);
}
/**
* Gets all items reforge-able by this NPC
*
* <p>If this is not empty, only the items specified can be repaired by this NPC.</p>
*
* @return <p>All items reforge-able by this NPC</p>
*/
public List<Material> getReforgeAbleItems() {
return new ArrayList<>(reforgeAbleItems);
}
/**
* Gets the minimum delay used to wait for a re-forge to finish.
*
* @return <p>The minimum reforge delay</p>
*/
public int getMinReforgeDelay() {
return asInt(NPCSetting.MIN_REFORGE_DELAY);
}
/**
* Gets the maximum delay used to wait for a re-forge to finish
*
* @return <p>The maximum reforge delay</p>
*/
public int getMaxReforgeDelay() {
return asInt(NPCSetting.MAX_REFORGE_DELAY);
}
/**
* Gets the cool-down between each reforge
*
* @return <p>The reforge cool-down</p>
*/
public int getReforgeCoolDown() {
return asInt(NPCSetting.REFORGE_COOL_DOWN);
}
/**
* Gets the chance to fail a re-forge
*
* @return <p>The fail chance</p>
*/
public int getFailChance() {
return asInt(NPCSetting.FAIL_CHANCE);
}
/**
* Gets the chance for adding an extra enchantment to an item
*
* @return <p>The extra enchantment chance</p>
*/
public int getExtraEnchantmentChance() {
return asInt(NPCSetting.EXTRA_ENCHANTMENT_CHANCE);
}
/**
* Gets the max number of enchantment to add to an item
*
* @return <p>The maximum enchantments</p>
*/
public int getMaxEnchantments() {
return asInt(NPCSetting.MAX_ENCHANTMENTS);
}
/**
* 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(NPCSetting.DROP_ITEM);
}
/**
* Gets whether to disable the reforge-cool-down
*
* @return <p>Whether to disable the reforge-cool-down</p>
*/
public boolean getDisableCoolDown() {
return asBoolean(NPCSetting.DISABLE_COOL_DOWN);
}
/**
* Gets whether to disable the delay between starting reforging and the re-forge finishing
*
* @return <p>Whether to disable the reforge delay</p>
*/
public boolean getDisableDelay() {
return asBoolean(NPCSetting.DISABLE_DELAY);
}
/**
* 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>
*/
private boolean asBoolean(NPCSetting setting) {
Object value = getValue(setting);
if (value instanceof String) {
return Boolean.parseBoolean((String) value);
} else {
return (Boolean) value;
}
}
/**
* 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(NPCSetting setting) {
Object value = getValue(setting);
if (value instanceof String) {
return Integer.parseInt((String) value);
} else {
return (Integer) value;
}
}
/**
* 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>
*/
private String asString(NPCSetting setting) {
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>
*/
private Object getValue(NPCSetting setting) {
Object value = currentValues.get(setting);
//If not set, use the default value from the config.yml file
if (value == null) {
Map<NPCSetting, Object> defaultNPCSettings = globalSettings.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;
}
/**
* Updates the reforge-able items according to the current value of the setting
*/
private void updateReforgeAbleItems() {
this.reforgeAbleItems.clear();
List<?> reforgeAbleItems = (List<?>) currentValues.get(NPCSetting.REFORGE_ABLE_ITEMS);
if (reforgeAbleItems == null) {
return;
}
for (Object item : reforgeAbleItems) {
if (item == null) {
continue;
}
Material material = Material.matchMaterial(String.valueOf(item));
if (material != null) {
this.reforgeAbleItems.add(material);
}
}
}
}

View File

@ -1,357 +0,0 @@
package net.knarcraft.blacksmith.config;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.YamlStorage;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import java.io.File;
/**
* A class which keeps track of all Blacksmith settings/config values
*/
public class Settings {
private String busyWithPlayerMessage = Setting.BUSY_WITH_PLAYER_MESSAGE.asString();
private String busyReforgingMessage = Setting.BUSY_WITH_REFORGE_MESSAGE.asString();
private String costMessage = Setting.COST_MESSAGE.asString();
private String invalidItemMessage = Setting.INVALID_ITEM_MESSAGE.asString();
private String startReforgeMessage = Setting.START_REFORGE_MESSAGE.asString();
private String successMessage = Setting.SUCCESS_MESSAGE.asString();
private String failMessage = Setting.FAIL_MESSAGE.asString();
private String insufficientFundsMessage = Setting.INSUFFICIENT_FUNDS_MESSAGE.asString();
private String coolDownUnexpiredMessage = Setting.COOL_DOWN_UNEXPIRED_MESSAGE.asString();
private String itemChangedMessage = Setting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE.asString();
private int minReforgeDelay = Setting.MIN_REFORGE_DELAY.asInt();
private int maxReforgeDelay = Setting.MAX_REFORGE_DELAY.asInt();
private int reforgeCoolDown = Setting.REFORGE_COOL_DOWN.asInt();
private int failChance = Setting.FAIL_CHANCE.asInt();
private int extraEnchantmentChance = Setting.EXTRA_ENCHANTMENT_CHANCE.asInt();
private int maxEnchantments = Setting.MAX_ENCHANTMENTS.asInt();
private boolean dropItem = Setting.DROP_ITEM.asBoolean();
private boolean disableCoolDown = Setting.DISABLE_COOL_DOWN.asBoolean();
private boolean disableDelay = Setting.DISABLE_DELAY.asBoolean();
private boolean naturalCost = Setting.NATURAL_COST.asBoolean();
private final YamlStorage config;
/**
* Instantiates a new "Settings"
*
* @param plugin <p>A reference to the blacksmith plugin</p>
*/
public Settings(BlacksmithPlugin plugin) {
config = new YamlStorage(new File(plugin.getDataFolder() + File.separator + "config.yml"),
"Blacksmith Configuration\nWarning: The values under defaults are the values set for a blacksmith" +
"upon creation. To change any values for existing NPCs, edit the citizens NPC file.");
}
/**
* Loads all configuration values from the config file
*/
public void load() {
//Load the config from disk
config.load();
DataKey root = config.getKey("");
for (Setting setting : Setting.values()) {
if (!root.keyExists(setting.getPath())) {
//If the setting does not exist in the config file, add it
root.setRaw(setting.getPath(), setting.get());
} else {
//Set the setting to the value found in the path
setting.set(root.getRaw(setting.getPath()));
}
}
//Save any modified values to disk
config.save();
}
/**
* Gets the configuration used for saving/loading from disk
*
* @return <p>The configuration</p>
*/
public YamlStorage getConfig() {
return config;
}
/**
* Loads variables from the given data key
*
* @param key <p>The data key to load variables from</p>
*/
public void loadVariables(DataKey key) {
// Override defaults if they exist
if (key.keyExists(Setting.BUSY_WITH_PLAYER_MESSAGE.getChildPath())) {
busyWithPlayerMessage = key.getString(Setting.BUSY_WITH_PLAYER_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.BUSY_WITH_REFORGE_MESSAGE.getChildPath())) {
busyReforgingMessage = key.getString(Setting.BUSY_WITH_REFORGE_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.COST_MESSAGE.getChildPath())) {
costMessage = key.getString(Setting.COST_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.INVALID_ITEM_MESSAGE.getChildPath())) {
invalidItemMessage = key.getString(Setting.INVALID_ITEM_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.START_REFORGE_MESSAGE.getChildPath())) {
startReforgeMessage = key.getString(Setting.START_REFORGE_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.SUCCESS_MESSAGE.getChildPath())) {
successMessage = key.getString(Setting.SUCCESS_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.FAIL_MESSAGE.getChildPath())) {
failMessage = key.getString(Setting.FAIL_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.INSUFFICIENT_FUNDS_MESSAGE.getChildPath())) {
insufficientFundsMessage = key.getString(Setting.INSUFFICIENT_FUNDS_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.COOL_DOWN_UNEXPIRED_MESSAGE.getChildPath())) {
coolDownUnexpiredMessage = key.getString(Setting.COOL_DOWN_UNEXPIRED_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE.getChildPath())) {
itemChangedMessage = key.getString(Setting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE.getChildPath());
}
if (key.keyExists(Setting.MIN_REFORGE_DELAY.getChildPath())) {
minReforgeDelay = key.getInt(Setting.MIN_REFORGE_DELAY.getChildPath());
}
if (key.keyExists(Setting.MAX_REFORGE_DELAY.getChildPath())) {
maxReforgeDelay = key.getInt(Setting.MAX_REFORGE_DELAY.getChildPath());
}
if (key.keyExists(Setting.REFORGE_COOL_DOWN.getChildPath())) {
reforgeCoolDown = key.getInt(Setting.REFORGE_COOL_DOWN.getChildPath());
}
if (key.keyExists(Setting.FAIL_CHANCE.getChildPath())) {
failChance = key.getInt(Setting.FAIL_CHANCE.getChildPath());
}
if (key.keyExists(Setting.MAX_ENCHANTMENTS.getChildPath())) {
maxEnchantments = key.getInt(Setting.MAX_ENCHANTMENTS.getChildPath());
}
if (key.keyExists(Setting.EXTRA_ENCHANTMENT_CHANCE.getChildPath())) {
extraEnchantmentChance = key.getInt(Setting.EXTRA_ENCHANTMENT_CHANCE.getChildPath());
}
if (key.keyExists(Setting.DROP_ITEM.getChildPath())) {
dropItem = key.getBoolean(Setting.DROP_ITEM.getChildPath());
}
if (key.keyExists(Setting.DISABLE_COOL_DOWN.getChildPath())) {
disableCoolDown = key.getBoolean(Setting.DISABLE_COOL_DOWN.getChildPath());
}
if (key.keyExists(Setting.DISABLE_DELAY.getChildPath())) {
disableDelay = key.getBoolean(Setting.DISABLE_DELAY.getChildPath());
}
if (key.keyExists(Setting.NATURAL_COST.getChildPath())) {
naturalCost = key.getBoolean(Setting.NATURAL_COST.getChildPath());
}
}
/**
* Saves variables to the given data key
*
* @param key <p>The data key to save variables to</p>
*/
public void saveVariables(DataKey key) {
//This saves variables to the specific NPC.
key.setString(Setting.BUSY_WITH_PLAYER_MESSAGE.getChildPath(), getBusyWithPlayerMessage());
key.setString(Setting.BUSY_WITH_REFORGE_MESSAGE.getChildPath(), getBusyReforgingMessage());
key.setString(Setting.COST_MESSAGE.getChildPath(), getCostMessage());
key.setString(Setting.INVALID_ITEM_MESSAGE.getChildPath(), getInvalidItemMessage());
key.setString(Setting.START_REFORGE_MESSAGE.getChildPath(), getStartReforgeMessage());
key.setString(Setting.SUCCESS_MESSAGE.getChildPath(), getSuccessMessage());
key.setString(Setting.FAIL_MESSAGE.getChildPath(), getFailMessage());
key.setString(Setting.INSUFFICIENT_FUNDS_MESSAGE.getChildPath(), getInsufficientFundsMessage());
key.setString(Setting.COOL_DOWN_UNEXPIRED_MESSAGE.getChildPath(), getCoolDownUnexpiredMessage());
key.setString(Setting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE.getChildPath(), getItemChangedMessage());
key.setInt(Setting.MIN_REFORGE_DELAY.getChildPath(), getMinReforgeDelay());
key.setInt(Setting.MAX_REFORGE_DELAY.getChildPath(), getMaxReforgeDelay());
key.setInt(Setting.REFORGE_COOL_DOWN.getChildPath(), getReforgeCoolDown());
key.setInt(Setting.FAIL_CHANCE.getChildPath(), getFailChance());
key.setInt(Setting.EXTRA_ENCHANTMENT_CHANCE.getChildPath(), getExtraEnchantmentChance());
key.setInt(Setting.MAX_ENCHANTMENTS.getChildPath(), getMaxEnchantments());
key.setBoolean(Setting.DROP_ITEM.getChildPath(), getDropItem());
key.setBoolean(Setting.DISABLE_DELAY.getChildPath(), getDisableDelay());
key.setBoolean(Setting.DISABLE_COOL_DOWN.getChildPath(), getDisableCoolDown());
key.setBoolean(Setting.DISABLE_COOL_DOWN.getChildPath(), getDisableCoolDown());
key.setBoolean(Setting.NATURAL_COST.getChildPath(), getNaturalCost());
}
/**
* Gets the message to display when the blacksmith is busy with another player
*
* @return <p>The busy with player message</p>
*/
public String getBusyWithPlayerMessage() {
return busyWithPlayerMessage;
}
/**
* Gets the message to display when the blacksmith is busy with reforging an item
*
* @return <p>The busy reforging message</p>
*/
public String getBusyReforgingMessage() {
return busyReforgingMessage;
}
/**
* Gets the message to use for displaying an item's cost
*
* @return <p>The message to use for displaying item cost</p>
*/
public String getCostMessage() {
return costMessage;
}
/**
* Gets the message to display when a blacksmith has been given an invalid item
*
* @return <p>The invalid item message</p>
*/
public String getInvalidItemMessage() {
return invalidItemMessage;
}
/**
* Gets the message to display when a blacksmith starts reforging an item
*
* @return <p>The start reforge message</p>
*/
public String getStartReforgeMessage() {
return startReforgeMessage;
}
/**
* Gets the message to display when a blacksmith has successfully repaired an item
*
* @return <p>The reforge success message</p>
*/
public String getSuccessMessage() {
return successMessage;
}
/**
* Gets the message to display when a blacksmith has failed to repair an item
*
* @return <p>The reforge fail message</p>
*/
public String getFailMessage() {
return failMessage;
}
/**
* Gets the message to display when a player cannot afford re-forging an item
*
* @return <p>The insufficient funds message</p>
*/
public String getInsufficientFundsMessage() {
return insufficientFundsMessage;
}
/**
* Gets the message to display when a blacksmith is still affected by a cool-down
*
* @return <p>The cool down unexpired message</p>
*/
public String getCoolDownUnexpiredMessage() {
return coolDownUnexpiredMessage;
}
/**
* Gets the message to display when a player has changed the item they're trying to reforge
*
* @return <p>The item changed message</p>
*/
public String getItemChangedMessage() {
return itemChangedMessage;
}
/**
* Gets the minimum delay used to wait for a re-forge to finish.
*
* @return <p>The minimum reforge delay</p>
*/
public int getMinReforgeDelay() {
return minReforgeDelay;
}
/**
* Gets the maximum delay used to wait for a re-forge to finish
*
* @return <p>The maximum reforge delay</p>
*/
public int getMaxReforgeDelay() {
return maxReforgeDelay;
}
/**
* Gets the cool-down between each reforge
*
* @return <p>The reforge cool-down</p>
*/
public int getReforgeCoolDown() {
return reforgeCoolDown;
}
/**
* Gets the chance to fail a re-forge
*
* @return <p>The fail chance</p>
*/
public int getFailChance() {
return failChance;
}
/**
* Gets the chance for adding an extra enchantment to an item
*
* @return <p>The extra enchantment chance</p>
*/
public int getExtraEnchantmentChance() {
return extraEnchantmentChance;
}
/**
* Gets the max number of enchantment to add to an item
*
* @return <p>The maximum enchantments</p>
*/
public int getMaxEnchantments() {
return maxEnchantments;
}
/**
* 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 dropItem;
}
/**
* Gets whether to disable the reforge-cool-down
*
* @return <p>Whether to disable the reforge-cool-down</p>
*/
public boolean getDisableCoolDown() {
return disableCoolDown;
}
/**
* Gets whether to disable the delay between starting reforging and the re-forge finishing
*
* @return <p>Whether to disable the reforge delay</p>
*/
public boolean getDisableDelay() {
return disableDelay;
}
/**
* Gets whether to use a natural cost calculation
*
* <p>The natural cost makes repairs more expensive the more damaged an item is, rather than the opposite.</p>
*
* @return <p>Whether to use a natural cost calculation</p>
*/
public boolean getNaturalCost() {
return naturalCost;
}
}