Java Code Examples for net.minecraft.item.ItemStack#writeToNBT()

The following examples show how to use net.minecraft.item.ItemStack#writeToNBT() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityCart.java    From TFC2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void writeEntityToNBT(NBTTagCompound tagCompound) 
{
	NBTTagList nbttaglist = new NBTTagList();

	for (int i = 0; i < this.cartInv.getSizeInventory(); ++i)
	{
		ItemStack itemstack = this.cartInv.getStackInSlot(i);

		if (itemstack != null)
		{
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte)i);
			itemstack.writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}
	}

	tagCompound.setTag("Items", nbttaglist);

}
 
Example 2
Source File: TileEntityCamoMine.java    From AdvancedMod with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void writeToNBT(NBTTagCompound tag){
    super.writeToNBT(tag);
    tag.setInteger("timer", timer);
    tag.setString("target", target);

    NBTTagList camoStackTag = new NBTTagList();
    for(int i = 0; i < camoStacks.length; i++) {
        ItemStack stack = camoStacks[i];
        if(stack != null) {
            NBTTagCompound t = new NBTTagCompound();
            stack.writeToNBT(t);
            t.setByte("index", (byte)i);
            camoStackTag.appendTag(t);
        }
    }
    tag.setTag("camoStacks", camoStackTag);
}
 
Example 3
Source File: TileEntityModularStorage.java    From AdvancedMod with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void writeToNBT(NBTTagCompound tag){
    super.writeToNBT(tag);

    tag.setBoolean("isMaster", isMaster);

    tag.setInteger("slots", inventory.length);
    NBTTagList camoStackTag = new NBTTagList();
    for(int i = 0; i < inventory.length; i++) {
        ItemStack stack = inventory[i];
        if(stack != null) {
            NBTTagCompound t = new NBTTagCompound();
            stack.writeToNBT(t);
            t.setByte("index", (byte)i);
            camoStackTag.appendTag(t);
        }
    }
    tag.setTag("inventory", camoStackTag);
}
 
Example 4
Source File: MoCAnimalChest.java    From mocreaturesdev with GNU General Public License v3.0 6 votes vote down vote up
public NBTTagList saveInventoryToNBT()
{
    NBTTagList var1 = new NBTTagList("HorseItems");

    for (int var2 = 0; var2 < this.getSizeInventory(); ++var2)
    {
        ItemStack var3 = this.getStackInSlot(var2);

        if (var3 != null)
        {
            NBTTagCompound var4 = new NBTTagCompound();
            var4.setByte("Slot", (byte) var2);
            var3.writeToNBT(var4);
            var1.appendTag(var4);
        }
    }

    return var1;
}
 
Example 5
Source File: TERecipes.java    From SimplyJetpacks with MIT License 6 votes vote down vote up
public static void addTransposerFill(int energy, ItemStack input, ItemStack output, FluidStack fluid, boolean reversible) {
    SimplyJetpacks.logger.info("Registering TE Fluid Transposer fill recipe");
    
    NBTTagCompound toSend = new NBTTagCompound();
    
    toSend.setInteger("energy", energy);
    toSend.setTag("input", new NBTTagCompound());
    toSend.setTag("output", new NBTTagCompound());
    toSend.setTag("fluid", new NBTTagCompound());
    
    input.writeToNBT(toSend.getCompoundTag("input"));
    output.writeToNBT(toSend.getCompoundTag("output"));
    toSend.setBoolean("reversible", reversible);
    fluid.writeToNBT(toSend.getCompoundTag("fluid"));
    
    FMLInterModComms.sendMessage("ThermalExpansion", "TransposerFillRecipe", toSend);
}
 
Example 6
Source File: NBTUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nonnull
public static NBTTagCompound storeItemStackInTag(@Nonnull ItemStack stack, @Nonnull NBTTagCompound tag)
{
    if (stack.isEmpty() == false)
    {
        stack.writeToNBT(tag);

        if (stack.getCount() > 127)
        {
            // Prevent overflow and negative stack sizes
            tag.setByte("Count", (byte) (stack.getCount() & 0x7F));
            tag.setInteger("ActualCount", stack.getCount());
        }
    }

    return tag;
}
 
Example 7
Source File: NEIClientConfig.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static void saveState(int state) {
    NBTTagCompound statesave = global.nbt.getCompoundTag("save" + state);
    GuiContainer currentContainer = NEIClientUtils.getGuiContainer();
    LinkedList<TaggedInventoryArea> saveAreas = new LinkedList<>();
    saveAreas.add(new TaggedInventoryArea(Minecraft.getMinecraft().player.inventory));

    for (INEIGuiHandler handler : GuiInfo.guiHandlers) {
        List<TaggedInventoryArea> areaList = handler.getInventoryAreas(currentContainer);
        if (areaList != null) {
            saveAreas.addAll(areaList);
        }
    }

    for (TaggedInventoryArea area : saveAreas) {
        NBTTagList areaTag = new NBTTagList();

        for (int i : area.slots) {
            ItemStack stack = area.getStackInSlot(i);
            if (stack.isEmpty()) {
                continue;
            }
            NBTTagCompound stacksave = new NBTTagCompound();
            stacksave.setByte("Slot", (byte) i);
            stack.writeToNBT(stacksave);
            areaTag.appendTag(stacksave);
        }
        statesave.setTag(area.tagName, areaTag);
    }

    global.nbt.setTag("save" + state, statesave);
    global.saveNBT();
    statesSaved[state] = true;
}
 
Example 8
Source File: ItemPanelDumper.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public void dumpJson(File file) throws IOException {
    PrintWriter p = new PrintWriter(file);
    for (ItemStack stack : ItemPanel.items) {
        NBTTagCompound tag = stack.writeToNBT(new NBTTagCompound());
        tag.removeTag("Count");
        p.println(tag);
    }

    p.close();
}
 
Example 9
Source File: EIORecipes.java    From SimplyJetpacks with MIT License 5 votes vote down vote up
private static void writeItemStack(NBTTagCompound nbt, String tagName, ItemStack stack) {
    if (stack != null) {
        NBTTagCompound stackTag = new NBTTagCompound();
        stack.writeToNBT(stackTag);
        nbt.setTag(tagName, stackTag);
    }
}
 
Example 10
Source File: ItemPanelDumper.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public void dumpJson(File file) throws IOException {
    PrintWriter p = new PrintWriter(file);
    for (ItemStack stack : ItemPanel.items) {
        NBTTagCompound tag = stack.writeToNBT(new NBTTagCompound());
        tag.removeTag("Count");
        p.println(tag);
    }

    p.close();
}
 
Example 11
Source File: DamageXpHandler.java    From TinkersToolLeveling with MIT License 5 votes vote down vote up
private NBTTagCompound convertItemDamageDataToTag(ItemStack stack, Float damage) {
  NBTTagCompound tag = new NBTTagCompound();

  NBTTagCompound itemTag = stack.writeToNBT(new NBTTagCompound());
  tag.setTag(TAG_ITEM, itemTag.copy());
  tag.setFloat(TAG_DAMAGE, damage);

  return tag;
}
 
Example 12
Source File: ItemBlueprint.java    From Cyberware with MIT License 5 votes vote down vote up
public static ItemStack getBlueprintForItem(ItemStack stack)
{
	if (stack != null && CyberwareAPI.canDeconstruct(stack))
	{
		ItemStack toBlue = stack.copy();
		

		toBlue.stackSize = 1;
		if (toBlue.isItemStackDamageable())
		{
			toBlue.setItemDamage(0);
		}
		toBlue.setTagCompound(null);
		
		ItemStack ret = new ItemStack(CyberwareContent.blueprint);
		NBTTagCompound tag = new NBTTagCompound();
		NBTTagCompound itemTag = new NBTTagCompound();
		toBlue.writeToNBT(itemTag);
		tag.setTag("blueprintItem", itemTag);
		
		ret.setTagCompound(tag);
		return ret;
	}
	else
	{
		return null;
	}
}
 
Example 13
Source File: CyberwareUserDataImpl.java    From Cyberware with MIT License 5 votes vote down vote up
@Override
public NBTTagCompound serializeNBT()
{
	NBTTagCompound compound = new NBTTagCompound();
	NBTTagList list = new NBTTagList();
	
	for (EnumSlot slot : EnumSlot.values())
	{
		NBTTagList list2 = new NBTTagList();
		for (ItemStack cyberware : getInstalledCyberware(slot))
		{
			NBTTagCompound temp = new NBTTagCompound();
			if (cyberware != null)
			{
				temp = cyberware.writeToNBT(temp);
			}
			list2.appendTag(temp);
		}
		list.appendTag(list2);
	}
	
	compound.setTag("cyberware", list);
	
	NBTTagList essentialList = new NBTTagList();
	for (int i = 0; i < this.missingEssentials.length; i++)
	{
		essentialList.appendTag(new NBTTagByte((byte) (this.missingEssentials[i] ? 1 : 0)));
	}
	compound.setTag("discard", essentialList);
	compound.setTag("powerBuffer", writeMap(this.powerBuffer));
	compound.setTag("powerBufferLast", writeMap(this.powerBufferLast));
	compound.setInteger("powerCap", this.powerCap);
	compound.setInteger("storedPower", this.storedPower);
	compound.setInteger("missingEssence", missingEssence);
	compound.setTag("hud", hudData);
	compound.setInteger("color", hudColor);
	compound.setBoolean("hasOpenedRadialMenu", hasOpenedRadialMenu);
	return compound;
}
 
Example 14
Source File: QuestBase.java    From ToroQuest with GNU General Public License v3.0 5 votes vote down vote up
protected static void setItemsToNbt(QuestData data, String name, List<ItemStack> items) {
	NBTTagCompound c = getCustomNbtTag(data);
	NBTTagList list = new NBTTagList();
	for (ItemStack stack : items) {
		NBTTagCompound cStack = new NBTTagCompound();
		stack.writeToNBT(cStack);
		list.appendTag(cStack);
	}
	c.setTag(name, list);
}
 
Example 15
Source File: SchematicBlock.java    From Framez with GNU General Public License v3.0 5 votes vote down vote up
protected void writeRequirementsToNBT(NBTTagCompound nbt, MappingRegistry registry) {
	if (storedRequirements.length > 0) {
		NBTTagList rq = new NBTTagList();

		for (ItemStack stack : storedRequirements) {
			NBTTagCompound sub = new NBTTagCompound();
			stack.writeToNBT(sub);
			registry.stackToRegistry(sub);
			rq.appendTag(sub);
		}

		nbt.setTag("rq", rq);
	}
}
 
Example 16
Source File: IMCHelper.java    From BigReactors with MIT License 5 votes vote down vote up
public static void addOreToMiningLaserFocus(ItemStack stack, int color) {
       NBTTagCompound laserOreMsg = new NBTTagCompound();
       stack.writeToNBT(laserOreMsg);
       laserOreMsg.setInteger("value", color);
       IMCHelper.sendInterModMessage("MineFactoryReloaded", "registerLaserOre", laserOreMsg);

}
 
Example 17
Source File: ItemMinigun.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setAmmoColorStack(ItemStack ammo){
    if(ammo != null) {
        NBTTagCompound tag = new NBTTagCompound();
        ammo.writeToNBT(tag);
        NBTUtil.setCompoundTag(stack, "ammoColorStack", tag);
    } else {
        NBTUtil.removeTag(stack, "ammoColorStack");
    }
}
 
Example 18
Source File: NbtUtils.java    From WearableBackpacks with MIT License 4 votes vote down vote up
/** Writes an item stack to an NBT compound. */
public static NBTTagCompound writeItem(ItemStack item, boolean writeNullAsEmptyCompound) {
	return (!item.isEmpty()) ? item.writeToNBT(new NBTTagCompound())
		: (writeNullAsEmptyCompound ? new NBTTagCompound() : null);
}
 
Example 19
Source File: TileEntityQuantumComputer.java    From qcraft-mod with Apache License 2.0 4 votes vote down vote up
public static void teleportPlayerRemote( EntityPlayer player, String remoteServerAddress, String remotePortalID, boolean takeItems )
{
    // Log the trip
    QCraft.log( "Sending player " + player.getDisplayName() + " to server \"" + remoteServerAddress + "\"" );

    NBTTagCompound luggage = new NBTTagCompound();
    if( takeItems )
    {
        // Remove and encode the items from the players inventory we want them to take with them
        NBTTagList items = new NBTTagList();
        InventoryPlayer playerInventory = player.inventory;
        for( int i = 0; i < playerInventory.getSizeInventory(); ++i )
        {
            ItemStack stack = playerInventory.getStackInSlot( i );
            if( stack != null && stack.stackSize > 0 )
            {
                // Ignore entangled items
                if( stack.getItem() == Item.getItemFromBlock( QCraft.Blocks.quantumComputer ) && ItemQuantumComputer.getEntanglementFrequency( stack ) >= 0 )
                {
                    continue;
                }
                if( stack.getItem() == Item.getItemFromBlock( QCraft.Blocks.qBlock ) && ItemQBlock.getEntanglementFrequency( stack ) >= 0 )
                {
                    continue;
                }

                // Store items
                NBTTagCompound itemTag = new NBTTagCompound();
                if (stack.getItem() == QCraft.Items.missingItem) {
                    itemTag = stack.stackTagCompound;
                } else {
                    GameRegistry.UniqueIdentifier uniqueId = GameRegistry.findUniqueIdentifierFor(stack.getItem());
                    String itemName = uniqueId.modId + ":" + uniqueId.name;
                    itemTag.setString("Name", itemName);
                    stack.writeToNBT( itemTag );
                }
                items.appendTag( itemTag );

                // Remove items
                playerInventory.setInventorySlotContents( i, null );
            }
        }

        if( items.tagCount() > 0 )
        {
            QCraft.log( "Removed " + items.tagCount() + " items from " + player.getDisplayName() + "'s inventory." );
            playerInventory.markDirty();
            luggage.setTag( "items", items );
        }
    }

    // Set the destination portal ID
    if( remotePortalID != null )
    {
        luggage.setString( "destinationPortal", remotePortalID );
    }

    try
    {
        // Cryptographically sign the luggage
        luggage.setString( "uuid", UUID.randomUUID().toString() );
        byte[] luggageData = CompressedStreamTools.compress( luggage );
        byte[] luggageSignature = EncryptionRegistry.Instance.signData( luggageData );
        NBTTagCompound signedLuggage = new NBTTagCompound();
        signedLuggage.setByteArray( "key", EncryptionRegistry.Instance.encodePublicKey( EncryptionRegistry.Instance.getLocalKeyPair().getPublic() ) );
        signedLuggage.setByteArray( "luggage", luggageData );
        signedLuggage.setByteArray( "signature", luggageSignature );

        // Send the player to the remote server with the luggage
        byte[] signedLuggageData = CompressedStreamTools.compress( signedLuggage );
        QCraft.requestGoToServer( player, remoteServerAddress, signedLuggageData );
    }
    catch( IOException e )
    {
        throw new RuntimeException( "Error encoding inventory" );
    }
    finally
    {
        // Prevent the player from being warped twice
        player.timeUntilPortal = 200;
    }
}
 
Example 20
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static NBTTagCompound savePersistant(ItemStack stack, NBTTagCompound tag) {
    stack.writeToNBT(tag);
    tag.removeTag("id");
    tag.setString("name", Item.itemRegistry.getNameForObject(stack.getItem()).toString());
    return tag;
}