Splits the blacksmith command into two commands, and much more

This commit is contained in:
2022-08-19 19:08:54 +02:00
parent 755db8c497
commit e1191dad7d
21 changed files with 1077 additions and 235 deletions

View File

@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.config;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.YamlStorage;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.util.ConfigHelper;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
@ -33,8 +34,8 @@ public class GlobalSettings {
*/
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.");
"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.");
}
/**
@ -62,6 +63,82 @@ public class GlobalSettings {
defaultConfig.save();
}
/**
* Changes the value of the given setting
*
* @param globalSetting <p>The global setting to change</p>
* @param newValue <p>The new value of the setting</p>
*/
public void changeValue(GlobalSetting globalSetting, Object newValue) {
globalSettings.put(globalSetting, newValue);
save();
}
/**
* Changes the value of the given setting
*
* @param npcSetting <p>The default NPC setting to change</p>
* @param newValue <p>The new value for the setting</p>
*/
public void changeValue(NPCSetting npcSetting, Object newValue) {
defaultNPCSettings.put(npcSetting, newValue);
save();
}
/**
* Sets the enchantment cost for the given enchantment
*
* @param enchantment <p>The enchantment to set the enchantment cost for</p>
* @param newEnchantmentCost <p>The new enchantment cost</p>
*/
public void setEnchantmentCost(Enchantment enchantment, double newEnchantmentCost) {
if (newEnchantmentCost < 0) {
throw new IllegalArgumentException("Enchantment cost cannot be negative!");
}
if (enchantment == null) {
globalSettings.put(GlobalSetting.ENCHANTMENT_COST, newEnchantmentCost);
} else {
enchantmentCosts.put(enchantment, newEnchantmentCost);
}
save();
}
/**
* Sets the price per durability point for the given material
*
* @param material <p>The material to set the price per durability point price for</p>
* @param newPrice <p>The new price per durability point price</p>
*/
public void setPricePerDurabilityPoint(Material material, double newPrice) {
if (newPrice < 0) {
throw new IllegalArgumentException("Price per durability point cannot be negative!");
}
if (material == null) {
globalSettings.put(GlobalSetting.PRICE_PER_DURABILITY_POINT, newPrice);
} else {
materialPricePerDurabilityPoints.put(material, newPrice);
}
save();
}
/**
* Sets the base price for the given material
*
* @param material <p>The material to set the base price for</p>
* @param newBasePrice <p>The new base price</p>
*/
public void setBasePrice(Material material, double newBasePrice) {
if (newBasePrice < 0) {
throw new IllegalArgumentException("Base price cannot be negative!");
}
if (material == null) {
globalSettings.put(GlobalSetting.BASE_PRICE, newBasePrice);
} else {
materialBasePrices.put(material, newBasePrice);
}
save();
}
/**
* Gets the current value of the default NPC settings
*
@ -135,12 +212,7 @@ public class GlobalSettings {
* @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;
}
return ConfigHelper.asBoolean(getValue(setting));
}
/**
@ -152,14 +224,7 @@ public class GlobalSettings {
* @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;
}
return ConfigHelper.asDouble(getValue(setting));
}
/**
@ -256,6 +321,16 @@ public class GlobalSettings {
return relevant;
}
/**
* Converts a normalized material name to the format used in the config file
*
* @param normalizedName <p>The normalized name to un-normalize</p>
* @return <p>The un-normalized name</p>
*/
private String unNormalizeName(String normalizedName) {
return normalizedName.toLowerCase().replace("_", "-");
}
/**
* Loads all default NPC settings
*
@ -273,4 +348,41 @@ public class GlobalSettings {
}
}
/**
* Saves all current settings to the config file
*/
private void save() {
DataKey root = defaultConfig.getKey("");
//Save all default NPC settings
for (NPCSetting setting : NPCSetting.values()) {
root.setRaw(setting.getPath(), defaultNPCSettings.get(setting));
}
//Save all normal global settings
for (GlobalSetting globalSetting : GlobalSetting.values()) {
root.setRaw(globalSetting.getPath(), globalSettings.get(globalSetting));
}
//Save all base prices
DataKey basePriceNode = root.getRelative(GlobalSetting.BASE_PRICE.getParent());
for (Material material : materialBasePrices.keySet()) {
basePriceNode.setRaw(unNormalizeName(material.name()), materialBasePrices.get(material));
}
//Save all per-durability-point prices
DataKey basePerDurabilityPriceNode = root.getRelative(GlobalSetting.PRICE_PER_DURABILITY_POINT.getParent());
for (Material material : materialPricePerDurabilityPoints.keySet()) {
basePerDurabilityPriceNode.setRaw(unNormalizeName(material.name()), materialPricePerDurabilityPoints.get(material));
}
//Load all enchantment prices
DataKey enchantmentCostNode = root.getRelative(GlobalSetting.ENCHANTMENT_COST.getParent());
for (Enchantment enchantment : enchantmentCosts.keySet()) {
enchantmentCostNode.setRaw(unNormalizeName(enchantment.getKey().toString()), enchantmentCosts.get(enchantment));
}
//Perform the actual save to disk
defaultConfig.save();
}
}