支持mcstructure结构导出旧版指令

This commit is contained in:
EillesWan 2023-09-10 11:43:39 +08:00
parent 357cb18c5b
commit 627a26f64a
4 changed files with 123 additions and 42 deletions

View File

@ -17,8 +17,8 @@ Terms & Conditions: License.md in the root directory
# 若需转载或借鉴 许可声明请查看仓库目录下的 License.md # 若需转载或借鉴 许可声明请查看仓库目录下的 License.md
__version__ = "1.4.0" __version__ = "1.4.1"
__vername__ = "红石指令音乐的生成" __vername__ = "提高mcstructure结构的兼容性"
__author__ = ( __author__ = (
("金羿", "Eilles Wan"), ("金羿", "Eilles Wan"),
("诸葛亮与八卦阵", "bgArray"), ("诸葛亮与八卦阵", "bgArray"),

View File

@ -18,7 +18,12 @@ from typing import Literal
from ...exceptions import CommandFormatError from ...exceptions import CommandFormatError
from ...main import MidiConvert from ...main import MidiConvert
from ..main import ConvertConfig from ..main import ConvertConfig
from ..mcstructure import commands_to_structure,commands_to_redstone_delay_structure from ..mcstructure import (
commands_to_structure,
commands_to_redstone_delay_structure,
COMPABILITY_VERSION_119,
COMPABILITY_VERSION_117,
)
def to_mcstructure_file_in_delay( def to_mcstructure_file_in_delay(
@ -46,8 +51,11 @@ def to_mcstructure_file_in_delay(
tuple[tuple[int,]结构大小, int音乐总延迟] tuple[tuple[int,]结构大小, int音乐总延迟]
""" """
if midi_cvt.enable_old_exe_format: compability_ver = (
raise CommandFormatError("使用mcstructure结构文件导出时不支持旧版本的指令格式。") COMPABILITY_VERSION_117
if midi_cvt.enable_old_exe_format
else COMPABILITY_VERSION_119
)
cmd_list, max_delay = midi_cvt.to_command_list_in_delay( cmd_list, max_delay = midi_cvt.to_command_list_in_delay(
data_cfg.volume_ratio, data_cfg.volume_ratio,
@ -58,7 +66,9 @@ def to_mcstructure_file_in_delay(
if not os.path.exists(data_cfg.dist_path): if not os.path.exists(data_cfg.dist_path):
os.makedirs(data_cfg.dist_path) os.makedirs(data_cfg.dist_path)
struct, size, end_pos = commands_to_structure(cmd_list, max_height - 1) struct, size, end_pos = commands_to_structure(
cmd_list, max_height - 1, compability_version_=compability_ver
)
with open( with open(
os.path.abspath( os.path.abspath(
@ -71,7 +81,6 @@ def to_mcstructure_file_in_delay(
return size, max_delay return size, max_delay
def to_mcstructure_file_in_redstone_CD( def to_mcstructure_file_in_redstone_CD(
midi_cvt: MidiConvert, midi_cvt: MidiConvert,
data_cfg: ConvertConfig, data_cfg: ConvertConfig,
@ -100,8 +109,11 @@ def to_mcstructure_file_in_redstone_CD(
tuple[tuple[int,]结构大小, int音乐总延迟] tuple[tuple[int,]结构大小, int音乐总延迟]
""" """
if midi_cvt.enable_old_exe_format: compability_ver = (
raise CommandFormatError("使用mcstructure结构文件导出时不支持旧版本的指令格式。") COMPABILITY_VERSION_117
if midi_cvt.enable_old_exe_format
else COMPABILITY_VERSION_119
)
cmd_list, max_delay, max_multiple_cmd = midi_cvt.to_command_list_in_delay( cmd_list, max_delay, max_multiple_cmd = midi_cvt.to_command_list_in_delay(
data_cfg.volume_ratio, data_cfg.volume_ratio,
@ -112,7 +124,14 @@ def to_mcstructure_file_in_redstone_CD(
if not os.path.exists(data_cfg.dist_path): if not os.path.exists(data_cfg.dist_path):
os.makedirs(data_cfg.dist_path) os.makedirs(data_cfg.dist_path)
struct, size, end_pos = commands_to_redstone_delay_structure(cmd_list,max_delay,max_multiple_cmd, basement_block, axis_side) struct, size, end_pos = commands_to_redstone_delay_structure(
cmd_list,
max_delay,
max_multiple_cmd,
basement_block,
axis_side,
compability_version_=compability_ver,
)
with open( with open(
os.path.abspath( os.path.abspath(
@ -123,4 +142,3 @@ def to_mcstructure_file_in_redstone_CD(
struct.dump(f) struct.dump(f)
return size, max_delay return size, max_delay

View File

@ -21,7 +21,12 @@ from ...exceptions import CommandFormatError
from ...main import MidiConvert from ...main import MidiConvert
from ..archive import behavior_mcpack_manifest, compress_zipfile from ..archive import behavior_mcpack_manifest, compress_zipfile
from ..main import ConvertConfig from ..main import ConvertConfig
from ..mcstructure import commands_to_structure, form_command_block_in_NBT_struct from ..mcstructure import (
commands_to_structure,
form_command_block_in_NBT_struct,
COMPABILITY_VERSION_117,
COMPABILITY_VERSION_119,
)
def to_mcstructure_addon_in_delay( def to_mcstructure_addon_in_delay(
@ -49,8 +54,11 @@ def to_mcstructure_addon_in_delay(
tuple[int指令数量, int音乐总延迟] tuple[int指令数量, int音乐总延迟]
""" """
if midi_cvt.enable_old_exe_format: compability_ver = (
raise CommandFormatError("使用mcstructure结构文件导出时不支持旧版本的指令格式。") COMPABILITY_VERSION_117
if midi_cvt.enable_old_exe_format
else COMPABILITY_VERSION_119
)
command_list, max_delay = midi_cvt.to_command_list_in_delay( command_list, max_delay = midi_cvt.to_command_list_in_delay(
data_cfg.volume_ratio, data_cfg.volume_ratio,
@ -84,7 +92,11 @@ def to_mcstructure_addon_in_delay(
f"{data_cfg.dist_path}/temp/functions/index.mcfunction", "w", encoding="utf-8" f"{data_cfg.dist_path}/temp/functions/index.mcfunction", "w", encoding="utf-8"
) )
struct, size, end_pos = commands_to_structure(command_list, max_height - 1) struct, size, end_pos = commands_to_structure(
command_list,
max_height - 1,
compability_version_=compability_ver,
)
with open( with open(
os.path.abspath( os.path.abspath(
os.path.join( os.path.join(
@ -103,9 +115,7 @@ def to_mcstructure_addon_in_delay(
scb_name = midi_cvt.midi_music_name[:3] + "Pgb" scb_name = midi_cvt.midi_music_name[:3] + "Pgb"
index_file.write("scoreboard objectives add {0} dummy {0}\n".format(scb_name)) index_file.write("scoreboard objectives add {0} dummy {0}\n".format(scb_name))
struct_a = Structure( struct_a = Structure((1, 1, 1), compability_version=compability_ver)
(1, 1, 1),
)
struct_a.set_block( struct_a.set_block(
(0, 0, 0), (0, 0, 0),
form_command_block_in_NBT_struct( form_command_block_in_NBT_struct(
@ -115,6 +125,7 @@ def to_mcstructure_addon_in_delay(
1, 1,
alwaysRun=False, alwaysRun=False,
customName="显示进度条并加分", customName="显示进度条并加分",
compability_version_number=compability_ver,
), ),
) )
@ -135,6 +146,7 @@ def to_mcstructure_addon_in_delay(
pgb_struct, pgbSize, pgbNowPos = commands_to_structure( pgb_struct, pgbSize, pgbNowPos = commands_to_structure(
midi_cvt.form_progress_bar(max_delay, scb_name, data_cfg.progressbar_style), midi_cvt.form_progress_bar(max_delay, scb_name, data_cfg.progressbar_style),
max_height - 1, max_height - 1,
compability_version_=compability_ver,
) )
with open( with open(
@ -163,6 +175,7 @@ def to_mcstructure_addon_in_delay(
0, 0,
alwaysRun=False, alwaysRun=False,
customName="重置进度条计分板", customName="重置进度条计分板",
compability_version_number=compability_ver,
), ),
) )

View File

@ -23,16 +23,16 @@ from TrimMCStruct import Block, Structure, TAG_Byte, TAG_Long
from ..constants import x, y, z from ..constants import x, y, z
from ..subclass import SingleCommand from ..subclass import SingleCommand
from .common import bottem_side_length_of_smallest_square_bottom_box from .common import bottem_side_length_of_smallest_square_bottom_box
import struct
def antiaxis(axis: Literal['x','z','X','Z']): def antiaxis(axis: Literal["x", "z", "X", "Z"]):
return z if axis == x else x return z if axis == x else x
def forward_IER(forward: bool): def forward_IER(forward: bool):
return 1 if forward else -1 return 1 if forward else -1
AXIS_PARTICULAR_VALUE = { AXIS_PARTICULAR_VALUE = {
x: { x: {
True: 5, True: 5,
@ -48,7 +48,19 @@ AXIS_PARTICULAR_VALUE = {
}, },
} }
def command_statevalue(axis_: Literal['x','y','z','X','Y','Z'],forward_:bool): # 1.19的结构兼容版本号
COMPABILITY_VERSION_119: int = 17959425
"""
Minecraft 1.19 兼容版本号
"""
# 1.17的结构兼容版本号
COMPABILITY_VERSION_117: int = 17879555
"""
Minecraft 1.17 兼容版本号
"""
def command_statevalue(axis_: Literal["x", "y", "z", "X", "Y", "Z"], forward_: bool):
return AXIS_PARTICULAR_VALUE[axis_.lower()][forward_] return AXIS_PARTICULAR_VALUE[axis_.lower()][forward_]
@ -57,6 +69,7 @@ def form_note_block_in_NBT_struct(
coordinate: Tuple[int, int, int], coordinate: Tuple[int, int, int],
instrument: str = "note.harp", instrument: str = "note.harp",
powered: bool = False, powered: bool = False,
compability_version_number: int = COMPABILITY_VERSION_119,
): ):
"""生成音符盒方块 """生成音符盒方块
:param note: `int`(0~24) :param note: `int`(0~24)
@ -87,10 +100,15 @@ def form_note_block_in_NBT_struct(
"z": coordinate[2], "z": coordinate[2],
} # type: ignore } # type: ignore
}, },
compability_version=compability_version_number,
) )
def form_repeater_in_NBT_struct(delay: int, facing: int): def form_repeater_in_NBT_struct(
delay: int,
facing: int,
compability_version_number: int = COMPABILITY_VERSION_119,
):
"""生成中继器方块 """生成中继器方块
:param facing: 朝向 :param facing: 朝向
Z- 0 Z- 0
@ -107,6 +125,7 @@ def form_repeater_in_NBT_struct(delay: int, facing: int):
"repeater_delay": delay, "repeater_delay": delay,
"direction": facing, "direction": facing,
}, },
compability_version=compability_version_number,
) )
@ -121,6 +140,7 @@ def form_command_block_in_NBT_struct(
customName: str = "", customName: str = "",
executeOnFirstTick: bool = False, executeOnFirstTick: bool = False,
trackOutput: bool = True, trackOutput: bool = True,
compability_version_number: int = COMPABILITY_VERSION_119,
): ):
""" """
使用指定项目返回指定的指令方块结构 使用指定项目返回指定的指令方块结构
@ -199,13 +219,14 @@ def form_command_block_in_NBT_struct(
"z": coordinate[2], "z": coordinate[2],
} # type: ignore } # type: ignore
}, },
compability_version=17959425, compability_version=compability_version_number,
) )
def commands_to_structure( def commands_to_structure(
commands: List[SingleCommand], commands: List[SingleCommand],
max_height: int = 64, max_height: int = 64,
compability_version_: int = COMPABILITY_VERSION_119,
): ):
""" """
:param commands: 指令列表 :param commands: 指令列表
@ -218,7 +239,8 @@ def commands_to_structure(
) )
struct = Structure( struct = Structure(
(_sideLength, max_height, _sideLength), # 声明结构大小 size=(_sideLength, max_height, _sideLength), # 声明结构大小
compability_version=compability_version_,
) )
y_forward = True y_forward = True
@ -255,6 +277,7 @@ def commands_to_structure(
customName=command.annotation_text, customName=command.annotation_text,
executeOnFirstTick=False, executeOnFirstTick=False,
trackOutput=True, trackOutput=True,
compability_version_number=compability_version_,
), ),
) )
@ -284,12 +307,14 @@ def commands_to_structure(
(now_x, now_y, now_z), (now_x, now_y, now_z),
) )
def commands_to_redstone_delay_structure( def commands_to_redstone_delay_structure(
commands: List[SingleCommand], commands: List[SingleCommand],
delay_length: int, delay_length: int,
max_multicmd_length: int, max_multicmd_length: int,
base_block: str = "concrete", base_block: str = "concrete",
axis_: Literal["z+", "z-", "Z+", "Z-", "x+", "x-", "X+", "X-"] = "z+", axis_: Literal["z+", "z-", "Z+", "Z-", "x+", "x-", "X+", "X-"] = "z+",
compability_version_: int = COMPABILITY_VERSION_119,
) -> Tuple[Structure, Tuple[int, int, int], Tuple[int, int, int]]: ) -> Tuple[Structure, Tuple[int, int, int], Tuple[int, int, int]]:
""" """
:param commands: 指令列表 :param commands: 指令列表
@ -322,14 +347,26 @@ def commands_to_redstone_delay_structure(
else: else:
raise ValueError(f"axis_({axis_}) 参数错误。") raise ValueError(f"axis_({axis_}) 参数错误。")
goahead = forward_IER(forward) goahead = forward_IER(forward)
struct = Structure( struct = Structure(
(round(delay_length/2+0.5+len(commands)) if extensioon_direction == x else max_multicmd_length, 3, round(delay_length/2+0.5+len(commands)) if extensioon_direction == z else max_multicmd_length) size=(
round(delay_length / 2 + 0.5 + len(commands))
if extensioon_direction == x
else max_multicmd_length,
3,
round(delay_length / 2 + 0.5 + len(commands))
if extensioon_direction == z
else max_multicmd_length,
),
compability_version=compability_version_,
) )
pos_now = {x:(0 if forward else struct.size[0]),y:0,z:(0 if forward else struct.size[2])} pos_now = {
x: (0 if forward else struct.size[0]),
y: 0,
z: (0 if forward else struct.size[2]),
}
first_impluse = True first_impluse = True
@ -339,13 +376,18 @@ def commands_to_redstone_delay_structure(
for i in range(additional_repeater): for i in range(additional_repeater):
struct.set_block( struct.set_block(
tuple(pos_now.values()), tuple(pos_now.values()),
Block("minecraft", base_block,), Block(
"minecraft",
base_block,
compability_version=compability_version_,
),
) )
struct.set_block( struct.set_block(
(pos_now[x], 1, pos_now[z]), (pos_now[x], 1, pos_now[z]),
form_repeater_in_NBT_struct( form_repeater_in_NBT_struct(
delay=3, delay=3,
facing=repeater_facing, facing=repeater_facing,
compability_version_number=compability_version_,
), ),
) )
pos_now[extensioon_direction] += goahead pos_now[extensioon_direction] += goahead
@ -353,13 +395,18 @@ def commands_to_redstone_delay_structure(
if single_repeater_value >= 0: if single_repeater_value >= 0:
struct.set_block( struct.set_block(
tuple(pos_now.values()), tuple(pos_now.values()),
Block("minecraft", base_block,), Block(
"minecraft",
base_block,
compability_version=compability_version_,
),
) )
struct.set_block( struct.set_block(
(pos_now[x], 1, pos_now[z]), (pos_now[x], 1, pos_now[z]),
form_repeater_in_NBT_struct( form_repeater_in_NBT_struct(
delay=single_repeater_value, delay=single_repeater_value,
facing=repeater_facing, facing=repeater_facing,
compability_version_number=compability_version_,
), ),
) )
pos_now[extensioon_direction] += goahead pos_now[extensioon_direction] += goahead
@ -376,16 +423,19 @@ def commands_to_redstone_delay_structure(
alwaysRun=False, alwaysRun=False,
tickDelay=0, tickDelay=0,
customName=cmd.annotation_text, customName=cmd.annotation_text,
compability_version_number=compability_version_,
), ),
) )
struct.set_block( struct.set_block(
(pos_now[x], 2, pos_now[z]), (pos_now[x], 2, pos_now[z]),
Block("minecraft", "redstone_wire"), Block(
"minecraft",
"redstone_wire",
compability_version=compability_version_,
),
) )
pos_now[extensioon_direction] += goahead pos_now[extensioon_direction] += goahead
first_impluse = False first_impluse = False
return struct, struct.size, tuple(pos_now.values()) return struct, struct.size, tuple(pos_now.values())