Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 1.9 chunk unpacking #218

Merged
merged 1 commit into from
Apr 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions spockbot/plugins/tools/smpmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
"""

import array
import struct
from math import floor

from spockbot.mcp.bbuff import BoundBuffer
from spockbot.mcp.datautils import unpack_varint


DIMENSION_NETHER = -0x01
DIMENSION_OVERWOLD = 0x00
Expand Down Expand Up @@ -133,9 +136,46 @@ def set(self, x, z, d):


class ChunkDataShort(ChunkData):
""" A 16x16x16 array for storing block IDs/Metadata. """
""" A 16x16x16 array for storing block IDs and metadata. """
length = 16 * 16 * 16 * 2
ty = 'H'
ty = 'H' # protocol allows larger block types

def unpack(self, buff):
block_bits = buff.read(1)[0]
uses_palette = block_bits > 0

if uses_palette:
palette_len = unpack_varint(buff)
palette = [unpack_varint(buff) for i in range(palette_len)]
else: # use global palette
block_bits = 13

data_longs = unpack_varint(buff)
block_data = [struct.unpack('>Q', buff.read(8))[0]
for i in range(data_longs)]

self.fill()
blocks = self.data
max_value = (1 << block_bits) - 1
for i in range(4096):
start_long = (i * block_bits) // 64
start_offset = (i * block_bits) % 64
end_long = ((i + 1) * block_bits - 1) // 64
if start_long == end_long:
block = (block_data[start_long] >> start_offset) & max_value
else:
end_offset = 64 - start_offset
block = (block_data[start_long] >> start_offset
| block_data[end_long] << end_offset
) & max_value

if uses_palette: # convert to global palette
blocks[i] = palette[block]
else:
blocks[i] = block

def pack(self):
raise NotImplementedError('1.9 block data packing not implemented')


class ChunkDataNibble(ChunkData):
Expand Down