diff --git a/README.md b/README.md index d6bae33..2e686c5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@

音·创 Musicreater

- +

@@ -17,17 +17,44 @@ 简体中文🇨🇳 | [English🇬🇧](README_EN.md) + +> 是谁把科技的领域布满政治的火药 +> +> 是谁把纯净的蓝天染上暗淡的沉灰 +> +> 中国人民无不热爱自己伟大的祖国 +> +> 我们不会忘记屈辱历史留下的惨痛 +> +> 我们希望世界和平 +> +> 我们希望获得世界的尊重 +> +> 愿世上再也没有战争 +> +> 无论是热还是冷 +> +> 无论是经济还是政治 +> +> 让美妙的和平的优雅的音乐响彻世界 +> +> ——金羿 +> 2022 5 7 + + ## 介绍🚀 音·创 Musicreater 是一款免费开源的 **《我的世界:基岩版》** 音乐制作软件 +音·创 pkgver (Musicreater Package Version) 是一款免费开源的 **《我的世界:基岩版》** 音乐转换库 + 欢迎加群:[861684859](https://jq.qq.com/?_wv=1027&k=hpeRxrYr) **此分支为音·创的包版本,即便于其他软件使用的可被import版本** ## 软件作者✒ -金羿 Eilles:我的世界基岩版指令师,个人开发者,B站不知名UP主,南昌在校高中生。 +金羿 Eilles:我的世界基岩版指令师,个人开发者,B站不知名UP主,江西在校高中生。 诸葛亮与八卦阵 bgArray:我的世界基岩版玩家,喜欢编程和音乐,深圳初一学生。 @@ -39,6 +66,48 @@ > 正在到来 +### 对于 进度条自定义 功能的说明 + +因为我们提供了可以自动转换进度条的功能,因此在这里给出进度条自定义参数的详细解释。 + +一个进度条,明显地,有**固定部分**和**可变部分**来构成。而可变部分又包括了文字和图形两种(当然,《我的世界》里头的进度条,可变的图形也就是那个“条”了)。这一点你需要了解,因为后文中包含了很多这方面的概念需要你了解。 + +进度条的自定义功能使用一个字符串来定义自己的样式,其中包含众多**标识符**来表示可变部分。 + +标识符如下(注意大小写): + +| 标识符 | 指定的可变量 | +|---------|----------------| +| `%%N` | 乐曲名(即传入的文件名)| +| `%%s` | 当前计分板值 | +| `%^s` | 计分板最大值 | +| `%%t` | 当前播放时间 | +| `%^t` | 曲目总时长 | +| `%%%` | 当前进度比率 | +| `_` | 用以表示进度条占位| + +表示进度条占位的 `_` 是用来标识你的进度条的。也就是可变部分的唯一的图形部分。 + +**样式定义字符串**的样例如下,这也是默认的进度条的样式: + +`▶ %%N [ %%s/%^s %%% __________ %%t|%^t]` + +这是单独一行的进度条,当然你也可以制作多行的,如果是一行的,输出时所使用的指令便是 `title`,而如果是多行的话,输出就会用 `titleraw` 作为进度条字幕。 + +哦对了,上面的只不过是样式定义,同时还需要定义的是可变图形的部分,也就是进度条上那个真正的“条”。 + +对于这个我们就采用了固定参数的方法,对于一个进度条,无非就是“已经播放过的”和“没播放过的”两种形态,所以,使用一个元组来传入这两个参数就是最简单的了。元组的格式也很简单:`(str: 播放过的部分长啥样, str: 没播放过的部分长啥样)` 。例如,我们默认的进度“条”的定义是这样的: + +`('§e=§r', '§7=§r')` + +综合起来,把这些参数传给函数需要一个参数整合,你猜用的啥?啊对对对,我用的还是元组! + +我们的默认定义参数如下: + +`(r'▶ %%N [ %%s/%^s %%% __________ %%t|%^t]',('§e=§r', '§7=§r'))` + +*对了!为了避免生成错误,请尽量避免使用标识符作为定义样式字符串的其他部分* + ## 致谢🙏 - 感谢 昀梦\ 找出指令生成错误bug并指正 diff --git a/README_EN.md b/README_EN.md index c07a952..8b9875f 100644 --- a/README_EN.md +++ b/README_EN.md @@ -1,7 +1,7 @@

音·创 Musicreater

- +

@@ -17,6 +17,31 @@ [简体中文🇨🇳](README.md) | English🇬🇧 + +> Who has dropped political gunpowder into the technology +> +> Who has dyed clear blue sky into the dark grey +> +> All Chinese people love our great homeland +> +> We *WILL* remember the remain pain of the humiliating history +> +> We love the whole world but in peace +> +> We love everyone but under respect +> +> It is to be hoped that the war ends forever +> +> Whatever it is cold or hot +> +> Whatever it is economical or political +> +> Just let the wonderful music of peace surround the world +> +> ---- Eilles Wan +> 7/5 2022 + + **Notice that the language support of *README* may be a little SLOW.** ## Introduction🚀 @@ -25,8 +50,6 @@ Musicreater(音·创) is an free open source software which is used for making a Welcome to join our QQ group: [861684859](https://jq.qq.com/?_wv=1027&k=hpeRxrYr) ->>>>> - ### Authors✒ Eilles (金羿):A high school student, individual developer, unfamous BilibiliUPer, which knows a little about commands in *Minecraft: Bedrock Edition* @@ -35,24 +58,66 @@ bgArray "诸葛亮与八卦阵": Fix bugs, improve code aesthetics, add new func ### Framework🏢 ->>>>> +A simple Python package. ## Instructions📕 ->>>>> +> comming soon. + +### Instructions for **Customize Progress Bar** + +We have supported the function of making progress bar in *Minecraft*'s music player. And also the method of customize them. So the following instructions are about the parameters of the Progress Bar Customizition. + +A Progress Bar, of course, is composed of **changeless** parts and **changable** parts. And the changable parts include texts or *images*(these images are made up of texts, or we can say, character paintings 😁). That is, for *Minecraft*, a changable image in a progress bar is just the "bar" part(which is like a stripe). + +We use a string to describe the style of progress bar you need, and it includes many **identifier**s to replace the changable parts. + +There are the identifiers: + +| Identifier | Changable Part | +|--------------|------------------------------------------------------| +| `%%N` | Music name(file name which is imported into program) | +| `%%s` | Value of scoreboard of now | +| `%^s` | Max value of scoreboard | +| `%%t` | Current playback time | +| `%^t` | Total music time | +| `%%%` | Current playback progress | +| `_` | To be replaced by the *Bar* part of the progress bar | + +The `_` is a placeholder to identifying the *bar* part, yeah, just the changable image. + +This is an example of **style description string**, and this is also the default style of *Musicreater*'s progress bar. + +`▶ %%N [ %%s/%^s %%% __________ %%t|%^t]` + +This is a progress bar with only one line, but it is possible if you want to give a multiline parameter into the style description string. + +But the string above is only for style identification, but we also need to identifying the changable image's image(just what the bar's look). + +A "bar", simply, included 2 parts: *Have Been Played* & *Not Been Played*. So we use a tuple to pass the parameter. It's under a simple format: `(str: played, str: not)`. For example, the default parameter is below: + +`('§e=§r', '§7=§r')` + +So it's time to combine what I said in one parameter now! + +This is a default definder parameter: + +`('▶ %%N [ %%s/%^s %%% __________ %%t|%^t]',('§e=§r', '§7=§r'))` + +*Tip: To avoid errors, please not to use the identifiers as the other part of your style.* ## Thanks🙏 - Thank [Fuckcraft](https://github.com/fuckcraft) *(“鸣凤鸽子” ,etc)* for the function of Creating the Websocket Server for Minecraft: Bedrock Edition. +- Thank [Fuckcraft](https://github.com/fuckcraft) *(“鸣凤鸽子” ,etc)* for the function of Creating the Websocket Server for Minecraft: Bedrock Edition. - *!! They have given me the rights to directly copy the lib into Musicreater* - Thank *昀梦*\ for finding and correcting the bugs in the commands that *Musicreater* Created. - Thank *Charlie_Ping “查理平”* for bdx convert funtion. - Thank *CMA_2401PT* for BDXWorkShop as the .bdx structure's operation guide. - Thank *Miracle Plume “神羽”* \ for the Miracle Plume Bedrock Edition Audio Resource Pack - 感谢由 Dislink Sforza \带来的midi转换算法,我们将其加入了我们众多算法之一 - Thank *Arthur Morgan* for his/her biggest support for the debugging of Musicreater - Thanks for a lot of groupmates who support me and help me to test the program. - If you have give me some help but u haven't been in the list, please contact me. +- Thank *昀梦*\ for finding and correcting the bugs in the commands that *Musicreater* Created. +- Thank *Charlie_Ping “查理平”* for bdx convert funtion. +- Thank *CMA_2401PT* for BDXWorkShop as the .bdx structure's operation guide. +- Thank *Miracle Plume “神羽”* \ for the Miracle Plume Bedrock Edition Audio Resource Pack +- 感谢由 Dislink Sforza \带来的midi转换算法,我们将其加入了我们众多算法之一 +- Thank *Arthur Morgan* for his/her biggest support for the debugging of Musicreater +- Thanks for a lot of groupmates who support me and help me to test the program. +- If you have give me some help but u haven't been in the list, please contact me. ## Contact Information📞 diff --git a/__pycache__/main.cpython-38.pyc b/__pycache__/main.cpython-38.pyc index 02a9369..a015211 100644 Binary files a/__pycache__/main.cpython-38.pyc and b/__pycache__/main.cpython-38.pyc differ diff --git a/example_convert_bdx.py b/example_convert_bdx.py index 87df11a..82702c1 100644 --- a/example_convert_bdx.py +++ b/example_convert_bdx.py @@ -1,9 +1,18 @@ - - - # THIS PROGRAM IS ONLY A TEST EXAMPLE from main import * -print(midiConvert(input('请输入midi文件路径:'), input('请输入输出路径:')).toBDXfile(1,input('请输入作者:'),int(input('请输入指令结构最大生成高度:')),input('请输入计分板名称:'),float(input('请输入音量(0-1]:')),float(input('请输入速度倍率:')))) \ No newline at end of file +convertion = midiConvert() +convertion.convert(input('请输入midi文件路径:'), input('请输入输出路径:')) +for i in convertion.toBDXfile( + 1, + input('请输入作者:'), + bool(int(input('是否开启进度条(1|0):'))), + int(input('请输入指令结构最大生成高度:')), + input('请输入计分板名称:'), + float(input('请输入音量(0-1]:')), + float(input('请输入速度倍率:')), + bool(int(input('是否自动重置计分板(1|0):'))), +): + print(i) diff --git a/example_convert_mcpack.py b/example_convert_mcpack.py index 3a0cec5..0ce0bbb 100644 --- a/example_convert_mcpack.py +++ b/example_convert_mcpack.py @@ -3,5 +3,13 @@ from main import * -midiConvert(input('请输入midi文件路径:'), input('请输入输出路径:')).tomcpack(1, input('请输入计分板名称:'), float(input('请输入音量(0-1):')), - float(input('请输入速度倍率:'))) +convertion = midiConvert() +convertion.convert(input('请输入midi文件路径:'), input('请输入输出路径:')) +convertion.tomcpack( + 1, + bool(int(input('是否自动重置计分板(1|0):'))), + bool(int(input('是否开启进度条(1|0):'))), + input('请输入计分板名称:'), + float(input('请输入音量(0-1):')), + float(input('请输入速度倍率:')), +) diff --git a/main.py b/main.py index c4e92b1..b2a35d4 100644 --- a/main.py +++ b/main.py @@ -42,8 +42,12 @@ def makeZip(sourceDir, outFilename, compression=8, exceptFile=None): class midiConvert: - def __init__(self, midiFile: str, outputPath: str): + def __init__(self): '''简单的midi转换类,将midi文件转换为我的世界结构或者包''' + pass + + def convert(self, midiFile: str, outputPath: str): + '''转换前需要先运行此函数来获取基本信息''' import mido @@ -95,6 +99,178 @@ class midiConvert: return 'note.xylophone' return default + def __score2time(self, score: int): + return str(int(int(score / 20) / 60)) + ':' + str(int(int(score / 20) % 60)) + + def __formProgressBar( + self, + maxscore: int, + scoreboardname: str, + progressbar: tuple = ( + r'▶ %%N [ %%s/%^s %%% __________ %%t|%^t ]', + ('§e=§r', '§7=§r'), + ), + ) -> list: + pgsstyle = progressbar[0] + + ''' + | 标识符 | 指定的可变量 | + |---------|----------------| + | `%%N` | 乐曲名(即传入的文件名)| + | `%%s` | 当前计分板值 | + | `%^s` | 计分板最大值 | + | `%%t` | 当前播放时间 | + | `%^t` | 曲目总时长 | + | `%%%` | 当前进度比率 | + | `_` | 用以表示进度条占位| + ''' + + def __replace( + s: str, tobeReplaced: str, replaceWith: str, times: int, other: str + ): + if times == 0: + return s.replace(tobeReplaced, other) + if times == s.count(tobeReplaced): + return s.replace(tobeReplaced, replaceWith) + result = '' + t = 0 + for i in s: + if i == tobeReplaced: + if t < times: + result += replaceWith + t += 1 + else: + result += other + else: + result += i + + return result + + idlist = { + r'%%N': self.midFileName, + r'%%s': r'%%s', + r'%^s': str(maxscore), + r'%%t': r'%%t', + r'%^t': self.__score2time(maxscore), + r'%%%': r'%%%', + } + + ids = {} + + for i, j in idlist.items(): + if i != j: + if i in pgsstyle: + pgsstyle = pgsstyle.replace(i, j) + else: + if i in pgsstyle: + ids[i] = True + else: + ids[i] = False + + del idlist + + pgblength = pgsstyle.count('_') + + finalprgsbar = [] + + for i in range(maxscore): + nowstr = pgsstyle + if ids[r'%%s'] == True: + nowstr = nowstr.replace(r'%%s', str(i + 1)) + if ids[r'%%t'] == True: + nowstr = nowstr.replace(r'%%t', self.__score2time(i + 1)) + if ids[r'%%%'] == True: + nowstr = nowstr.replace( + r'%%%', str(int((i + 1) / maxscore * 10000) / 100) + '%' + ) + + countof_s = int((i + 1) / maxscore * pgblength) + + finalprgsbar.append( + 'title @a[scores={' + + scoreboardname + + '=' + + str(i + 1) + + '}] actionbar ' + + __replace( + nowstr, '_', progressbar[1][0], countof_s, progressbar[1][1] + ) + ) + + return finalprgsbar + + def __formCMDblk( + self, + command: str, + particularValue: int, + impluse: int = 0, + condition: bool = False, + needRedstone: bool = True, + tickDelay: int = 0, + customName: str = '', + executeOnFirstTick: bool = False, + trackOutput: bool = True, + ): + """ + 使用指定项目返回指定的指令方块放置指令项 + :param command: `str` + 指令 + :param particularValue: + 方块特殊值,即朝向 + :0 下 无条件 + :1 上 无条件 + :2 z轴负方向 无条件 + :3 z轴正方向 无条件 + :4 x轴负方向 无条件 + :5 x轴正方向 无条件 + :6 下 无条件 + :7 下 无条件 + + :8 下 有条件 + :9 上 有条件 + :10 z轴负方向 有条件 + :11 z轴正方向 有条件 + :12 x轴负方向 有条件 + :13 x轴正方向 有条件 + :14 下 有条件 + :14 下 有条件 + 注意!此处特殊值中的条件会被下面condition参数覆写 + :param impluse: `int 0|1|2` + 方块类型 + 0脉冲 1循环 2连锁 + :param condition: `bool` + 是否有条件 + :param needRedstone: `bool` + 是否需要红石 + :param tickDelay: `int` + 执行延时 + :param customName: `str` + 悬浮字 + :param lastOutput: `str` + 上次输出字符串,注意此处需要留空 + :param executeOnFirstTick: `bool` + 执行第一个已选项(循环指令方块是否激活后立即执行,若为False,则从激活时起延迟后第一次执行) + :param trackOutput: `bool` + 是否输出 + + :return:str + """ + block = b"\x24" + particularValue.to_bytes(2, byteorder="big", signed=False) + + for i in [ + impluse.to_bytes(4, byteorder="big", signed=False), + bytes(command, encoding="utf-8") + b"\x00", + bytes(customName, encoding="utf-8") + b"\x00", + bytes('', encoding="utf-8") + b"\x00", + tickDelay.to_bytes(4, byteorder="big", signed=True), + executeOnFirstTick.to_bytes(1, byteorder="big"), + trackOutput.to_bytes(1, byteorder="big"), + condition.to_bytes(1, byteorder="big"), + needRedstone.to_bytes(1, byteorder="big"), + ]: + block += i + return block + def _toCmdList_m1( self, scoreboardname: str = 'mscplay', volume: float = 1.0, speed: float = 1.0 ) -> list: @@ -103,7 +279,7 @@ class midiConvert: :param scoreboardname: 我的世界的计分板名称 :param volume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频 :param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed - :return: 我的世界命令列表 + :return: tuple(命令列表, 命令个数, 计分板最大值) """ tracks = [] if volume > 1: @@ -112,6 +288,7 @@ class midiConvert: volume = 0.001 commands = 0 + maxscore = 0 for i, track in enumerate(self.midi.tracks): @@ -128,31 +305,84 @@ class midiConvert: else: ticks += msg.time if msg.type == 'note_on' and msg.velocity != 0: + nowscore = round( + (ticks * tempo) + / ((self.midi.ticks_per_beat * float(speed)) * 50000) + ) + maxscore = max(maxscore, nowscore) singleTrack.append( 'execute @a[scores={' - + scoreboardname + + str(scoreboardname) + '=' - + str( - round( - (ticks * tempo) - / ( - (self.midi.ticks_per_beat * float(speed)) - * 50000 - ) - ) - ) + + str(nowscore) + '}' + f'] ~ ~ ~ playsound {self.__Inst2SoundID(instrumentID)} @s ~ ~{1/volume-1} ~ {msg.velocity*(0.7 if msg.channel == 0 else 0.9)} {2**((msg.note-66)/12)}' ) commands += 1 + if len(singleTrack) != 0: + tracks.append(singleTrack) + + return tracks, commands, maxscore + + def _toCmdList_withDelay_m1(self, volume: float = 1.0, speed: float = 1.0) -> list: + """ + 使用Dislink Sforza的转换算法,将midi转换为我的世界命令列表,并输出每个音符之后的延迟 + :param volume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频 + :param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed + :return: tuple(命令列表[音轨[(命令,此命令的延迟),...],...], 命令个数,) + """ + tracks = [] + if volume > 1: + volume = 1 + if volume <= 0: + volume = 0.001 + + commands = 0 + maxscore = 0 + + for i, track in enumerate(self.midi.tracks): + + ticks = 0 + instrumentID = 0 + singleTrack = [] + + for msg in track: + if msg.is_meta: + if msg.type == 'set_tempo': + tempo = msg.tempo + if msg.type == 'program_change': + instrumentID = msg.program + else: + ticks += msg.time + if msg.type == 'note_on' and msg.velocity != 0: + nowscore = round( + (ticks * tempo) + / ((self.midi.ticks_per_beat * float(speed)) * 50000) + ) + maxscore = max(maxscore, nowscore) + singleTrack.append( + f'playsound {self.__Inst2SoundID(instrumentID)} @s ~ ~{1/volume-1} ~ {msg.velocity*(0.7 if msg.channel == 0 else 0.9)} {2**((msg.note-66)/12)}' + ) + commands += 1 tracks.append(singleTrack) - return tracks, commands + return tracks, commands, maxscore + + def __fillSquareSideLength(self, total: int, maxHeight: int): + '''给定总方块数量和最大高度,返回所构成的图形外切正方形的边长 + :param total: 总方块数量 + :param maxHeight: 最大高度 + :return: 外切正方形的边长 int''' + import math + + return math.ceil(math.sqrt(math.ceil(total / maxHeight))) def tomcpack( self, method: int = 1, + isAutoReset: bool = False, + progressbar=None, scoreboardname: str = 'mscplay', volume: float = 1.0, speed: float = 1.0, @@ -160,13 +390,15 @@ class midiConvert: """ 使用method指定的转换算法,将midi转换为我的世界mcpack格式的包 :param method: 转换算法 + :param isAutoReset: 是否自动重置计分板 + :param progressbar: 进度条,(当此参数为True时使用默认进度条,为其他的值为真的参数时识别为进度条自定义参数,为其他值为假的时候不生成进度条) :param scoreboardname: 我的世界的计分板名称 :param volume: 音量,注意:这里的音量范围为(0,1],其原理为在距离玩家 (1 / volume -1) 的地方播放音频 :param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed :return 成功与否,成功返回(True,True),失败返回(False,str失败原因) """ if method == 1: - cmdlist, _a = self._toCmdList_m1(scoreboardname, volume, speed) + cmdlist, _a, maxscore = self._toCmdList_m1(scoreboardname, volume, speed) else: return (False, f'无法找到算法ID{method}对应的转换算法') del _a @@ -182,7 +414,9 @@ class midiConvert: # 写入manifest.json if not os.path.exists(f'{self.outputPath}/temp/manifest.json'): - with open(f"{self.outputPath}/temp/manifest.json", "w") as f: + with open( + f"{self.outputPath}/temp/manifest.json", "w", encoding='utf-8' + ) as f: f.write( "{\n \"format_version\": 1,\n \"header\": {\n \"description\": \"" + self.midFileName @@ -197,7 +431,9 @@ class midiConvert: + "\"\n }\n ]\n}" ) else: - with open(f'{self.outputPath}/temp/manifest.json', 'r') as manifest: + with open( + f'{self.outputPath}/temp/manifest.json', 'r', encoding='utf-8' + ) as manifest: data = json.loads(manifest.read()) data['header'][ 'description' @@ -207,7 +443,9 @@ class midiConvert: data['modules'][0]['description'] = 'None' data['modules'][0]['uuid'] = str(uuid.uuid4()) manifest.close() - open(f'{self.outputPath}/temp/manifest.json', 'w').write(json.dumps(data)) + open(f'{self.outputPath}/temp/manifest.json', 'w', encoding='utf-8').write( + json.dumps(data) + ) # 将命令列表写入文件 indexfile = open( @@ -223,13 +461,51 @@ class midiConvert: encoding='utf-8', ) as f: f.write('\n'.join(track)) - indexfile.write( - 'scoreboard players add @a[scores={' - + scoreboardname - + '=1..}] ' - + scoreboardname - + ' 1\n' + indexfile.writelines( + ( + 'scoreboard players add @a[scores={' + + scoreboardname + + '=1..}] ' + + scoreboardname + + ' 1\n', + ( + 'scoreboard players reset @a[scores={' + + scoreboardname + + '=' + + str(maxscore + 20) + + '..}]' + + f' {scoreboardname}\n' + ) + if isAutoReset + else '', + f'function mscplay/progressShow\n' if progressbar else '', + ) ) + + if progressbar: + if progressbar == True: + with open( + f'{self.outputPath}/temp/functions/mscplay/progressShow.mcfunction', + 'w', + encoding='utf-8', + ) as f: + f.writelines( + '\n'.join(self.__formProgressBar(maxscore, scoreboardname)) + ) + else: + with open( + f'{self.outputPath}/temp/functions/mscplay/progressShow.mcfunction', + 'w', + encoding='utf-8', + ) as f: + f.writelines( + '\n'.join( + self.__formProgressBar( + maxscore, scoreboardname, progressbar + ) + ) + ) + indexfile.close() makeZip( @@ -240,26 +516,34 @@ class midiConvert: def toBDXfile( self, - method: int, - author: str, - maxheight: int, + method: int = 1, + author: str = 'Eilles', + progressbar=False, + maxheight: int = 64, scoreboardname: str = 'mscplay', volume: float = 1.0, speed: float = 1.0, + isAutoReset: bool = False, ): """ 使用method指定的转换算法,将midi转换为BDX结构文件 :param method: 转换算法 + :param author: 作者名称 + :param progressbar: 进度条,(当此参数为True时使用默认进度条,为其他的值为真的参数时识别为进度条自定义参数,为其他值为假的时候不生成进度条) + :param maxheight: 生成结构最大高度 :param scoreboardname: 我的世界的计分板名称 :param volume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频 :param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed + :param isAutoReset: 是否自动重置计分板 :return 成功与否,成功返回(True,未经过压缩的源,结构占用大小),失败返回(False,str失败原因) """ import brotli if method == 1: - cmdlist, totalcount = self._toCmdList_m1(scoreboardname, volume, speed) + cmdlist, totalcount, maxScore = self._toCmdList_m1( + scoreboardname, volume, speed + ) else: return (False, f'无法找到算法ID{method}对应的转换算法') @@ -285,87 +569,7 @@ class midiConvert: y = 'y' z = 'z' - def __formCMDblk( - command: str, - particularValue: int, - impluse: int = 0, - condition: bool = False, - needRedstone: bool = True, - tickDelay: int = 0, - customName: str = '', - executeOnFirstTick: bool = False, - trackOutput: bool = True, - ): - """ - 使用指定项目返回指定的指令方块放置指令项 - :param command: `str` - 指令 - :param particularValue: - 方块特殊值,即朝向 - :0 下 无条件 - :1 上 无条件 - :2 z轴负方向 无条件 - :3 z轴正方向 无条件 - :4 x轴负方向 无条件 - :5 x轴正方向 无条件 - :6 下 无条件 - :7 下 无条件 - - :8 下 有条件 - :9 上 有条件 - :10 z轴负方向 有条件 - :11 z轴正方向 有条件 - :12 x轴负方向 有条件 - :13 x轴正方向 有条件 - :14 下 有条件 - :14 下 有条件 - 注意!此处特殊值中的条件会被下面condition参数覆写 - :param impluse: `int 0|1|2` - 方块类型 - 0脉冲 1循环 2连锁 - :param condition: `bool` - 是否有条件 - :param needRedstone: `bool` - 是否需要红石 - :param tickDelay: `int` - 执行延时 - :param customName: `str` - 悬浮字 - :param lastOutput: `str` - 上次输出字符串,注意此处需要留空 - :param executeOnFirstTick: `bool` - 执行第一个已选项(循环指令方块是否激活后立即执行,若为False,则从激活时起延迟后第一次执行) - :param trackOutput: `bool` - 是否输出 - - :return:str - """ - block = b"\x24" + particularValue.to_bytes(2, byteorder="big", signed=False) - - for i in [ - impluse.to_bytes(4, byteorder="big", signed=False), - bytes(command, encoding="utf-8") + b"\x00", - bytes(customName, encoding="utf-8") + b"\x00", - bytes('', encoding="utf-8") + b"\x00", - tickDelay.to_bytes(4, byteorder="big", signed=True), - executeOnFirstTick.to_bytes(1, byteorder="big"), - trackOutput.to_bytes(1, byteorder="big"), - condition.to_bytes(1, byteorder="big"), - needRedstone.to_bytes(1, byteorder="big"), - ]: - block += i - return block - - def __fillSquareSideLength(total: int, maxHeight: int): - '''给定总方块数量和最大高度,返回所构成的图形外切正方形的边长 - :param total: 总方块数量 - :param maxHeight: 最大高度 - :return: 外切正方形的边长 int''' - import math - - return math.ceil(math.sqrt(total / maxHeight)) - - _sideLength = __fillSquareSideLength(totalcount, maxheight) + _sideLength = self.__fillSquareSideLength(totalcount, maxheight) yforward = True zforward = True @@ -374,45 +578,75 @@ class midiConvert: nowz = 0 nowx = 0 + commands = [] + for track in cmdlist: - for cmd in track: - _bytes += __formCMDblk( - cmd, - (1 if yforward else 0) if (((nowy != 0) and (not yforward)) or ((yforward) and (nowy != maxheight))) else (3 if zforward else 2) if (((nowz != 0) and (not zforward)) or ((zforward) and (nowz != _sideLength))) else 5, - impluse=2, - condition=False, - needRedstone=False, - tickDelay=0, - customName='', - executeOnFirstTick=False, - trackOutput=True, + commands += track + + if isAutoReset: + commands += ( + 'scoreboard players reset @a[scores={' + + scoreboardname + + '=' + + str(maxScore + 20) + + '}] ' + + scoreboardname + ) + + if progressbar: + if progressbar == True: + commands += self.__formProgressBar(maxScore, scoreboardname) + else: + commands += self.__formProgressBar( + maxScore, scoreboardname, progressbar ) - nowy += 1 if yforward else -1 + for cmd in commands: + _bytes += self.__formCMDblk( + cmd, + (1 if yforward else 0) + if ( + ((nowy != 0) and (not yforward)) + or ((yforward) and (nowy != maxheight)) + ) + else (3 if zforward else 2) + if ( + ((nowz != 0) and (not zforward)) + or ((zforward) and (nowz != _sideLength)) + ) + else 5, + impluse=2, + condition=False, + needRedstone=False, + tickDelay=0, + customName='', + executeOnFirstTick=False, + trackOutput=True, + ) - if ((nowy > maxheight) and (yforward)) or ( - (nowy < 0) and (not yforward) + nowy += 1 if yforward else -1 + + if ((nowy > maxheight) and (yforward)) or ((nowy < 0) and (not yforward)): + nowy -= 1 if yforward else -1 + + yforward = not yforward + + nowz += 1 if zforward else -1 + + if ((nowz > _sideLength) and (zforward)) or ( + (nowz < 0) and (not zforward) ): - nowy -= 1 if yforward else -1 - - yforward = not yforward - - nowz += 1 if zforward else -1 - - if ((nowz > _sideLength) and (zforward)) or ( - (nowz < 0) and (not zforward) - ): - nowz -= 1 if zforward else -1 - zforward = not zforward - _bytes += key[x][1] - nowx += 1 - else: - - _bytes += key[z][int(zforward)] - + nowz -= 1 if zforward else -1 + zforward = not zforward + _bytes += key[x][1] + nowx += 1 else: - _bytes += key[y][int(yforward)] + _bytes += key[z][int(zforward)] + + else: + + _bytes += key[y][int(yforward)] with open(f"{self.outputPath}/{self.midFileName}.bdx", "ab+") as f: f.write(brotli.compress(_bytes + b'XE'))