Search in sources :

Example 81 with NBTTagString

use of net.minecraft.nbt.NBTTagString in project RFToolsControl by McJty.

the class ProcessorTileEntity method writeLog.

private void writeLog(NBTTagCompound tagCompound) {
    NBTTagList logList = new NBTTagList();
    for (String message : logMessages) {
        logList.appendTag(new NBTTagString(message));
    }
    tagCompound.setTag("log", logList);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagString(net.minecraft.nbt.NBTTagString)

Example 82 with NBTTagString

use of net.minecraft.nbt.NBTTagString in project ArsMagica2 by Mithion.

the class Story method splitStoryPartIntoPages.

public static ArrayList<NBTTagString> splitStoryPartIntoPages(String storyPart) {
    ArrayList<NBTTagString> parts = new ArrayList<NBTTagString>();
    String[] words = storyPart.split(" ");
    String currentPage = "";
    for (String word : words) {
        // special commands
        if (word.contains("<newpage>")) {
            int idx = word.indexOf("<newpage>");
            String preNewPage = word.substring(0, idx);
            String postNewPage = word.substring(idx + "<newpage>".length());
            while (preNewPage.endsWith("\n")) preNewPage = preNewPage.substring(0, preNewPage.lastIndexOf('\n'));
            if (getStringOverallLength(currentPage + preNewPage) > 256) {
                parts.add(new NBTTagString(currentPage));
                currentPage = preNewPage.trim();
            } else {
                currentPage += " " + preNewPage.trim();
            }
            parts.add(new NBTTagString(currentPage));
            while (postNewPage.startsWith("\n")) postNewPage = postNewPage.replaceFirst("\n", "");
            currentPage = postNewPage.trim();
            continue;
        }
        // length limit
        if (getStringOverallLength(currentPage + word) > 256) {
            parts.add(new NBTTagString(currentPage));
            currentPage = word;
            if (getStringOverallLength(currentPage) > 256) {
                currentPage = currentPage.substring(0, getStringSplitIndex(currentPage, 255));
                parts.add(new NBTTagString(currentPage));
                currentPage = "";
            }
            continue;
        }
        // 
        if (currentPage.equals("")) {
            currentPage = word.trim();
        } else {
            currentPage += " " + word;
        }
    }
    parts.add(new NBTTagString(currentPage));
    return parts;
}
Also used : ArrayList(java.util.ArrayList) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagString(net.minecraft.nbt.NBTTagString)

Example 83 with NBTTagString

use of net.minecraft.nbt.NBTTagString 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 84 with NBTTagString

use of net.minecraft.nbt.NBTTagString in project ArsMagica2 by Mithion.

the class PlaceBlock method setPlaceBlock.

private void setPlaceBlock(ItemStack stack, Block block, int meta) {
    if (!stack.hasTagCompound())
        stack.stackTagCompound = new NBTTagCompound();
    stack.stackTagCompound.setInteger(KEY_BLOCKID, Block.getIdFromBlock(block));
    stack.stackTagCompound.setInteger(KEY_META, meta);
    // set lore entry so that the stack displays the name of the block to place
    if (!stack.stackTagCompound.hasKey("Lore"))
        stack.stackTagCompound.setTag("Lore", new NBTTagList());
    ItemStack blockStack = new ItemStack(block, 1, meta);
    NBTTagList tagList = stack.stackTagCompound.getTagList("Lore", Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < tagList.tagCount(); ++i) {
        String str = tagList.getStringTagAt(i);
        if (str.startsWith(String.format(StatCollector.translateToLocal("am2.tooltip.placeBlockSpell"), ""))) {
            tagList.removeTag(i);
        }
    }
    tagList.appendTag(new NBTTagString(String.format(StatCollector.translateToLocal("am2.tooltip.placeBlockSpell"), blockStack.getDisplayName())));
    stack.stackTagCompound.setTag("Lore", tagList);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagString(net.minecraft.nbt.NBTTagString) ItemStack(net.minecraft.item.ItemStack)

Example 85 with NBTTagString

use of net.minecraft.nbt.NBTTagString in project ct.js by ChatTriggers.

the class Book method addPage.

/**
 * Add a page to the book.
 *
 * @param message the entire message for what the page should be
 * @return the current book to allow method chaining
 */
public Book addPage(Message message) {
    NBTTagList pages = (NBTTagList) bookData.getTag("pages");
    pages.appendTag(new NBTTagString(IChatComponent.Serializer.componentToJson(message.getChatMessage())));
    updateBookScreen(pages);
    return this;
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagString(net.minecraft.nbt.NBTTagString)

Aggregations

NBTTagString (net.minecraft.nbt.NBTTagString)101 NBTTagList (net.minecraft.nbt.NBTTagList)82 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)57 ItemStack (net.minecraft.item.ItemStack)18 NBTTagIntArray (net.minecraft.nbt.NBTTagIntArray)11 NBTBase (net.minecraft.nbt.NBTBase)10 NBTTagByteArray (net.minecraft.nbt.NBTTagByteArray)10 NBTTagInt (net.minecraft.nbt.NBTTagInt)10 NBTTagDouble (net.minecraft.nbt.NBTTagDouble)9 NBTTagFloat (net.minecraft.nbt.NBTTagFloat)9 NBTTagByte (net.minecraft.nbt.NBTTagByte)8 NBTTagLong (net.minecraft.nbt.NBTTagLong)8 ResourceLocation (net.minecraft.util.ResourceLocation)8 List (java.util.List)7 NBTTagShort (net.minecraft.nbt.NBTTagShort)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Item (net.minecraft.item.Item)4