From 488d4c758986db7294ab246e6730a4c95f1059e9 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 11 Jan 2023 00:51:05 +0100 Subject: [PATCH] Makes some settings inherit properly #16 --- .../blacksmith/BlacksmithPlugin.java | 2 +- .../blacksmith/config/GlobalSettings.java | 29 ++++++++- .../blacksmith/config/NPCSettings.java | 63 ++++++++++++++----- 3 files changed, 77 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index 7fe0b74..669d591 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -56,8 +56,8 @@ public class BlacksmithPlugin extends JavaPlugin { * Reloads the configuration file from disk */ public void reload() { - config.load(); this.reloadConfig(); + config.load(); translator.loadLanguages(this.getDataFolder(), this.getConfig().getString("language", "en")); } diff --git a/src/main/java/net/knarcraft/blacksmith/config/GlobalSettings.java b/src/main/java/net/knarcraft/blacksmith/config/GlobalSettings.java index 7087ca9..5e3411a 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/GlobalSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/GlobalSettings.java @@ -10,7 +10,9 @@ import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import java.io.File; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.logging.Level; @@ -22,8 +24,9 @@ public class GlobalSettings { private final Map materialBasePrices = new HashMap<>(); private final Map materialPricePerDurabilityPoints = new HashMap<>(); private final Map enchantmentCosts = new HashMap<>(); - private final Map defaultNPCSettings = new HashMap<>(); + private final List defaultReforgeAbleMaterials = new ArrayList<>(); + private final List defaultEnchantmentBlocklist = new ArrayList<>(); private final Map globalSettings = new HashMap<>(); private final YamlStorage defaultConfig; @@ -252,6 +255,24 @@ public class GlobalSettings { } } + /** + * Gets the value of reforgeAbleItems + * + * @return

The value of reforgeAbleItems

+ */ + public List getReforgeAbleItems() { + return this.defaultReforgeAbleMaterials; + } + + /** + * Gets the value of enchantmentBlocklist + * + * @return

The value of enchantmentBlocklist

+ */ + public List getEnchantmentBlocklist() { + return this.defaultEnchantmentBlocklist; + } + /** * Gets the cost for repairing the given type of anvil * @@ -461,6 +482,12 @@ public class GlobalSettings { 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)))); } /** diff --git a/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java b/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java index 67e248e..a20366e 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java @@ -199,7 +199,12 @@ public class NPCSettings { * @return

All items reforge-able by this NPC

*/ public List getReforgeAbleItems() { - return new ArrayList<>(this.reforgeAbleItems); + Object currentValue = currentValues.get(NPCSetting.REFORGE_ABLE_ITEMS); + if (currentValue == null || String.valueOf(currentValue).isEmpty()) { + return globalSettings.getReforgeAbleItems(); + } else { + return new ArrayList<>(this.reforgeAbleItems); + } } /** @@ -208,7 +213,12 @@ public class NPCSettings { * @return

The list of blocked enchantments

*/ public List getEnchantmentBlocklist() { - return new ArrayList<>(this.enchantmentBlocklist); + Object currentValue = currentValues.get(NPCSetting.ENCHANTMENT_BLOCKLIST); + if (currentValue == null || String.valueOf(currentValue).isEmpty()) { + return globalSettings.getEnchantmentBlocklist(); + } else { + return new ArrayList<>(this.enchantmentBlocklist); + } } /** @@ -360,7 +370,7 @@ public class NPCSettings { * @param value

The value specified by a user

* @return

The value with placeholders replaced

*/ - private Object replaceReforgeAblePresets(Object value) { + private static Object replaceReforgeAblePresets(Object value) { if (value instanceof String string) { String[] list = string.split(","); List replaced = new ArrayList<>(list.length); @@ -384,22 +394,33 @@ public class NPCSettings { */ private void updateEnchantmentBlocklist() { this.enchantmentBlocklist.clear(); - List 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

The enchantment names to block

+ * @return

The enchantments to be blocked

+ */ + public static List getEnchantmentBlocklist(List enchantments) { + List enchantmentBlocklist = new ArrayList<>(); + for (String item : enchantments) { if (InputParsingHelper.isEmpty(item)) { continue; } Enchantment enchantment = InputParsingHelper.matchEnchantment(item); if (enchantment != null) { - this.enchantmentBlocklist.add(enchantment); + enchantmentBlocklist.add(enchantment); } else { BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item + " as a valid enchantment"); } } + return enchantmentBlocklist; } /** @@ -407,23 +428,34 @@ public class NPCSettings { */ private void updateReforgeAbleItems() { this.reforgeAbleItems.clear(); - String newReforgeAbleItems = (String) currentValues.get(NPCSetting.REFORGE_ABLE_ITEMS); - if (newReforgeAbleItems == null) { - return; - } + this.reforgeAbleItems.addAll(getReforgeAbleItems(ConfigHelper.asStringList(getValue( + NPCSetting.REFORGE_ABLE_ITEMS)))); + } - //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

The list of items defined by the user

+ * @return

The materials contained in the item list

+ */ + public static List getReforgeAbleItems(List itemList) { + List reforgeAbleItems = new ArrayList<>(); + if (itemList == null) { + return null; + } Set blacklisted = new HashSet<>(); //Parse every material, and add to reforgeAble items - for (String item : newReforgeAbleItems.split(",")) { + for (String item : itemList) { //Ignore ,, if (InputParsingHelper.isEmpty(item)) { continue; } + //Convert any presets with a list of materials + item = (String) replaceReforgeAblePresets(item); + boolean blacklist = false; if (item.startsWith("-")) { blacklist = true; @@ -433,7 +465,7 @@ public class NPCSettings { Material material = InputParsingHelper.matchMaterial(item); if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) { if (!blacklist) { - this.reforgeAbleItems.add(material); + reforgeAbleItems.add(material); } else { 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 - this.reforgeAbleItems.removeAll(blacklisted); + reforgeAbleItems.removeAll(blacklisted); + return reforgeAbleItems; } } \ No newline at end of file