use of am2.api.spell.component.interfaces.ISpellPart in project ArsMagica2 by Mithion.
the class SpellRecipeManager method matchRecipe.
private ISpellPart matchRecipe(ArrayList<ItemStack> recipe) {
// make a safe working copy
HashMap<ArrayList<Object>, ISpellPart> safeCopy = new HashMap<ArrayList<Object>, ISpellPart>();
safeCopy.putAll(recipes);
// list of non-matching items to be removed at the next opportunity
ArrayList<ArrayList<Object>> toRemove = new ArrayList<ArrayList<Object>>();
// ==========================================================
for (ArrayList<Object> arr : safeCopy.keySet()) {
if (arr.size() != recipe.size())
toRemove.add(arr);
}
for (ArrayList<Object> arr : toRemove) safeCopy.remove(arr);
// ==========================================================
// spin through the list and check each item. If the recipe at the current index does not match,
// then the entire recipe is not a match. Remove all non-matching recipes from the possibility list.
// ==========================================================
int index = 0;
for (ItemStack recipeItem : recipe) {
toRemove.clear();
for (ArrayList<Object> arr : safeCopy.keySet()) {
Object o = arr.get(index);
boolean matches = false;
if (o instanceof ItemStack) {
matches = compareItemStacks(((ItemStack) o), recipeItem);
} else if (o instanceof Item) {
matches = ((Item) o) == recipeItem.getItem();
} else if (o instanceof Block) {
matches = ((Block) o) == Block.getBlockFromItem(recipeItem.getItem());
} else if (o instanceof String) {
if (((String) o).startsWith("P:")) {
// potion
String potionDefinition = ((String) o).substring(2);
matches |= matchPotion(recipeItem, potionDefinition);
} else {
ArrayList<ItemStack> oreDictItems = OreDictionary.getOres((String) o);
for (ItemStack stack : oreDictItems) {
matches |= OreDictionary.itemMatches(stack, recipeItem, false);
}
}
}
if (!matches) {
toRemove.add(arr);
}
}
index++;
for (ArrayList<Object> arr : toRemove) safeCopy.remove(arr);
}
if (safeCopy.size() > 1) {
StringBuilder sb = new StringBuilder();
sb.append("Ars Magica >> Duplicate recipe match on the following spell parts: ");
for (ISpellPart part : safeCopy.values()) {
sb.append(SkillManager.instance.getSkillName(part) + " ");
}
sb.append("- this should be corrected as soon as possible!");
LogHelper.warn(sb.toString());
}
if (safeCopy.size() > 0) {
ISpellPart part = safeCopy.values().iterator().next();
LogHelper.info("Matched Spell Component: %s", part.getClass().toString());
return part;
}
return null;
}
Aggregations