Skip to content

Lesson 8 of 13 · 16m

Tagging Things: PersistentDataContainer

Typed, namespaced tags on items, entities, and chunks that survive renames — the mechanism lore-parsing hacks replaced.

Every plugin that ever shipped a "soulbound sword" did it the same way once: write §5Soulbound into the item's lore, then parse it back out of getItemMeta().getLore(). That approach survived about as long as it took an admin to rename the item in an anvil and strip the tag. The correct mechanism has existed since Minecraft 1.14: PersistentDataContainer, a typed key-value store the server attaches to objects and serializes for you.

Where it lives

PersistentDataContainer hangs off the objects that already persist: ItemMeta (so every item), Entity, TileState (block states that carry more than a material — chests, signs, spawners), Chunk, and World. You tag the item itself, not its display text.

NamespacedKey key = new NamespacedKey(plugin, "soulbound");

ItemStack item = player.getInventory().getItemInMainHand();
ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer()
    .set(key, PersistentDataType.STRING, owner.toString());
item.setItemMeta(meta);   // meta is a copy — put it back

Reading is the inverse: container.has(key), container.get(key, PersistentDataType.STRING), getOrDefault(key, type, fallback). And the copy-trap is the one bug everyone writes once — getItemMeta() returns a copy, so edits without setItemMeta are silently discarded.

Entities are tagged directly, no meta hop:

creeper.getPersistentDataContainer()
    .set(key, PersistentDataType.BYTE, (byte) 1);

For TileState — say a chest that remembers its owner — block.getState() gives the TileState, you tag its container, then state.update(true) writes it back to the world. Miss the update and the tag never persisted.

Nested data is a TAG_CONTAINER: container.set(complexKey, PersistentDataType.TAG_CONTAINER, inner) where inner is built with adapterContext.newPersistentDataContainer() — one level of key/value maps inside another, still typed, still serialized for free.

NamespacedKey namespaces your keys under your plugin — sudotools:soulbound — so fifty plugins on one server never collide. PersistentDataType covers the primitives, STRING, byte and int arrays, TAG_CONTAINER for nested maps, and TAG_CONTAINER_ARRAY for lists of tags. A nested TAG_CONTAINER is how you store a whole stat block on one item.

remove(key) and isEmpty() close out the read-write surface, and copyTo(target, true) migrates a container wholesale — the move that matters when a version bump changes what meta an object carries. Keep each NamespacedKey in a static final field — it is immutable and cheap — and mind its spelling rules: lowercase letters, digits, ., _, -, so new NamespacedKey(plugin, "SoulBound") throws IllegalArgumentException before the plugin enables.

Types past the primitives are a PersistentDataType<P,C> implementation — getPrimitiveType(), getComplexType(), toPrimitive(complex, context), fromPrimitive(primitive, context). That is how a UUID rides as a 16-byte INT_ARRAY instead of a 36-character string. Most plugins never write one: STRING and INTEGER cover ordinary tags, and TAG_CONTAINER covers the rest.

Why not the alternatives

  • Lore parsing reads display text. Players see it, anvils rewrite it, and other plugins append to it. It is a presentation layer, not a data layer.
  • External maps keyed by UUID die the moment the item splits or stacks — a stack of 64 shares one ItemMeta, and "which item is it" has no answer that survives an inventory shuffle.
  • Hand-rolled NBT is what PDC wraps — correctly namespaced, typed, and future-proofed. Reach in raw only when the container cannot express the shape.

The classic applications: marking custom items (key → item id), storing per-item charge counts, tagging spawned entities so a listener knows "mine" from "vanilla," anchoring region data onto Chunks. If a value must survive restarts, renames, and inventory shuffles, PDC is where it goes. If it is server-global runtime state, it belongs in memory or a database instead.

One honest limitation: PDC data lives in the world save, not your plugins/ data folder. Backups that skip the world — and map tools that strip tile state — take your tags with them.

Field notes

  • container.getKeys() enumerates every tag an object carries — the fastest way to discover whether another plugin already claims an entity.
  • PersistentDataType.LIST.listTypeFrom(PersistentDataType.STRING) gives you typed lists without nesting containers.
  • Items moved through a player's inventory keep their meta; items dropped, hopped, and re-picked keep it too. The tag is on the ItemStack, not the slot.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.