Adds a new enchantmentBlocklist option

The enchantmentBlocklist allows specifying a list of enchantments blacksmiths should be blocked from adding to items. Especially curse of binding and curse of vanishing are problematic as the effect is quite bad. Mending is possibly undesirable as it's supposed to be quite rare.
This commit is contained in:
2022-11-05 04:21:47 +01:00
parent 012d1df099
commit 89c1c4a56c
10 changed files with 118 additions and 9 deletions

View File

@ -81,7 +81,12 @@ public class GlobalSettings {
* @param newValue <p>The new value for the setting</p>
*/
public void changeValue(NPCSetting npcSetting, Object newValue) {
defaultNPCSettings.put(npcSetting, newValue);
if (npcSetting.getValueType() == SettingValueType.STRING_LIST) {
//Workaround to make sure it's treated as the correct type
defaultNPCSettings.put(npcSetting, ConfigHelper.asStringList(newValue));
} else {
defaultNPCSettings.put(npcSetting, newValue);
}
save();
}

View File

@ -46,7 +46,7 @@ public enum NPCSetting {
/**
* The setting for which items the blacksmith is able to reforge
*/
REFORGE_ABLE_ITEMS("reforgeAbleItems", SettingValueType.STRING_LIST, new String[]{}, "reforgeAbleItems"),
REFORGE_ABLE_ITEMS("reforgeAbleItems", SettingValueType.REFORGE_ABLE_ITEMS, "", "reforgeAbleItems"),
/**
* The setting for the title used to display which kind of blacksmith the NPC is
@ -56,6 +56,12 @@ public enum NPCSetting {
*/
BLACKSMITH_TITLE("blacksmithTitle", SettingValueType.STRING, "blacksmith", "blacksmithTitle"),
/**
* The setting for the enchantments a blacksmith cannot apply to items
*/
ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, new String[]{"binding_curse",
"mending", "vanishing_curse"}, "enchantmentBlocklist"),
/*-----------
| Messages |
-----------*/

View File

@ -6,6 +6,7 @@ import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
@ -20,6 +21,7 @@ import java.util.logging.Level;
public class NPCSettings {
private final List<Material> reforgeAbleItems = new ArrayList<>();
private final List<Enchantment> enchantmentBlocklist = new ArrayList<>();
private final Map<NPCSetting, Object> currentValues = new HashMap<>();
private final GlobalSettings globalSettings;
@ -43,6 +45,7 @@ public class NPCSettings {
}
//Updates the list of reforge-able items/materials
updateReforgeAbleItems();
updateEnchantmentBlocklist();
}
/**
@ -63,10 +66,18 @@ public class NPCSettings {
* @param newValue <p>The new value of the setting</p>
*/
public void changeSetting(NPCSetting setting, Object newValue) {
currentValues.put(setting, newValue);
if (setting.getValueType() == SettingValueType.STRING_LIST) {
//Workaround to make sure it's treated as the correct type
currentValues.put(setting, ConfigHelper.asStringList(newValue));
} else {
currentValues.put(setting, newValue);
}
if (setting == NPCSetting.REFORGE_ABLE_ITEMS) {
updateReforgeAbleItems();
}
if (setting == NPCSetting.ENCHANTMENT_BLOCKLIST) {
updateEnchantmentBlocklist();
}
}
/**
@ -186,7 +197,16 @@ public class NPCSettings {
* @return <p>All items reforge-able by this NPC</p>
*/
public List<Material> getReforgeAbleItems() {
return new ArrayList<>(reforgeAbleItems);
return new ArrayList<>(this.reforgeAbleItems);
}
/**
* Gets the list of blocked enchantments
*
* @return <p>The list of blocked enchantments</p>
*/
public List<Enchantment> getEnchantmentBlocklist() {
return new ArrayList<>(this.enchantmentBlocklist);
}
/**
@ -348,6 +368,29 @@ public class NPCSettings {
}
}
/**
* Updates the list of blocked enchantments
*/
private void updateEnchantmentBlocklist() {
this.enchantmentBlocklist.clear();
List<String> newEnchantmentBlocklist = ConfigHelper.asStringList(getValue(NPCSetting.ENCHANTMENT_BLOCKLIST));
for (String item : newEnchantmentBlocklist) {
if (InputParsingHelper.isEmpty(item)) {
continue;
}
Enchantment enchantment = InputParsingHelper.matchEnchantment(item);
if (enchantment != null) {
this.enchantmentBlocklist.add(enchantment);
} else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item +
" as a valid enchantment");
}
}
}
/**
* Updates the reforge-able items according to the current value of the setting
*/

View File

@ -43,6 +43,11 @@ public enum SettingValueType {
/**
* An enchantment
*/
ENCHANTMENT
ENCHANTMENT,
/**
* A comma-separated list of reforge-able items
*/
REFORGE_ABLE_ITEMS,
}