Skip to content

Commit 8e5b521

Browse files
Refactor code functions
Refactor the code functions to interact directly with the NBT classes instead of a weird format
1 parent ccc1435 commit 8e5b521

20 files changed

+429
-445
lines changed

PyMCTranslate/build_number.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
367
1+
368
Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
from amulet_nbt import CompoundTag, ListTag, IntTag
2+
13
def main(nbt):
2-
if (
3-
nbt[0] == "compound"
4-
and "Patterns" in nbt[1]
5-
and nbt[1]["Patterns"][0] == "list"
6-
):
7-
return [
8-
[
9-
"",
10-
"compound",
11-
[("utags", "compound"), ("Patterns", "list"), (index, "compound")],
12-
"Color",
13-
["int", 15 - pattern[1]["Color"][1]],
14-
]
15-
for index, pattern in enumerate(nbt[1]["Patterns"][1])
16-
if pattern[0] == "compound"
17-
and "Color" in pattern[1]
18-
and pattern[1]["Color"][0] == "int"
19-
]
20-
else:
4+
if not isinstance(nbt, CompoundTag):
215
return []
6+
patterns = nbt.get("Patterns")
7+
if not isinstance(patterns, ListTag):
8+
return []
9+
10+
tags = []
11+
index = 0
12+
for pattern in patterns:
13+
if not isinstance(pattern, CompoundTag):
14+
continue
15+
colour = pattern.get("Color")
16+
if not isinstance(colour, IntTag):
17+
continue
18+
tags.append([
19+
"",
20+
"compound",
21+
[("utags", "compound"), ("Patterns", "list"), (index, "compound")],
22+
"Color",
23+
IntTag(15 - colour.py_int),
24+
])
25+
index += 1
26+
return tags
Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
from amulet_nbt import CompoundTag, ListTag, IntTag
2+
3+
14
def main(nbt):
2-
if (
3-
nbt[0] == "compound"
4-
and "utags" in nbt[1]
5-
and nbt[1]["utags"][0] == "compound"
6-
and "Patterns" in nbt[1]["utags"][1]
7-
and nbt[1]["utags"][1]["Patterns"][0] == "list"
8-
):
9-
return [
10-
[
11-
"",
12-
"compound",
13-
[("Patterns", "list"), (index, "compound")],
14-
"Color",
15-
["int", 15 - pattern[1]["Color"][1]],
16-
]
17-
for index, pattern in enumerate(nbt[1]["utags"][1]["Patterns"][1])
18-
if pattern[0] == "compound"
19-
and "Color" in pattern[1]
20-
and pattern[1]["Color"][0] == "int"
21-
]
22-
else:
5+
if not isinstance(nbt, CompoundTag):
236
return []
7+
utags = nbt.get("utags")
8+
if not isinstance(utags, CompoundTag):
9+
return []
10+
patterns = utags.get("Patterns")
11+
if not isinstance(patterns, ListTag):
12+
return []
13+
tags = []
14+
index = 0
15+
for pattern in patterns:
16+
if not isinstance(pattern, CompoundTag):
17+
continue
18+
colour = pattern.get("Color")
19+
if not isinstance(colour, IntTag):
20+
continue
21+
tags.append([
22+
"",
23+
"compound",
24+
[("Patterns", "list"), (index, "compound")],
25+
"Color",
26+
IntTag(15 - colour.py_int),
27+
])
28+
index += 1
29+
return tags
Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
def main(nbt, properties, location):
2-
if (
3-
nbt[0] == "compound"
4-
and "pairlead" in nbt[1]
5-
and nbt[1]["pairlead"][0] == "byte"
6-
and nbt[1]["pairlead"][1] == 1
7-
and "pairx" in nbt[1]
8-
and nbt[1]["pairx"][0] == "int"
9-
and "pairz" in nbt[1]
10-
and nbt[1]["pairz"][0] == "int"
11-
and "facing_direction" in properties
12-
):
13-
dx = nbt[1]["pairx"][1] - location[0]
14-
dz = nbt[1]["pairz"][1] - location[2]
15-
if properties["facing_direction"].py_int == 2: # north
16-
if dz == 0:
17-
if dx == -1:
18-
return {"connection": "left"}
19-
elif properties["facing_direction"].py_int == 3: # south
20-
if dz == 0:
21-
if dx == 1:
22-
return {"connection": "left"}
23-
elif properties["facing_direction"].py_int == 4: # west
24-
if dx == 0:
25-
if dz == 1:
26-
return {"connection": "left"}
27-
elif properties["facing_direction"].py_int == 5: # east
28-
if dx == 0:
29-
if dz == -1:
30-
return {"connection": "left"}
1+
from typing import Union
2+
3+
from amulet_nbt import CompoundTag, ByteTag, ShortTag, IntTag, LongTag, StringTag
4+
5+
6+
def main(nbt, properties: dict[str, Union[ByteTag, ShortTag, IntTag, LongTag, StringTag]], location: tuple[int, int, int]) -> dict[str, str]:
7+
if not isinstance(nbt, CompoundTag):
8+
return {}
9+
facing_direction = properties.get("facing_direction")
10+
if not isinstance(facing_direction, IntTag):
11+
return {}
12+
pairlead = nbt.get("pairlead")
13+
if isinstance(pairlead, ByteTag) and pairlead.py_int == 1:
14+
pair_x = nbt.get("pairx")
15+
if not isinstance(pair_x, IntTag):
16+
return {}
17+
pair_z = nbt.get("pairz")
18+
if not isinstance(pair_z, IntTag):
19+
return {}
20+
21+
dx = pair_x.py_int - location[0]
22+
dz = pair_z.py_int - location[2]
23+
if facing_direction.py_int == 2: # north
24+
if dz == 0 and dx == -1:
25+
return {"connection": "left"}
26+
elif facing_direction.py_int == 3: # south
27+
if dz == 0 and dx == 1:
28+
return {"connection": "left"}
29+
elif facing_direction.py_int == 4: # west
30+
if dx == 0 and dz == 1:
31+
return {"connection": "left"}
32+
elif facing_direction.py_int == 5: # east
33+
if dx == 0 and dz == -1:
34+
return {"connection": "left"}
3135
return {}
Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
def main(nbt, properties, location):
2-
if (
3-
nbt[0] == "compound"
4-
and "pairlead" in nbt[1]
5-
and nbt[1]["pairlead"][0] == "byte"
6-
and nbt[1]["pairlead"][1] == 1
7-
and "pairx" in nbt[1]
8-
and nbt[1]["pairx"][0] == "int"
9-
and "pairz" in nbt[1]
10-
and nbt[1]["pairz"][0] == "int"
11-
and "minecraft:cardinal_direction" in properties
12-
):
13-
dx = nbt[1]["pairx"][1] - location[0]
14-
dz = nbt[1]["pairz"][1] - location[2]
15-
if properties["minecraft:cardinal_direction"].py_str == "north": # north
16-
if dz == 0:
17-
if dx == -1:
18-
return {"connection": "left"}
19-
elif properties["minecraft:cardinal_direction"].py_str == "south": # south
20-
if dz == 0:
21-
if dx == 1:
22-
return {"connection": "left"}
23-
elif properties["minecraft:cardinal_direction"].py_str == "west": # west
24-
if dx == 0:
25-
if dz == 1:
26-
return {"connection": "left"}
27-
elif properties["minecraft:cardinal_direction"].py_str == "east": # east
28-
if dx == 0:
29-
if dz == -1:
30-
return {"connection": "left"}
1+
from typing import Union
2+
3+
from amulet_nbt import CompoundTag, ByteTag, ShortTag, IntTag, LongTag, StringTag
4+
5+
6+
def main(nbt, properties: dict[str, Union[ByteTag, ShortTag, IntTag, LongTag, StringTag]], location: tuple[int, int, int]) -> dict[str, str]:
7+
if not isinstance(nbt, CompoundTag):
8+
return {}
9+
facing_direction = properties.get("minecraft:cardinal_direction")
10+
if not isinstance(facing_direction, StringTag):
11+
return {}
12+
pairlead = nbt.get("pairlead")
13+
if isinstance(pairlead, ByteTag) and pairlead.py_int == 1:
14+
pair_x = nbt.get("pairx")
15+
if not isinstance(pair_x, IntTag):
16+
return {}
17+
pair_z = nbt.get("pairz")
18+
if not isinstance(pair_z, IntTag):
19+
return {}
20+
21+
dx = pair_x.py_int - location[0]
22+
dz = pair_z.py_int - location[2]
23+
if facing_direction.py_str == "north": # north
24+
if dz == 0 and dx == -1:
25+
return {"connection": "left"}
26+
elif facing_direction.py_str == "south": # south
27+
if dz == 0 and dx == 1:
28+
return {"connection": "left"}
29+
elif facing_direction.py_str == "west": # west
30+
if dx == 0 and dz == 1:
31+
return {"connection": "left"}
32+
elif facing_direction.py_str == "east": # east
33+
if dx == 0 and dz == -1:
34+
return {"connection": "left"}
3135
return {}
Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
def main(nbt, properties, location):
2-
if (
3-
nbt[0] == "compound"
4-
and "pairlead" in nbt[1]
5-
and nbt[1]["pairlead"][0] == "byte"
6-
and nbt[1]["pairlead"][1] == 1
7-
and "pairx" in nbt[1]
8-
and nbt[1]["pairx"][0] == "int"
9-
and "pairz" in nbt[1]
10-
and nbt[1]["pairz"][0] == "int"
11-
and "facing_direction" in properties
12-
):
13-
dx = nbt[1]["pairx"][1] - location[0]
14-
dz = nbt[1]["pairz"][1] - location[2]
15-
if properties["facing_direction"].py_int == 2: # north
16-
if dz == 0:
17-
if dx == 1:
18-
return {"connection": "right"}
19-
elif properties["facing_direction"].py_int == 3: # south
20-
if dz == 0:
21-
if dx == -1:
22-
return {"connection": "right"}
23-
elif properties["facing_direction"].py_int == 4: # west
24-
if dx == 0:
25-
if dz == -1:
26-
return {"connection": "right"}
27-
elif properties["facing_direction"].py_int == 5: # east
28-
if dx == 0:
29-
if dz == 1:
30-
return {"connection": "right"}
1+
from typing import Union
2+
3+
from amulet_nbt import CompoundTag, ByteTag, ShortTag, IntTag, LongTag, StringTag
4+
5+
6+
def main(nbt, properties: dict[str, Union[ByteTag, ShortTag, IntTag, LongTag, StringTag]], location: tuple[int, int, int]) -> dict[str, str]:
7+
if not isinstance(nbt, CompoundTag):
8+
return {}
9+
facing_direction = properties.get("facing_direction")
10+
if not isinstance(facing_direction, IntTag):
11+
return {}
12+
pairlead = nbt.get("pairlead")
13+
if isinstance(pairlead, ByteTag) and pairlead.py_int == 1:
14+
pair_x = nbt.get("pairx")
15+
if not isinstance(pair_x, IntTag):
16+
return {}
17+
pair_z = nbt.get("pairz")
18+
if not isinstance(pair_z, IntTag):
19+
return {}
20+
21+
dx = pair_x.py_int - location[0]
22+
dz = pair_z.py_int - location[2]
23+
if facing_direction.py_int == 2: # north
24+
if dz == 0 and dx == 1:
25+
return {"connection": "right"}
26+
elif facing_direction.py_int == 3: # south
27+
if dz == 0 and dx == -1:
28+
return {"connection": "right"}
29+
elif facing_direction.py_int == 4: # west
30+
if dx == 0 and dz == -1:
31+
return {"connection": "right"}
32+
elif facing_direction.py_int == 5: # east
33+
if dx == 0 and dz == 1:
34+
return {"connection": "right"}
3135
return {}
Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
def main(nbt, properties, location):
2-
if (
3-
nbt[0] == "compound"
4-
and "pairlead" in nbt[1]
5-
and nbt[1]["pairlead"][0] == "byte"
6-
and nbt[1]["pairlead"][1] == 1
7-
and "pairx" in nbt[1]
8-
and nbt[1]["pairx"][0] == "int"
9-
and "pairz" in nbt[1]
10-
and nbt[1]["pairz"][0] == "int"
11-
and "minecraft:cardinal_direction" in properties
12-
):
13-
dx = nbt[1]["pairx"][1] - location[0]
14-
dz = nbt[1]["pairz"][1] - location[2]
15-
if properties["minecraft:cardinal_direction"].py_str == "north": # north
16-
if dz == 0:
17-
if dx == 1:
18-
return {"connection": "right"}
19-
elif properties["minecraft:cardinal_direction"].py_str == "south": # south
20-
if dz == 0:
21-
if dx == -1:
22-
return {"connection": "right"}
23-
elif properties["minecraft:cardinal_direction"].py_str == "west": # west
24-
if dx == 0:
25-
if dz == -1:
26-
return {"connection": "right"}
27-
elif properties["minecraft:cardinal_direction"].py_str == "east": # east
28-
if dx == 0:
29-
if dz == 1:
30-
return {"connection": "right"}
1+
from typing import Union
2+
3+
from amulet_nbt import CompoundTag, ByteTag, ShortTag, IntTag, LongTag, StringTag
4+
5+
6+
def main(nbt, properties: dict[str, Union[ByteTag, ShortTag, IntTag, LongTag, StringTag]], location: tuple[int, int, int]) -> dict[str, str]:
7+
if not isinstance(nbt, CompoundTag):
8+
return {}
9+
facing_direction = properties.get("minecraft:cardinal_direction")
10+
if not isinstance(facing_direction, StringTag):
11+
return {}
12+
pairlead = nbt.get("pairlead")
13+
if isinstance(pairlead, ByteTag) and pairlead.py_int == 1:
14+
pair_x = nbt.get("pairx")
15+
if not isinstance(pair_x, IntTag):
16+
return {}
17+
pair_z = nbt.get("pairz")
18+
if not isinstance(pair_z, IntTag):
19+
return {}
20+
21+
dx = pair_x.py_int - location[0]
22+
dz = pair_z.py_int - location[2]
23+
if facing_direction.py_str == "north": # north
24+
if dz == 0 and dx == 1:
25+
return {"connection": "right"}
26+
elif facing_direction.py_str == "south": # south
27+
if dz == 0 and dx == -1:
28+
return {"connection": "right"}
29+
elif facing_direction.py_str == "west": # west
30+
if dx == 0 and dz == -1:
31+
return {"connection": "right"}
32+
elif facing_direction.py_str == "east": # east
33+
if dx == 0 and dz == 1:
34+
return {"connection": "right"}
3135
return {}

0 commit comments

Comments
 (0)