Skip to content

Commit

Permalink
Improve EnumParser to better take in the closeness of names into account
Browse files Browse the repository at this point in the history
This is necessary to support new 1.20.5 items as well as better support some older ones.

Also use the properly formatted name when trying to valueOf get the Enum directly.
  • Loading branch information
Phoenix616 committed Apr 30, 2024
1 parent 60b10a8 commit b6b7a6b
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,9 @@ public class MaterialUtil {
private static final Map<String, String> ABBREVIATIONS = StringUtil.map(
"Egg", "Eg",
"Spawn", "Spaw",
"Ender", "End",
"Tropical", "Tropic",
"Terracotta", "Terracot",
"Stained", "Stain",
"Sandstone", "Sandston",
"Sandston", "Sandsto",
"Sandsto", "Sandst",
"Block", "Bloc",
"Brewing", "Brew",
"Dolphin", "Dolph",
"Chicken", "Chick",
"Pottery", "Pot",
"Heartbreak", "Heartbr",
"Sherd", "Sher",
"Template", "Templ"
"Sherd", "Sher"
);

private static final Map<String, String> UNIDIRECTIONAL_ABBREVIATIONS = StringUtil.map(
Expand Down Expand Up @@ -463,18 +451,18 @@ public static ItemMeta getMetadata(String itemName) {

private static class EnumParser<E extends Enum<E>> {
private E parse(String name, E[] values) {
String formatted = name.replaceAll("(?<!^)(?>\\s?)([A-Z1-9])", "_$1").toUpperCase(Locale.ROOT).replace(' ', '_');
try {
return E.valueOf(values[0].getDeclaringClass(), name.toUpperCase(Locale.ROOT));
return E.valueOf(values[0].getDeclaringClass(), formatted);
} catch (IllegalArgumentException exception) {
E currentEnum = null;
String[] typeParts = name.replaceAll("(?<!^)(?>\\s?)([A-Z1-9])", "_$1").toUpperCase(Locale.ROOT).split("[ _]");
List<E> possibleEnums = new ArrayList<>();
String[] typeParts = formatted.split("_");
int length = Short.MAX_VALUE;
name = name.toUpperCase(Locale.ROOT);
for (E e : values) {
String enumName = e.name();
if (enumName.length() < length && enumName.startsWith(name)) {
length = (short) enumName.length();
currentEnum = e;
if (enumName.length() < length && enumName.startsWith(formatted)) {
length = enumName.length();
possibleEnums.add(e);
} else if (typeParts.length > 1) {
String[] nameParts = enumName.split("_");
if (typeParts.length == nameParts.length) {
Expand All @@ -486,13 +474,28 @@ private E parse(String name, E[] values) {
}
}
if (matched) {
currentEnum = e;
break;
possibleEnums.add(e);
}
}
}
}
return currentEnum;

if (possibleEnums.size() == 1) {
return possibleEnums.get(0);
} else if (possibleEnums.size() > 1) {
int formattedLength = formatted.length();
int closestDeviation = Short.MAX_VALUE;
E closestEnum = null;
for (E possibleEnum : possibleEnums) {
int deviation = possibleEnum.name().length() - formattedLength;
if (deviation < closestDeviation) {
closestDeviation = deviation;
closestEnum = possibleEnum;
}
}
return closestEnum;
}
return null;
}
}
}
Expand Down

0 comments on commit b6b7a6b

Please sign in to comment.