-
Notifications
You must be signed in to change notification settings - Fork 0
NBT Path
Lazulib provides a basic NBTPath implementation, with similar utilities as classic IO paths.
They can be used to easily traverse NBT elements, optionally creating intermediate paths if they don't exist.
NBT paths are used by NBT Predicate, which are useful for recipe arguments.
There are two types of NBT path nodes, name nodes and list index nodes.
Name nodes are separated by dots, and consist of literal names. It's possible to specify special characters by surrounding a name in double quotes.
Most of the time you will only ever need name nodes.
Name.SubNameFireworks.FlightName.Nodes.Can."Contain spaces and other unusual characters if".Quoted
NBT names are always case-sensitive.
If you want to access an NBT node within a list, you may use indexing notation to access it, by specifying an index within brackets after the name of the list. List nodes can follow one another, to access nested lists.
Indices start at 0, and you may use negative indices to start counting from the end of the list (-1 is the last element).
-
List[0]first element -
List[1]second element -
List[-1]last element -
List[0][0]first element of the first element assuming it's also a list
NBT paths define a few methods, that provide functionality similar to that of classic IO paths.
-
makePath(rootNBT)Creates this path underroot, making all necessary intermediate compounds and lists, if possible. If an NBT node with the same name that is not a compound or list as required by the path, or if a list where the path wants to create an element doesn't have an adequate size, the path won't be created, and this method will returnfalse. -
exists(rootNBT)Check if this path exists. -
contains(path)Check if this path contains another. -
relativize(path)Make another path relative to this one. Will fail ifcontains(path)would return false. -
resolve(relativePath)Create a combined path. -
parent()Get the parent path, or null if this path has no parent. -
isRoot()Check ifparent()would return null. -
apply(rootNBT)Return the NBT value found at this path fromrootNBT, or null if it doesn't exist. -
delete(rootNBT)Delete the NBT value found at this path fromrootNBT, if it exists.
Additionally, there is also a static traverse(compoundNBT) that can be useful to work with NBT structures.
This method only traverses named paths, it does not traverse lists.
CompoundTag nbt = new CompoundTag(); // ...
for (Map.Entry<NBTPath, INBT> entry : NBTPath.traverse(nbt)) {
// Do stuff for every named subpath ...
}Finally, there's also a cosmetic getDisplay method to obtain a pretty formatted path, styled according to an NBTPath.Style object,
which simply specifies different styles for name, quote, dot, index and bracket styles.
If you have any doubts, feel free to drop by the official Discord Server.
- Recipes
- Math
- Network
- NBT
- Text