diff --git a/README.md b/README.md index 61cfda6..f1a5e91 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main/java/net/knarcraft/blacksmith/config/GlobalSettings.java b/src/main/java/net/knarcraft/blacksmith/config/GlobalSettings.java index 5ff0e06..4a59cfa 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/GlobalSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/GlobalSettings.java @@ -81,7 +81,12 @@ public class GlobalSettings { * @param newValue

The new value for the setting

*/ 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(); } diff --git a/src/main/java/net/knarcraft/blacksmith/config/NPCSetting.java b/src/main/java/net/knarcraft/blacksmith/config/NPCSetting.java index d6f4aa7..f932b10 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/NPCSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/NPCSetting.java @@ -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 | -----------*/ diff --git a/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java b/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java index e3f7ed8..0541816 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java @@ -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 reforgeAbleItems = new ArrayList<>(); + private final List enchantmentBlocklist = new ArrayList<>(); private final Map 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

The new value of the setting

*/ 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

All items reforge-able by this NPC

*/ public List getReforgeAbleItems() { - return new ArrayList<>(reforgeAbleItems); + return new ArrayList<>(this.reforgeAbleItems); + } + + /** + * Gets the list of blocked enchantments + * + * @return

The list of blocked enchantments

+ */ + public List 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 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 */ diff --git a/src/main/java/net/knarcraft/blacksmith/config/SettingValueType.java b/src/main/java/net/knarcraft/blacksmith/config/SettingValueType.java index 0b32122..942a3bf 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/SettingValueType.java +++ b/src/main/java/net/knarcraft/blacksmith/config/SettingValueType.java @@ -43,6 +43,11 @@ public enum SettingValueType { /** * An enchantment */ - ENCHANTMENT + ENCHANTMENT, + + /** + * A comma-separated list of reforge-able items + */ + REFORGE_ABLE_ITEMS, } diff --git a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java index 5160600..5bd9082 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java @@ -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())); diff --git a/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java index e303630..6cd7245 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java @@ -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

The raw string list value

+ * @return

The value as a string list

+ */ + public static List asStringList(Object value) { + if (value instanceof String) { + return List.of(((String) value).split(",")); + } else { + List 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 * diff --git a/src/main/java/net/knarcraft/blacksmith/util/TabCompleteValuesHelper.java b/src/main/java/net/knarcraft/blacksmith/util/TabCompleteValuesHelper.java index 82c5c51..766b80a 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/TabCompleteValuesHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/TabCompleteValuesHelper.java @@ -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

Some example enchantment block lists

+ */ + private static List getExampleEnchantmentBlockLists() { + List 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 * diff --git a/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java b/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java index dd56982..7b7d850 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java @@ -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

True if the value is a string list

*/ 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); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5449a15..1de3689 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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%