Makes some settings inherit properly #16

This commit is contained in:
Kristian Knarvik 2023-01-11 00:51:05 +01:00
parent 753c7c6275
commit 488d4c7589
3 changed files with 77 additions and 17 deletions

View File

@ -56,8 +56,8 @@ public class BlacksmithPlugin extends JavaPlugin {
* Reloads the configuration file from disk * Reloads the configuration file from disk
*/ */
public void reload() { public void reload() {
config.load();
this.reloadConfig(); this.reloadConfig();
config.load();
translator.loadLanguages(this.getDataFolder(), this.getConfig().getString("language", "en")); translator.loadLanguages(this.getDataFolder(), this.getConfig().getString("language", "en"));
} }

View File

@ -10,7 +10,9 @@ import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -22,8 +24,9 @@ public class GlobalSettings {
private final Map<Material, Double> materialBasePrices = new HashMap<>(); private final Map<Material, Double> materialBasePrices = new HashMap<>();
private final Map<Material, Double> materialPricePerDurabilityPoints = new HashMap<>(); private final Map<Material, Double> materialPricePerDurabilityPoints = new HashMap<>();
private final Map<Enchantment, Double> enchantmentCosts = new HashMap<>(); private final Map<Enchantment, Double> enchantmentCosts = new HashMap<>();
private final Map<NPCSetting, Object> defaultNPCSettings = new HashMap<>(); private final Map<NPCSetting, Object> defaultNPCSettings = new HashMap<>();
private final List<Material> defaultReforgeAbleMaterials = new ArrayList<>();
private final List<Enchantment> defaultEnchantmentBlocklist = new ArrayList<>();
private final Map<GlobalSetting, Object> globalSettings = new HashMap<>(); private final Map<GlobalSetting, Object> globalSettings = new HashMap<>();
private final YamlStorage defaultConfig; private final YamlStorage defaultConfig;
@ -252,6 +255,24 @@ public class GlobalSettings {
} }
} }
/**
* Gets the value of reforgeAbleItems
*
* @return <p>The value of reforgeAbleItems</p>
*/
public List<Material> getReforgeAbleItems() {
return this.defaultReforgeAbleMaterials;
}
/**
* Gets the value of enchantmentBlocklist
*
* @return <p>The value of enchantmentBlocklist</p>
*/
public List<Enchantment> getEnchantmentBlocklist() {
return this.defaultEnchantmentBlocklist;
}
/** /**
* Gets the cost for repairing the given type of anvil * Gets the cost for repairing the given type of anvil
* *
@ -461,6 +482,12 @@ public class GlobalSettings {
defaultNPCSettings.put(setting, root.getRaw(setting.getPath())); defaultNPCSettings.put(setting, root.getRaw(setting.getPath()));
} }
} }
defaultReforgeAbleMaterials.clear();
defaultReforgeAbleMaterials.addAll(NPCSettings.getReforgeAbleItems(ConfigHelper.asStringList(
defaultNPCSettings.get(NPCSetting.REFORGE_ABLE_ITEMS))));
defaultEnchantmentBlocklist.clear();
defaultEnchantmentBlocklist.addAll(NPCSettings.getEnchantmentBlocklist(ConfigHelper.asStringList(
defaultNPCSettings.get(NPCSetting.ENCHANTMENT_BLOCKLIST))));
} }
/** /**

View File

@ -199,8 +199,13 @@ public class NPCSettings {
* @return <p>All items reforge-able by this NPC</p> * @return <p>All items reforge-able by this NPC</p>
*/ */
public List<Material> getReforgeAbleItems() { public List<Material> getReforgeAbleItems() {
Object currentValue = currentValues.get(NPCSetting.REFORGE_ABLE_ITEMS);
if (currentValue == null || String.valueOf(currentValue).isEmpty()) {
return globalSettings.getReforgeAbleItems();
} else {
return new ArrayList<>(this.reforgeAbleItems); return new ArrayList<>(this.reforgeAbleItems);
} }
}
/** /**
* Gets the list of blocked enchantments * Gets the list of blocked enchantments
@ -208,8 +213,13 @@ public class NPCSettings {
* @return <p>The list of blocked enchantments</p> * @return <p>The list of blocked enchantments</p>
*/ */
public List<Enchantment> getEnchantmentBlocklist() { public List<Enchantment> getEnchantmentBlocklist() {
Object currentValue = currentValues.get(NPCSetting.ENCHANTMENT_BLOCKLIST);
if (currentValue == null || String.valueOf(currentValue).isEmpty()) {
return globalSettings.getEnchantmentBlocklist();
} else {
return new ArrayList<>(this.enchantmentBlocklist); return new ArrayList<>(this.enchantmentBlocklist);
} }
}
/** /**
* Gets the minimum delay used to wait for a reforging to finish. * Gets the minimum delay used to wait for a reforging to finish.
@ -360,7 +370,7 @@ public class NPCSettings {
* @param value <p>The value specified by a user</p> * @param value <p>The value specified by a user</p>
* @return <p>The value with placeholders replaced</p> * @return <p>The value with placeholders replaced</p>
*/ */
private Object replaceReforgeAblePresets(Object value) { private static Object replaceReforgeAblePresets(Object value) {
if (value instanceof String string) { if (value instanceof String string) {
String[] list = string.split(","); String[] list = string.split(",");
List<String> replaced = new ArrayList<>(list.length); List<String> replaced = new ArrayList<>(list.length);
@ -384,22 +394,33 @@ public class NPCSettings {
*/ */
private void updateEnchantmentBlocklist() { private void updateEnchantmentBlocklist() {
this.enchantmentBlocklist.clear(); this.enchantmentBlocklist.clear();
List<String> newEnchantmentBlocklist = ConfigHelper.asStringList(getValue(NPCSetting.ENCHANTMENT_BLOCKLIST)); this.enchantmentBlocklist.addAll(getEnchantmentBlocklist(ConfigHelper.asStringList(getValue(
NPCSetting.ENCHANTMENT_BLOCKLIST))));
}
for (String item : newEnchantmentBlocklist) { /**
* Gets the list of enchantments listed in the given string list
*
* @param enchantments <p>The enchantment names to block</p>
* @return <p>The enchantments to be blocked</p>
*/
public static List<Enchantment> getEnchantmentBlocklist(List<String> enchantments) {
List<Enchantment> enchantmentBlocklist = new ArrayList<>();
for (String item : enchantments) {
if (InputParsingHelper.isEmpty(item)) { if (InputParsingHelper.isEmpty(item)) {
continue; continue;
} }
Enchantment enchantment = InputParsingHelper.matchEnchantment(item); Enchantment enchantment = InputParsingHelper.matchEnchantment(item);
if (enchantment != null) { if (enchantment != null) {
this.enchantmentBlocklist.add(enchantment); enchantmentBlocklist.add(enchantment);
} else { } else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item + BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item +
" as a valid enchantment"); " as a valid enchantment");
} }
} }
return enchantmentBlocklist;
} }
/** /**
@ -407,23 +428,34 @@ public class NPCSettings {
*/ */
private void updateReforgeAbleItems() { private void updateReforgeAbleItems() {
this.reforgeAbleItems.clear(); this.reforgeAbleItems.clear();
String newReforgeAbleItems = (String) currentValues.get(NPCSetting.REFORGE_ABLE_ITEMS); this.reforgeAbleItems.addAll(getReforgeAbleItems(ConfigHelper.asStringList(getValue(
if (newReforgeAbleItems == null) { NPCSetting.REFORGE_ABLE_ITEMS))));
return;
} }
//Convert any presets with a list of materials /**
newReforgeAbleItems = (String) replaceReforgeAblePresets(newReforgeAbleItems); * Gets a list of the reforgeAbleItems described in the given item list
*
* @param itemList <p>The list of items defined by the user</p>
* @return <p>The materials contained in the item list</p>
*/
public static List<Material> getReforgeAbleItems(List<String> itemList) {
List<Material> reforgeAbleItems = new ArrayList<>();
if (itemList == null) {
return null;
}
Set<Material> blacklisted = new HashSet<>(); Set<Material> blacklisted = new HashSet<>();
//Parse every material, and add to reforgeAble items //Parse every material, and add to reforgeAble items
for (String item : newReforgeAbleItems.split(",")) { for (String item : itemList) {
//Ignore ,, //Ignore ,,
if (InputParsingHelper.isEmpty(item)) { if (InputParsingHelper.isEmpty(item)) {
continue; continue;
} }
//Convert any presets with a list of materials
item = (String) replaceReforgeAblePresets(item);
boolean blacklist = false; boolean blacklist = false;
if (item.startsWith("-")) { if (item.startsWith("-")) {
blacklist = true; blacklist = true;
@ -433,7 +465,7 @@ public class NPCSettings {
Material material = InputParsingHelper.matchMaterial(item); Material material = InputParsingHelper.matchMaterial(item);
if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) { if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) {
if (!blacklist) { if (!blacklist) {
this.reforgeAbleItems.add(material); reforgeAbleItems.add(material);
} else { } else {
blacklisted.add(material); blacklisted.add(material);
} }
@ -444,7 +476,8 @@ public class NPCSettings {
} }
//Remove any blacklisted materials at the end to make sure order of arguments won't matter //Remove any blacklisted materials at the end to make sure order of arguments won't matter
this.reforgeAbleItems.removeAll(blacklisted); reforgeAbleItems.removeAll(blacklisted);
return reforgeAbleItems;
} }
} }