Fix issues with spaces in shortened item names

This commit is contained in:
Phoenix616 2023-08-27 16:29:06 +01:00
parent d426492561
commit 17bd2a1fcb
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
2 changed files with 11 additions and 2 deletions

View File

@ -182,7 +182,7 @@ public class MaterialUtil {
replacedName = replacedName.replaceAll(entry.getValue() + "(_|$)?", entry.getKey() + "$1");
}
String formatted = name.replaceAll("(?<!^)([A-Z1-9])", "_$1").replace(' ', '_').toUpperCase(Locale.ROOT);
String formatted = name.replaceAll("(?<!^)(?>\\s?)([A-Z1-9])", "_$1").replace(' ', '_').toUpperCase(Locale.ROOT);
Material material = MATERIAL_CACHE.get(formatted);
if (material != null) {
@ -467,7 +467,7 @@ public class MaterialUtil {
return E.valueOf(values[0].getDeclaringClass(), name.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException exception) {
E currentEnum = null;
String[] typeParts = name.replaceAll("(?<!^)([A-Z1-9])", "_$1").toUpperCase(Locale.ROOT).split("[ _]");
String[] typeParts = name.replaceAll("(?<!^)(?>\\s?)([A-Z1-9])", "_$1").toUpperCase(Locale.ROOT).split("[ _]");
int length = Short.MAX_VALUE;
name = name.toUpperCase(Locale.ROOT);
for (E e : values) {

View File

@ -8,6 +8,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
@ -49,4 +50,12 @@ public class MaterialTest {
assertSame(shortenedName + " did not produce " + material, material, MaterialUtil.getMaterial(shortenedName));
}
}
@Test
public void testCodesWithAndWithoutSpace() {
assertNotNull(MaterialUtil.getMaterial("DiamonPicka"));
assertNotNull(MaterialUtil.getMaterial("Diamon Picka"));
assertNotNull(MaterialUtil.getMaterial("ExpBottle"));
assertNotNull(MaterialUtil.getMaterial("Exp Bottle"));
}
}