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
This commit is contained in:
2022-10-13 18:40:38 +02:00
parent 6a55300c1e
commit 12f807060e
16 changed files with 552 additions and 55 deletions

View File

@@ -1,39 +1,77 @@
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 swords
*/
SWORD_SMITH,
/**
* A blacksmith capable of re-forging all weapons (including shields)
*/
WEAPON_SMITH,
WEAPON_SMITH(new SmithPresetFilter[]{SmithPresetFilter.BOW, SmithPresetFilter.SWORD, SmithPresetFilter.RANGED}),
/**
* A blacksmith capable of re-forging all armor
*/
ARMOR_SMITH,
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,
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;
/**
* A blacksmith capable of re-forging all ranged weapons (bow, crossbow, trident)
* Instantiates a new smith preset
*
* @param filters <p>The filters applicable to this preset</p>
*/
RANGED_SMITH;
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
@@ -42,13 +80,47 @@ public enum SmithPreset {
* @return <p>The string, possibly with the preset replaced</p>
*/
public static String replacePreset(String possiblePreset) {
for (SmithPreset smithPreset : SmithPreset.values()) {
if (possiblePreset.replace('-', '_').equalsIgnoreCase("preset:" +
smithPreset.name())) {
return String.join(",", smithPreset.getMaterialNames());
}
String upperCasedPreset = possiblePreset.replace('-', '_').toUpperCase();
if (!upperCasedPreset.startsWith("PRESET:")) {
return possiblePreset;
}
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;
}
/**
@@ -58,11 +130,9 @@ public enum SmithPreset {
*/
public List<Material> getMaterials() {
return switch (this) {
case SWORD_SMITH -> getSwords();
case WEAPON_SMITH -> getWeapons();
case ARMOR_SMITH -> getArmor();
case TOOL_SMITH -> getTools();
case RANGED_SMITH -> getRanged();
};
}
@@ -149,8 +219,28 @@ public enum SmithPreset {
* @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 : this.getMaterials()) {
for (Material material : materials) {
items.add(material.name().toLowerCase().replace("_", "-"));
}
return items;