String itemString[] = string.split("\\|");
String stringArray[] = itemString[0].trim().split(":");
if(stringArray.length <= 0 || stringArray[0].isEmpty())
return new Item(0);
stringArray[0] = stringArray[0].trim();
String alias = RecipeManager.plugin.getAliases().get(stringArray[0]);
if(alias != null)
{
if(stringArray.length > 2 && printRecipeErrors)
recipeError("'" + stringArray[0] + "' is an alias with data and amount.", "You can only set amount e.g.: alias:amount.");
return processItem(string.replace(stringArray[0], alias), defaultData, allowData, allowAmount, allowEnchantments, printRecipeErrors);
}
Material mat = Material.matchMaterial(stringArray[0]);
if(mat == null)
{
if(printRecipeErrors)
recipeError("Item '" + stringArray[0] + "' does not exist!", "Name could be different, look in readme.txt for links");
return null;
}
int type = mat.getId();
if(type <= 0)
return new Item(0);
int data = defaultData;
if(stringArray.length > 1)
{
if(allowData)
{
try
{
stringArray[1] = stringArray[1].trim();
if(stringArray[1].charAt(0) != '*')
data = Math.max(Integer.valueOf(stringArray[1]), data);
}
catch(Exception e)
{
if(printRecipeErrors)
recipeError("Item '" + mat + " has data value that is not a number: '" + stringArray[1] + "', defaulting to " + defaultData);
}
}
else if(printRecipeErrors)
recipeError("Item '" + mat + "' can't have data value defined in this recipe's slot, data value ignored.");
}
int amount = 1;
if(stringArray.length > 2)
{
if(allowAmount)
{
try
{
amount = Math.max(Integer.valueOf(stringArray[2].trim()), 1);
}
catch(Exception e)
{
if(printRecipeErrors)
recipeError("Item '" + mat + "' has amount value that is not a number: " + stringArray[2] + ", defaulting to 1");
}
}
else if(printRecipeErrors)
recipeError("Item '" + mat + "' can't have amount defined in this recipe's slot, amount ignored.");
}
Item item = new Item(type, amount, (short)data);
if(itemString.length > 1)
{
if(allowEnchantments)
{
if(item.getAmount() > 1)
{
if(printRecipeErrors)
recipeError("Item '" + mat + "' has enchantments and more than 1 amount, it can't have both, amount set to 1.");
item.setAmount(1);
}
String[] enchants = itemString[1].split(",");
String[] enchData;
Enchantment ench;
int level;
for(String enchant : enchants)
{
enchant = enchant.trim();
enchData = enchant.split(":");
if(enchData.length != 2)
{
if(printRecipeErrors)
recipeError("Enchantments have to be 'ENCHANTMENT:LEVEL' format.", "Look in readme.txt for enchantment list link.");
continue;
}
ench = Enchantment.getByName(enchData[0]);
if(ench == null)
{
try
{
ench = Enchantment.getById(Integer.valueOf(enchData[0]));
}
catch(Exception e)
{
ench = null;
}
if(ench == null)
{
if(printRecipeErrors)
recipeError("Enchantment '" + enchData[0] + "' does not exist!", "Name or ID could be different, look in readme.txt for enchantments list links.");
continue;
}
}
if(enchData[1].equals("MAX"))
level = ench.getMaxLevel();
else
{
try
{
level = Integer.valueOf(enchData[1]);
}
catch(Exception e)
{
if(printRecipeErrors)
recipeError("Invalid enchantment level: '" + enchData[1] + "' must be a valid number, positive, zero or negative.");
continue;
}
}
item.addEnchantment(ench, level);
}
}
else if(printRecipeErrors)
recipeError("Item '" + mat + "' can't use enchantments in this recipe slot!");
}