From 67fbbced23097e3661dad87c2aa40e155a110016 Mon Sep 17 00:00:00 2001 From: Dinnerbone Date: Tue, 1 Feb 2011 12:55:29 +0000 Subject: Added Material.matchMaterial(String) --- src/main/java/org/bukkit/Material.java | 41 +++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src') 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()) { -- cgit v1.2.3