Minecraft – How to stop Items from despawning

minecraft-commandsminecraft-java-edition

I searched up how to do this several times and I could only find ways to spawn items that cannot be picked up, and if it was a tutorial about stopping item despawning, it would still despawn after being picked up and dropped on the ground again.

I'm working on a game, fully with command blocks, and I have absolutely no idea how to stop items from despawning.

Could NBT tags be put on item entities too? So that when they get dropped, they still have the NBT tag?

Does anyone know how to stop them from despawning?

Best Answer

First, you can stop a specific item from despawning unless it is picked up and then dropped again by setting it's Age to -32768 with a command block.

Second, you can add arbitrary custom NBT data to an item, which will be preserved across drop-and-pickup cycles.

You can combine these two techniques to make specific items never despawn by running the command:

/execute as @e[type=item,nbt={Item:{tag:{noDespawn:1b}}}] run data merge entity @s {Age:-32768}

This would make any item given with a noDespawn:1b NBT tag (/give <player> <item>{noDespawn:1b}) never despawn, even if it is picked up and dropped again.