Allows specifying the material tag prefix
All checks were successful
EpicKnarvik97/KnarLib/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2023-07-08 17:49:39 +02:00
parent 94d74e7794
commit 46f85d37ab

View File

@ -29,16 +29,18 @@ public final class MaterialHelper {
* Loads the materials specified in the given list * Loads the materials specified in the given list
* *
* @param materials <p>The list of material strings to load</p> * @param materials <p>The list of material strings to load</p>
* @param tagPrefix <p>The prefix differentiating a tag from a material</p>
* @param logger <p>The logger to use for logging parsing errors</p> * @param logger <p>The logger to use for logging parsing errors</p>
*/ */
public static @NotNull Set<Material> loadMaterialList(@NotNull List<?> materials, @NotNull Logger logger) { public static @NotNull Set<Material> loadMaterialList(@NotNull List<?> materials, @NotNull String tagPrefix,
@NotNull Logger logger) {
Set<Material> parsedMaterials = new HashSet<>(); Set<Material> parsedMaterials = new HashSet<>();
for (Object value : materials) { for (Object value : materials) {
if (!(value instanceof String string)) { if (!(value instanceof String string)) {
continue; continue;
} }
parsedMaterials.addAll(loadMaterialString(string, logger)); parsedMaterials.addAll(loadMaterialString(string, tagPrefix, logger));
} }
return parsedMaterials; return parsedMaterials;
} }
@ -47,13 +49,15 @@ public final class MaterialHelper {
* Parses a string representing a material or a material tag * Parses a string representing a material or a material tag
* *
* @param materialString <p>The material string to parse</p> * @param materialString <p>The material string to parse</p>
* @param tagPrefix <p>The prefix differentiating a tag from a material</p>
* @param logger <p>The logger to use for logging parsing errors</p> * @param logger <p>The logger to use for logging parsing errors</p>
* @return <p>The materials defined by the material string, or an empty list if none were found</p> * @return <p>The materials defined by the material string, or an empty list if none were found</p>
*/ */
public static @NotNull Set<Material> loadMaterialString(@NotNull String materialString, @NotNull Logger logger) { public static @NotNull Set<Material> loadMaterialString(@NotNull String materialString, @NotNull String tagPrefix,
@NotNull Logger logger) {
Set<Material> parsedMaterials = new HashSet<>(); Set<Material> parsedMaterials = new HashSet<>();
// Try to parse a material tag first // Try to parse a material tag first
if (parseMaterialTag(parsedMaterials, materialString, logger)) { if (parseMaterialTag(parsedMaterials, materialString, tagPrefix, logger)) {
return parsedMaterials; return parsedMaterials;
} }
@ -73,12 +77,13 @@ public final class MaterialHelper {
* *
* @param targetSet <p>The set all parsed materials should be added to</p> * @param targetSet <p>The set all parsed materials should be added to</p>
* @param materialName <p>The material name that might be a material tag</p> * @param materialName <p>The material name that might be a material tag</p>
* @param tagPrefix <p>The prefix differentiating a tag from a material</p>
* @param logger <p>The logger to use for logging parsing errors</p> * @param logger <p>The logger to use for logging parsing errors</p>
* @return <p>True if a tag was found</p> * @return <p>True if a tag was found</p>
*/ */
private static boolean parseMaterialTag(@NotNull Set<Material> targetSet, @NotNull String materialName, private static boolean parseMaterialTag(@NotNull Set<Material> targetSet, @NotNull String materialName,
@NotNull Logger logger) { @NotNull String tagPrefix, @NotNull Logger logger) {
Pattern pattern = Pattern.compile("^\\+([a-zA-Z_]+)"); Pattern pattern = Pattern.compile("^" + Pattern.quote(tagPrefix) + "([a-zA-Z_]+)");
Matcher matcher = pattern.matcher(materialName); Matcher matcher = pattern.matcher(materialName);
if (matcher.find()) { if (matcher.find()) {
// The material is a material tag // The material is a material tag