Class NbtFactory

java.lang.Object
com.comphenix.protocol.wrappers.nbt.NbtFactory

public class NbtFactory extends Object
Factory methods for creating NBT elements, lists and compounds.
  • Constructor Details

    • NbtFactory

      public NbtFactory()
  • Method Details

    • asCompound

      public static NbtCompound asCompound(NbtBase<?> tag)
      Attempt to cast this NBT tag as a compund.
      Parameters:
      tag - - the NBT tag to cast.
      Returns:
      This instance as a compound.
      Throws:
      UnsupportedOperationException - If this is not a compound.
    • asList

      public static NbtList<?> asList(NbtBase<?> tag)
      Attempt to cast this NBT tag as a list.
      Parameters:
      tag - - the NBT tag to cast.
      Returns:
      This instance as a list.
      Throws:
      UnsupportedOperationException - If this is not a list.
    • fromBase

      public static <T> NbtWrapper<T> fromBase(NbtBase<T> base)
      Get a NBT wrapper from a NBT base.

      This may clone the content if the NbtBase is not a NbtWrapper.

      Type Parameters:
      T - Type
      Parameters:
      base - - the base class.
      Returns:
      A NBT wrapper.
    • setItemTag

      public static void setItemTag(org.bukkit.inventory.ItemStack stack, NbtCompound compound)
      Set the NBT compound tag of a given item stack.

      The item stack must be a wrapper for a CraftItemStack. Use MinecraftReflection.getBukkitItemStack(Object) if not.

      Parameters:
      stack - - the item stack, cannot be air.
      compound - - the new NBT compound, or NULL to remove it.
      Throws:
      IllegalArgumentException - If the stack is not a CraftItemStack, or it represents air.
    • fromItemTag

      public static NbtWrapper<?> fromItemTag(org.bukkit.inventory.ItemStack stack)
      Construct a wrapper for an NBT tag stored (in memory) in an item stack. This is where auxiliary data such as enchanting, name and lore is stored. It doesn't include the items material, damage value or count.

      The item stack must be a wrapper for a CraftItemStack. Use MinecraftReflection.getBukkitItemStack(Object) if not.

      Parameters:
      stack - - the item stack.
      Returns:
      A wrapper for its NBT tag.
    • fromItemOptional

      public static Optional<NbtWrapper<?>> fromItemOptional(org.bukkit.inventory.ItemStack stack)
      Constructs a wrapper for a NBT tag in an ItemStack. This is where auxiliary data such as enchantments, name, and lore is stored. It doesn't include the material, damage value, or stack size.

      This differs from fromItemTag(ItemStack) in that the tag is not created if it doesn't already exist.

      Parameters:
      stack - the ItemStack. Must be a CraftItemStack. Use MinecraftReflection.getBukkitItemStack(Object)
      Returns:
      A wrapper for the NBT tag if it exists, an empty Optional if not
    • fromFile

      public static NbtCompound fromFile(String file) throws IOException
      Load a NBT compound from a GZIP compressed file.
      Parameters:
      file - - the source file.
      Returns:
      The compound.
      Throws:
      IOException - Unable to load file.
    • toFile

      public static void toFile(NbtCompound compound, String file) throws IOException
      Save a NBT compound to a new compressed file, overwriting any existing files in the process.
      Parameters:
      compound - - the compound to save.
      file - - the destination file.
      Throws:
      IOException - Unable to save compound.
    • readBlockState

      public static NbtCompound readBlockState(org.bukkit.block.Block block)
      Retrieve the NBT tile entity that represents the given block.
      Parameters:
      block - - the block.
      Returns:
      The NBT compound, or NULL if the state doesn't have a tile entity.
    • writeBlockState

      public static void writeBlockState(org.bukkit.block.Block target, NbtCompound blockState)
      Write to the NBT tile entity in the given block.
      Parameters:
      target - - the target block.
      blockState - - the new tile entity.
      Throws:
      IllegalArgumentException - If the block doesn't contain a tile entity.
    • fromNMS

      @Deprecated public static <T> NbtWrapper<T> fromNMS(Object handle)
      Deprecated.
      Initialize a NBT wrapper.

      Use fromNMS(Object, String) instead.

      Type Parameters:
      T - Type
      Parameters:
      handle - - the underlying net.minecraft.server object to wrap.
      Returns:
      A NBT wrapper.
    • fromNMS

      public static <T> NbtWrapper<T> fromNMS(Object handle, String name)
      Initialize a NBT wrapper with a name.
      Type Parameters:
      T - Type
      Parameters:
      handle - - the underlying net.minecraft.server object to wrap.
      name - - the name of the tag, or NULL if not valid.
      Returns:
      A NBT wrapper.
    • fromNMSCompound

      public static NbtCompound fromNMSCompound(@Nonnull Object handle)
      Retrieve the NBT compound from a given NMS handle.
      Parameters:
      handle - - the underlying net.minecraft.server object to wrap.
      Returns:
      A NBT compound wrapper
    • of

      public static NbtBase<String> of(String name, String value)
      Constructs a NBT tag of type string.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • of

      public static NbtBase<Byte> of(String name, byte value)
      Constructs a NBT tag of type byte.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • of

      public static NbtBase<Short> of(String name, short value)
      Constructs a NBT tag of type short.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • of

      public static NbtBase<Integer> of(String name, int value)
      Constructs a NBT tag of type int.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • of

      public static NbtBase<Long> of(String name, long value)
      Constructs a NBT tag of type long.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • of

      public static NbtBase<Float> of(String name, float value)
      Constructs a NBT tag of type float.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • of

      public static NbtBase<Double> of(String name, double value)
      Constructs a NBT tag of type double.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • of

      public static NbtBase<byte[]> of(String name, byte[] value)
      Constructs a NBT tag of type byte array.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • of

      public static NbtBase<int[]> of(String name, int[] value)
      Constructs a NBT tag of type int array.
      Parameters:
      name - - name of the tag.
      value - - value of the tag.
      Returns:
      The constructed NBT tag.
    • ofCompound

      public static NbtCompound ofCompound(String name, Collection<? extends NbtBase<?>> list)
      Construct a new NBT compound initialized with a given list of NBT values.
      Parameters:
      name - - the name of the compound wrapper.
      list - - the list of elements to add.
      Returns:
      The new wrapped NBT compound.
    • ofCompound

      public static NbtCompound ofCompound(String name)
      Construct a new NBT compound wrapper.
      Parameters:
      name - - the name of the compound wrapper.
      Returns:
      The new wrapped NBT compound.
    • ofList

      @SafeVarargs public static <T> NbtList<T> ofList(String name, T... elements)
      Construct a NBT list of out an array of values.
      Type Parameters:
      T - Type
      Parameters:
      name - - name of this list.
      elements - - elements to add.
      Returns:
      The new filled NBT list.
    • ofList

      public static <T> NbtList<T> ofList(String name, Collection<? extends T> elements)
      Construct a NBT list of out a list of values.
      Type Parameters:
      T - Type
      Parameters:
      name - - name of this list.
      elements - - elements to add.
      Returns:
      The new filled NBT list.
    • ofWrapper

      public static <T> NbtWrapper<T> ofWrapper(NbtType type, String name)
      Create a new NBT wrapper from a given type.
      Type Parameters:
      T - Type
      Parameters:
      type - - the NBT type.
      name - - the name of the NBT tag.
      Returns:
      The new wrapped NBT tag.
      Throws:
      FieldAccessException - If we're unable to create the underlying tag.
    • ofWrapper

      public static <T> NbtWrapper<T> ofWrapper(NbtType type, String name, T value)
      Create a new NBT wrapper from a given type.
      Type Parameters:
      T - Type
      Parameters:
      type - - the NBT type.
      name - - the name of the NBT tag.
      value - - the value of the new tag.
      Returns:
      The new wrapped NBT tag.
      Throws:
      FieldAccessException - If we're unable to create the underlying tag.
    • ofWrapper

      public static <T> NbtWrapper<T> ofWrapper(Class<?> type, String name, T value)
      Create a new NBT wrapper from a given type.
      Type Parameters:
      T - Type
      Parameters:
      type - - type of the NBT value.
      name - - the name of the NBT tag.
      value - - the value of the new tag.
      Returns:
      The new wrapped NBT tag.
      Throws:
      FieldAccessException - If we're unable to create the underlying tag.
      IllegalArgumentException - If the given class type is not valid NBT.