-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructureFile.py
68 lines (53 loc) · 1.95 KB
/
StructureFile.py
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
from pathlib import Path
import nbtlib
from glm import ivec3
from nbtlib import Compound
class StructureFile:
name: str
nbt: Compound
file: bytes
def __init__(
self,
filePath: Path
):
filePath = filePath.with_suffix('.nbt')
self.name = filePath.name
self.nbt = nbtlib.load(filename=filePath, gzipped=True)
with open(filePath, 'rb') as file:
self.file = file.read()
def getBlockAt(self, pos: ivec3) -> ivec3:
for block in self.nbt['blocks']:
if block["pos"][0] == pos.x and block["pos"][1] == pos.y and block["pos"][2] == pos.z:
return block
def getBlockMaterial(self, block):
return self.nbt["palette"][block["state"]]['Name']
def getBlockMaterialAt(self, pos: ivec3):
return self.getBlockMaterial(self.getBlockAt(pos))
# Get block properties (also known as block states: https://minecraft.fandom.com/wiki/Block_states) of a block.
# This may contain information on the orientation of a block or open or closed stated of a door.
def getBlockProperties(self, block) -> dict:
properties = dict()
if "Properties" in self.nbt["palette"][block["state"]].keys():
for key in self.nbt["palette"][block["state"]]["Properties"].keys():
properties[key] = self.nbt["palette"][block["state"]]["Properties"][key]
return properties
def getBlockPropertiesAt(self, pos: ivec3) -> dict:
return self.getBlockProperties(self.getBlockAt(pos))
@property
def sizeX(self) -> int:
return self.nbt["size"][0]
@property
def sizeY(self) -> int:
return self.nbt["size"][1]
@property
def sizeZ(self) -> int:
return self.nbt["size"][2]
@property
def centerPivot(self) -> ivec3:
return ivec3(
self.sizeX // 2,
0,
self.sizeZ // 2
)
def __repr__(self):
return f'{self.name}'