Class NbtFactory
java.lang.Object
com.comphenix.protocol.wrappers.nbt.NbtFactory
Factory methods for creating NBT elements, lists and compounds.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic NbtCompoundasCompound(NbtBase<?> tag) Attempt to cast this NBT tag as a compund.static NbtList<?> Attempt to cast this NBT tag as a list.static <T> NbtWrapper<T> Get a NBT wrapper from a NBT base.static NbtCompoundLoad a NBT compound from a GZIP compressed file.static Optional<NbtWrapper<?>> fromItemOptional(org.bukkit.inventory.ItemStack stack) Constructs a wrapper for a NBT tag in an ItemStack.static NbtWrapper<?> fromItemTag(org.bukkit.inventory.ItemStack stack) Construct a wrapper for an NBT tag stored (in memory) in an item stack.static <T> NbtWrapper<T> Deprecated.static <T> NbtWrapper<T> Initialize a NBT wrapper with a name.static NbtCompoundfromNMSCompound(Object handle) Retrieve the NBT compound from a given NMS handle.Constructs a NBT tag of type byte.static NbtBase<byte[]> Constructs a NBT tag of type byte array.Constructs a NBT tag of type double.Constructs a NBT tag of type float.Constructs a NBT tag of type int.static NbtBase<int[]> Constructs a NBT tag of type int array.Constructs a NBT tag of type long.Constructs a NBT tag of type short.Constructs a NBT tag of type string.static NbtCompoundofCompound(String name) Construct a new NBT compound wrapper.static NbtCompoundofCompound(String name, Collection<? extends NbtBase<?>> list) Construct a new NBT compound initialized with a given list of NBT values.static <T> NbtList<T> ofList(String name, Collection<? extends T> elements) Construct a NBT list of out a list of values.static <T> NbtList<T> Construct a NBT list of out an array of values.static <T> NbtWrapper<T> Create a new NBT wrapper from a given type.static <T> NbtWrapper<T> Create a new NBT wrapper from a given type.static <T> NbtWrapper<T> Create a new NBT wrapper from a given type.static NbtCompoundreadBlockState(org.bukkit.block.Block block) Retrieve the NBT tile entity that represents the given block.static voidsetItemTag(org.bukkit.inventory.ItemStack stack, NbtCompound compound) Set the NBT compound tag of a given item stack.static voidtoFile(NbtCompound compound, String file) Save a NBT compound to a new compressed file, overwriting any existing files in the process.static voidwriteBlockState(org.bukkit.block.Block target, NbtCompound blockState) Write to the NBT tile entity in the given block.
-
Constructor Details
-
NbtFactory
public NbtFactory()
-
-
Method Details
-
asCompound
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
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
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
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
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
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. UseMinecraftReflection.getBukkitItemStack(Object)- Returns:
- A wrapper for the NBT tag if it exists, an empty Optional if not
-
fromFile
Load a NBT compound from a GZIP compressed file.- Parameters:
file- - the source file.- Returns:
- The compound.
- Throws:
IOException- Unable to load file.
-
toFile
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
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
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.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
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
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
Constructs a NBT tag of type string.- Parameters:
name- - name of the tag.value- - value of the tag.- Returns:
- The constructed NBT tag.
-
of
Constructs a NBT tag of type byte.- Parameters:
name- - name of the tag.value- - value of the tag.- Returns:
- The constructed NBT tag.
-
of
Constructs a NBT tag of type short.- Parameters:
name- - name of the tag.value- - value of the tag.- Returns:
- The constructed NBT tag.
-
of
Constructs a NBT tag of type int.- Parameters:
name- - name of the tag.value- - value of the tag.- Returns:
- The constructed NBT tag.
-
of
Constructs a NBT tag of type long.- Parameters:
name- - name of the tag.value- - value of the tag.- Returns:
- The constructed NBT tag.
-
of
Constructs a NBT tag of type float.- Parameters:
name- - name of the tag.value- - value of the tag.- Returns:
- The constructed NBT tag.
-
of
Constructs a NBT tag of type double.- Parameters:
name- - name of the tag.value- - value of the tag.- Returns:
- The constructed NBT tag.
-
of
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
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
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
Construct a new NBT compound wrapper.- Parameters:
name- - the name of the compound wrapper.- Returns:
- The new wrapped NBT compound.
-
ofList
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
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
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
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
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.
-