Class NBTUtil

java.lang.Object
com.ranull.graves.util.NBTUtil

public class NBTUtil extends Object
Utility class for handling raw SNBT (Stringified NBT) data on ItemStacks and block entities.

Provides methods to read the full NBT data as SNBT strings and to apply SNBT payloads without direct use of NMS, leveraging the de.tr7zw.nbtapi library.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.bukkit.inventory.ItemStack
    applyRawNBTToItem(org.bukkit.inventory.ItemStack item, String rawSNBT)
    Applies the given SNBT payload to a clone of the specified ItemStack and returns the resulting ItemStack.
    static String
    getBlockRawNBT(org.bukkit.block.Block block)
    Retrieves the SNBT data of the given block's tile-entity, if present.
    static String
    getItemRawNBT(org.bukkit.inventory.ItemStack item)
    Retrieves the full SNBT representation of the given ItemStack, including its id, count, and all nested tag data.
    static void
    placeBlockWithRawNBT(org.bukkit.Location loc, org.bukkit.Material material, String rawSNBT)
    Places a block of the specified Material at the given Location and merges the provided SNBT payload into its tile-entity data.
    static void
    setBlockRawNBT(org.bukkit.block.Block block, String rawSNBT)
    Applies the given SNBT payload to the specified block, replacing its existing tile-entity data.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NBTUtil

      public NBTUtil()
  • Method Details

    • getItemRawNBT

      public static String getItemRawNBT(org.bukkit.inventory.ItemStack item)
      Retrieves the full SNBT representation of the given ItemStack, including its id, count, and all nested tag data.
      Parameters:
      item - the ItemStack to read NBT from; may be null
      Returns:
      a SNBT string containing the item's NBT, or "{}" if the item is null or has no data
    • getBlockRawNBT

      public static String getBlockRawNBT(org.bukkit.block.Block block)
      Retrieves the SNBT data of the given block's tile-entity, if present.
      Parameters:
      block - the Block whose tile-entity NBT should be read; may be null
      Returns:
      a SNBT string of the block-entity's data, or "{}" if the block is null or has no tile-entity
    • setBlockRawNBT

      public static void setBlockRawNBT(org.bukkit.block.Block block, String rawSNBT)
      Applies the given SNBT payload to the specified block, replacing its existing tile-entity data. Does nothing if the block is null.
      Parameters:
      block - the Block whose NBT should be set; must not be null
      rawSNBT - the SNBT string to apply to the block's tile-entity; must be valid SNBT
    • placeBlockWithRawNBT

      public static void placeBlockWithRawNBT(org.bukkit.Location loc, org.bukkit.Material material, String rawSNBT)
      Places a block of the specified Material at the given Location and merges the provided SNBT payload into its tile-entity data. Does nothing if the Location or its world is null.
      Parameters:
      loc - the Location where the block should be placed; must not be null
      material - the Material of the block to place; must not be null
      rawSNBT - the SNBT string to apply to the placed block's tile-entity; must be valid SNBT
    • applyRawNBTToItem

      public static org.bukkit.inventory.ItemStack applyRawNBTToItem(org.bukkit.inventory.ItemStack item, String rawSNBT)
      Applies the given SNBT payload to a clone of the specified ItemStack and returns the resulting ItemStack. Returns null if the input item is null.
      Parameters:
      item - the ItemStack to modify; if null, this method returns null
      rawSNBT - the SNBT string to merge into the item; must be valid SNBT
      Returns:
      a new ItemStack with the SNBT applied, or null if the input item was null