Skip to content

Commit

Permalink
Allow for modifications that change the size of the compressed chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
DonoA committed Aug 21, 2018
1 parent 24cd56e commit d5a4114
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
15 changes: 10 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import sys, world

with world.World("New World", save_location="/home/dallen/snap/minecraft/common/.minecraft/saves") as world:
with world.World("New World", save_location="/home/dallen/snap/minecraft/common/.minecraft/saves") as wrld:
print("World loaded!")
# results = world.get_chunk((6, 6)).find_like("redstone_wall_torch")
# for r in results:
# print(r[0], r[1])
# print((r[0][0] % 16) + (r[0][2] % 16) * 16 + (r[0][1] % 16) * 16 ** 2)
b1 = world.get_block((100, 5, 100))
b2 = world.get_block((100, 4, 99))
b1.set_state(b2.get_state())
print(b1, b2)
ns = world.BlockState("minecraft:air", {})
chnk = wrld.get_chunk((6, 4))
for s in chnk.sections:
sec = chnk.sections[s]
for b in sec.blocks:
b.set_state(ns)

print("Saved!")
25 changes: 16 additions & 9 deletions world.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __str__(self):
class Block:
def __init__(self, state):
self.state = state
self.dirty = False
self.dirty = True

def __str__(self):
return "Block(" + str(self.state) + ")"
Expand All @@ -38,10 +38,11 @@ def get_block(self, block_pos):

def serialize(self):
serial_section = self.raw_section


serial_section.add_child(self._serialize_palette())
serial_section.add_child(self._serialize_blockstates())
dirty = any([b.dirty for b in self.blocks])
if dirty:
self.palette = list(set([b.get_state() for b in self.blocks]))
serial_section.add_child(self._serialize_palette())
serial_section.add_child(self._serialize_blockstates())

return serial_section

Expand Down Expand Up @@ -203,17 +204,23 @@ def close(self):
datalen = len(data)
block_data_len = math.ceil(datalen/4096.0)*4096
data_len_diff = block_data_len - loc[1]
print(data_len_diff)
if data_len_diff != 0:
print(data_len_diff, block_data_len)
print(block_data_len - datalen)
print("diff is not 0, I would stop now")
sys.exit(0)
# sys.exit(0)

# shift file as needed handle new data
region.seek(loc[0])
region.write(datalen.to_bytes(4, byteorder='big', signed=False))
region.write((2).to_bytes(1, byteorder="big", signed=False))
region.write(data)

region.write((0).to_bytes(block_data_len - datalen, byteorder="big", signed=False))

region.write(rest_of_file)
required_padding = (math.ceil(region.tell()/4096.0) * 4096) - region.tell()
region.write((0).to_bytes(required_padding, byteorder="big", signed=False))

# print(datalen, chunk_len, len(strm.get_data()))
# write in the location and length we will be using
for c_loc in locations:
Expand Down Expand Up @@ -259,7 +266,7 @@ def _load_binary_chunk_at(self, region_file, offset):
datalen = int.from_bytes(region_file.read(4), byteorder='big', signed=False)
compr = region_file.read(1)
decompressed = zlib.decompress(region_file.read(datalen))
print(datalen, len(decompressed), len(zlib.compress(decompressed)))
# print(datalen, len(decompressed), len(zlib.compress(decompressed)))
data = nbt.parse_nbt(stream.InputStream(decompressed))
# data.print()
# nstrm = stream.OutputStream()
Expand Down

0 comments on commit d5a4114

Please sign in to comment.