Class NbtSerialization

java.lang.Object
net.glowstone.io.nbt.NbtSerialization

public final class NbtSerialization extends Object
Utility methods for transforming various objects to and from NBT. All strings in this class are subtag names and thus not localizable.
  • Method Details

    • readItem

      public static org.bukkit.inventory.ItemStack readItem(CompoundTag tag)
      Read an item stack in from an NBT tag.

      Returns null if no item exists.

      Parameters:
      tag - The tag to read from.
      Returns:
      The resulting ItemStack, or null.
    • writeItem

      public static CompoundTag writeItem(org.bukkit.inventory.ItemStack stack, int slot)
      Write an item stack to an NBT tag.

      Null stacks produce an empty tag, and if slot is negative it is omitted from the result.

      Parameters:
      stack - The stack to write, or null.
      slot - The slot, or negative to omit.
      Returns:
      The resulting tag.
    • readBlockData

      public static org.bukkit.block.data.BlockData readBlockData(CompoundTag tag)
    • writeBlockData

      public static CompoundTag writeBlockData(org.bukkit.block.data.BlockData blockData)
    • readInventory

      public static org.bukkit.inventory.ItemStack[] readInventory(List<CompoundTag> tagList, int start, int size)
      Read a full inventory (players, chests, etc.) from a compound list.
      Parameters:
      tagList - The list of CompoundTags to read from.
      start - The slot number to consider the inventory's start.
      size - The desired size of the inventory.
      Returns:
      An array with the contents of the inventory.
    • writeInventory

      public static List<CompoundTag> writeInventory(org.bukkit.inventory.ItemStack[] items, int start)
      Write a full inventory (players, chests, etc.) to a compound list.
      Parameters:
      items - An array with the contents of the inventory.
      start - The slot number to consider the inventory's start.
      Returns:
      The list of CompoundTags.
    • readWorld

      public static org.bukkit.World readWorld(GlowServer server, CompoundTag compound)
      Attempt to resolve a world based on the contents of a compound tag.
      Parameters:
      server - The server to look up worlds in.
      compound - The tag to read the world from.
      Returns:
      The world, or null if none could be found.
    • writeWorld

      public static void writeWorld(org.bukkit.World world, CompoundTag compound)
      Save world identifiers (UUID and dimension) to a compound tag for later lookup.
      Parameters:
      world - The world to identify.
      compound - The tag to write to.
    • listTagsToLocation

      public static org.bukkit.Location listTagsToLocation(org.bukkit.World world, CompoundTag tag)
      Read a Location from the "Pos" and "Rotation" children of a tag.

      If "Pos" is absent or invalid, null is returned.

      If "Rotation" is absent or invalid, it is skipped and a location without rotation is returned.

      Parameters:
      world - The world of the location (see readWorld).
      tag - The tag to read from.
      Returns:
      The location, or null.
    • locationToListTags

      public static void locationToListTags(org.bukkit.Location loc, CompoundTag tag)
      Write a Location to the "Pos" and "Rotation" children of a tag.

      Does not save world information, use writeWorld instead.

      Parameters:
      loc - The location to write.
      tag - The tag to write to.
    • listToVector

      public static org.bukkit.util.Vector listToVector(List<Double> list)
      Create a Vector from a list of doubles.

      If the list is invalid, a zero vector is returned.

      Parameters:
      list - The list to read from.
      Returns:
      The Vector.
    • vectorToList

      public static List<Double> vectorToList(org.bukkit.util.Vector vec)
      Create a list of doubles from a Vector.
      Parameters:
      vec - The vector to write.
      Returns:
      The list.
    • namespacedKeyFromString

      public static org.bukkit.NamespacedKey namespacedKeyFromString(String keyRaw)