endstone icon indicating copy to clipboard operation
endstone copied to clipboard

[feature] NBT API for ItemStack

Open ye111566 opened this issue 1 year ago • 7 comments

as the title

ye111566 avatar Nov 29 '24 07:11 ye111566

I put an enchanted_book in the first slot of slot.hotbar. I use the"print(str(self.server.get_player(sender.name).inventory.get_item(0)))" and the console returns: ItemStack(minecraft:enchanted_book x 1) whitout any nbt. but when I use"self.server.get_player(sender.name).inventory.add_item(self.server.get_player(sender.name).inventory.get_item(0))" I find that the enchanted_book is successfully copied with nbt. WHERE WAS THE NBT STORAGED?

ye111566 avatar Nov 29 '24 08:11 ye111566

e0e3364c16445fc75fa7fa917780cb70 935ec9e63b38d07fe5c684254833e74a

ye111566 avatar Nov 29 '24 08:11 ye111566

The NBT data is stored internally within the handle to the ItemStack hence it is preserved during operations like copying. However, currently, there is no way to directly access or modify the NBT data.

I am planning to introduce a (very basic) NBT API in the next version which will allow you to interact with the NBT data of ItemStack objects directly.

wu-vincent avatar Dec 24 '24 01:12 wu-vincent

The NBT data is stored internally within the handle to the ItemStack hence it is preserved during operations like copying. However, currently, there is no way to directly access or modify the NBT data.

I am planning to introduce a (very basic) NBT API in the next version which will allow you to interact with the NBT data of ItemStack objects directly.

There are also player nbt, physical nbt, creature nbt, block nbt

Atrium4641 avatar Dec 29 '24 10:12 Atrium4641

This issue has been automatically marked as stale because it did not have recent activity. It will be closed in 15 days if no further activity occurs. If you wish not to mark it as stale, please leave a comment in this issue.

github-actions[bot] avatar Feb 27 '25 21:02 github-actions[bot]

1

zmsgsb avatar Feb 28 '25 06:02 zmsgsb

This issue has been automatically marked as stale because it did not have recent activity. It will be closed in 15 days if no further activity occurs. If you wish not to mark it as stale, please leave a comment in this issue.

github-actions[bot] avatar May 21 '25 21:05 github-actions[bot]

1

zmsgsb avatar May 23 '25 05:05 zmsgsb

I wonder vincent if you are going to implement nbt serializing/deserializing yourself or going to use an already written library, reasons I'm asking to keep this issue opened and also curious of what you planning

programminglaboratorys avatar Jun 12 '25 23:06 programminglaboratorys

@programminglaboratorys Most item metadata can already be accessed and modified via the ItemMeta API. We’re still evaluating whether it’s necessary to expose a low-level NBT API.

For storing custom data, a Persistent Data Container (PDC) API would be safer and more appropriate. With the PDC API, we can eliminate the need to support every NBT type - users can pass a Python dict directly instead of writing wrappers for each NBT tag.

For example, with a PDC API, one may write:

item.custom_data = {
    'font': 'Roboto',
    'size': 14,
    'items': ['apple', 'banana', 'orange'],
    'flag': True,
}

Instead of:

from endstone.nbt import ByteTag, CompoundTag, IntTag, ListTag, StringTag

item.nbt['custom_data'] = CompoundTag({
    'font': StringTag('Roboto'),
    'size': IntTag(14),
    'items': ListTag(StringTag('apple'), StringTag('banana'), StringTag('orange')),
    'flag': ByteTag(1)
})

Direct binary NBT operations aren’t under consideration at this stage, due to uncertainty like endianness, varint encoding, and the risk of corrupting item data with malformed payloads.

wu-vincent avatar Jun 13 '25 17:06 wu-vincent

I would say that probably dictionary is the better way for this

Mih4n avatar Jun 13 '25 20:06 Mih4n

This issue has been automatically marked as stale because it did not have recent activity. It will be closed in 15 days if no further activity occurs. If you wish not to mark it as stale, please leave a comment in this issue.

github-actions[bot] avatar Aug 12 '25 21:08 github-actions[bot]

clearly want to keep this open till further updates

programminglaboratorys avatar Aug 14 '25 05:08 programminglaboratorys

This issue has been automatically marked as stale because it did not have recent activity. It will be closed in 15 days if no further activity occurs. If you wish not to mark it as stale, please leave a comment in this issue.

github-actions[bot] avatar Oct 14 '25 21:10 github-actions[bot]

This issue has been automatically marked as stale because it did not have recent activity. It will be closed in 15 days if no further activity occurs. If you wish not to mark it as stale, please leave a comment in this issue.

guh

programminglaboratorys avatar Oct 14 '25 21:10 programminglaboratorys

Two methods will be added to ItemStack class since v0.11, ItemStack::fromNbt and ItemStack::toNbt.

wu-vincent avatar Nov 17 '25 22:11 wu-vincent