Jump to content

[1.11.2] ItemStack.loadItemStackFromNBT();


rowan662

Recommended Posts

Does anyone knows how I can call ItemStack.loadItemStackFromNBT(); in 1.11.2? It seems like something has changed.

Look at ItemStackHandler#deserializeNBT for how it loads an ItemStack from NBT and ItemStackHandler#serializeNBT for writing of ItemStacks.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

And how can I implement that? I also got an error for the void return type of public void writeToNBT

 

@Override
public void readFromNBT(NBTTagCompound compound){
	super.readFromNBT(compound);
	NBTTagList nbttaglist = compound.getTagList("Items", 10);
	compressorItemStackArray = new ItemStack[getSizeInventory()];

	for(int i = 0; i < nbttaglist.tagCount(); i++){
		NBTTagCompound nbtTagCompound = nbttaglist.getCompoundTagAt(i);
		byte b0 = nbtTagCompound.getByte("Slot");

		if(b0 >= 0 && b0 < compressorItemStackArray.length){
			compressorItemStackArray[b0] = ItemStack.loadItemStackFromNBT(nbtTagCompound);
		}
	}
	timeCanWork = compound.getShort("WorkTime");
	ticksCompressingItemSoFar = compound.getShort("CompressTime");
	ticksPerItem = compound.getShort("CompressTimeTotal");

	if(compound.hasKey("CustomName", ){
		compressorCustomName = compound.getString("CustomName");
	}
}

@Override
 public void writeToNBT(NBTTagCompound compound){
	super.writeToNBT(compound);
	compound.setShort("WorkTime", (short)timeCanWork);
	compound.setShort("CompressTime", (short)ticksCompressingItemSoFar);
	compound.setShort("ticksPerItem", (short)ticksPerItem);
	NBTTagList nbttaglist = new NBTTagList();

	for(int i = 0; i < compressorItemStackArray.length; i++){
		if(compressorItemStackArray[i] != null){
			NBTTagCompound nbtTagCompound = new NBTTagCompound();
			nbtTagCompound.setByte("Slot", (byte)i);
			compressorItemStackArray[i].writeToNBT(nbtTagCompound);
			nbttaglist.appendTag(nbtTagCompound);
		}
	}
	compound.setTag("Items", nbttaglist);

	if(hasCustomName()){
		compound.setString("CustomName", compressorCustomName);
	}
}

Link to comment
Share on other sites

If you are saving an inventory to a TE, you should just be able to do itemStackHandlerField.serialize/deserialize. Have you switched over to the new Capability system?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.