Skip to content

Commit ce8350a

Browse files
Refactoring 'getMaterial()' method, adding '#' as an alias for 'tag='
1 parent 53268b0 commit ce8350a

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/main/java/de/redstoneworld/redutilities/material/MaterialHelper.java

+20-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static Set<Material> getMaterials(Set<String> inputSet) {
4444
* This method returns a list of materials based on the input string. Various formats
4545
* of material specifications are supported here:
4646
*
47-
* <li>Material-Tag via "tag=" (see <a href="https://jd.papermc.io/paper/1.21.1/org/bukkit/Tag.html">PaperMC Java-Doc</a> and
47+
* <li>Material-Tag starting with "#" or "tag=" (see <a href="https://jd.papermc.io/paper/1.21.1/org/bukkit/Tag.html">PaperMC Java-Doc</a> and
4848
* <a href="https://minecraft.wiki/w/Tag">Minecraft-Wiki</a> for the tag lists)</li>
4949
* <li>Regex via "r="</li>
5050
* <li>MATERIAL names with wildcards via "*"</li>
@@ -63,17 +63,30 @@ public static Set<Material> getMaterials(String input) {
6363
// Material-Tag Definition:
6464
// - https://jd.papermc.io/paper/1.21/org/bukkit/Tag.html
6565
// - https://minecraft.wiki/w/Tag
66-
if (input.startsWith("tag=")) {
67-
String nameSpace = NamespacedKey.MINECRAFT;
68-
String tagName = input.substring(4).toLowerCase();
66+
if ((input.startsWith("#")) || (input.startsWith("tag="))) {
67+
String nameSpace = "";
68+
String tagName = "";
69+
70+
if (input.startsWith("#")) {
71+
tagName = input.substring(1).toLowerCase();
72+
} else if (input.startsWith("tag=")) {
73+
tagName = input.substring(4).toLowerCase();
74+
}
75+
6976
String[] parts = tagName.split(":");
70-
if (parts.length == 2) {
77+
if (parts.length == 1) {
78+
nameSpace = NamespacedKey.MINECRAFT;
79+
tagName = parts[0].toLowerCase();
80+
} else if (parts.length == 2) {
7181
nameSpace = parts[0].toLowerCase();
7282
tagName = parts[1].toLowerCase();
7383
}
7484

85+
Tag<Material> tag;
86+
7587
// Blocks:
76-
Tag<Material> tag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, new NamespacedKey(nameSpace, tagName), Material.class);
88+
tag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, new NamespacedKey(nameSpace, tagName), Material.class);
89+
7790
// Items:
7891
if (tag == null) {
7992
tag = Bukkit.getTag(Tag.REGISTRY_ITEMS, new NamespacedKey(nameSpace, tagName), Material.class);
@@ -96,6 +109,7 @@ public static Set<Material> getMaterials(String input) {
96109
// Regex Definition:
97110
} else if (input.startsWith("r=") || input.contains("*")) {
98111
Pattern p = Pattern.compile(input.startsWith("r=") ? input.substring(2) : input.replace("*", "(.*)"));
112+
99113
for (Material material : Material.values()) {
100114
if (p.matcher(material.name()).matches()) {
101115
materials.add(material);

0 commit comments

Comments
 (0)