mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-01 19:06:41 +00:00
Factored lookup code from BlockType and ItemType into a method in StringUtil.
This commit is contained in:
@ -19,6 +19,7 @@
|
||||
package com.sk89q.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* String utilities.
|
||||
@ -270,4 +271,39 @@ public class StringUtil {
|
||||
// actually has the most recent cost counts
|
||||
return p[n];
|
||||
}
|
||||
|
||||
public static <T extends Enum<?>> T lookup(Map<String, T> lookup, String name, boolean fuzzy) {
|
||||
String testName = name.replace("[ _]", "").toLowerCase();
|
||||
|
||||
T type = lookup.get(testName);
|
||||
if (type != null) {
|
||||
return type;
|
||||
}
|
||||
|
||||
if (!fuzzy) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int minDist = Integer.MAX_VALUE;
|
||||
|
||||
for (Map.Entry<String, T> entry : lookup.entrySet()) {
|
||||
final String key = entry.getKey();
|
||||
if (key.charAt(0) != testName.charAt(0)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int dist = getLevenshteinDistance(key, testName);
|
||||
|
||||
if (dist >= minDist) {
|
||||
minDist = dist;
|
||||
type = entry.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
if (minDist > 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user