mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-30 16:19:03 +01:00
Added Material.matchMaterial(String)
By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
parent
c76bf04e6d
commit
d45f410092
1 changed files with 40 additions and 1 deletions
|
@ -308,14 +308,53 @@ public enum Material {
|
|||
public boolean isBlock() {
|
||||
return id < 256;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Attempts to get the Material with the given ID
|
||||
*
|
||||
* @param id ID of the material to get
|
||||
* @return Material if found, or null
|
||||
*/
|
||||
public static Material getMaterial(final int id) {
|
||||
return lookupId.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to get the Material with the given name.
|
||||
* This is a normal lookup, names must be the precise name they are given
|
||||
* in the enum.
|
||||
*
|
||||
* @param name Name of the material to get
|
||||
* @return Material if found, or null
|
||||
*/
|
||||
public static Material getMaterial(final String name) {
|
||||
return lookupName.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to match the Material with the given name.
|
||||
* This is a match lookup; names will be converted to uppercase, then stripped
|
||||
* of special characters in an attempt to format it like the enum
|
||||
*
|
||||
* @param name Name of the material to get
|
||||
* @return Material if found, or null
|
||||
*/
|
||||
public static Material matchMaterial(final String name) {
|
||||
Material result = null;
|
||||
|
||||
try {
|
||||
result = getMaterial(Integer.parseInt(name));
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
|
||||
if (result == null) {
|
||||
String filtered = name.toUpperCase();
|
||||
filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", "");
|
||||
result = lookupName.get(filtered);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static {
|
||||
for (Material material : values()) {
|
||||
|
|
Loading…
Reference in a new issue