How to stop Items from despawning?

Solution 1:

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.