From ea54492ccfde800dd2a861b5a4c7ec8c9e095b7e Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 9 Jan 2023 05:03:28 +0100 Subject: [PATCH] Adds negation for material names and presets #13 --- README.md | 63 +++++++++++-------- .../blacksmith/config/NPCSettings.java | 21 ++++++- .../blacksmith/config/SmithPreset.java | 38 ++++++++++- .../knarcraft/blacksmith/util/ItemHelper.java | 21 +++++++ .../util/TabCompleteValuesHelper.java | 19 +++--- 5 files changed, 120 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 209501a..a1e7670 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,9 @@ the default one instead. ### Presets and filters: +Presets are a nice way to make specialized blacksmiths by specifying categories of materials, instead of manually +listing every material manually. They can only be used for the `reforgeAbleItems` option. + Note: All of these can be used when specifying reforge-able items, such as "preset:weapon-smith:bow,preset:armor_smith:gold,PRESET:TOOL_SMITH,shield" @@ -81,35 +84,41 @@ to make the blacksmith only repair bows. You can use the same preset several tim example: "preset:ARMOR_SMITH:DIAMOND,preset:ARMOR_SMITH:NETHERITE" would allow the blacksmith to repair all diamond and netherite armor. +Presets and filters can also be negated by applying a "-" character in front of the material name. For example, +"-SHIELD" would make shields unrepairable, even if included in a preset. "preset:WEAPON_SMITH,-SHIELD" +would allow the blacksmith to repair any weapon included in the WEAPON_SMITH preset except shields. +"preset:BLACKSMITH,-ELYTRA"would allow the blacksmith to repair any repairable item, except elytra. + All currently supported presets, and available filters for each preset: -- WEAPON_SMITH: - - BOW - - SWORD - - RANGED -- ARMOR_SMITH: - - LEATHER - - IRON - - CHAINMAIL - - GOLD - - DIAMOND - - NETHERITE - - HELMET - - BOOTS - - LEGGINGS - - CHESTPLATE -- TOOL_SMITH - - WOOD - - STONE - - IRON - - GOLD - - DIAMOND - - NETHERITE - - PICKAXE - - AXE - - HOE - - SHOVEL - - MISC +- BLACKSMITH (WEAPON_SMITH + ARMOR_SMITH + TOOL_SMITH) +- WEAPON_SMITH: (RANGED + SWORD + SHIELD) + - BOW (bows and crossbows) + - SWORD (swords) + - RANGED (bows, crossbows and tridents) +- ARMOR_SMITH: (HELMET + BOOTS + LEGGINGS + CHESTPLATE + ELYTRA) + - LEATHER (all pieces of leather armor) + - IRON (all pieces of iron armor) + - CHAINMAIL (all pieces of chainmail armor) + - GOLD (all pieces of gold armor) + - DIAMOND (all pieces of diamond armor) + - NETHERITE (all pieces of netherite armor) + - HELMET (all helmets) + - BOOTS (all boots) + - LEGGINGS (all leggings) + - CHESTPLATE (all chest-plates) +- TOOL_SMITH: (PICKAXE + AXE + HOE + SHOVEL + MISC) + - WOOD (all wood tools) + - STONE (all stone tools) + - IRON (all iron tools) + - GOLD (all gold tools) + - DIAMOND (all diamond tools) + - NETHERITE (all netherite tools) + - PICKAXE (all pickaxes) + - AXE (all axes) + - HOE (all hoes) + - SHOVEL (all shovels) + - MISC (FISHING_ROD + SHEARS + FLINT_AND_STEEL) ## Permissions diff --git a/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java b/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java index a70d77f..6cb7c58 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/NPCSettings.java @@ -11,8 +11,10 @@ import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.logging.Level; /** @@ -404,18 +406,35 @@ public class NPCSettings { //Convert any presets with a list of materials newReforgeAbleItems = (String) replaceReforgeAblePresets(newReforgeAbleItems); + Set blacklisted = new HashSet<>(); + + //Parse every material, and add to reforgeAble items for (String item : newReforgeAbleItems.split(",")) { if (InputParsingHelper.isEmpty(item)) { continue; } + boolean blacklist = false; + if (item.startsWith("-")) { + blacklist = true; + item = item.substring(1); + } Material material = InputParsingHelper.matchMaterial(item); if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) { - this.reforgeAbleItems.add(material); + if (!blacklist) { + this.reforgeAbleItems.add(material); + } else { + blacklisted.add(material); + } } else { BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item + " as a valid reforge-able item"); } } + + //Remove any blacklisted materials at the end to make sure order of arguments won't matter + for (Material material : blacklisted) { + reforgeAbleItems.remove(material); + } } } \ No newline at end of file diff --git a/src/main/java/net/knarcraft/blacksmith/config/SmithPreset.java b/src/main/java/net/knarcraft/blacksmith/config/SmithPreset.java index efbc311..39d775c 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/SmithPreset.java +++ b/src/main/java/net/knarcraft/blacksmith/config/SmithPreset.java @@ -1,6 +1,7 @@ package net.knarcraft.blacksmith.config; import net.knarcraft.blacksmith.BlacksmithPlugin; +import net.knarcraft.blacksmith.util.ItemHelper; import org.bukkit.Material; import java.util.ArrayList; @@ -12,6 +13,11 @@ import java.util.logging.Level; */ public enum SmithPreset { + /** + * A blacksmith capable of re-forging everything + */ + BLACKSMITH(new SmithPresetFilter[]{}), + /** * A blacksmith capable of re-forging all weapons (including shields) */ @@ -81,6 +87,12 @@ public enum SmithPreset { * @return

The string, possibly with the preset replaced

*/ public static String replacePreset(String possiblePreset) { + boolean negated = false; + if (possiblePreset.startsWith("-")) { + negated = true; + possiblePreset = possiblePreset.substring(1); + } + String upperCasedPreset = possiblePreset.replace('-', '_').toUpperCase(); if (!upperCasedPreset.startsWith("PRESET:")) { return possiblePreset; @@ -105,11 +117,16 @@ public enum SmithPreset { } //Return the list of materials included in the preset + List materialNames; if (filter != null) { - return String.join(",", preset.getMaterialNames(filter)); + materialNames = preset.getMaterialNames(filter); } else { - return String.join(",", preset.getMaterialNames()); + materialNames = preset.getMaterialNames(); } + if (negated) { + materialNames = negateMaterials(materialNames); + } + return String.join(",", materialNames); } /** @@ -131,6 +148,7 @@ public enum SmithPreset { */ public List getMaterials() { return switch (this) { + case BLACKSMITH -> ItemHelper.getAllReforgeAbleMaterials(); case WEAPON_SMITH -> getWeapons(); case ARMOR_SMITH -> getArmor(); case TOOL_SMITH -> getTools(); @@ -247,4 +265,20 @@ public enum SmithPreset { return items; } + /** + * Negates the given material names + * + * @param materials

The material names to negate

+ * @return

The negated material names

+ */ + private static List negateMaterials(List materials) { + List negatedMaterials = new ArrayList<>(materials.size()); + materials.forEach((material) -> { + if (material != null && !material.isBlank()) { + negatedMaterials.add("-" + material); + } + }); + return negatedMaterials; + } + } diff --git a/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java index 0257b03..db2c759 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java @@ -1,8 +1,13 @@ package net.knarcraft.blacksmith.util; +import org.bukkit.Material; +import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; +import java.util.ArrayList; +import java.util.List; + public final class ItemHelper { private ItemHelper() { @@ -40,4 +45,20 @@ public final class ItemHelper { } } + /** + * Gets a complete list of all reforge-able materials + * + * @return

A complete list of reforge-able materials

+ */ + public static List getAllReforgeAbleMaterials() { + List reforgeAbleMaterials = new ArrayList<>(); + for (Material material : Material.values()) { + ItemStack item = new ItemStack(material); + if (item.getItemMeta() instanceof Damageable && EnchantmentTarget.BREAKABLE.includes(item)) { + reforgeAbleMaterials.add(material); + } + } + return reforgeAbleMaterials; + } + } diff --git a/src/main/java/net/knarcraft/blacksmith/util/TabCompleteValuesHelper.java b/src/main/java/net/knarcraft/blacksmith/util/TabCompleteValuesHelper.java index bff30c4..e979a29 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/TabCompleteValuesHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/TabCompleteValuesHelper.java @@ -5,9 +5,6 @@ import net.knarcraft.blacksmith.config.SmithPreset; import net.knarcraft.blacksmith.config.SmithPresetFilter; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; -import org.bukkit.enchantments.EnchantmentTarget; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.Damageable; import java.util.ArrayList; import java.util.List; @@ -35,7 +32,7 @@ public final class TabCompleteValuesHelper { case STRING -> getStrings(); case PERCENTAGE -> getPercentages(); case REFORGE_ABLE_ITEMS -> getReforgeAbleMaterials(); - case MATERIAL -> getAllReforgeAbleMaterials(); + case MATERIAL -> getAllReforgeAbleMaterialNames(); case ENCHANTMENT -> getAllEnchantments(); case STRING_LIST -> getExampleEnchantmentBlockLists(); }; @@ -55,18 +52,16 @@ public final class TabCompleteValuesHelper { return exampleBlockLists; } + /** - * Gets a complete list of all reforge-able materials + * Gets a complete list of all reforge-able material names * - * @return

A complete list of reforge-able materials

+ * @return

A complete list of reforge-able material names

*/ - private static List getAllReforgeAbleMaterials() { + private static List getAllReforgeAbleMaterialNames() { List reforgeAbleMaterials = new ArrayList<>(); - for (Material material : Material.values()) { - ItemStack item = new ItemStack(material); - if (item.getItemMeta() instanceof Damageable && EnchantmentTarget.BREAKABLE.includes(item)) { - reforgeAbleMaterials.add(material.name()); - } + for (Material material : ItemHelper.getAllReforgeAbleMaterials()) { + reforgeAbleMaterials.add(material.name()); } return reforgeAbleMaterials; }