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
*/
public void reload() {
config.load();
this.reloadConfig();
config.load();
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 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<Material, Double> materialBasePrices = new HashMap<>();
private final Map<Material, Double> materialPricePerDurabilityPoints = new HashMap<>();
private final Map<Enchantment, Double> enchantmentCosts = 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 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
*
@ -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))));
}
/**

View File

@ -199,7 +199,12 @@ public class NPCSettings {
* @return <p>All items reforge-able by this NPC</p>
*/
public List<Material> 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 <p>The list of blocked enchantments</p>
*/
public List<Enchantment> 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 <p>The value specified by a user</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) {
String[] list = string.split(",");
List<String> replaced = new ArrayList<>(list.length);
@ -384,22 +394,33 @@ public class NPCSettings {
*/
private void updateEnchantmentBlocklist() {
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)) {
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 <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<>();
//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;
}
}