Skip to content

Commit e1ef647

Browse files
committed
impl Registry#match by ourselves
1 parent fd72ffd commit e1ef647

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
package top.mrxiaom.pluginbase.utils;
22

33
import org.bukkit.Keyed;
4+
import org.bukkit.NamespacedKey;
45
import org.bukkit.Registry;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
58

69
public class RegistryUtils {
710
@SuppressWarnings("unchecked")
811
public static <T> T fromType(Class<T> type, String s) {
912
Registry<?> registry = RegistryConverter.fromType(type);
1013
if (registry != null) {
11-
Keyed matched = registry.match(s);
14+
Keyed matched = match(registry, s);
1215
if (/*matched != null && */type.isInstance(matched)) {
1316
return (T) matched;
1417
}
15-
Keyed newerMatched = registry.match(s.replace('_', '.'));
18+
Keyed newerMatched = match(registry, s.replace('_', '.'));
1619
if (/*newerMatched != null && */type.isInstance(matched)) {
1720
return (T) newerMatched;
1821
}
1922
}
2023
return null;
2124
}
25+
26+
@Nullable
27+
public static <T extends Keyed> T match(@NotNull Registry<T> registry, @NotNull String input) {
28+
String filtered = input.toLowerCase().replaceAll("\\s+", "_");
29+
NamespacedKey namespacedKey = NamespacedKey.fromString(filtered);
30+
return (namespacedKey != null) ? registry.get(namespacedKey) : null;
31+
}
2232
}

0 commit comments

Comments
 (0)