0
0

MinecraftのDataPackのテンプレートをPythonで構築してみる

Last updated at Posted at 2024-08-14

はじめに

Minecraftのデータパック製作で関数を作ってささっと試したいとき、それにいちいち手間をかけないで爆速でデータパックの準備を済ませたいと思ったことありますね?
今回はPyhtonでMinecraftのデータパックの土台を自動構築していこうと思います。

環境

Python: 3.12.5

Minecraft: 1.21

OS: Windows 11

やること

Pythonをインストールしてない人はインストールしちゃってください。
https://qiita.com/icoxfog417/items/e8f97a6acad07903b5b0
Pythonをインストールしている人だったら、下のコードをデータパックを生成したいフォルダーに下のPythonをとりあえず貼ってください。

data_pack_gen.py
import os
import json

pack_name = input("pass the pack name:")
os.makedirs(f"./{pack_name}/data/minecraft/")

with open(f"{pack_name}/pack.mcmeta", "w") as f:
    pf = input("pass the pack_format:")
    desc = input("pass the description:")
    
    pm = {
        "pack": {
            "pack_format": int(pf),
            "description": desc
        }
    }
    writing = json.dumps(pm)
    f.write(writing)



def builds(name_space):
    elements = ["advancements","chat_type","dimension_type","functions","loot_tables","recipes","structures","worldgen","tags"]
    dirs = []
    for ele in elements:
        dirs.append(f"./{pack_name}/data/{name_space}/{ele}")
    
    return dirs

directories = builds("minecraft")

for dir in directories:
  os.mkdir(dir)

own_ns = input("Would you like to create your own namespace? (If not, pass \"No\"):")

os.mkdir(f"./{pack_name}/data/{own_ns}")
directories = builds(own_ns)

if own_ns != "No":
    for dir in directories:
        os.mkdir(dir)

os.mkdir(f"./{pack_name}/data/minecraft/tags/functions")

tick_load_json = json.dumps({"values":[]})

with open(f"./{pack_name}/data/minecraft/tags/functions/tick.json", "w") as f:
    f.write(tick_load_json)

with open(f"./{pack_name}/data/minecraft/tags/functions/load.json", "w") as f:
    f.write(tick_load_json)

解説

インポート

まず、この部分で必要なモジュールをインポートしときます。

import os
import json

osはフォルダー操作、jsonはそれのエンコード・デコードに使います。

大まかなディレクトリ生成

次にinputで渡されるデータパック名をpack_nameに格納して、ディレクトリを再帰的に生成しときます。

pack_name = input("pass the pack name:")
os.makedirs(f"./{pack_name}/data/minecraft/")

pack.mcmeta

ここではpack.mcmetaに記述をしていきます。pack.mcmetaではjsonのエンコードを使っていきます。

with open(f"{pack_name}/pack.mcmeta", "w") as f:
    pf = input("pass the pack_format:")
    desc = input("pass the description:")
    
    pm = {
        "pack": {
            "pack_format": int(pf),
            "description": desc
        }
    }
    writing = json.dumps(pm)
    f.write(writing)

名前空間とその内容を生成する関数

ここではディレクトリ名をハードコーディングしなくていいように、名前空間を渡してディレクトリのリストを渡しています。

def builds(name_space):
    elements = ["advancements","chat_type","dimension_type","functions","loot_tables","recipes","structures","worldgen","tags"]
    dirs = []
    for ele in elements:
        dirs.append(f"./{pack_name}/data/{name_space}/{ele}")
    
    return dirs

minecraft名前空間を配置

minecraftの名前空間とその内容をbuilds()を使って生成します。後でこの中のtags/functionstick.jsonload.jsonを作ります。

directories = builds("minecraft")

for dir in directories:
  os.mkdir(dir)

独自の名前空間を生成する

own_nsに独自の名前空間を格納してown_ns != "No"だったら名前空間とその内容を作成するという簡単な処理です。

own_ns = input("Would you like to create your own namespace? (If not, pass \"No\"):")

os.mkdir(f"./{pack_name}/data/{own_ns}")
directories = builds(own_ns)

if own_ns != "No":
    for dir in directories:
        os.mkdir(dir)

tick.json と load.json

最後にminecraft/tags/functionstick.jsonload.jsonを作ります。
まずはminecraft/tags/functionsのディレクトリを作ります。
tick.jsonload.jsonは中身が同じなのでtick_load_jsonに入れて使いまわします。

os.mkdir(f"./{pack_name}/data/minecraft/tags/functions")

tick_load_json = json.dumps({"values":[]})

with open(f"./{pack_name}/data/minecraft/tags/functions/tick.json", "w") as f:
    f.write(tick_load_json)

with open(f"./{pack_name}/data/minecraft/tags/functions/load.json", "w") as f:
    f.write(tick_load_json)

さいごに

参考になりましたか?僕はファイルやフォルダーを爆速で生成する時にPythonを重宝しています。
今回はPythonを活用して爆速でデータパックのテンプレートを構築しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0