Search in sources :

Example 16 with ISpellPart

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ISpellPart(am2.api.spell.component.interfaces.ISpellPart) Item(net.minecraft.item.Item) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ItemStack (net.minecraft.item.ItemStack)8 KeyValuePair (am2.utility.KeyValuePair)7 ISpellPart (am2.api.spell.component.interfaces.ISpellPart)6 ArrayList (java.util.ArrayList)5 Block (net.minecraft.block.Block)4 Item (net.minecraft.item.Item)4 SpellRecipeItemsEvent (am2.api.events.SpellRecipeItemsEvent)3 AMDataWriter (am2.network.AMDataWriter)3 Summon (am2.spell.components.Summon)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 ISkillTreeEntry (am2.api.spell.component.interfaces.ISkillTreeEntry)2 ISpellShape (am2.api.spell.component.interfaces.ISpellShape)2 NBTTagList (net.minecraft.nbt.NBTTagList)2 AMVector3 (am2.api.math.AMVector3)1 IPowerNode (am2.api.power.IPowerNode)1 PowerTypes (am2.api.power.PowerTypes)1 ISpellComponent (am2.api.spell.component.interfaces.ISpellComponent)1 ISpellModifier (am2.api.spell.component.interfaces.ISpellModifier)1 Affinity (am2.api.spell.enums.Affinity)1 SpellModifiers (am2.api.spell.enums.SpellModifiers)1