mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-01 17:01:56 +01:00
#812: Add Registry#match(String)
By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
parent
f72dbc96cd
commit
ab2503b967
1 changed files with 19 additions and 0 deletions
|
@ -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<T extends Keyed> extends Iterable<T> {
|
|||
@Nullable
|
||||
T get(@NotNull NamespacedKey key);
|
||||
|
||||
/**
|
||||
* Attempts to match the registered object with the given key.
|
||||
* <p>
|
||||
* 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<T extends Enum<T> & Keyed> implements Registry<T> {
|
||||
|
||||
private final Map<NamespacedKey, T> map;
|
||||
|
|
Loading…
Reference in a new issue