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 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 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" "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 example: "preset:ARMOR_SMITH:DIAMOND,preset:ARMOR_SMITH:NETHERITE" would allow the blacksmith to repair all diamond and
netherite armor. 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: All currently supported presets, and available filters for each preset:
- WEAPON_SMITH: - BLACKSMITH (WEAPON_SMITH + ARMOR_SMITH + TOOL_SMITH)
- BOW - WEAPON_SMITH: (RANGED + SWORD + SHIELD)
- SWORD - BOW (bows and crossbows)
- RANGED - SWORD (swords)
- ARMOR_SMITH: - RANGED (bows, crossbows and tridents)
- LEATHER - ARMOR_SMITH: (HELMET + BOOTS + LEGGINGS + CHESTPLATE + ELYTRA)
- IRON - LEATHER (all pieces of leather armor)
- CHAINMAIL - IRON (all pieces of iron armor)
- GOLD - CHAINMAIL (all pieces of chainmail armor)
- DIAMOND - GOLD (all pieces of gold armor)
- NETHERITE - DIAMOND (all pieces of diamond armor)
- HELMET - NETHERITE (all pieces of netherite armor)
- BOOTS - HELMET (all helmets)
- LEGGINGS - BOOTS (all boots)
- CHESTPLATE - LEGGINGS (all leggings)
- TOOL_SMITH - CHESTPLATE (all chest-plates)
- WOOD - TOOL_SMITH: (PICKAXE + AXE + HOE + SHOVEL + MISC)
- STONE - WOOD (all wood tools)
- IRON - STONE (all stone tools)
- GOLD - IRON (all iron tools)
- DIAMOND - GOLD (all gold tools)
- NETHERITE - DIAMOND (all diamond tools)
- PICKAXE - NETHERITE (all netherite tools)
- AXE - PICKAXE (all pickaxes)
- HOE - AXE (all axes)
- SHOVEL - HOE (all hoes)
- MISC - SHOVEL (all shovels)
- MISC (FISHING_ROD + SHEARS + FLINT_AND_STEEL)
## Permissions ## Permissions

View File

@ -11,8 +11,10 @@ import org.bukkit.inventory.ItemStack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
@ -404,18 +406,35 @@ public class NPCSettings {
//Convert any presets with a list of materials //Convert any presets with a list of materials
newReforgeAbleItems = (String) replaceReforgeAblePresets(newReforgeAbleItems); newReforgeAbleItems = (String) replaceReforgeAblePresets(newReforgeAbleItems);
Set<Material> blacklisted = new HashSet<>();
//Parse every material, and add to reforgeAble items
for (String item : newReforgeAbleItems.split(",")) { for (String item : newReforgeAbleItems.split(",")) {
if (InputParsingHelper.isEmpty(item)) { if (InputParsingHelper.isEmpty(item)) {
continue; continue;
} }
boolean blacklist = false;
if (item.startsWith("-")) {
blacklist = true;
item = item.substring(1);
}
Material material = InputParsingHelper.matchMaterial(item); Material material = InputParsingHelper.matchMaterial(item);
if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) { if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) {
if (!blacklist) {
this.reforgeAbleItems.add(material); this.reforgeAbleItems.add(material);
} else {
blacklisted.add(material);
}
} else { } else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item + BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item +
" as a valid reforge-able 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; package net.knarcraft.blacksmith.config;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material; import org.bukkit.Material;
import java.util.ArrayList; import java.util.ArrayList;
@ -12,6 +13,11 @@ import java.util.logging.Level;
*/ */
public enum SmithPreset { public enum SmithPreset {
/**
* A blacksmith capable of re-forging everything
*/
BLACKSMITH(new SmithPresetFilter[]{}),
/** /**
* A blacksmith capable of re-forging all weapons (including shields) * 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> * @return <p>The string, possibly with the preset replaced</p>
*/ */
public static String replacePreset(String possiblePreset) { public static String replacePreset(String possiblePreset) {
boolean negated = false;
if (possiblePreset.startsWith("-")) {
negated = true;
possiblePreset = possiblePreset.substring(1);
}
String upperCasedPreset = possiblePreset.replace('-', '_').toUpperCase(); String upperCasedPreset = possiblePreset.replace('-', '_').toUpperCase();
if (!upperCasedPreset.startsWith("PRESET:")) { if (!upperCasedPreset.startsWith("PRESET:")) {
return possiblePreset; return possiblePreset;
@ -105,11 +117,16 @@ public enum SmithPreset {
} }
//Return the list of materials included in the preset //Return the list of materials included in the preset
List<String> materialNames;
if (filter != null) { if (filter != null) {
return String.join(",", preset.getMaterialNames(filter)); materialNames = preset.getMaterialNames(filter);
} else { } 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() { public List<Material> getMaterials() {
return switch (this) { return switch (this) {
case BLACKSMITH -> ItemHelper.getAllReforgeAbleMaterials();
case WEAPON_SMITH -> getWeapons(); case WEAPON_SMITH -> getWeapons();
case ARMOR_SMITH -> getArmor(); case ARMOR_SMITH -> getArmor();
case TOOL_SMITH -> getTools(); case TOOL_SMITH -> getTools();
@ -247,4 +265,20 @@ public enum SmithPreset {
return items; 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; package net.knarcraft.blacksmith.util;
import org.bukkit.Material;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
import java.util.ArrayList;
import java.util.List;
public final class ItemHelper { public final class ItemHelper {
private 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 net.knarcraft.blacksmith.config.SmithPresetFilter;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; 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.ArrayList;
import java.util.List; import java.util.List;
@ -35,7 +32,7 @@ public final class TabCompleteValuesHelper {
case STRING -> getStrings(); case STRING -> getStrings();
case PERCENTAGE -> getPercentages(); case PERCENTAGE -> getPercentages();
case REFORGE_ABLE_ITEMS -> getReforgeAbleMaterials(); case REFORGE_ABLE_ITEMS -> getReforgeAbleMaterials();
case MATERIAL -> getAllReforgeAbleMaterials(); case MATERIAL -> getAllReforgeAbleMaterialNames();
case ENCHANTMENT -> getAllEnchantments(); case ENCHANTMENT -> getAllEnchantments();
case STRING_LIST -> getExampleEnchantmentBlockLists(); case STRING_LIST -> getExampleEnchantmentBlockLists();
}; };
@ -55,19 +52,17 @@ public final class TabCompleteValuesHelper {
return exampleBlockLists; 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<>(); List<String> reforgeAbleMaterials = new ArrayList<>();
for (Material material : Material.values()) { for (Material material : ItemHelper.getAllReforgeAbleMaterials()) {
ItemStack item = new ItemStack(material);
if (item.getItemMeta() instanceof Damageable && EnchantmentTarget.BREAKABLE.includes(item)) {
reforgeAbleMaterials.add(material.name()); reforgeAbleMaterials.add(material.name());
} }
}
return reforgeAbleMaterials; return reforgeAbleMaterials;
} }