Skip to content

Commit

Permalink
Fix issue with shifting causing chunk corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
DonoA committed Jan 5, 2019
1 parent 82582c2 commit d715001
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
31 changes: 27 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,32 @@

with World('A', save_location='/home/dallen/.minecraft/saves') as wrld:
print('World loaded!')
blk = wrld.get_block((106, 95, -252))#(8, 73, 150))
print(blk)
cv = Canvas(wrld)
cv.brush(BlockState(Material.gold_block, {})).square((106, 95, -252), 100)
wrld.get_block((99, 95, -203)).set_state(Material.diamond_block)
wrld.get_block((99, 94, -203)).set_state(Material.coal_block)
wrld.get_block((99, 93, -203)).set_state(Material.gold_block)
wrld.get_block((99, 92, -203)).set_state(Material.iron_block)
wrld.get_block((99, 91, -203)).set_state(Material.glowstone)

wrld.get_block((98, 95, -203)).set_state(Material.diamond_ore)
wrld.get_block((98, 94, -203)).set_state(Material.coal_ore)
wrld.get_block((98, 93, -203)).set_state(Material.gold_ore)
wrld.get_block((98, 92, -203)).set_state(Material.iron_ore)
wrld.get_block((98, 91, -203)).set_state(Material.nether_bricks)

wrld.get_block((100, 95, -203)).set_state(Material.acacia_planks)
wrld.get_block((100, 94, -203)).set_state(Material.birch_planks)
wrld.get_block((100, 93, -203)).set_state(Material.dark_oak_planks)
wrld.get_block((100, 92, -203)).set_state(Material.jungle_planks)
wrld.get_block((100, 91, -203)).set_state(Material.spruce_planks)

wrld.get_block((101, 95, -203)).set_state(Material.stone)
wrld.get_block((101, 94, -203)).set_state(Material.andesite)
wrld.get_block((101, 93, -203)).set_state(Material.gravel)
wrld.get_block((101, 92, -203)).set_state(Material.gray_wool)
wrld.get_block((101, 91, -203)).set_state(Material.black_wool)

# print(blk)
# cv = Canvas(wrld)
# cv.brush(BlockState(Material.gold_block, {})).square((106, 95, -252), 100)

print('Saved!')
28 changes: 15 additions & 13 deletions world.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ def __init__(self, state, block_light, sky_light):
self._state = state
self.block_light = 0
self.sky_light = 0
self._dirty = True
self._dirty = False

def __str__(self):
return f'Block({str(self._state)}, {self.block_light}, {self.sky_light})'

def set_state(self, state):
self._dirty = True
self._state = state
if type(state) is BlockState:
self._state = state
else:
self._state = BlockState(state, {})

def get_state(self):
return self._state.clone()
Expand Down Expand Up @@ -269,25 +272,24 @@ def close(self):
# timestamps[((chunk.xpos % 32) + (chunk.zpos % 32) * 32)] = int(time.time())

loc = locations[((chunk.xpos % 32) + (chunk.zpos % 32) * 32)]
data_len_diff = block_data_len - loc[1]
if data_len_diff != 0:
# chunkNBT.print()
# print('===vs===')
# chunk.raw_nbt.print()
print('Danger: Diff is not 0, shifting required!')
sys.exit(0)
original_sector_length = loc[1]
data_len_diff = block_data_len - original_sector_length
# if data_len_diff != 0:
# print(f'Danger: Diff is {data_len_diff}, shifting required!')

locations[((chunk.xpos % 32) + (chunk.zpos % 32) * 32)][1] = block_data_len

if loc[0] == 0 or loc[1] == 0:
print("Chunk not generated", chunk)
sys.exit(0)

for c_loc in locations:
if c_loc[0] > loc[0]:
c_loc[0] = c_loc[0] + data_len_diff
# Adjust sectors after this one that need their locations recalculated
for i, other_loc in enumerate(locations):
if other_loc[0] > loc[0]:
locations[i][0] = other_loc[0] + data_len_diff

data_in_file[(loc[0] - 8192):(loc[0] + loc[1] - 8192)] = data
header_length = 2*4096
data_in_file[(loc[0] - header_length):(loc[0] + original_sector_length - header_length)] = data
print('Saving', chunk, 'With', {'loc': loc, 'new_len': datalen, 'old_len': chunk.orig_size, 'sector_len': block_data_len})

region.seek(0)
Expand Down

0 comments on commit d715001

Please sign in to comment.