Search in sources :

Example 11 with ISpellPart

use of am2.api.spell.component.interfaces.ISpellPart in project ArsMagica2 by Mithion.

the class TileEntityCraftingAltar method setCrafting.

private void setCrafting(boolean crafting) {
    this.isCrafting = crafting;
    if (!worldObj.isRemote) {
        AMDataWriter writer = new AMDataWriter();
        writer.add(xCoord);
        writer.add(yCoord);
        writer.add(zCoord);
        writer.add(CRAFTING_CHANGED);
        writer.add(crafting);
        AMNetHandler.INSTANCE.sendPacketToAllClientsNear(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 32, AMPacketIDs.CRAFTING_ALTAR_DATA, writer.generate());
    }
    if (crafting) {
        allAddedItems.clear();
        currentAddedItems.clear();
        spellDef.clear();
        for (ArrayList<KeyValuePair<ISpellPart, byte[]>> groups : shapeGroups) groups.clear();
        // find otherworld auras
        IPowerNode[] nodes = PowerNodeRegistry.For(worldObj).getAllNearbyNodes(worldObj, new AMVector3(this), PowerTypes.DARK);
        for (IPowerNode node : nodes) {
            if (node instanceof TileEntityOtherworldAura) {
                ((TileEntityOtherworldAura) node).setActive(true, this);
                break;
            }
        }
    }
}
Also used : AMVector3(am2.api.math.AMVector3) KeyValuePair(am2.utility.KeyValuePair) AMDataWriter(am2.network.AMDataWriter) IPowerNode(am2.api.power.IPowerNode)

Example 12 with ISpellPart

use of am2.api.spell.component.interfaces.ISpellPart in project ArsMagica2 by Mithion.

the class TileEntityInscriptionTable method HandleUpdatePacket.

public void HandleUpdatePacket(byte[] data) {
    if (this.worldObj == null)
        return;
    AMDataReader rdr = new AMDataReader(data);
    switch(rdr.ID) {
        case FULL_UPDATE:
            if (!rdr.getBoolean()) {
                Entity e = this.worldObj.getEntityByID(rdr.getInt());
                if (e instanceof EntityPlayer) {
                    EntityPlayer player = (EntityPlayer) e;
                    this.setInUse(player);
                } else {
                    this.setInUse(null);
                }
            } else {
                this.setInUse(null);
            }
            currentRecipe.clear();
            int partLength = rdr.getInt();
            for (int i = 0; i < partLength; ++i) {
                ISkillTreeEntry part = SkillManager.instance.getSkill(rdr.getInt());
                if (part instanceof ISpellPart)
                    this.currentRecipe.add((ISpellPart) part);
            }
            this.shapeGroups.clear();
            int numGroups = rdr.getInt();
            for (int i = 0; i < numGroups; ++i) {
                ArrayList<ISpellPart> group = new ArrayList<ISpellPart>();
                int[] partData = rdr.getIntArray();
                for (int n : partData) {
                    ISkillTreeEntry part = SkillManager.instance.getSkill(n);
                    if (part instanceof ISpellPart)
                        group.add((ISpellPart) part);
                }
                this.shapeGroups.add(group);
            }
            countModifiers();
            this.currentSpellName = rdr.getString();
            this.currentSpellIsReadOnly = rdr.getBoolean();
            break;
        case MAKE_SPELL:
            int entityID = rdr.getInt();
            EntityPlayer player = (EntityPlayer) worldObj.getEntityByID(entityID);
            if (player != null) {
                createSpellForPlayer(player);
            }
            break;
        case RESET_NAME:
            entityID = rdr.getInt();
            player = (EntityPlayer) worldObj.getEntityByID(entityID);
            if (player != null) {
                ((ContainerInscriptionTable) player.openContainer).resetSpellNameAndIcon();
            }
            break;
    }
}
Also used : S35PacketUpdateTileEntity(net.minecraft.network.play.server.S35PacketUpdateTileEntity) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) ContainerInscriptionTable(am2.containers.ContainerInscriptionTable) EntityPlayer(net.minecraft.entity.player.EntityPlayer) AMDataReader(am2.network.AMDataReader)

Example 13 with ISpellPart

use of am2.api.spell.component.interfaces.ISpellPart in project ArsMagica2 by Mithion.

the class TileEntityInscriptionTable method writeRecipeAndDataToBook.

public ItemStack writeRecipeAndDataToBook(ItemStack bookstack, EntityPlayer player, String title) {
    if (bookstack.getItem() == Items.written_book && this.currentRecipe != null) {
        if (!currentRecipeIsValid().valid)
            return bookstack;
        if (!bookstack.hasTagCompound())
            bookstack.setTagCompound(new NBTTagCompound());
        else if (// don't overwrite a completed spell
        bookstack.getTagCompound().getBoolean("spellFinalized"))
            return bookstack;
        LinkedHashMap<String, Integer> materialsList = new LinkedHashMap<String, Integer>();
        materialsList.put(ItemsCommonProxy.rune.getItemStackDisplayName(new ItemStack(ItemsCommonProxy.rune, 1, ItemsCommonProxy.rune.META_BLANK)), 1);
        ArrayList<ItemStack> componentRecipeList = new ArrayList<ItemStack>();
        int count = 0;
        ArrayList<ISpellPart> allRecipeItems = new ArrayList<ISpellPart>();
        for (ArrayList<ISpellPart> shapeGroup : shapeGroups) {
            if (shapeGroup == null || shapeGroup.size() == 0)
                continue;
            allRecipeItems.addAll(shapeGroup);
        }
        allRecipeItems.addAll(currentRecipe);
        for (ISpellPart part : allRecipeItems) {
            if (part == null) {
                LogHelper.error("Unable to write recipe to book.  Recipe part is null!");
                return bookstack;
            }
            Object[] recipeItems = part.getRecipeItems();
            SpellRecipeItemsEvent event = new SpellRecipeItemsEvent(SkillManager.instance.getSkillName(part), SkillManager.instance.getShiftedPartID(part), recipeItems);
            MinecraftForge.EVENT_BUS.post(event);
            recipeItems = event.recipeItems;
            if (recipeItems == null) {
                LogHelper.error("Unable to write recipe to book.  Recipe items are null for part %d!", part.getID());
                return bookstack;
            }
            for (int i = 0; i < recipeItems.length; ++i) {
                Object o = recipeItems[i];
                String materialkey = "";
                int qty = 1;
                ItemStack recipeStack = null;
                if (o instanceof ItemStack) {
                    materialkey = ((ItemStack) o).getDisplayName();
                    recipeStack = (ItemStack) o;
                } else if (o instanceof Item) {
                    recipeStack = new ItemStack((Item) o);
                    materialkey = ((Item) o).getItemStackDisplayName(new ItemStack((Item) o));
                } else if (o instanceof Block) {
                    recipeStack = new ItemStack((Block) o);
                    materialkey = ((Block) o).getLocalizedName();
                } else if (o instanceof String) {
                    if (((String) o).startsWith("P:")) {
                        String s = ((String) o).substring(2);
                        int pfx = SpellRecipeManager.parsePotionMeta(s);
                        recipeStack = new ItemStack(Items.potionitem, 1, pfx);
                        materialkey = recipeStack.getDisplayName();
                    } else if (((String) o).startsWith("E:")) {
                        int[] ids = SpellRecipeManager.ParseEssenceIDs((String) o);
                        materialkey = "Essence (";
                        for (int powerID : ids) {
                            PowerTypes type = PowerTypes.getByID(powerID);
                            materialkey += type.name() + "/";
                        }
                        if (materialkey.equals("Essence (")) {
                            ++i;
                            continue;
                        }
                        o = recipeItems[++i];
                        if (materialkey.startsWith("Essence (")) {
                            materialkey = materialkey.substring(0, materialkey.lastIndexOf("/")) + ")";
                            qty = (Integer) o;
                            int flag = 0;
                            for (int f : ids) {
                                flag |= f;
                            }
                            recipeStack = new ItemStack(ItemsCommonProxy.essence, qty, ItemsCommonProxy.essence.META_MAX + flag);
                        }
                    } else {
                        ArrayList<ItemStack> ores = OreDictionary.getOres((String) o);
                        recipeStack = ores.size() > 0 ? ores.get(1) : null;
                        materialkey = (String) o;
                    }
                }
                if (materialsList.containsKey(materialkey)) {
                    int old = materialsList.get(materialkey);
                    old += qty;
                    materialsList.put(materialkey, old);
                } else {
                    materialsList.put(materialkey, qty);
                }
                if (recipeStack != null)
                    componentRecipeList.add(recipeStack);
            }
        }
        materialsList.put(ItemsCommonProxy.spellParchment.getItemStackDisplayName(new ItemStack(ItemsCommonProxy.spellParchment)), 1);
        StringBuilder sb = new StringBuilder();
        int sgCount = 0;
        int[][] shapeGroupCombos = new int[shapeGroups.size()][];
        for (ArrayList<ISpellPart> shapeGroup : shapeGroups) {
            sb.append("Shape Group " + ++sgCount + "\n\n");
            Iterator<ISpellPart> it = shapeGroup.iterator();
            shapeGroupCombos[sgCount - 1] = SpellPartListToStringBuilder(it, sb, " -");
            sb.append("\n");
        }
        sb.append("Combination:\n\n");
        Iterator<ISpellPart> it = currentRecipe.iterator();
        ArrayList<Integer> outputCombo = new ArrayList<Integer>();
        int[] outputData = SpellPartListToStringBuilder(it, sb, null);
        ArrayList<NBTTagString> pages = Story.splitStoryPartIntoPages(sb.toString());
        sb = new StringBuilder();
        sb.append("\n\nMaterials List:\n\n");
        for (String s : materialsList.keySet()) {
            sb.append(materialsList.get(s) + " x " + s + "\n");
        }
        pages.addAll(Story.splitStoryPartIntoPages(sb.toString()));
        sb = new StringBuilder();
        sb.append("Affinity Breakdown:\n\n");
        it = currentRecipe.iterator();
        HashMap<Affinity, Integer> affinityData = new HashMap<Affinity, Integer>();
        int cpCount = 0;
        while (it.hasNext()) {
            ISpellPart part = it.next();
            if (part instanceof ISpellComponent) {
                EnumSet<Affinity> aff = ((ISpellComponent) part).getAffinity();
                for (Affinity affinity : aff) {
                    int qty = 1;
                    if (affinityData.containsKey(affinity)) {
                        qty = 1 + affinityData.get(affinity);
                    }
                    affinityData.put(affinity, qty);
                }
                cpCount++;
            }
        }
        ValueComparator vc = new ValueComparator(affinityData);
        TreeMap<Affinity, Integer> sorted = new TreeMap<Affinity, Integer>(vc);
        sorted.putAll(affinityData);
        for (Affinity aff : sorted.keySet()) {
            float pct = (float) sorted.get(aff) / (float) cpCount * 100f;
            sb.append(String.format("%s: %.2f%%", aff.toString(), pct));
            sb.append("\n");
        }
        pages.addAll(Story.splitStoryPartIntoPages(sb.toString()));
        Story.WritePartToNBT(bookstack.stackTagCompound, pages);
        bookstack = Story.finalizeStory(bookstack, title, player.getCommandSenderName());
        int[] recipeData = new int[componentRecipeList.size() * 3];
        int idx = 0;
        for (ItemStack stack : componentRecipeList) {
            recipeData[idx++] = Item.getIdFromItem(stack.getItem());
            recipeData[idx++] = stack.stackSize;
            recipeData[idx++] = stack.getItemDamage();
        }
        bookstack.stackTagCompound.setIntArray("spell_combo", recipeData);
        bookstack.stackTagCompound.setIntArray("output_combo", outputData);
        bookstack.stackTagCompound.setInteger("numShapeGroups", shapeGroupCombos.length);
        int index = 0;
        for (int[] sgArray : shapeGroupCombos) {
            bookstack.stackTagCompound.setIntArray("shapeGroupCombo_" + index++, sgArray);
        }
        bookstack.stackTagCompound.setString("spell_mod_version", AMCore.instance.getVersion());
        if (currentSpellName.equals(""))
            currentSpellName = "Spell Recipe";
        bookstack.setStackDisplayName(currentSpellName);
        this.currentRecipe.clear();
        for (ArrayList<ISpellPart> list : shapeGroups) list.clear();
        currentSpellName = "";
        bookstack.stackTagCompound.setBoolean("spellFinalized", true);
        worldObj.playSound(xCoord, yCoord, zCoord, "arsmagica2:misc.inscriptiontable.takebook", 1.0f, 1.0f, true);
        worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    }
    return bookstack;
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) NBTTagString(net.minecraft.nbt.NBTTagString) Item(net.minecraft.item.Item) PowerTypes(am2.api.power.PowerTypes) NBTTagString(net.minecraft.nbt.NBTTagString) SpellRecipeItemsEvent(am2.api.events.SpellRecipeItemsEvent) Block(net.minecraft.block.Block) Affinity(am2.api.spell.enums.Affinity) ItemStack(net.minecraft.item.ItemStack)

Example 14 with ISpellPart

use of am2.api.spell.component.interfaces.ISpellPart in project ArsMagica2 by Mithion.

the class SpellValidator method splitToStages.

public static ArrayList<ArrayList<ISpellPart>> splitToStages(ArrayList<ISpellPart> currentRecipe) {
    ArrayList<ArrayList<ISpellPart>> segmented = new ArrayList<ArrayList<ISpellPart>>();
    int idx = (currentRecipe.size() > 0 && currentRecipe.get(0) instanceof ISpellShape) ? -1 : 0;
    for (int i = 0; i < currentRecipe.size(); ++i) {
        ISpellPart part = currentRecipe.get(i);
        if (part instanceof ISpellShape)
            idx++;
        if (// while loop not necessary as this will keep up
        segmented.size() - 1 < idx)
            segmented.add(new ArrayList<ISpellPart>());
        segmented.get(idx).add(part);
    }
    return segmented;
}
Also used : ArrayList(java.util.ArrayList) ISpellShape(am2.api.spell.component.interfaces.ISpellShape) ISpellPart(am2.api.spell.component.interfaces.ISpellPart)

Example 15 with ISpellPart

use of am2.api.spell.component.interfaces.ISpellPart in project ArsMagica2 by Mithion.

the class SpellUtils method createSpellStack.

public ItemStack createSpellStack(ArrayList<ArrayList<KeyValuePair<ISpellPart, byte[]>>> shapeGroups, ArrayList<KeyValuePair<ISpellPart, byte[]>> spell) {
    ArrayList<KeyValuePair<ISpellPart, byte[]>> recipeCopy = (ArrayList<KeyValuePair<ISpellPart, byte[]>>) spell.clone();
    if (recipeCopy.size() > 0 && !(recipeCopy.get(0).getKey() instanceof ISpellShape))
        recipeCopy.add(0, new KeyValuePair<ISpellPart, byte[]>(SkillManager.instance.missingShape, new byte[0]));
    ItemStack stack = new ItemStack(ItemsCommonProxy.spell);
    boolean hasSummon = false;
    while (recipeCopy.size() > 0) {
        ISpellShape shape;
        ArrayList<Integer> components = new ArrayList<Integer>();
        ArrayListMultimap<Integer, byte[]> modifiers = ArrayListMultimap.create();
        KeyValuePair<ISpellPart, byte[]> part = recipeCopy.get(0);
        recipeCopy.remove(0);
        if (part.getKey() instanceof ISpellShape) {
            shape = (ISpellShape) part.getKey();
            part = recipeCopy.size() > 0 ? recipeCopy.get(0) : null;
            while (part != null && !(part.getKey() instanceof ISpellShape)) {
                recipeCopy.remove(0);
                if (part.getKey() instanceof ISpellComponent) {
                    components.add(SkillManager.instance.getShiftedPartID(part.getKey()));
                    if (part.getKey() instanceof Summon) {
                        hasSummon = true;
                    }
                } else if (part.getKey() instanceof ISpellModifier) {
                    modifiers.put(SkillManager.instance.getShiftedPartID(part.getKey()), part.getValue());
                }
                part = recipeCopy.size() > 0 ? recipeCopy.get(0) : null;
            }
            if (hasSummon) {
                ((Summon) SkillManager.instance.getSkill("Summon")).setSummonType(stack, EntitySkeleton.class);
            }
            SpellUtils.instance.addSpellStageToScroll(stack, shape.getID(), ArrayListToIntArray(components), modifiers);
        }
    }
    for (int i = 0; i < shapeGroups.size(); ++i) {
        ArrayList<KeyValuePair<ISpellPart, byte[]>> shapeGroup = shapeGroups.get(i);
        if (shapeGroup.size() == 0)
            continue;
        int[] sgp = new int[shapeGroup.size()];
        byte[][] sgp_m = new byte[shapeGroup.size()][];
        for (int n = 0; n < shapeGroup.size(); ++n) {
            sgp[n] = SkillManager.instance.getShiftedPartID(shapeGroup.get(n).getKey());
            sgp_m[n] = shapeGroup.get(n).getValue();
        }
        SpellUtils.instance.addShapeGroup(sgp, sgp_m, stack);
    }
    SpellUtils.instance.writeModVersionToStack(stack);
    ItemStack checkStack = constructSpellStack(stack);
    int silkTouchLevel = 0;
    int fortuneLevel = 0;
    for (int i = 0; i < numStages(checkStack); ++i) {
        int st = countModifiers(SpellModifiers.SILKTOUCH_LEVEL, checkStack, 0);
        int fn = countModifiers(SpellModifiers.FORTUNE_LEVEL, checkStack, 0);
        if (st > silkTouchLevel)
            silkTouchLevel = st;
        if (fn > fortuneLevel)
            fortuneLevel = fn;
    }
    if (fortuneLevel > 0) {
        AMEnchantmentHelper.fortuneStack(stack, fortuneLevel);
        AMEnchantmentHelper.lootingStack(stack, fortuneLevel);
    }
    if (silkTouchLevel > 0)
        AMEnchantmentHelper.silkTouchStack(stack, silkTouchLevel);
    return stack;
}
Also used : KeyValuePair(am2.utility.KeyValuePair) Summon(am2.spell.components.Summon) ArrayList(java.util.ArrayList) 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