How Do I Add NBT To Items In A Blocks Inventory?

Started by temutsaj on

Topic category: Help with modding (Java Edition)

Last seen on 18:02, 21. Jun 2024
Joined Jul 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How Do I Add NBT To Items In A Blocks Inventory?
Mon, 05/27/2024 - 19:07 (edited)

HI I'm trying to make an energy storage card for a battery so that when I place it in the battery blocks slot 0 it will add NBT number to the item during the blocks update tick.

I tried using player nbts, global player persistent variables and item nbts, I added a tooltip to the item to display this nbt, it only displays using global variables but that isn't great because it will dupe the energy among every item.
I tried using "If "this item" = provided itemstack , but it show missing dependencies and then I cant trigger the procedure through the update tick.
How do you add nbts to items? Can anyone please help? I appreciate it.

Update:

I figured out how to get something similiar to work! What I did was add a "Right Clicked On Block" procedure to the item (although it only works when sneaking..?)and put:
Close any GUI, 

If "Get energy of block >/= 1) do: Set NBT of "Get Item In Main Hand of x) to "Get energy of block at xyz"

Extract: "Get NBT x of: "Item In Main Hand of x" Energy from block at: "Look Position xyz of x".

This will extract the blocks energy and save the number to the item. Then to fill the battery I put:

If "Get NBT of: "Item In Main Hand of x" > 1 do:

Send:  "Get NBT x of: "Item In Main Hand of x" energy to block at: "Look Position xyz of x"

Set NBT of "Item In Main Hand of x"  to 0.

It works well although to me it seems too complex for a supposed simple concept. Hope this helps!

Edited by temutsaj on Mon, 05/27/2024 - 19:07
Last seen on 18:02, 21. Jun 2024
Joined Jul 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Update: Okay so I've managed…
Mon, 05/27/2024 - 16:33

Update:

Okay so I've managed to get it to work halfway. 

In the blocks update tick I used a "For Each Player In Current World As" and then i Inserted "Get Nearest Entity range of 10 type "Player" as the entity Iterator, and then I used the "GUI & Slot Procedures" block "Get Item From Slot x" to define the item. Then I Set NBT Number Tag of "Get Item From Slot x" to(math +x). This works very well and the items NBT is changing counting up(I can see it in the tooltip). However now when I close the blocks GUI and open it again, the item in the slot is GONE.

The block is definitely bound to the correct inventory. So what I'm wondering is why do the items disappear? Does changing the NBT of the Item make the block think that item in that slot no longer exists?
This could be very useful implementation if we can find any solutions, Thanks.