Remove spaces from shortened strings for better readability

This is necessary due to the extended length of some material names in 1.13 as they directly include the variation into the material where previously only the data value was used. (E.g. BLACK_STAINED_GLASS_PANE) By removing the spaces and using uppercase characters to separate parts the string stays both readable to the user and parseable by the plugin. It should also be backwards compatible with the old format. (At least with 1.13 material names)
This commit is contained in:
Phoenix616 2018-09-10 00:33:27 +01:00
parent e1998bcd7c
commit adf5340932

View File

@ -86,7 +86,7 @@ public class MaterialUtil {
* @return Material found
*/
public static Material getMaterial(String name) {
String formatted = name.toUpperCase();
String formatted = name.replaceAll("([a-z])([A-Z1-9])", "$1_$2").replace(' ', '_').toUpperCase();
Material material = MATERIAL_CACHE.get(formatted);
if (material != null) {
@ -211,8 +211,13 @@ public class MaterialUtil {
if (width <= maxWidth) {
return itemName;
}
int exceeding = width - maxWidth;
String[] itemParts = itemName.split(" ");
itemName = String.join("", itemParts);
width = getMinecraftStringWidth(itemName);
if (width <= maxWidth) {
return itemName;
}
int exceeding = width - maxWidth;
int shortestIndex = 0;
int longestIndex = 0;
for (int i = 0; i < itemParts.length; i++) {
@ -259,7 +264,7 @@ public class MaterialUtil {
exceeding -= endWidth;
}
}
return String.join(" ", itemParts);
return String.join("", itemParts);
}
/**
@ -342,13 +347,11 @@ public class MaterialUtil {
private static class EnumParser<E extends Enum<E>> {
private E parse(String name, E[] values) {
name = name.toUpperCase();
try {
return E.valueOf(values[0].getDeclaringClass(), name);
return E.valueOf(values[0].getDeclaringClass(), name.toUpperCase());
} catch (IllegalArgumentException exception) {
E currentEnum = null;
String[] typeParts = name.split("[ _]");
String[] typeParts = name.replaceAll("([a-z])([A-Z1-9])", "$1_$2").toUpperCase().split("[ _]");
int length = Short.MAX_VALUE;
for (E e : values) {
String enumName = e.name();