Adds a helper class for parsing materials and tags
All checks were successful
EpicKnarvik97/KnarLib/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2023-07-08 16:49:16 +02:00
parent 0bd029db20
commit 914485c67b

View File

@ -0,0 +1,98 @@
package net.knarcraft.knarlib.util;
import net.knarcraft.knarlib.formatting.StringFormatter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A helper class for dealing with and parsing materials
*/
@SuppressWarnings("unused")
public final class MaterialHelper {
private MaterialHelper() {
}
/**
* Loads the materials specified in the given list
*
* @param materials <p>The list of material strings to load</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) {
Set<Material> parsedMaterials = new HashSet<>();
for (Object value : materials) {
if (!(value instanceof String string)) {
continue;
}
parsedMaterials.addAll(loadMaterialString(string, logger));
}
return parsedMaterials;
}
/**
* Parses a string representing a material or a material tag
*
* @param materialString <p>The material string to parse</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>
*/
public static @NotNull Set<Material> loadMaterialString(@NotNull String materialString, @NotNull Logger logger) {
Set<Material> parsedMaterials = new HashSet<>();
// Try to parse a material tag first
if (parseMaterialTag(parsedMaterials, materialString, logger)) {
return parsedMaterials;
}
// Try to parse a material name
Material matched = Material.matchMaterial(materialString);
if (matched != null) {
parsedMaterials.add(matched);
} else {
logger.log(Level.WARNING, StringFormatter.replacePlaceholder("Unable to parse material: {material}",
"{material}", materialString));
}
return parsedMaterials;
}
/**
* Tries to parse the material tag in the specified material name
*
* @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 logger <p>The logger to use for logging parsing errors</p>
* @return <p>True if a tag was found</p>
*/
private static boolean parseMaterialTag(@NotNull Set<Material> targetSet, @NotNull String materialName,
@NotNull Logger logger) {
Pattern pattern = Pattern.compile("^\\+([a-zA-Z_]+)");
Matcher matcher = pattern.matcher(materialName);
if (matcher.find()) {
// The material is a material tag
Tag<Material> tag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, NamespacedKey.minecraft(
matcher.group(1).toLowerCase()), Material.class);
if (tag != null) {
targetSet.addAll(tag.getValues());
} else {
logger.log(Level.WARNING, StringFormatter.replacePlaceholder(
"Unable to parse material: {material}", "{material}", materialName));
}
return true;
}
return false;
}
}