支持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
__version__ = "1.4.0"
__vername__ = "红石指令音乐的生成"
__version__ = "1.4.1"
__vername__ = "提高mcstructure结构的兼容性"
__author__ = (
("金羿", "Eilles Wan"),
("诸葛亮与八卦阵", "bgArray"),

View File

@ -18,7 +18,12 @@ from typing import Literal
from ...exceptions import CommandFormatError
from ...main import MidiConvert
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(
@ -46,8 +51,11 @@ def to_mcstructure_file_in_delay(
tuple[tuple[int,]结构大小, int音乐总延迟]
"""
if midi_cvt.enable_old_exe_format:
raise CommandFormatError("使用mcstructure结构文件导出时不支持旧版本的指令格式。")
compability_ver = (
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(
data_cfg.volume_ratio,
@ -58,7 +66,9 @@ def to_mcstructure_file_in_delay(
if not os.path.exists(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(
os.path.abspath(
@ -71,12 +81,11 @@ def to_mcstructure_file_in_delay(
return size, max_delay
def to_mcstructure_file_in_redstone_CD(
midi_cvt: MidiConvert,
data_cfg: ConvertConfig,
player: str = "@a",
axis_side: Literal["z+","z-","Z+","Z-","x+","x-","X+","X-"] = "z+",
axis_side: Literal["z+", "z-", "Z+", "Z-", "x+", "x-", "X+", "X-"] = "z+",
basement_block: str = "concrete",
):
"""
@ -100,8 +109,11 @@ def to_mcstructure_file_in_redstone_CD(
tuple[tuple[int,]结构大小, int音乐总延迟]
"""
if midi_cvt.enable_old_exe_format:
raise CommandFormatError("使用mcstructure结构文件导出时不支持旧版本的指令格式。")
compability_ver = (
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(
data_cfg.volume_ratio,
@ -112,7 +124,14 @@ def to_mcstructure_file_in_redstone_CD(
if not os.path.exists(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(
os.path.abspath(
@ -123,4 +142,3 @@ def to_mcstructure_file_in_redstone_CD(
struct.dump(f)
return size, max_delay

View File

@ -21,7 +21,12 @@ from ...exceptions import CommandFormatError
from ...main import MidiConvert
from ..archive import behavior_mcpack_manifest, compress_zipfile
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(
@ -49,8 +54,11 @@ def to_mcstructure_addon_in_delay(
tuple[int指令数量, int音乐总延迟]
"""
if midi_cvt.enable_old_exe_format:
raise CommandFormatError("使用mcstructure结构文件导出时不支持旧版本的指令格式。")
compability_ver = (
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(
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"
)
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(
os.path.abspath(
os.path.join(
@ -103,9 +115,7 @@ def to_mcstructure_addon_in_delay(
scb_name = midi_cvt.midi_music_name[:3] + "Pgb"
index_file.write("scoreboard objectives add {0} dummy {0}\n".format(scb_name))
struct_a = Structure(
(1, 1, 1),
)
struct_a = Structure((1, 1, 1), compability_version=compability_ver)
struct_a.set_block(
(0, 0, 0),
form_command_block_in_NBT_struct(
@ -115,6 +125,7 @@ def to_mcstructure_addon_in_delay(
1,
alwaysRun=False,
customName="显示进度条并加分",
compability_version_number=compability_ver,
),
)
@ -135,6 +146,7 @@ def to_mcstructure_addon_in_delay(
pgb_struct, pgbSize, pgbNowPos = commands_to_structure(
midi_cvt.form_progress_bar(max_delay, scb_name, data_cfg.progressbar_style),
max_height - 1,
compability_version_=compability_ver,
)
with open(
@ -163,6 +175,7 @@ def to_mcstructure_addon_in_delay(
0,
alwaysRun=False,
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 ..subclass import SingleCommand
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
def forward_IER(forward: bool):
return 1 if forward else -1
AXIS_PARTICULAR_VALUE = {
x: {
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_]
@ -57,6 +69,7 @@ def form_note_block_in_NBT_struct(
coordinate: Tuple[int, int, int],
instrument: str = "note.harp",
powered: bool = False,
compability_version_number: int = COMPABILITY_VERSION_119,
):
"""生成音符盒方块
:param note: `int`(0~24)
@ -87,10 +100,15 @@ def form_note_block_in_NBT_struct(
"z": coordinate[2],
} # 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: 朝向
Z- 0
@ -107,6 +125,7 @@ def form_repeater_in_NBT_struct(delay: int, facing: int):
"repeater_delay": delay,
"direction": facing,
},
compability_version=compability_version_number,
)
@ -121,6 +140,7 @@ def form_command_block_in_NBT_struct(
customName: str = "",
executeOnFirstTick: bool = False,
trackOutput: bool = True,
compability_version_number: int = COMPABILITY_VERSION_119,
):
"""
使用指定项目返回指定的指令方块结构
@ -199,13 +219,14 @@ def form_command_block_in_NBT_struct(
"z": coordinate[2],
} # type: ignore
},
compability_version=17959425,
compability_version=compability_version_number,
)
def commands_to_structure(
commands: List[SingleCommand],
max_height: int = 64,
compability_version_: int = COMPABILITY_VERSION_119,
):
"""
:param commands: 指令列表
@ -218,7 +239,8 @@ def commands_to_structure(
)
struct = Structure(
(_sideLength, max_height, _sideLength), # 声明结构大小
size=(_sideLength, max_height, _sideLength), # 声明结构大小
compability_version=compability_version_,
)
y_forward = True
@ -255,6 +277,7 @@ def commands_to_structure(
customName=command.annotation_text,
executeOnFirstTick=False,
trackOutput=True,
compability_version_number=compability_version_,
),
)
@ -284,12 +307,14 @@ def commands_to_structure(
(now_x, now_y, now_z),
)
def commands_to_redstone_delay_structure(
commands: List[SingleCommand],
delay_length: int,
max_multicmd_length: int,
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]]:
"""
:param commands: 指令列表
@ -322,30 +347,47 @@ def commands_to_redstone_delay_structure(
else:
raise ValueError(f"axis_({axis_}) 参数错误。")
goahead = forward_IER(forward)
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
for cmd in commands:
single_repeater_value = round(cmd.delay / 2) % 4 - 1
additional_repeater = round(cmd.delay / 2)// 4
additional_repeater = round(cmd.delay / 2) // 4
for i in range(additional_repeater):
struct.set_block(
tuple(pos_now.values()),
Block("minecraft", base_block,),
Block(
"minecraft",
base_block,
compability_version=compability_version_,
),
)
struct.set_block(
(pos_now[x],1,pos_now[z]),
(pos_now[x], 1, pos_now[z]),
form_repeater_in_NBT_struct(
delay=3,
facing=repeater_facing,
compability_version_number=compability_version_,
),
)
pos_now[extensioon_direction] += goahead
@ -353,22 +395,27 @@ def commands_to_redstone_delay_structure(
if single_repeater_value >= 0:
struct.set_block(
tuple(pos_now.values()),
Block("minecraft", base_block,),
Block(
"minecraft",
base_block,
compability_version=compability_version_,
),
)
struct.set_block(
(pos_now[x],1,pos_now[z]),
(pos_now[x], 1, pos_now[z]),
form_repeater_in_NBT_struct(
delay=single_repeater_value,
facing=repeater_facing,
compability_version_number=compability_version_,
),
)
pos_now[extensioon_direction] += goahead
first_impluse = True
struct.set_block(
(pos_now[x],1,pos_now[z]),
(pos_now[x], 1, pos_now[z]),
form_command_block_in_NBT_struct(
command=cmd.command_text,
coordinate=(pos_now[x],1,pos_now[z]),
coordinate=(pos_now[x], 1, pos_now[z]),
particularValue=command_statevalue(extensioon_direction, forward),
# impluse= (0 if first_impluse else 2),
impluse=0,
@ -376,16 +423,19 @@ def commands_to_redstone_delay_structure(
alwaysRun=False,
tickDelay=0,
customName=cmd.annotation_text,
compability_version_number=compability_version_,
),
)
struct.set_block(
(pos_now[x],2,pos_now[z]),
Block("minecraft", "redstone_wire"),
(pos_now[x], 2, pos_now[z]),
Block(
"minecraft",
"redstone_wire",
compability_version=compability_version_,
),
)
pos_now[extensioon_direction] += goahead
first_impluse = False
return struct, struct.size, tuple(pos_now.values())