package net.knarcraft.blacksmith.config; import net.knarcraft.blacksmith.BlacksmithPlugin; import org.bukkit.Material; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; /** * A representation of the presets for different kinds of smiths */ public enum SmithPreset { /** * A blacksmith capable of re-forging all weapons (including shields) */ WEAPON_SMITH(new SmithPresetFilter[]{SmithPresetFilter.BOW, SmithPresetFilter.SWORD, SmithPresetFilter.RANGED}), /** * A blacksmith capable of re-forging all armor */ ARMOR_SMITH(new SmithPresetFilter[]{SmithPresetFilter.LEATHER, SmithPresetFilter.IRON, SmithPresetFilter.CHAINMAIL, SmithPresetFilter.GOLD, SmithPresetFilter.DIAMOND, SmithPresetFilter.NETHERITE}), /** * A blacksmith capable of re-forging all tools (hoe, axe, shovel, pickaxe, flint and steel, shears, fishing rod) */ TOOL_SMITH(new SmithPresetFilter[]{SmithPresetFilter.WOOD, SmithPresetFilter.STONE, SmithPresetFilter.IRON, SmithPresetFilter.GOLD, SmithPresetFilter.DIAMOND, SmithPresetFilter.NETHERITE, SmithPresetFilter.PICKAXE, SmithPresetFilter.AXE, SmithPresetFilter.HOE, SmithPresetFilter.SHOVEL, SmithPresetFilter.MISC}); private final SmithPresetFilter[] filters; /** * Instantiates a new smith preset * * @param filters

The filters applicable to this preset

*/ SmithPreset(SmithPresetFilter[] filters) { this.filters = filters; } /** * Gets whether this preset supports the given filter * * @param filter

The filter to check

* @return

True if the filter is supported

*/ public boolean supportsFilter(SmithPresetFilter filter) { return List.of(filters).contains(filter); } /** * Gets all filters supported by this preset * * @return

The filters supported by this preset

*/ public List getSupportedFilters() { return List.of(filters); } /** * Gets the names of all available smith presets * * @return

All available smith presets

*/ public static List getPresetNames() { List presetNames = new ArrayList<>(); for (SmithPreset preset : SmithPreset.values()) { presetNames.add(preset.name()); } return presetNames; } /** * Replaces the given string if it's a smith type preset * * @param possiblePreset

The string that might be a preset

* @return

The string, possibly with the preset replaced

*/ public static String replacePreset(String possiblePreset) { String upperCasedPreset = possiblePreset.replace('-', '_').toUpperCase(); if (!upperCasedPreset.startsWith("PRESET:")) { return possiblePreset; } //Parse the input SmithPresetFilter filter = null; SmithPreset preset; try { String[] parts = upperCasedPreset.split(":"); if (parts.length > 2) { filter = SmithPresetFilter.valueOf(parts[2]); } preset = SmithPreset.valueOf(parts[1]); } catch (IllegalArgumentException exception) { /* This case means that either the preset or the filter given is invalid, and thus the preset string should be ignored to prevent any problems. */ BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, String.format("The smith preset %s is " + "invalid, and will be ignored. Please fix it!", possiblePreset)); return ""; } //Return the list of materials included in the preset if (filter != null) { return String.join(",", preset.getMaterialNames(filter)); } else { return String.join(",", preset.getMaterialNames()); } } /** * Gets the materials included in this preset, filtered using the given filter * * @param filter

The filter to use for filtering

* @return

The materials included in this preset, filtered using the given filter

*/ public List getFilteredMaterials(SmithPresetFilter filter) { List materials = new ArrayList<>(this.getMaterials()); materials.removeIf((item) -> !filter.isIncluded(item)); return materials; } /** * Gets all materials included in this preset * * @return

All materials in this preset

*/ public List getMaterials() { return switch (this) { case WEAPON_SMITH -> getWeapons(); case ARMOR_SMITH -> getArmor(); case TOOL_SMITH -> getTools(); }; } /** * Gets all ranged weapon materials * * @return

All ranged weapon materials

*/ private List getRanged() { List ranged = new ArrayList<>(); ranged.add(Material.TRIDENT); ranged.add(Material.BOW); ranged.add(Material.CROSSBOW); return ranged; } /** * Gets all tool materials * * @return

All tool materials

*/ private List getTools() { List tools = new ArrayList<>(); tools.addAll(getMaterialsEndingWith("_HOE")); tools.addAll(getMaterialsEndingWith("_SHOVEL")); tools.addAll(getMaterialsEndingWith("_AXE")); tools.addAll(getMaterialsEndingWith("_PICKAXE")); tools.add(Material.FLINT_AND_STEEL); tools.add(Material.FISHING_ROD); tools.add(Material.SHEARS); return tools; } /** * Gets all weapon materials * * @return

All weapon materials

*/ private List getWeapons() { List weapons = new ArrayList<>(getSwords()); weapons.addAll(getRanged()); weapons.add(Material.SHIELD); return weapons; } /** * Gets all sword materials * * @return

All sword materials

*/ private List getSwords() { return getMaterialsEndingWith("_SWORD"); } private List getArmor() { List armor = new ArrayList<>(); armor.addAll(getMaterialsEndingWith("HELMET")); armor.addAll(getMaterialsEndingWith("CHESTPLATE")); armor.addAll(getMaterialsEndingWith("LEGGINGS")); armor.addAll(getMaterialsEndingWith("BOOTS")); armor.add(Material.ELYTRA); return armor; } /** * Gets all materials ending with the given string * * @param end

The string to look for

* @return

The resulting materials

*/ private List getMaterialsEndingWith(String end) { List swords = new ArrayList<>(); for (Material material : Material.values()) { if (!material.name().startsWith("LEGACY") && material.name().endsWith(end)) { swords.add(material); } } return swords; } /** * Gets material names of all materials reforge-able by this smith * * @return

All material names for this smith

*/ private List getMaterialNames() { return getNames(this.getMaterials()); } /** * Gets material names of all materials reforge-able by this smith * * @param filter

The filter used for filtering materials

* @return

All material names for this smith

*/ private List getMaterialNames(SmithPresetFilter filter) { return getNames(this.getFilteredMaterials(filter)); } /** * Gets a list of material names from the given materials * * @param materials

The materials to get the names of

* @return

The names of the materials

*/ private List getNames(List materials) { List items = new ArrayList<>(); for (Material material : materials) { items.add(material.name().toLowerCase().replace("_", "-")); } return items; } }