Update ItemParser to (hopefully) support more items.

This commit is contained in:
Andreas Troelsen 2013-06-10 00:22:21 +02:00
parent 7ef98d4538
commit 4bcb5c04ac

View File

@ -16,6 +16,9 @@ import com.garbagemule.MobArena.MobArena;
public class ItemParser public class ItemParser
{ {
private static final int WOOL_ID = Material.WOOL.getId();
private static final int DYE_ID = Material.INK_SACK.getId();
public static String parseString(ItemStack... stacks) { public static String parseString(ItemStack... stacks) {
String result = ""; String result = "";
@ -152,38 +155,43 @@ public class ItemParser
int amount = Integer.parseInt(item.substring(1)); int amount = Integer.parseInt(item.substring(1));
return new ItemStack(MobArena.ECONOMY_MONEY_ID, amount); return new ItemStack(MobArena.ECONOMY_MONEY_ID, amount);
} }
Material m = getMaterial(item); int id = getTypeId(item);
return m == null ? null : new ItemStack(m,1); return new ItemStack(id);
} }
private static ItemStack withAmount(String item, String amount) { private static ItemStack withAmount(String item, String amount) {
Material m = getMaterial(item); int id = getTypeId(item);
int a = getAmount(amount); int a = getAmount(amount);
return m == null ? null : new ItemStack(m,a); return new ItemStack(id,a);
} }
private static ItemStack withDataAndAmount(String item, String data, String amount) { private static ItemStack withDataAndAmount(String item, String data, String amount) {
Material m = getMaterial(item); int id = getTypeId(item);
short d = getData(data, m); short d = getData(data, id);
int a = getAmount(amount); int a = getAmount(amount);
return new ItemStack(id,a,d);
if (m == null) {
return null;
}
if (m.getId() == 35) {
d = (byte) (15-d);
}
return new ItemStack(m,a,d);
} }
private static Material getMaterial(String item) { private static int getTypeId(String item) {
if (item.matches("[0-9]*")) { if (item.matches("(-)?[0-9]*")) {
return Material.getMaterial(Integer.parseInt(item)); return Integer.parseInt(item);
} }
Material m = Enums.getEnumFromString(Material.class, item);
return Material.getMaterial(item.toUpperCase()); return (m != null ? m.getId() : 0);
}
private static short getData(String data, int id) {
// Wool and ink are special
if (id == WOOL_ID) {
DyeColor dye = Enums.getEnumFromString(DyeColor.class, data);
if (dye == null) dye = DyeColor.getByWoolData(Byte.parseByte(data));
return dye.getWoolData();
} else if (id == DYE_ID) {
DyeColor dye = Enums.getEnumFromString(DyeColor.class, data);
if (dye == null) dye = DyeColor.getByDyeData(Byte.parseByte(data));
return dye.getDyeData();
}
return (data.matches("(-)?[0-9]+") ? Short.parseShort(data) : 0);
} }
private static int getAmount(String amount) { private static int getAmount(String amount) {
@ -194,22 +202,6 @@ public class ItemParser
return 1; return 1;
} }
private static short getData(String data, Material m) {
// If ink sack or wool, parse wisely.
if (m == Material.INK_SACK || m == Material.WOOL) {
// Ink sacks must be offset.
byte offset = (m == Material.INK_SACK ? (byte) 15 : (byte) 0);
DyeColor dye = (data.matches("[0-9]+")) ?
DyeColor.getByData((byte) Math.abs(offset - Integer.parseInt(data))) :
DyeColor.valueOf(data.toUpperCase());
return (short) Math.abs(offset - dye.getData());
}
return Short.parseShort(data);
}
private static void addEnchantments(ItemStack stack, String list) { private static void addEnchantments(ItemStack stack, String list) {
String[] parts = list.split(";"); String[] parts = list.split(";");