13
0
geforkt von Mirrors/Paper

Make matchMaterial accept the minecraft: namespace

By: md_5 <git@md-5.net>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2018-08-29 19:26:58 +10:00
Ursprung 30d8b71004
Commit 6ea05f1541
2 geänderte Dateien mit 32 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -2781,9 +2781,9 @@ public enum Material implements Keyed {
/** /**
* Attempts to match the Material with the given name. * Attempts to match the Material with the given name.
* <p> * <p>
* This is a match lookup; names will be converted to uppercase, then * This is a match lookup; names will be stripped of the "minecraft:"
* stripped of special characters in an attempt to format it like the * namespace, converted to uppercase, then stripped of special characters in
* enum. * an attempt to format it like the enum.
* *
* @param name Name of the material to get * @param name Name of the material to get
* @return Material if found, or null * @return Material if found, or null
@ -2795,9 +2795,9 @@ public enum Material implements Keyed {
/** /**
* Attempts to match the Material with the given name. * Attempts to match the Material with the given name.
* <p> * <p>
* This is a match lookup; names will be converted to uppercase, then * This is a match lookup; names will be stripped of the "minecraft:"
* stripped of special characters in an attempt to format it like the * namespace, converted to uppercase, then stripped of special characters in
* enum. * an attempt to format it like the enum.
* *
* @param name Name of the material to get * @param name Name of the material to get
* @param legacyName whether this is a legacy name * @param legacyName whether this is a legacy name
@ -2806,7 +2806,12 @@ public enum Material implements Keyed {
public static Material matchMaterial(final String name, boolean legacyName) { public static Material matchMaterial(final String name, boolean legacyName) {
Validate.notNull(name, "Name cannot be null"); Validate.notNull(name, "Name cannot be null");
String filtered = name.toUpperCase(java.util.Locale.ENGLISH); String filtered = name;
if (filtered.startsWith(NamespacedKey.MINECRAFT + ":")) {
filtered = filtered.substring((NamespacedKey.MINECRAFT + ":").length());
}
filtered = filtered.toUpperCase(java.util.Locale.ENGLISH);
filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", ""); filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", "");
return getMaterial(filtered, legacyName); return getMaterial(filtered, legacyName);

Datei anzeigen

@ -43,6 +43,26 @@ public class MaterialTest {
} }
} }
@Test
public void matchMaterialByKey() {
for (Material material : Material.values()) {
if (material.isLegacy()) {
continue;
}
assertThat(Material.matchMaterial(material.getKey().toString()), is(material));
}
}
@Test
public void matchMaterialByWrongNamespace() {
for (Material material : Material.values()) {
if (material.isLegacy()) {
continue;
}
assertNull(Material.matchMaterial("bogus:" + material.getKey().getKey()));
}
}
@Test @Test
public void matchMaterialByLowerCaseAndSpaces() { public void matchMaterialByLowerCaseAndSpaces() {
for (Material material : Material.values()) { for (Material material : Material.values()) {