Skip to content

Commit

Permalink
Simplify NBT implementation
Browse files Browse the repository at this point in the history
 * Remove the TYPE class variable to rely in the actual type of each tag
 * Change the from_object and to_object to use schemas describing the data instead of using integer ranges and choosing types arbitrarily
  • Loading branch information
LiteApplication committed Apr 30, 2024
1 parent 7ab62db commit 638f6d5
Show file tree
Hide file tree
Showing 3 changed files with 999 additions and 603 deletions.
7 changes: 5 additions & 2 deletions changes/257.feature.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
- Added the `NBTag` to deal with NBT data:
- The `NBTag` class is the base class for all NBT tags and provides the basic functionality to serialize and deserialize NBT data from and to a `Buffer` object.
- The classes `EndNBT`, `ByteNBT`, `ShortNBT`, `IntNBT`, `LongNBT`, `FloatNBT`, `DoubleNBT`, `ByteArrayNBT`, `StringNBT`, `ListNBT`, `CompoundNBT`, `IntArrayNBT`and `LongArrayNBT` were added and correspond to the NBT types described in the [NBT specification](https://wiki.vg/NBT#Specification).
- NBT tags can be created using the `NBTag.from_object()` method, which automatically selects the correct tag type based on the object's type and works recursively for lists and dictionaries.
- The `NBTag.to_object()` method can be used to convert an NBT tag back to a Python object.
- NBT tags can be created using the `NBTag.from_object()` method and a schema that describes the NBT tag structure.
Compound tags are represented as dictionaries, list tags as lists, and primitive tags as their respective Python types.
The implementation allows to add custom classes to the schema to handle custom NBT tags if they inherit the `:class: NBTagConvertible` class.
- The `NBTag.to_object()` method can be used to convert an NBT tag back to a Python object. Use include_schema=True to include the schema in the output, and `include_name=True` to include the name of the tag in the output. In that case the output will be a dictionary with a single key that is the name of the tag and the value is the object representation of the tag.
- The `NBTag.serialize()` can be used to serialize an NBT tag to a new `Buffer` object.
- The `NBTag.deserialize(buffer)` can be used to deserialize an NBT tag from a `Buffer` object.
- If the buffer already exists, the `NBTag.write_to(buffer, with_type=True, with_name=True)` method can be used to write the NBT tag to the buffer (and in that case with the type and name in the right format).
- The `NBTag.read_from(buffer, with_type=True, with_name=True)` method can be used to read an NBT tag from the buffer (and in that case with the type and name in the right format).
- The `NBTag.value` property can be used to get the value of the NBT tag as a Python object.
Loading

0 comments on commit 638f6d5

Please sign in to comment.