Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Support static Chunks in EmptyRegions
Browse files Browse the repository at this point in the history
asked by #11
  • Loading branch information
matcool committed Jun 8, 2020
1 parent 45acabc commit 0c886f4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
9 changes: 8 additions & 1 deletion anvil/empty_region.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Union, List, BinaryIO
from .empty_chunk import EmptyChunk
from .chunk import Chunk
from .empty_section import EmptySection
from .block import Block
from .errors import OutOfBoundsCoordinates
Expand Down Expand Up @@ -204,7 +205,13 @@ def save(self, file: Union[str, BinaryIO]=None) -> bytes:
chunks_data.append(None)
continue
chunk_data = BytesIO()
chunk.save().write_file(buffer=chunk_data)
if isinstance(chunk, Chunk):
nbt_data = nbt.NBTFile()
nbt_data.tags.append(nbt.TAG_Int(name='DataVersion', value=chunk.version))
nbt_data.tags.append(chunk.data)
else:
nbt_data = chunk.save()
nbt_data.write_file(buffer=chunk_data)
chunk_data.seek(0)
chunk_data = zlib.compress(chunk_data.read())
chunks_data.append(chunk_data)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='anvil-parser',
version='0.6.3',
version='0.6.4',
author='mat',
description='A Minecraft anvil file format parser',
long_description=long_description,
Expand Down
26 changes: 26 additions & 0 deletions tests/test_mixed_chunk_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import context as _
from anvil import Chunk, EmptyChunk, EmptyRegion, Block, Region
import random

def test_mixed_chunk_types():
colors = ['red', 'orange', 'yellow', 'green']

region = EmptyRegion(0, 0)

chunk = EmptyChunk(0, 0)
empty_chunk = EmptyChunk(1, 0)
for i, color in enumerate(colors):
chunk.set_block(Block(color), i, 0, 0)
empty_chunk.set_block(Block(color), i, 0, 0)

chunk = Chunk(chunk.save())

region.add_chunk(chunk)
region.add_chunk(empty_chunk)

region = Region(region.save())

for i in range(2):
chunk = region.get_chunk(i, 0)
for i, color in enumerate(colors):
assert chunk.get_block(i, 0, 0).id == color

0 comments on commit 0c886f4

Please sign in to comment.