diff --git a/assets/data/block/air.toml b/assets/data/block/air.toml index 7436a4c..9a0c5d3 100644 --- a/assets/data/block/air.toml +++ b/assets/data/block/air.toml @@ -1,5 +1,5 @@ -name = "air" id = 0 +is_cross_plane = false is_liquid = false is_passable = true -is_cross_plane = false \ No newline at end of file +name = 'air' \ No newline at end of file diff --git a/assets/data/block/dirt.toml b/assets/data/block/dirt.toml index b2487ef..95166b6 100644 --- a/assets/data/block/dirt.toml +++ b/assets/data/block/dirt.toml @@ -1,5 +1,5 @@ -name = "dirt" id = 2 +is_cross_plane = false is_liquid = false is_passable = false -is_cross_plane = false \ No newline at end of file +name = 'dirt' \ No newline at end of file diff --git a/assets/data/block/grass.toml b/assets/data/block/grass.toml index 495563f..ad88446 100644 --- a/assets/data/block/grass.toml +++ b/assets/data/block/grass.toml @@ -1,5 +1,5 @@ -name = "grass" id = 9 +is_cross_plane = true is_liquid = false is_passable = true -is_cross_plane = true \ No newline at end of file +name = 'grass' \ No newline at end of file diff --git a/assets/data/block/grass_block.toml b/assets/data/block/grass_block.toml index 607d08f..46f6f33 100644 --- a/assets/data/block/grass_block.toml +++ b/assets/data/block/grass_block.toml @@ -1,5 +1,5 @@ -name = "grass_block" id = 1 +is_cross_plane = false is_liquid = false is_passable = false -is_cross_plane = false \ No newline at end of file +name = 'grass_block' \ No newline at end of file diff --git a/assets/data/block/leaf.toml b/assets/data/block/leaf.toml index c944683..0f0182a 100644 --- a/assets/data/block/leaf.toml +++ b/assets/data/block/leaf.toml @@ -1,5 +1,5 @@ -name = "leaf" id = 6 +is_cross_plane = false is_liquid = false is_passable = false -is_cross_plane = false \ No newline at end of file +name = 'leaf' \ No newline at end of file diff --git a/assets/data/block/log.toml b/assets/data/block/log.toml index c2e568a..e8ac626 100644 --- a/assets/data/block/log.toml +++ b/assets/data/block/log.toml @@ -1,5 +1,5 @@ -name = "log" id = 5 +is_cross_plane = false is_liquid = false is_passable = false -is_cross_plane = false \ No newline at end of file +name = 'log' \ No newline at end of file diff --git a/assets/data/block/sand.toml b/assets/data/block/sand.toml index 0a27b3c..6c0a86e 100644 --- a/assets/data/block/sand.toml +++ b/assets/data/block/sand.toml @@ -1,5 +1,5 @@ -name = "sand" id = 4 +is_cross_plane = false is_liquid = false is_passable = false -is_cross_plane = false \ No newline at end of file +name = 'sand' \ No newline at end of file diff --git a/assets/data/block/snowy_grass_block.toml b/assets/data/block/snowy_grass_block.toml index 5c386fe..fcfa7b4 100644 --- a/assets/data/block/snowy_grass_block.toml +++ b/assets/data/block/snowy_grass_block.toml @@ -1,5 +1,5 @@ -name = "snowy_grass_block" id = 8 +is_cross_plane = false is_liquid = false is_passable = false -is_cross_plane = false \ No newline at end of file +name = 'snowy_grass_block' \ No newline at end of file diff --git a/assets/data/block/stone.toml b/assets/data/block/stone.toml index 4840093..4b2ef23 100644 --- a/assets/data/block/stone.toml +++ b/assets/data/block/stone.toml @@ -1,5 +1,5 @@ -name = "stone" id = 3 +is_cross_plane = false is_liquid = false is_passable = false -is_cross_plane = false \ No newline at end of file +name = 'stone' \ No newline at end of file diff --git a/assets/data/block/water.toml b/assets/data/block/water.toml index 466ed58..e7ea4f5 100644 --- a/assets/data/block/water.toml +++ b/assets/data/block/water.toml @@ -1,5 +1,5 @@ -name = "water" id = 7 +is_cross_plane = false is_liquid = true is_passable = false -is_cross_plane = false \ No newline at end of file +name = 'water' \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 5267d16..b11e9b9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,6 +6,7 @@ readme = "README.md" requires-python = ">=3.14" dependencies = [ "loguru>=0.7.3", + "pytomlpp>=1.1.0", ] [dependency-groups] diff --git a/scripts/blocks_tool.py b/scripts/blocks_tool.py index 2a0b5cf..7bd80e7 100644 --- a/scripts/blocks_tool.py +++ b/scripts/blocks_tool.py @@ -1,47 +1,240 @@ import argparse import sys -import tomllib +from functools import singledispatch from pathlib import Path +from pprint import pprint from typing import Any +import pytomlpp from loguru import logger VERSION = "0.0.1" DATA_PATH = "assets/data/block" TEXTURE_PATH = "assets/texture/block" +work_path = Path(__file__).parent.parent +data_path = work_path / DATA_PATH +texture_path = work_path / TEXTURE_PATH -def show_data_list(): - work_path = Path().parent - data_path = work_path / DATA_PATH + +def collect_blocks() -> list[dict[str, Any]]: blocks: list[dict[str, Any]] = [] for block in data_path.rglob("*.toml"): if not block.is_file(): continue if block.name == "template.toml": continue - with open(block, "rb") as f: - blocks.append(tomllib.load(f)) - blocks.sort(key=lambda x: x["id"]) + blocks.append(pytomlpp.loads(block.read_text(encoding="utf-8"))) + blocks.sort(key=lambda x: x["id"]) + return blocks + + +def save_data(blocks: list[dict[str, Any]]): + for block in blocks: + block_path: Path = data_path / (block["name"] + ".toml") + if not block_path.is_file(): + logger.error(f"Block: {block_path} is not Exists or a File") + continue + block_path.write_text(pytomlpp.dumps(block)) + + +def sync_template_value(): + blocks = collect_blocks() + template_path = data_path / "template.toml" + if not template_path.is_file(): + logger.error("Template.toml is not Exists!") + return + template_block = pytomlpp.loads(template_path.read_text(encoding="utf-8")) + add_count = 0 + for key, value in template_block.items(): + for block in blocks: + if key not in block: + block[key] = value + add_count += 1 + save_data(blocks) + logger.info(f"Synced {add_count} template fields to blocks") + + +@singledispatch +def show_data_info(arg: Any): + logger.error("No Match show_data_info") + + +@show_data_info.register(type(None)) +def _(arg: None): + blocks = collect_blocks() + print("Please Input Block Name or Id, Input exit or e to Exit") + while True: + input_str = input("Name or Id: ") + try: + id = int(input_str) + if id >= len(blocks) or id < 0: + print(f"Id: {id} Not Find, Input e or exit to Exit") + continue + pprint(blocks[id]) + except ValueError: + if input_str.lower() == "exit" or input_str.lower() == "e": + break + find = False + for block in blocks: + if block["name"] == input_str: + pprint(block) + find = True + break + if not find: + print(f"Name: {input_str} Not Find, Input e or exit to Exit") + + +@show_data_info.register(int) +def _(id: int): + blocks = collect_blocks() + if id >= len(blocks) or id < 0: + logger.error(f"ID: {id} is Not Invaild!") + return + pprint(blocks[id]) + + +@show_data_info.register(str) +def _(name: str): + blocks = collect_blocks() + find = False + for block in blocks: + if block["name"] == name: + pprint(block) + find = True + break + if not find: + logger.error(f"Block Name: {name} Not Find") + + +def handle_change(block: dict[str, Any]) -> dict[str, Any]: + print("Please Input Block Key, Input exit or e to Exit") + while True: + key = input("Key: ") + if key.lower() == "exit" or key.lower() == "e": + break + if key not in block: + logger.error("The Key Is Not Exists!") + continue + value = input("Value: ") + + if isinstance(block[key], str): + if value.lower() == "exit" or value.lower() == "e": + print(f"The Value Is {value}, Do You Want to Exit or Write (Y/N)") + ans = input() + if ans.lower() == "y": + break + elif ans.lower() != "n": + logger.error(f"Unknow {ans}") + continue + block[key] = value + elif isinstance(block[key], int): + try: + v = int(value) + block[key] = v + except ValueError: + logger.error("The Value Is Not A Int") + continue + elif isinstance(block[key], bool): + if value.lower() == "true": + block[key] = True + elif value.lower() == "false": + block[key] = False + else: + logger.error("The Value Is Not A Bool") + continue + elif isinstance(block[key], float): + try: + v = float(value) + block[key] = v + except ValueError: + logger.error("The Value Is Not A Float") + continue + else: + logger.error("Unkown Key Type") + continue + print("Change Success") + pprint(block) + return block + + +@singledispatch +def change_data(arg: Any): + logger.error("Not Match change") + + +@change_data.register(int) +def _(id: int): + blocks = collect_blocks() + if id >= len(blocks) or id < 0: + logger.error(f"ID: {id} is Invaild!") + return + pprint(blocks[id]) + blocks[id] = handle_change(blocks[id]) + save_data(blocks) + + +@change_data.register(str) +def _(name: str): + blocks = collect_blocks() + find = False + for i, block in enumerate(blocks): + if block["name"] == name: + pprint(block) + blocks[i] = handle_change(block) + save_data(blocks) + find = True + break + if not find: + logger.error(f"Block Name: {name} Not Find") + + +@change_data.register(type(None)) +def _(arg: None): + blocks = collect_blocks() + print("Please Input Block Name or Id, Input exit or e to Exit") + while True: + input_str = input("Name or Id: ") + try: + id = int(input_str) + if id >= len(blocks) or id < 0: + print(f"Id: {id} Not Find, Input e or exit to Exit") + continue + pprint(blocks[id]) + blocks[id] = handle_change(blocks[id]) + save_data(blocks) + except ValueError: + if input_str.lower() == "exit" or input_str.lower() == "e": + break + find = False + for i, block in enumerate(blocks): + if block["name"] == input_str: + pprint(block) + blocks[i] = handle_change(block) + save_data(blocks) + find = True + break + if not find: + print(f"Name: {input_str} Not Find, Input e or exit to Exit") + + +def show_data_list(): + blocks = collect_blocks() for block in blocks: print(f"id: {block['id']} name: {block['name']}") def check_path(): - work_path = Path().parent + logger.info(f"Work Path {work_path.resolve()}") logger.info(f"Script Dir {sys.path[0]}") - data_path = work_path / DATA_PATH - if not data_path.exists(): - logger.error(f"Blcoks Data Path {data_path} not Exists!") + logger.error(f"Blocks Data Path {data_path} not Exists!") else: logger.info(f"Blocks Data Path {data_path}") - texture_path = work_path / TEXTURE_PATH - if not texture_path.exists(): logger.error(f"Blocks Texture Path {texture_path} not Exists!") else: @@ -56,6 +249,26 @@ def handle_args(args: argparse.Namespace): check_path() if args.list: show_data_list() + if args.sync: + sync_template_value() + if args.info: + if args.info == "EMPTY": + show_data_info(None) + else: + try: + id = int(args.info) + show_data_info(id) + except ValueError: + show_data_info(args.info) + if args.change: + if args.change == "EMPTY": + change_data(None) + else: + try: + id = int(args.change) + change_data(id) + except ValueError: + change_data(args.change) def init_parser(parser: argparse.ArgumentParser): @@ -66,6 +279,22 @@ def init_parser(parser: argparse.ArgumentParser): "--path", action="store_true", help="Check Blcoks Data and Texture Path" ) parser.add_argument("-l", "--list", action="store_true", help="Show Blocks List") + parser.add_argument( + "-s", + "--sync", + action="store_true", + help="Sync Template.toml Value to Other Toml, Only New Value Will Add", + ) + parser.add_argument( + "-i", + "--info", + nargs="?", + const="EMPTY", + help="Show Block Data, If Provide Id Will Print the Corresponding Blcok Data, You Can Input Id or Name", + ) + parser.add_argument( + "-c", "--change", nargs="?", const="EMPTY", help="Change Block Data" + ) def main(): diff --git a/uv.lock b/uv.lock index 0cb01ed..e82cdf7 100644 --- a/uv.lock +++ b/uv.lock @@ -17,6 +17,7 @@ version = "0.1.0" source = { virtual = "." } dependencies = [ { name = "loguru" }, + { name = "pytomlpp" }, ] [package.dev-dependencies] @@ -25,7 +26,10 @@ dev = [ ] [package.metadata] -requires-dist = [{ name = "loguru", specifier = ">=0.7.3" }] +requires-dist = [ + { name = "loguru", specifier = ">=0.7.3" }, + { name = "pytomlpp", specifier = ">=1.1.0" }, +] [package.metadata.requires-dev] dev = [{ name = "ruff", specifier = ">=0.15.14" }] @@ -43,6 +47,12 @@ wheels = [ { url = "https://mirrors.ustc.edu.cn/pypi/packages/0c/29/0348de65b8cc732daa3e33e67806420b2ae89bdce2b04af740289c5c6c8c/loguru-0.7.3-py3-none-any.whl", hash = "sha256:31a33c10c8e1e10422bfd431aeb5d351c7cf7fa671e3c4df004162264b28220c", size = 61595, upload-time = "2024-12-06T11:20:54.538Z" }, ] +[[package]] +name = "pytomlpp" +version = "1.1.0" +source = { registry = "https://mirrors.ustc.edu.cn/pypi/simple" } +sdist = { url = "https://mirrors.ustc.edu.cn/pypi/packages/10/fe/50ca1ac0c1a932d3e71311baff531c0395e546ec0cd42bc8a8a88c36e00b/pytomlpp-1.1.0.tar.gz", hash = "sha256:61a0f73e7ba2fe8bba4e99ce6d2701491885850b53aad8f3e46d03c4b31e594d", size = 1323755, upload-time = "2025-11-29T20:52:09.582Z" } + [[package]] name = "ruff" version = "0.15.14"