Class Rtag
Rtag class to edit NBTTagCompound & NBTTagList objects.
Uses a tree-like path format to find the required tag
instead of creating multiple classes for deep-tags.
Object conversion
The Rtag instance extends RtagMirror
to convert
objects between TagBase <-> Object.
By default it's only compatible with regular Java
objects like String, Short, Integer, Double, Float,
Long, Byte, Map and List.
It also convert Byte, Integer and Long arrays as well.
If you want to add "custom object conversion" just
register a properly RtagSerializer
and RtagDeserializer
that aims the specified object that you want to write and read
from tag.
See putSerializer(Class, RtagSerializer)
and putDeserializer(RtagDeserializer)
for details.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Rtag
Rtag
public instance only compatible with regular Java objects.static final Object
Single object that represents a Java unit defined by Mojang.
On versions before 1.14 this object just act like a dummy object.Fields inherited from class com.saicone.rtag.RtagMirror
TAG_BASE, TAG_COMPOUND, TAG_LIST
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Add value to an NBTTagList on specified path inside tag.
Note that empty path returns false because this method is only made for lists inside compounds or lists.
Seeget(Object, Object...)
for path information.boolean
Merge the provided value with NBTTagCompound at provided path using deep method.<T> T
Deprecated.fromTagExact
(Object tag) Deprecated.To get tag value without conversion usegetTagValue(Object)
instead.<T> T
Get value from the specified path inside tag.
The value will be cast to the type are you looking for after conversion.
Path formatGet exact NBTBase value without any conversion, from the specified path inside tag.
Seeget(Object, Object...)
for path information.getExactOrCreate
(Object tag, Object[] path, BiPredicate<Integer, Object[]> listPredicate) Get exact NBTBase value without any conversion, from the specified path inside tag.
Seeget(Object, Object...)
for path information.boolean
getExactOrCreate
(Object tag, Object[] path, BiPredicate<Integer, Object[]> listPredicate, Predicate<Object> predicate) Get and test exact NBTBase value without any conversion, from the specified path inside tag.
Seeget(Object, Object...)
for path information.static Object
Get a globalized registry from Bukkit registry.Deprecated.Rtag
extendsRtagMirror
.getOptional
(Object tag, Object... path) Same hasget(Object, Object...)
but save the value intoOptionalType
.getTagValue
(Object tag) Convert any NBTBase tag to exact regular Java object or custom by deserializer without any cast.boolean
Merge the provided value with NBTTagCompound at provided path.boolean
Move tag from specified path to any path.boolean
Move tag from specified path to any path.Convert any object to NBTBase tag.
This method first check for any serializer and then use the currentRtagMirror
.<T> Rtag
putDeserializer
(RtagDeserializer<T> deserializer) Register anRtagDeserializer
forgetTagValue(Object)
operations.<T> Rtag
putSerializer
(Class<T> type, RtagSerializer<T> serializer) Register anRtagSerializer
fornewTag(Object)
operations.boolean
removeExact
(Object tag, Object key) Remove value from exact NBTTag list or compound.boolean
Set value to specified path inside tag.
Note that empty path returns false because this method is only made for tags inside compounds or lists.
If you want something like "remove", just put a null value.
Seeget(Object, Object...)
for path information.boolean
Set value to exact NBTTag list or compound.Deprecated.To create tag object usenewTag(Object)
instead.Methods inherited from class com.saicone.rtag.RtagMirror
clone, getRtag, setRtag
-
Field Details
-
INSTANCE
Rtag
public instance only compatible with regular Java objects. -
UNIT
Single object that represents a Java unit defined by Mojang.
On versions before 1.14 this object just act like a dummy object.
-
-
Constructor Details
-
Rtag
public Rtag()Create newRtag
instance. -
Rtag
Deprecated.Rtag
extendsRtagMirror
.Create newRtag
instance without use mirror parameter, because the class extendsRtagMirror
itself.- Parameters:
mirror
- Mirror instance.
-
-
Method Details
-
getMinecraftRegistry
Get a globalized registry from Bukkit registry.- Returns:
- A custom registry.
-
getMirror
Deprecated.Rtag
extendsRtagMirror
.Get this object asRtagMirror
instance.- Returns:
- The Rtag itself.
-
putDeserializer
Register anRtagDeserializer
forgetTagValue(Object)
operations.- Type Parameters:
T
- Deserializable object type.- Parameters:
deserializer
- Deserializer instance.- Returns:
- Current
Rtag
instance.
-
putSerializer
Register anRtagSerializer
fornewTag(Object)
operations.- Type Parameters:
T
- Serializable object type.- Parameters:
type
- Serializable object class that match with Serializer.serializer
- Serializer instance.- Returns:
- Current
Rtag
instance.
-
add
Add value to an NBTTagList on specified path inside tag.
Note that empty path returns false because this method is only made for lists inside compounds or lists.
Seeget(Object, Object...)
for path information.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.value
- Value to add.path
- Final list path to add the specified value.- Returns:
- true if value was added.
-
set
Set value to specified path inside tag.
Note that empty path returns false because this method is only made for tags inside compounds or lists.
If you want something like "remove", just put a null value.
Seeget(Object, Object...)
for path information.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.value
- Value to set.path
- Final value path to set.- Returns:
- true if the value was set.
-
setExact
Set value to exact NBTTag list or compound.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.value
- Value to set.key
- Key associated with value.- Returns:
- true if the value was set.
-
merge
Merge the provided value with NBTTagCompound at provided path.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.value
- The value to merge.replace
- True to replace the repeated values inside NBTTagCompound.path
- Final value path to merge into.- Returns:
- true if the value was merged.
-
deepMerge
Merge the provided value with NBTTagCompound at provided path using deep method.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.value
- The value to merge.replace
- True to replace the repeated values inside NBTTagCompound.path
- Final value path to merge into.- Returns:
- true if the value was merged.
-
move
Move tag from specified path to any path.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.from
- Path to get the value.to
- Path to set the value.- Returns:
- true if the value was moved.
-
move
Move tag from specified path to any path.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.from
- Path to get the value.to
- Path to set the value.clear
- True to clear empty paths.- Returns:
- true if the value was moved.
-
removeExact
Remove value from exact NBTTag list or compound.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.key
- Key associated with value.- Returns:
- true if the value is removed (or don't exist).
-
get
Get value from the specified path inside tag.
The value will be cast to the type are you looking for after conversion.
Path formatRtag uses a tree-like format for paths, every object inside path can be
Integer
orString
and will used to obtain the last possible NBTBase instance.
Path like ["normal", "path", "asd"] will look inside the NBTTagCompound for "normal" key, if value assigned for that key is instance of NBTTagCompound will look inside for the next key in path.
If current path key is instance of Integer and the current value that Rtag looking at is instance of NBTTagList, will get list index value for that path key.- Type Parameters:
T
- Object type to cast the value.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.path
- Final value path to get.- Returns:
- The value assigned to specified path, null if not exist or a ClassCastException occurs.
-
getOptional
Same hasget(Object, Object...)
but save the value intoOptionalType
.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.path
- Final value path to get.- Returns:
- The value assigned to specified path has
OptionalType
.
-
getExact
Get exact NBTBase value without any conversion, from the specified path inside tag.
Seeget(Object, Object...)
for path information.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.path
- Final value path to get.- Returns:
- The value assigned to specified path, null if not exist.
-
getExactOrCreate
public Object getExactOrCreate(Object tag, Object[] path, BiPredicate<Integer, Object[]> listPredicate) Get exact NBTBase value without any conversion, from the specified path inside tag.
Seeget(Object, Object...)
for path information.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.path
- Final value path to get.listPredicate
- Predicate to set new NBTTagList if NBTTagCompound doesn't contain key.- Returns:
- The value assigned to specified path or null.
-
getExactOrCreate
public boolean getExactOrCreate(Object tag, Object[] path, BiPredicate<Integer, Object[]> listPredicate, Predicate<Object> predicate) Get and test exact NBTBase value without any conversion, from the specified path inside tag.
Seeget(Object, Object...)
for path information.- Parameters:
tag
- Tag instance, can be NBTTagCompound or NBTTagList.path
- Final value path to get.listPredicate
- Predicate to set new NBTTagList if NBTTagCompound doesn't contain key.predicate
- Consumer that accept non-null value.- Returns:
- true if the value was consumed.
-
getTagValue
Convert any NBTBase tag to exact regular Java object or custom by deserializer without any cast.- Overrides:
getTagValue
in classRtagMirror
- Parameters:
tag
- NBTBase tag.- Returns:
- Converted value or null.
-
newTag
Convert any object to NBTBase tag.
This method first check for any serializer and then use the currentRtagMirror
.- Overrides:
newTag
in classRtagMirror
- Parameters:
object
- Object to convert.- Returns:
- Converted object instance of NBTBase or null.
-
toTag
Deprecated.To create tag object usenewTag(Object)
instead.Convert any object to NBTBase tag.- Parameters:
object
- Object to convert.- Returns:
- NBTBase tag or null.
- See Also:
-
fromTag
Deprecated.Convert any NBTBase tag to regular Java object or custom by deserializer.
This method will cast the object to the type you're looking for.- Type Parameters:
T
- Object type to cast the value.- Parameters:
tag
- NBTBase tag.- Returns:
- Converted value, null if any error occurs.
- See Also:
-
fromTagExact
Deprecated.To get tag value without conversion usegetTagValue(Object)
instead.Convert any NBTBase tag to regular Java object.- Parameters:
tag
- Tag to convert.- Returns:
- Converted object.
- See Also:
-
Rtag
extendsRtagMirror
.