-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave.py
More file actions
86 lines (64 loc) · 2.81 KB
/
save.py
File metadata and controls
86 lines (64 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import nbtlib as nbt
import base36
import chunk
class Save:
def __init__(self, world, path = "save"):
self.world = world
self.path = path
def chunk_position_to_path(self, chunk_position):
x, y, z = chunk_position
chunk_path = '/'.join((
self.path,
base36.dumps(x % 64),
base36.dumps(z % 64),
f"c.{base36.dumps(x)}.{base36.dumps(z)}.dat"
))
return chunk_path
def load_chunk(self, chunk_position):
# load the chunk file
chunk_path = self.chunk_position_to_path(chunk_position)
try: chunk_blocks = nbt.load(chunk_path)["Level"]["Blocks"]
except FileNotFoundError: return
# create chunk and fill it with blocks from the chunk file
self.world.chunks[chunk_position] = chunk.Chunk(self.world, chunk_position)
for x in range(chunk.CHUNK_WIDTH):
for y in range(chunk.CHUNK_HEIGHT):
for z in range(chunk.CHUNK_LENGTH):
self.world.chunks[chunk_position].blocks[x][y][z] = chunk_blocks[
x * chunk.CHUNK_LENGTH * chunk.CHUNK_HEIGHT +
z * chunk.CHUNK_HEIGHT +
y
]
def load(self):
for x in range(-4, 4):
for z in range (-4, 4):
self.load_chunk((x, 0, z))
def save_chunk(self, chunk_position):
x, y, z = chunk_position
chunk_path = self.chunk_position_to_path(chunk_position)
# if chunk cannot be loaded, create a new one
try: chunk_data = nbt.load(chunk_path)
except FileNotFoundError:
chunk_data = nbt.File({"": nbt.Compound({"Level": nbt.Compound()})})
chunk_data["Level"]["xPos"] = x
chunk_data["Level"]["zPos"] = z
# fill the chunk with blocks from our chunk
chunk_blocks = nbt.ByteArray([0] * (chunk.CHUNK_WIDTH * chunk.CHUNK_HEIGHT * chunk.CHUNK_LENGTH))
for x in range(chunk.CHUNK_WIDTH):
for y in range(chunk.CHUNK_HEIGHT):
for z in range(chunk.CHUNK_LENGTH):
chunk_blocks[
x * chunk.CHUNK_LENGTH * chunk.CHUNK_HEIGHT +
z * chunk.CHUNK_HEIGHT +
y
] = self.world.chunks[chunk_position].blocks[x][y][z]
# save the chunk file
chunk_data["Level"]["Blocks"] = chunk_blocks
chunk_data.save(chunk_path, gzipped = True) # save with compression
def save(self):
for chunk_position in self.world.chunks:
if chunk_position[1] != 0: continue
chunk = self.world.chunks[chunk_position]
if chunk.modified:
self.save_chunk(chunk_position)
chunk.modified = False