Adds negation for material names and presets #13

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

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;
}
}