Class Rtag


public class Rtag extends RtagMirror

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.

Other Objects

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 Details

    • INSTANCE

      public static final Rtag INSTANCE
      Rtag public instance only compatible with regular Java objects.
    • UNIT

      @Experimental public static final Object 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

  • Method Details

    • getMinecraftRegistry

      @Experimental public static Object getMinecraftRegistry()
      Get a globalized registry from Bukkit registry.
      Returns:
      A custom registry.
    • getMirror

      Deprecated.
      Rtag extends RtagMirror.
      Get this object as RtagMirror instance.
      Returns:
      The Rtag itself.
    • putDeserializer

      public <T> Rtag putDeserializer(RtagDeserializer<T> deserializer)
      Register an RtagDeserializer for getTagValue(Object) operations.
      Type Parameters:
      T - Deserializable object type.
      Parameters:
      deserializer - Deserializer instance.
      Returns:
      Current Rtag instance.
    • putSerializer

      public <T> Rtag putSerializer(Class<T> type, RtagSerializer<T> serializer)
      Register an RtagSerializer for newTag(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

      public boolean add(Object tag, Object value, Object... path)
      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.
      See get(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

      public boolean set(Object tag, Object value, Object... path)
      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.
      See get(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

      public boolean setExact(Object tag, Object value, Object key)
      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

      public boolean merge(Object tag, Object value, boolean replace, Object... path)
      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

      public boolean deepMerge(Object tag, Object value, boolean replace, Object... path)
      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

      public boolean move(Object tag, Object[] from, Object[] to)
      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

      public boolean move(Object tag, Object[] from, Object[] to, boolean clear)
      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

      public boolean removeExact(Object tag, Object key)
      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

      public <T> T get(Object tag, Object... path)
      Get value from the specified path inside tag.
      The value will be cast to the type are you looking for after conversion.

      Path format

      Rtag uses a tree-like format for paths, every object inside path can be Integer or String 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

      public OptionalType getOptional(Object tag, Object... path)
      Same has get(Object, Object...) but save the value into OptionalType.
      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

      public Object getExact(Object tag, Object... path)
      Get exact NBTBase value without any conversion, from the specified path inside tag.
      See get(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.
      See get(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.
      See get(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

      public Object getTagValue(Object tag)
      Convert any NBTBase tag to exact regular Java object or custom by deserializer without any cast.
      Overrides:
      getTagValue in class RtagMirror
      Parameters:
      tag - NBTBase tag.
      Returns:
      Converted value or null.
    • newTag

      public Object newTag(Object object)
      Convert any object to NBTBase tag.
      This method first check for any serializer and then use the current RtagMirror.
      Overrides:
      newTag in class RtagMirror
      Parameters:
      object - Object to convert.
      Returns:
      Converted object instance of NBTBase or null.
    • toTag

      @ScheduledForRemoval(inVersion="1.6.0") @Deprecated public Object toTag(Object object)
      Deprecated.
      To create tag object use newTag(Object) instead.
      Convert any object to NBTBase tag.
      Parameters:
      object - Object to convert.
      Returns:
      NBTBase tag or null.
      See Also:
    • fromTag

      @ScheduledForRemoval(inVersion="1.6.0") @Deprecated public <T> T fromTag(Object tag)
      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

      @ScheduledForRemoval(inVersion="1.6.0") @Deprecated public Object fromTagExact(Object tag)
      Deprecated.
      To get tag value without conversion use getTagValue(Object) instead.
      Convert any NBTBase tag to regular Java object.
      Parameters:
      tag - Tag to convert.
      Returns:
      Converted object.
      See Also: