-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMDConverter.py
69 lines (59 loc) · 3.12 KB
/
CMDConverter.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
69
# *-* coding: utf-8 *-*
# Python version: 3.12.6
# Profile: 以通用格式提取出 .mcstructure 文件中的命令方块数据
# License: MIT
# Author: EGGYLAN
# GitHub: https://github.com/eggylan/MCStruct2FuncPy
import json
import nbtlib
from TrimMCStruct import Structure
def CmdConvert(filepath):
structure = nbtlib.load(filepath, byteorder='little')
with open(filepath, "rb") as f:
struct = Structure.load(f)
origin_x, origin_y, origin_z = structure["structure_world_origin"]
command_block_list = []
block_position_data = structure['structure']['palette']['default']['block_position_data']
for block_index, block_data in block_position_data.items():
if 'block_entity_data' in block_data:
block_entity_data = block_data['block_entity_data']
if block_entity_data['id'] == 'CommandBlock':
command = block_entity_data['Command']
relative_x = block_entity_data['x'] - origin_x
relative_y = block_entity_data['y'] - origin_y
relative_z = block_entity_data['z'] - origin_z
isAuto = block_entity_data['auto']
CustomName = block_entity_data['CustomName']
Delay = block_entity_data['TickDelay']
Conditional = block_entity_data['conditionalMode']
ExecuteOnFirstTick = block_entity_data['ExecuteOnFirstTick']
cmdblock = struct.get_block((relative_x, relative_y, relative_z))
cmdblocktype = cmdblock.stringify(with_namespace=False,with_states=False)
cmdblockdirection = cmdblock.states['facing_direction']
command_block_data = {
"CustomName": CustomName, # 悬浮文字
"Command": command, # 命令
"Type": cmdblocktype, # 类型
"x": relative_x,
"y": relative_y,
"z": relative_z,
"Delay": Delay, # 延迟
"isAuto": isAuto, # 红石控制或始终活动
"Conditional": Conditional, # 是否有条件
"ExecuteOnFirstTick": ExecuteOnFirstTick, # 在首次延迟时执行
"Direction": cmdblockdirection # 方向
}
command_block_list.append(command_block_data)
command_block_list_sorted = sorted(command_block_list, key=lambda k: (k['y'], k['z'], k['x']))
command_block_data_json = json.dumps(command_block_list_sorted, indent=4, ensure_ascii=False)
return command_block_data_json
if __name__ == "__main__":
inputpath = input("Enter the path to the mcstructure file: ")
outputpath = input("Enter the path to the output file, or leave blank to use the input file path: ")
if outputpath == "":
outputpath = inputpath[:-12] + ".json"
command_block_data = CmdConvert(inputpath)
with open(outputpath, 'w', encoding="utf-8") as f:
f.write(command_block_data)
print(f"Command block data written to {outputpath}")
input("Press Enter to exit...")