From ab2503b9679528ce3cf2a27200ce659a484e5a8d Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Mon, 13 Feb 2023 21:31:07 +1100 Subject: [PATCH] #812: Add Registry#match(String) By: Parker Hawke --- .../src/main/java/org/bukkit/Registry.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/paper-api/src/main/java/org/bukkit/Registry.java b/paper-api/src/main/java/org/bukkit/Registry.java index a77320257b..ea80520c19 100644 --- a/paper-api/src/main/java/org/bukkit/Registry.java +++ b/paper-api/src/main/java/org/bukkit/Registry.java @@ -1,5 +1,6 @@ package org.bukkit; +import com.google.common.base.Preconditions; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableMap; import java.util.Arrays; @@ -220,6 +221,24 @@ public interface Registry extends Iterable { @Nullable T get(@NotNull NamespacedKey key); + /** + * Attempts to match the registered object with the given key. + *

+ * This will attempt to find a reasonable match based on the provided input + * and may do so through unspecified means. + * + * @param input non-null input + * @return registered object or null if does not exist + */ + @Nullable + default T match(@NotNull String input) { + Preconditions.checkArgument(input != null, "input must not be null"); + + String filtered = input.toLowerCase().replaceAll("\\s+", "_").replaceAll("\\W", ""); + NamespacedKey namespacedKey = NamespacedKey.fromString(filtered); + return (namespacedKey != null) ? get(namespacedKey) : null; + } + static final class SimpleRegistry & Keyed> implements Registry { private final Map map;