EpicKnarvik97 12f807060e Implements a mix of #3 and #7
Adds a preset command which can be used to see available presets
Adds preset filters which can be used to specify item sub-categories within a preset
Removes SWORD_SMITH and RANGED_SMITH, as those are replaced by the RANGED and SWORD filters
Adds a list of usable filters for each preset
2022-10-13 18:40:38 +02:00

250 lines
8.0 KiB
Java

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 <p>The filters applicable to this preset</p>
*/
SmithPreset(SmithPresetFilter[] filters) {
this.filters = filters;
}
/**
* Gets whether this preset supports the given filter
*
* @param filter <p>The filter to check</p>
* @return <p>True if the filter is supported</p>
*/
public boolean supportsFilter(SmithPresetFilter filter) {
return List.of(filters).contains(filter);
}
/**
* Gets all filters supported by this preset
*
* @return <p>The filters supported by this preset</p>
*/
public List<SmithPresetFilter> getSupportedFilters() {
return List.of(filters);
}
/**
* Gets the names of all available smith presets
*
* @return <p>All available smith presets</p>
*/
public static List<String> getPresetNames() {
List<String> 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 <p>The string that might be a preset</p>
* @return <p>The string, possibly with the preset replaced</p>
*/
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 <p>The filter to use for filtering</p>
* @return <p>The materials included in this preset, filtered using the given filter</p>
*/
public List<Material> getFilteredMaterials(SmithPresetFilter filter) {
List<Material> materials = new ArrayList<>(this.getMaterials());
materials.removeIf((item) -> !filter.isIncluded(item));
return materials;
}
/**
* Gets all materials included in this preset
*
* @return <p>All materials in this preset</p>
*/
public List<Material> getMaterials() {
return switch (this) {
case WEAPON_SMITH -> getWeapons();
case ARMOR_SMITH -> getArmor();
case TOOL_SMITH -> getTools();
};
}
/**
* Gets all ranged weapon materials
*
* @return <p>All ranged weapon materials</p>
*/
private List<Material> getRanged() {
List<Material> ranged = new ArrayList<>();
ranged.add(Material.TRIDENT);
ranged.add(Material.BOW);
ranged.add(Material.CROSSBOW);
return ranged;
}
/**
* Gets all tool materials
*
* @return <p>All tool materials</p>
*/
private List<Material> getTools() {
List<Material> 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 <p>All weapon materials</p>
*/
private List<Material> getWeapons() {
List<Material> weapons = new ArrayList<>(getSwords());
weapons.addAll(getRanged());
weapons.add(Material.SHIELD);
return weapons;
}
/**
* Gets all sword materials
*
* @return <p>All sword materials</p>
*/
private List<Material> getSwords() {
return getMaterialsEndingWith("_SWORD");
}
private List<Material> getArmor() {
List<Material> 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 <p>The string to look for</p>
* @return <p>The resulting materials</p>
*/
private List<Material> getMaterialsEndingWith(String end) {
List<Material> 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 <p>All material names for this smith</p>
*/
private List<String> getMaterialNames() {
return getNames(this.getMaterials());
}
/**
* Gets material names of all materials reforge-able by this smith
*
* @param filter <p>The filter used for filtering materials</p>
* @return <p>All material names for this smith</p>
*/
private List<String> getMaterialNames(SmithPresetFilter filter) {
return getNames(this.getFilteredMaterials(filter));
}
/**
* Gets a list of material names from the given materials
*
* @param materials <p>The materials to get the names of</p>
* @return <p>The names of the materials</p>
*/
private List<String> getNames(List<Material> materials) {
List<String> items = new ArrayList<>();
for (Material material : materials) {
items.add(material.name().toLowerCase().replace("_", "-"));
}
return items;
}
}