From 8c1983adff42ebfcc2fffdab017b38dd4e576994 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Fri, 28 Jun 2024 17:59:10 +0200 Subject: [PATCH] Adding methods to get Materials by String specification --- pom.xml | 2 +- .../redutilities/material/MaterialHelper.java | 104 ++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/redstoneworld/redutilities/material/MaterialHelper.java diff --git a/pom.xml b/pom.xml index 64a21a7..c805039 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.redstoneworld.redutilities redutilities - 0.0.12-Snapshot + 0.0.13-Snapshot UTF-8 diff --git a/src/main/java/de/redstoneworld/redutilities/material/MaterialHelper.java b/src/main/java/de/redstoneworld/redutilities/material/MaterialHelper.java new file mode 100644 index 0000000..2c16ca8 --- /dev/null +++ b/src/main/java/de/redstoneworld/redutilities/material/MaterialHelper.java @@ -0,0 +1,104 @@ +package de.redstoneworld.redutilities.material; + +import com.destroystokyo.paper.MaterialTags; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Tag; + +import java.lang.reflect.Field; +import java.util.EnumSet; +import java.util.Set; +import java.util.regex.Pattern; + +public class MaterialHelper { + + /** + * This method returns a list of materials that can be defined with + * a set of inputs in the form of a Material-Tag, Regex or the + * normal Material-Name specification. + * + * Basic-Design by Phoenix616 + * + * @param inputSet (Set of Strings) the input String + * @return (Set of Materials) the resulted Materials + */ + public static Set getMaterials(Set inputSet) { + if ((inputSet == null) || (inputSet.isEmpty())) return null; + + Set materials = EnumSet.noneOf(Material.class); + + for (String input : inputSet.stream().toList()) { + materials.addAll(getMaterials(input)); + } + + return materials; + } + + /** + * This method returns a list of materials that can be defined with + * a (single) input in the form of a Material-Tag, Regex or the + * normal Material-Name specification. + * + * Basic-Design by Phoenix616 + * + * @param input (String) the input String + * @return (Set of Materials) the resulted Materials + */ + public static Set getMaterials(String input) { + if ((input == null) || (input.isEmpty())) return null; + + Set materials = EnumSet.noneOf(Material.class); + + // Material-Tag Definition: + // - https://jd.papermc.io/paper/1.21/org/bukkit/Tag.html + // - https://minecraft.wiki/w/Tag + if (input.startsWith("tag=")) { + String nameSpace = NamespacedKey.MINECRAFT; + String tagName = input.substring(4).toLowerCase(); + String[] parts = tagName.split(":"); + if (parts.length == 2) { + nameSpace = parts[0].toLowerCase(); + tagName = parts[1].toLowerCase(); + } + + // Blocks: + Tag tag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, new NamespacedKey(nameSpace, tagName), Material.class); + // Items: + if (tag == null) { + tag = Bukkit.getTag(Tag.REGISTRY_ITEMS, new NamespacedKey(nameSpace, tagName), Material.class); + } + + // https://jd.papermc.io/paper/1.21/com/destroystokyo/paper/MaterialTags.html + if (tag == null) { + try { + Field field = MaterialTags.class.getField(tagName); + tag = (Tag) field.get(null); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new IllegalArgumentException(e.getMessage()); + } + } + + if (tag != null) { + materials.addAll(tag.getValues()); + } + + // Regex Definition: + } else if (input.startsWith("r=") || input.contains("*")) { + Pattern p = Pattern.compile(input.startsWith("r=") ? input.substring(2) : input.replace("*", "(.*)")); + for (Material material : Material.values()) { + if (p.matcher(material.name()).matches()) { + materials.add(material); + } + } + + // Material-Name Definition: + } else { + materials.add(Material.valueOf(input.toUpperCase())); + + } + + return materials; + } + +}