Ccmmutty logo
Commutty IT
5 min read

pythonでマイクラのfillコマンドを分割生成

https://cdn.magicode.io/media/notebox/264b8f73-8547-4dfe-9ce2-f741e1d3ee58.jpeg
恐らくMagicode初のマイクラ関連投稿です。

fillコマンドの仕様

本記事は統合版マイクラを前提としています。
fillコマンドは指定した範囲をブロックで埋めるマイクラのコマンドです。
/fill <from:x y z> <to: x y z> <tileName:Block> <blockStates:blockstates>
指定できる範囲には上限があり
32 ^ 3 = 32768 ブロック
までとなっています。
これは実際に埋められたブロック数(replaceの対象など)ではなく
対象となる範囲全体のブロック数です。
32768ブロック以上の範囲を指定するとエラーになります。

pythonでfillコマンドを分割する

広範囲を一度にfillすることはできないため、
fillの対象となる範囲を分割、32768ブロック以下になるようにします。

1辺の長さが32以下になるように座標を分割
fillコマンドに成形します。

定数・関数

python
# 定数
SPACE = " "
FILL = "fill"

# タプルでの位置(X, Y, Z)
X = 0
Y = 1
Z = 2

# 分割座標のfromリストを作成
def create_from_coordinates(coordinate, end_coordinate):
    
    # 最初と最後の座標が同じ場合
    if coordinate == end_coordinate:
        return [coordinate, end_coordinate]

    coordinate_divisions = []

    # 32ずつ分割
    while coordinate < end_coordinate:
        coordinate_divisions.append(coordinate)
        coordinate = coordinate + 32

    # 最後のエリアの1辺の長さが33になる場合
    if coordinate == end_coordinate:
        coordinate_divisions.append(end_coordinate - 1)
    
    # 最後の座標
    coordinate_divisions.append(end_coordinate)

    return coordinate_divisions

# fromの座標からtoの座標を生成する
def create_to_coordinate(position, coordinate_divisions):
    length = len(coordinate_divisions)
    if position == length - 2:
        # 最後の座標を返す
        return str(coordinate_divisions[length - 1])

    # 次の座標 -1 を返す
    return str(coordinate_divisions[position + 1] - 1)

座標の指定

開始座標より終了座標が大きくなるようにします。
python
# 開始座標(北西の角)
start_coordinate = (-32, 63, 1021)
# 終了座標(南東の角)
end_coordinate = (71, 126, 1203)

# 埋めるブロック
fill_block = "stone 0"

# 必要なものを一つコメントアウトしてください。
# option = "" # 指定なし
# option = "\n" # 改行コード
option = SPACE + "replace air 0" # 置換
# option = SPACE + "destroy"

fillコマンド生成

python
# 各座標軸の分割リストを生成
x_division = create_from_coordinates(start_coordinate[X], end_coordinate[X])
y_division = create_from_coordinates(start_coordinate[Y], end_coordinate[Y])
z_division = create_from_coordinates(start_coordinate[Z], end_coordinate[Z])

# 生成したfillコマンド
command = []

for x in range(len(x_division) - 1):
    x_from = str(x_division[x])
    x_to = create_to_coordinate(x, x_division)

    for y in range(len(y_division) - 1):
        y_from = str(y_division[y])
        y_to = create_to_coordinate(y, y_division)

        for z in range(len(z_division) - 1):
            z_from = str(z_division[z])
            z_to = create_to_coordinate(z, z_division)
            
            # コマンド用テキスト生成
            fill_from = x_from + SPACE + y_from + SPACE + z_from
            fill_to = x_to + SPACE + y_to + SPACE + z_to
            _command = FILL + SPACE + fill_from + SPACE + fill_to + SPACE + fill_block + option
            command.append(_command)

print("生成したfill", len(command))
# 表示
for c in command:
    print(c)

生成したfill 48 fill -32 63 1021 -1 94 1052 stone 0 replace air 0 fill -32 63 1053 -1 94 1084 stone 0 replace air 0 fill -32 63 1085 -1 94 1116 stone 0 replace air 0 fill -32 63 1117 -1 94 1148 stone 0 replace air 0 fill -32 63 1149 -1 94 1180 stone 0 replace air 0 fill -32 63 1181 -1 94 1203 stone 0 replace air 0 fill -32 95 1021 -1 126 1052 stone 0 replace air 0 fill -32 95 1053 -1 126 1084 stone 0 replace air 0 fill -32 95 1085 -1 126 1116 stone 0 replace air 0 fill -32 95 1117 -1 126 1148 stone 0 replace air 0 fill -32 95 1149 -1 126 1180 stone 0 replace air 0 fill -32 95 1181 -1 126 1203 stone 0 replace air 0 fill 0 63 1021 31 94 1052 stone 0 replace air 0 fill 0 63 1053 31 94 1084 stone 0 replace air 0 fill 0 63 1085 31 94 1116 stone 0 replace air 0 fill 0 63 1117 31 94 1148 stone 0 replace air 0 fill 0 63 1149 31 94 1180 stone 0 replace air 0 fill 0 63 1181 31 94 1203 stone 0 replace air 0 fill 0 95 1021 31 126 1052 stone 0 replace air 0 fill 0 95 1053 31 126 1084 stone 0 replace air 0 fill 0 95 1085 31 126 1116 stone 0 replace air 0 fill 0 95 1117 31 126 1148 stone 0 replace air 0 fill 0 95 1149 31 126 1180 stone 0 replace air 0 fill 0 95 1181 31 126 1203 stone 0 replace air 0 fill 32 63 1021 63 94 1052 stone 0 replace air 0 fill 32 63 1053 63 94 1084 stone 0 replace air 0 fill 32 63 1085 63 94 1116 stone 0 replace air 0 fill 32 63 1117 63 94 1148 stone 0 replace air 0 fill 32 63 1149 63 94 1180 stone 0 replace air 0 fill 32 63 1181 63 94 1203 stone 0 replace air 0 fill 32 95 1021 63 126 1052 stone 0 replace air 0 fill 32 95 1053 63 126 1084 stone 0 replace air 0 fill 32 95 1085 63 126 1116 stone 0 replace air 0 fill 32 95 1117 63 126 1148 stone 0 replace air 0 fill 32 95 1149 63 126 1180 stone 0 replace air 0 fill 32 95 1181 63 126 1203 stone 0 replace air 0 fill 64 63 1021 71 94 1052 stone 0 replace air 0 fill 64 63 1053 71 94 1084 stone 0 replace air 0 fill 64 63 1085 71 94 1116 stone 0 replace air 0 fill 64 63 1117 71 94 1148 stone 0 replace air 0 fill 64 63 1149 71 94 1180 stone 0 replace air 0 fill 64 63 1181 71 94 1203 stone 0 replace air 0 fill 64 95 1021 71 126 1052 stone 0 replace air 0 fill 64 95 1053 71 126 1084 stone 0 replace air 0 fill 64 95 1085 71 126 1116 stone 0 replace air 0 fill 64 95 1117 71 126 1148 stone 0 replace air 0 fill 64 95 1149 71 126 1180 stone 0 replace air 0 fill 64 95 1181 71 126 1203 stone 0 replace air 0

ビヘイビアーパック作成

生成したfillコマンドを1つ1つコピペして実行するのは大変なので
ビヘイビアーパックのfunctionにします。
構成
ビヘイビアーパック/ ←任意の名前
 ├ manifest.json
 └ functions/
   └ fillcommand.mcfunction ←任意の名前.mcfunction
manifest.jsonは生成サイトを利用するのが楽です。
テキストファイルに作成したfillコマンドを貼り付け
拡張子を.mcfunctionにします。
ビヘイビアーパックフォルダごとzipに圧縮します。
zipファイルの拡張子を.mcpackに書き換えるとマイクラのアイコンになります。
クリックすると自動でマイクラにインポートされます。
適当なワールドを選択または作成し、
設定画面のビヘイビアーパックから
作成したパックを選択します。
ワールドに入り、コマンドに
/function 
と入力すると作成したmcfunctionが選択できます。
実行するとmcfunctionに記述されたコマンドが一度に実行されます。
コマンドの数やマシンスペックによっては重くなるのでご注意ください。

Discussion

コメントにはログインが必要です。