|
| 1 | +package de.redstoneworld.redutilities.material; |
| 2 | + |
| 3 | +import com.destroystokyo.paper.MaterialTags; |
| 4 | +import org.bukkit.Bukkit; |
| 5 | +import org.bukkit.Material; |
| 6 | +import org.bukkit.NamespacedKey; |
| 7 | +import org.bukkit.Tag; |
| 8 | + |
| 9 | +import java.lang.reflect.Field; |
| 10 | +import java.util.EnumSet; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.regex.Pattern; |
| 13 | + |
| 14 | +public class MaterialHelper { |
| 15 | + |
| 16 | + /** |
| 17 | + * This method returns a list of materials that can be defined with |
| 18 | + * a set of inputs in the form of a Material-Tag, Regex or the |
| 19 | + * normal Material-Name specification. |
| 20 | + * |
| 21 | + * Basic-Design by <a href="https://github.com/Phoenix616">Phoenix616</a> |
| 22 | + * |
| 23 | + * @param inputSet (Set of Strings) the input String |
| 24 | + * @return (Set of Materials) the resulted Materials |
| 25 | + */ |
| 26 | + public static Set<Material> getMaterials(Set<String> inputSet) { |
| 27 | + if ((inputSet == null) || (inputSet.isEmpty())) return null; |
| 28 | + |
| 29 | + Set<Material> materials = EnumSet.noneOf(Material.class); |
| 30 | + |
| 31 | + for (String input : inputSet.stream().toList()) { |
| 32 | + materials.addAll(getMaterials(input)); |
| 33 | + } |
| 34 | + |
| 35 | + return materials; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * This method returns a list of materials that can be defined with |
| 40 | + * a (single) input in the form of a Material-Tag, Regex or the |
| 41 | + * normal Material-Name specification. |
| 42 | + * |
| 43 | + * Basic-Design by <a href="https://github.com/Phoenix616">Phoenix616</a> |
| 44 | + * |
| 45 | + * @param input (String) the input String |
| 46 | + * @return (Set of Materials) the resulted Materials |
| 47 | + */ |
| 48 | + public static Set<Material> getMaterials(String input) { |
| 49 | + if ((input == null) || (input.isEmpty())) return null; |
| 50 | + |
| 51 | + Set<Material> materials = EnumSet.noneOf(Material.class); |
| 52 | + |
| 53 | + // Material-Tag Definition: |
| 54 | + // - https://jd.papermc.io/paper/1.21/org/bukkit/Tag.html |
| 55 | + // - https://minecraft.wiki/w/Tag |
| 56 | + if (input.startsWith("tag=")) { |
| 57 | + String nameSpace = NamespacedKey.MINECRAFT; |
| 58 | + String tagName = input.substring(4).toLowerCase(); |
| 59 | + String[] parts = tagName.split(":"); |
| 60 | + if (parts.length == 2) { |
| 61 | + nameSpace = parts[0].toLowerCase(); |
| 62 | + tagName = parts[1].toLowerCase(); |
| 63 | + } |
| 64 | + |
| 65 | + // Blocks: |
| 66 | + Tag<Material> tag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, new NamespacedKey(nameSpace, tagName), Material.class); |
| 67 | + // Items: |
| 68 | + if (tag == null) { |
| 69 | + tag = Bukkit.getTag(Tag.REGISTRY_ITEMS, new NamespacedKey(nameSpace, tagName), Material.class); |
| 70 | + } |
| 71 | + |
| 72 | + // https://jd.papermc.io/paper/1.21/com/destroystokyo/paper/MaterialTags.html |
| 73 | + if (tag == null) { |
| 74 | + try { |
| 75 | + Field field = MaterialTags.class.getField(tagName); |
| 76 | + tag = (Tag<Material>) field.get(null); |
| 77 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 78 | + throw new IllegalArgumentException(e.getMessage()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (tag != null) { |
| 83 | + materials.addAll(tag.getValues()); |
| 84 | + } |
| 85 | + |
| 86 | + // Regex Definition: |
| 87 | + } else if (input.startsWith("r=") || input.contains("*")) { |
| 88 | + Pattern p = Pattern.compile(input.startsWith("r=") ? input.substring(2) : input.replace("*", "(.*)")); |
| 89 | + for (Material material : Material.values()) { |
| 90 | + if (p.matcher(material.name()).matches()) { |
| 91 | + materials.add(material); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Material-Name Definition: |
| 96 | + } else { |
| 97 | + materials.add(Material.valueOf(input.toUpperCase())); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + return materials; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments