NBT Paths
Two related helpers for the NBT half of a datapack: NbtPath replaces interpolated path strings with typed segments, and resolveNbt writes one tree whose values mix literals and runtime scores.
Typed paths
Every core data and execute store API takes an NBT path as a String, so a typo compiles. NbtPath builds the path from segments instead, quoting each one only when vanilla requires it:
| Operation | Result |
|---|---|
path / "name" |
path.name |
path / otherPath |
Appends every segment |
path[0] |
path[0] |
path.all() |
path[] |
path.matching { } |
path[{Slot:0b}] |
path.isRoot |
true for the root compound |
String.toNbtPath() parses a path already written in Minecraft's syntax, so an existing constant can join the typed API. It splits only on the dots that separate segments, leaving quoted keys, list indices, and compound filters intact:
The helpers module adds NbtPath overloads to Data.modify, Data.get, Data.remove, ExecuteStore.storage, ExecuteStore.entity, and ScoreboardEntity.copyTo, so both halves of a store-plus-modify pair share one value:
Resolving scores into NBT
Minecraft cannot write a score into an NBT tree directly. The vanilla shape is one data modify ... set value for the literal parts, then one execute store result per runtime value - two halves that drift apart as the tree grows.
resolveNbt declares the tree once and emits both halves from it:
execute store result creates the parent compounds it needs, so a nested compound holding only scores emits no data modify line of its own. A tree with no literal at all emits only the store lines.
Values accept NbtTag and every Kotlin primitive, a ScoreboardEntity, or a ScoreboardDelegate. Scores default to int with no scaling; pass a DataType and a scale for fixed-point scores:
Passing the root path (the default) writes the literal half with data merge instead of data modify. An Entity overload targets entity NBT rather than storage.
Feeding a macro
The usual reason to assemble a tree at runtime is calling a macro function with it, which makes resolveNbt and function ... with storage a natural pair:
See also
- Data Storage - The core
datacommand DSL these helpers wrap. - Scoreboards - Fake players and the score handles
resolveNbtreads from. - State Delegates - Delegated scores usable as
resolveNbtvalues.
