Adds negation for material names and presets #13
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user