Adds negation for material names and presets #13

This commit is contained in:
Kristian Knarvik 2023-01-09 05:03:28 +01:00
parent 30b8507b9f
commit ea54492ccf
5 changed files with 120 additions and 42 deletions

View File

@ -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

View File

@ -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<Material> 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);
}
}
}

View File

@ -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 <p>The string, possibly with the preset replaced</p>
*/
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<String> 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<Material> 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 <p>The material names to negate</p>
* @return <p>The negated material names</p>
*/
private static List<String> negateMaterials(List<String> materials) {
List<String> negatedMaterials = new ArrayList<>(materials.size());
materials.forEach((material) -> {
if (material != null && !material.isBlank()) {
negatedMaterials.add("-" + material);
}
});
return negatedMaterials;
}
}

View File

@ -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 <p>A complete list of reforge-able materials</p>
*/
public static List<Material> getAllReforgeAbleMaterials() {
List<Material> 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;
}
}

View File

@ -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 <p>A complete list of reforge-able materials</p>
* @return <p>A complete list of reforge-able material names</p>
*/
private static List<String> getAllReforgeAbleMaterials() {
private static List<String> getAllReforgeAbleMaterialNames() {
List<String> 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;
}