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 22:04, 23. 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 22:04, 23. 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.

 

Last seen on 22:04, 23. Jun 2024
Joined Jul 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Update:I  know this is late…
Sat, 06/22/2024 - 21:51

Update:
I  know this is late but for anyone who would find it helpful I figured out a way to add nbt as well as enchantments into a item in a blocks inventory slot. Its not exactly amazing but it works, if anyone can refine the code let me know.

In this case its a machine block that adds enchantments to the item in the input slot then adds to ouput slot.  I triggered a procedure when the timer of said machine counts down to place the output item, spawn a copy of the item in the input slot at y+1 above the block, then I call a procedure at xyz to set nbt to the block for output result. In this called procedure I use the for each entity block to check a cube of 5 for convert entity iterator to itemstack = get copy of item from input slot of block. (with logic of is entity iterator item) Add x enchantment to convert entity iterator to itemstack. Set get copy of entity iterator in output slot of block at xyz, then finally despawn entity iterator and remove whatever items from input necessary. 

Cheers!