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:
Kristian Knarvik 2022-11-05 04:21:47 +01:00
parent 012d1df099
commit 89c1c4a56c
10 changed files with 118 additions and 9 deletions

View File

@ -151,6 +151,7 @@ All currently supported presets, and available filters for each preset:
| reforgeCoolDown | 0-3600 | The cool-down, in seconds, a player has to wait between each time they use one specific blacksmith. |
| reforgeAbleItems | DIAMOND_LEGGINGS,GOLD-pickaxe,bow, etc. | Specifies which items this blacksmith is able to reforge. If set to "" or null, all normally repairable items can be repaired. If set to a list of items, only the items specified can be repaired. Some presets have been included for ease of use. Use a preset by specifying "preset:sword-smith" instead of a material such as "gold-pickaxe". |
| blacksmithTitle | text string | The title displayed as part of the message explaining that a blacksmith doesn't recognize a player's held item |
| enchantmentBlocklist | string list | A string list of all enchantments a blacksmith should not be allowed to add to items. |
#### Messages

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) {
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) {
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,
}

View File

@ -145,6 +145,13 @@ public class ReforgeSession implements Runnable {
usableEnchantments.add(enchantment);
}
}
//Remove any enchantments in the block list
usableEnchantments.removeAll(blacksmithTrait.getSettings().getEnchantmentBlocklist());
//In case all usable enchantments have been blocked, abort
if (usableEnchantments.isEmpty()) {
return;
}
//Choose a random enchantment
Enchantment randomEnchantment = usableEnchantments.get(random.nextInt(usableEnchantments.size()));

View File

@ -1,5 +1,8 @@
package net.knarcraft.blacksmith.util;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class for getting an object value as the correct type
*/
@ -9,6 +12,25 @@ public final class ConfigHelper {
}
/**
* Gets the given value as a string list
*
* @param value <p>The raw string list value</p>
* @return <p>The value as a string list</p>
*/
public static List<String> asStringList(Object value) {
if (value instanceof String) {
return List.of(((String) value).split(","));
} else {
List<String> strings = new ArrayList<>();
List<?> list = (List<?>) value;
for (Object object : list) {
strings.add(String.valueOf(object));
}
return strings;
}
}
/**
* Gets the given value as a double
*

View File

@ -32,12 +32,27 @@ public final class TabCompleteValuesHelper {
case POSITIVE_DOUBLE -> getPositiveDoubles();
case STRING -> getStrings();
case PERCENTAGE -> getPercentages();
case STRING_LIST -> getReforgeAbleMaterials();
case REFORGE_ABLE_ITEMS -> getReforgeAbleMaterials();
case MATERIAL -> getAllReforgeAbleMaterials();
case ENCHANTMENT -> getAllEnchantments();
case STRING_LIST -> getExampleEnchantmentBlockLists();
};
}
/**
* Gets example enchantment block lists
*
* @return <p>Some example enchantment block lists</p>
*/
private static List<String> getExampleEnchantmentBlockLists() {
List<String> exampleBlockLists = new ArrayList<>();
exampleBlockLists.add(Enchantment.VANISHING_CURSE.getKey().getKey() + "," +
Enchantment.BINDING_CURSE.getKey().getKey() + "," + Enchantment.MENDING.getKey().getKey());
exampleBlockLists.add(Enchantment.VANISHING_CURSE.getKey().getKey() + "," +
Enchantment.BINDING_CURSE.getKey().getKey());
return exampleBlockLists;
}
/**
* Gets a complete list of all reforge-able materials
*

View File

@ -4,6 +4,8 @@ import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.formatting.TranslatableMessage;
import org.bukkit.command.CommandSender;
import java.util.List;
import static net.knarcraft.blacksmith.formatting.StringFormatter.displayErrorMessage;
/**
@ -31,7 +33,7 @@ public final class TypeValidationHelper {
case POSITIVE_INTEGER -> isPositiveInteger(value, sender);
case PERCENTAGE -> isPercentage(value, sender);
case BOOLEAN -> true;
case STRING_LIST -> isStringList(value, sender);
case STRING_LIST, REFORGE_ABLE_ITEMS -> isStringList(value, sender);
case MATERIAL, ENCHANTMENT -> false;
};
} catch (ClassCastException exception) {
@ -48,7 +50,7 @@ public final class TypeValidationHelper {
* @return <p>True if the value is a string list</p>
*/
private static boolean isStringList(Object value, CommandSender sender) {
boolean isStringList = value instanceof String[] || value instanceof String;
boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String;
if (!isStringList && sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_STRING_LIST_REQUIRED);
}
@ -65,7 +67,7 @@ public final class TypeValidationHelper {
private static boolean isPercentage(Object value, CommandSender sender) {
try {
int intValue = ConfigHelper.asInt(value);
return intValue > 0 && intValue <= 100;
return intValue >= 0 && intValue <= 100;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_PERCENTAGE_REQUIRED);

View File

@ -39,6 +39,9 @@ defaults:
# up yet.
reforgeAbleItems: [ ]
# The enchantments a blacksmith is denied from applying to an item. Disable anything you find too op or annoying.
enchantmentBlocklist: [ "binding_curse", "mending", "vanishing_curse" ]
# The chance to fail reforging an item, which only repairs the item a tiny bit or not at all (0-100)
failReforgeChance: 10 # Default = 10%