-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
293 lines (276 loc) · 11 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import pynbs
from amulet_nbt import CompoundTag as Compound, ListTag as List, IntTag as Int, StringTag as String, ByteTag as Byte
from os.path import splitext, splitdrive
def wait_input(message, allow_empty=True, only_number=False, only_string=False, out_lowercase=False, strip="both",
number_range=False):
while True:
temp = input(message)
if not temp and allow_empty:
return temp
if only_number:
try:
int(temp)
except ValueError:
pass
else:
if number_range:
if number_range[0] <= int(temp) <= number_range[-1]:
return temp
else:
return temp
elif only_string and temp.isalpha():
if out_lowercase:
temp = temp.lower()
if strip == "both":
temp = temp.strip()
elif strip == "left":
temp = temp.lstrip()
elif strip == "right":
temp = temp.rstrip()
else:
raise Exception("无效的类型 %s" % strip)
return temp
elif not only_number and not only_string:
return temp
print("输入不合规!%s" % ("请输入数字!" if only_number else "请仅输入字母!"))
real_keys = {}
for i in range(9):
real_keys[i] = ['', '0.000000']
for i in range(9, 33):
real_keys[i] = ['_-1', "%.6f" % 2 ** ((-12 + i - 9) / 12)]
for i in range(33, 58):
real_keys[i] = ['', "%.6f" % 2 ** ((-12 + i - 33) / 12)]
for i in range(58, 82):
real_keys[i] = ['_1', "%.6f" % 2 ** ((-12 + i - 57) / 12)]
for i in range(82, 88):
real_keys[i] = ['', '0.000000']
instruments = {
0: "harp",
1: "bass",
2: "basedrum",
3: "snare",
4: "hat",
5: "guitar",
6: "flute",
7: "bell",
8: "chime",
9: "xylophone",
10: "iron_xylophone",
11: "cow_bell",
12: "didgeridoo",
13: "bit",
14: "banjo",
15: "pling"
}
for k, v in instruments.items():
instruments[k] = "minecraft:block.note_block." + v
structure = Compound({
'size': List([Int(2), Int(2), Int(2)]),
'entities': List([]),
'blocks': List([]),
'palette': List([
Compound({
'Name': String("minecraft:air") # id: 0
}),
Compound({
'Name': String("minecraft:stone") # id: 1
}),
Compound({
'Name': String("minecraft:repeater"), # id: 2
'Properties': Compound({
'delay': String("1"),
'facing': String("west"),
'powered': String("false"),
'locked': String("false")
})
}),
Compound({
'Name': String("minecraft:command_block"), # id: 3
'Properties': Compound({
'facing': String("east")
})
}),
Compound({
'Name': String("minecraft:oak_sign"), # id: 4
'Properties': Compound({
'rotation': String("4")
})
}),
Compound({
'Name': String("minecraft:stone_button"), # id: 5
'Properties': Compound({
'facing': String("west"),
'face': String("wall")
})
}),
Compound({
'Name': String("minecraft:redstone_lamp") # id: 6
})
])
})
filename = input("请输入文件路径: ")
nbs = pynbs.read(filename)
config_height = wait_input("请输入玩家悬浮高度(留空默认,默认为10): ", only_number=True)
if not config_height:
config_height = 10
config_offset = wait_input("请输入玩家相对于当前播放进度位置的偏移格数(留空默认,默认为-5): ", only_number=True)
if not config_offset:
config_offset = -5
config_play_location = wait_input("请选择播放时玩家位置:\n[1] 位于整个谱子正中间的上方\n[2] 位于最长轨道的上方\n> ",
only_number=True, number_range=[1, 2])
print("载入NBS内自定义音色")
for i in nbs.instruments:
instruments[16 + i.id] = i.file
print("载入NBS轨道...")
meta = {} # layer, note
for x in nbs.layers:
meta[x.id] = {'volume': x.volume, 'notes': []}
_progress_note = 0
for note in nbs.notes:
_progress_note += 1
print("载入NBS音符... %d/%d" % (_progress_note, len(nbs.notes)), end="\r")
meta[note.layer]['notes'].append({'tick': note.tick, 'instrument': note.instrument, 'key': note.key,
'volume': note.velocity * meta[note.layer]['volume'] / 10000})
print("\n删除空轨道...")
index = 0
cleaned = {}
for k, v in meta.items():
if v['notes']:
cleaned[index] = v
index += 1
song = {} # {layer: [tick for note]}
for layer_id, value in cleaned.items():
song[layer_id] = []
if value['notes']:
for i in range(max([x['tick'] for x in value['notes']]) + 1):
song[layer_id].append(None)
for note in value['notes']:
song[layer_id][note['tick']] = {'key': note['key'], 'instrument': note['instrument'],
'volume': note['volume']}
_progress_zone_setup = 6
_progress_zone_setup_max = len(cleaned.keys()) * 2 * 6 * (nbs.header.song_length + 1) * 2
zone = [] # z y x, z//2代表轨道
for z in range(len(cleaned.keys()) * 2):
zone.append([[], [], [], [], [], []])
for x in range((nbs.header.song_length + 1) * 2):
print("初始化方块矩阵...%d/%d" % (_progress_zone_setup, _progress_zone_setup_max), end="\r")
if z % 2 == 0:
if song.get(z // 2, False) and x // 2 < len(song[z // 2]):
zone[z][0].append({'state': 1})
_progress_zone_setup += 1
if x % 2 == 1:
if song[z // 2][x // 2]:
zone[z][1].append({'state': 3, 'command':
"execute as @a at @a run playsound "
+ instruments[song[z // 2][x // 2]['instrument']]
+ real_keys[song[z // 2][x // 2]['key']][0]
+ " block @s ~1 ~ ~ "
+ str(song[z // 2][x // 2]['volume']) + " "
+ real_keys[song[z // 2][x // 2]['key']][1]})
_progress_zone_setup += 1
else:
zone[z][1].append({'state': 6})
_progress_zone_setup += 1
else:
zone[z][1].append({'state': 2})
_progress_zone_setup += 1
else:
zone[z][0].append({'state': 0})
zone[z][1].append({'state': 0})
_progress_zone_setup += 2
else:
zone[z][0].append({'state': 0})
zone[z][1].append({'state': 0})
_progress_zone_setup += 2
zone[z][2].append({'state': 0})
zone[z][3].append({'state': 0})
zone[z][4].append({'state': 0})
zone[z][5].append({'state': 0})
_progress_zone_setup += 4
print("\n寻找最长轨道便于放置传送用命令方块")
longest = None
for i in range(len(zone)):
if zone[i][0][-1] != {'state': 0}:
longest = i
if not longest:
print("出现错误!将不会创建传送用的命令方块轨道")
else:
_progress_teleport_commandblock = 0
_progress_teleport_commandblock_max = len(zone[longest][0]) // 2
for i in range(len(zone[longest][0])):
if i % 2 == 1:
_progress_teleport_commandblock += 1
print("正在放置...%d/%d\r" % (_progress_teleport_commandblock, _progress_teleport_commandblock_max), end='')
zone[longest][0][i] = {
'state': 3,
'command': f"tp @a ~{config_offset} ~{config_height} ~%s" % (
"" if config_play_location == '2' else f"{-longest + len(zone)//2 -1}"
)
}
print("\n进行零碎方块放置")
if len(zone) < 5:
while len(zone) < 5:
zone.append([[{'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}],
[{'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}],
[{'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}],
[{'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}],
[{'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}, {'state': 0}]])
for z in range(5): # 布置小平台
for x in range(5):
zone[z][2][x] = {'state': 1}
finalz = -2 + len(zone)
zone[2][3][2] = { # 放置启动用命令方块
'state': 3,
'command': "fill ~-3 ~-2 ~-2 ~-3 ~-2 ~%d minecraft:redstone_block" % finalz
}
zone[2][3][1] = {'state': 5} # 放置启动按钮
zone[1][1][0] = { # 放置清除红石块的命令方块
'state': 3,
'command': "fill ~-1 ~ ~-1 ~-1 ~ ~%d minecraft:air" % (finalz + 1)
}
zone[2][4][2] = {
'state': 4,
'sign': True,
'glow': True,
'color': "black",
'message1': splitdrive(filename)[-1],
'message2': nbs.header.description,
'message3': f"红石刻速度: {nbs.header.tempo}tick/s",
'message4': f"推荐游戏刻{nbs.header.tempo * 2}"
}
print()
_progress_zone_fill = 0
_progress_zone_fill_max = len(zone) * len(zone[0]) * len(zone[0][0])
for z in range(len(zone)):
for y in range(len(zone[z])):
for x in range(len(zone[z][y])):
_progress_zone_fill += 1
print("填充方块...%d/%d" % (_progress_zone_fill, _progress_zone_fill_max), end="\r")
block = Compound({
'pos': List([Int(x), Int(y), Int(z)]),
'state': Int(zone[z][y][x]['state']),
})
if zone[z][y][x].get('command'):
block['nbt'] = Compound({'Command': String(zone[z][y][x]['command'])})
if zone[z][y][x].get('sign'):
block['nbt'] = Compound({
'is_waxed': Byte(1),
'front_text': Compound({
'has_glowing_text': Byte(zone[z][y][x]['glow']),
'color': String(zone[z][y][x]['color']),
'messages': List([
String(zone[z][y][x].get('message1', '{"text":""}')),
String(zone[z][y][x].get('message2', '{"text":""}')),
String(zone[z][y][x].get('message3', '{"text":""}')),
String(zone[z][y][x].get('message4', '{"text":""}'))
])
})
})
structure['blocks'].append(block)
print()
filename = splitext(filename)[0].lower().replace(' ', '_').replace("'", '_') + ".nbt"
with open(filename, 'wb') as f:
print("正在写入文件...")
structure.save_to(f)
print("完成!文件保存在:\n%s" % filename)
print("本歌曲采用的红石刻速度为%d tick/s, 请在游戏内做适当调整" % nbs.header.tempo)