Skip to content

Commit

Permalink
Add Buffer.pack_slot()
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Sep 15, 2017
1 parent 159e8aa commit 78d8c2a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
master
------

- Nothing yet
- Added ``Buffer.pack_slot()`` method.
- Added tests for slot packing and unpacking.

v0.8
----
Expand Down
1 change: 1 addition & 0 deletions docs/data_types/buffers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ a byte string. A reference to the :class:`Buffer` class is available from
.. automethod:: Buffer.pack_varint
.. automethod:: Buffer.pack_uuid
.. automethod:: Buffer.pack_position
.. automethod:: Buffer.pack_slot
.. automethod:: Buffer.pack_nbt
.. automethod:: Buffer.pack_chunk_section
23 changes: 22 additions & 1 deletion quarry/types/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,28 @@ def pack_twos_comp(bits, number):
pack_twos_comp(26, z))))

@classmethod
def pack_nbt(cls, tag):
def pack_slot(cls, id=-1, count=1, damage=0, tag=None):
"""
Packs a slot.
"""

if id == -1:
return cls.pack('h', id)

return cls.pack('hbh', id, count, damage) + cls.pack_nbt(tag)

@classmethod
def pack_nbt(cls, tag=None):
"""
Packs an NBT tag
"""

if tag is None:
# slower but more obvious:
# from quarry.types import nbt
# tag = nbt.TagRoot({})
return "\x00"

return tag.to_bytes()

@classmethod
Expand Down
3 changes: 3 additions & 0 deletions quarry/types/nbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def to_obj(self):
def __repr__(self):
return "%s(%r)" % (type(self).__name__, self.value)

def __cmp__(self, other):
return cmp(self.to_obj(), other.to_obj())


class _DataTag(_Tag):
fmt = None
Expand Down
41 changes: 40 additions & 1 deletion tests/types/test_buffer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from quarry.types.buffer import Buffer, BufferUnderrun
from quarry.types.nbt import *
from quarry.types.uuid import UUID

pack_unpack_vectors = [
Expand Down Expand Up @@ -34,6 +35,33 @@
(2147483647, b"\xFF\xFF\xFF\xFF\x07"),
(-2147483648, b"\x80\x80\x80\x80\x08"),
]
slot_vectors = [
# Empty slot
({'id': -1}, '\xFF\xFF'),

# 20 stone blocks
({'id': 276, 'count': 20, 'damage': 0, 'tag': TagRoot({})},

'\x01\x14' # ID
'\x14' # Count
'\x00\x00' # Damage
'\x00'), # NBT

# Sharpness 4 diamond sword
({'id': 276, 'count': 1, 'damage': 0, 'tag': TagRoot({u'': TagCompound({
u'ench': TagList([
TagCompound({
u'id': TagShort(16),
u'lvl': TagShort(4)})])})})}, # hmm

'\x01\x14' # ID
'\x01' # Count
'\x00\x00' # Damage
'\x0A\x00\x00\x09\x00\x04ench\n\x00\x00\x00\x01' # NBT container start
'\x02\x00\x03lvl\x00\x04' # Enchantment level
'\x02\x00\x02id\x00\x10' # Enchantment type
'\x00\x00'), # NBT container end
]

def test_add():
buffer = Buffer()
Expand Down Expand Up @@ -100,6 +128,13 @@ def test_unpack_uuid():
buffer.add(uuid_vector)
assert buffer.unpack_uuid().to_bytes() == uuid_vector

def test_unpack_slot():
buffer = Buffer()
for value, data in slot_vectors:
buffer.add(data)
assert buffer.unpack_slot() == value
assert len(buffer) == 0

def test_pack():
for fmt, data, values in pack_unpack_vectors:
if not isinstance(values, tuple):
Expand All @@ -120,4 +155,8 @@ def test_pack_varint():
assert Buffer.pack_varint(value, signed=True) == data

def test_pack_uuid():
assert Buffer.pack_uuid(UUID.from_bytes(uuid_vector)) == uuid_vector
assert Buffer.pack_uuid(UUID.from_bytes(uuid_vector)) == uuid_vector

def test_pack_slot():
for value, data in slot_vectors:
assert Buffer.pack_slot(**value) == data

0 comments on commit 78d8c2a

Please sign in to comment.