From ed47df93e9efe2ef50e487d76b328bf3c05ef285 Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 9 May 2011 00:22:45 +0000 Subject: [trunk] Rewritten ItemDb.get(), removed the getUnsafe function. git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1383 e251c2fe-e539-e718-e476-b85c1f46cddb --- Essentials/src/com/earth2me/essentials/ItemDb.java | 64 ++++++++++++---------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/ItemDb.java b/Essentials/src/com/earth2me/essentials/ItemDb.java index 3d845f005..6eb35c50b 100644 --- a/Essentials/src/com/earth2me/essentials/ItemDb.java +++ b/Essentials/src/com/earth2me/essentials/ItemDb.java @@ -31,7 +31,10 @@ public class ItemDb FileWriter tx = new FileWriter(file); try { - for (int i = 0; (i = res.read()) > 0;) tx.write(i); + for (int i = 0; (i = res.read()) > 0;) + { + tx.write(i); + } } finally { @@ -58,14 +61,18 @@ public class ItemDb { String line = rx.readLine().trim().toLowerCase(); if (line.startsWith("#")) + { continue; - + } + String[] parts = line.split("[^a-z0-9]"); if (parts.length < 2) + { continue; - + } + int numeric = Integer.parseInt(parts[1]); - + durabilities.put(parts[0].toLowerCase(), parts.length > 2 && !parts[2].equals("0") ? Short.parseShort(parts[2]) : 0); items.put(parts[0].toLowerCase(), numeric); } @@ -80,8 +87,9 @@ public class ItemDb rx.close(); } } - - public static ItemStack get(String id, int quantity) throws Exception { + + public static ItemStack get(String id, int quantity) throws Exception + { ItemStack retval = get(id.toLowerCase()); retval.setAmount(quantity); return retval; @@ -90,37 +98,37 @@ public class ItemDb public static ItemStack get(String id) throws Exception { int itemid; - short metaData =0; - if(id.matches("^\\d+:\\d+$")) + short metaData = 0; + if (id.matches("^\\d+:\\d+$")) { - itemid = getUnsafe(id.split(":")[0]); - metaData = (short)getUnsafe(id.split(":")[1]); + itemid = Integer.parseInt(id.split(":")[0]); + metaData = Short.parseShort(id.split(":")[1]); + } + else if (id.matches("^\\d+$")) + { + itemid = Integer.parseInt(id); + } + else if (items.containsKey(id.toLowerCase())) + { + itemid = items.get(id.toLowerCase()); + if (durabilities.containsKey(id.toLowerCase())) + { + metaData = durabilities.get(id.toLowerCase()); + } } else { - itemid = getUnsafe(id); + throw new Exception("Unknown item name: " + id); } - + Material mat = Material.getMaterial(itemid); - if (mat == null) { - throw new Exception("Unknown item id: "+itemid); + if (mat == null) + { + throw new Exception("Unknown item id: " + itemid); } ItemStack retval = new ItemStack(mat); retval.setAmount(Essentials.getStatic().getSettings().getDefaultStackSize()); - retval.setDurability(metaData !=0 ? metaData :(durabilities.containsKey(id.toLowerCase()) ? durabilities.get(id.toLowerCase()) : 0)); + retval.setDurability(metaData); return retval; } - - private static int getUnsafe(String id) throws Exception - { - try - { - return Integer.parseInt(id); - } - catch (NumberFormatException ex) - { - if (items.containsKey(id.toLowerCase())) return items.get(id.toLowerCase()); - throw new Exception("Unknown item name: " + id); - } - } } -- cgit v1.2.3