summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDinnerbone <dinnerbone@dinnerbone.com>2011-02-01 12:55:29 +0000
committerDinnerbone <dinnerbone@dinnerbone.com>2011-02-01 12:55:29 +0000
commit67fbbced23097e3661dad87c2aa40e155a110016 (patch)
treea352fa8ea466b1e92370bca71baabe76873b0c66 /src
parent1dba6e8874f5db0d14c14825f80279cb16f80fbd (diff)
downloadbukkit-67fbbced23097e3661dad87c2aa40e155a110016.tar
bukkit-67fbbced23097e3661dad87c2aa40e155a110016.tar.gz
bukkit-67fbbced23097e3661dad87c2aa40e155a110016.tar.lz
bukkit-67fbbced23097e3661dad87c2aa40e155a110016.tar.xz
bukkit-67fbbced23097e3661dad87c2aa40e155a110016.zip
Added Material.matchMaterial(String)
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/bukkit/Material.java41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/main/java/org/bukkit/Material.java b/src/main/java/org/bukkit/Material.java
index efb1cd54..05b0973b 100644
--- a/src/main/java/org/bukkit/Material.java
+++ b/src/main/java/org/bukkit/Material.java
@@ -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()) {