LoginSignup
4
2

More than 1 year has passed since last update.

C#でMinecraftを操作する【中編】

Last updated at Posted at 2022-12-21

C# ではじめるマインクラフトプログラミングという無料で読める本を見つけた。前編に引き続き触ってみる。

動作環境

以下の環境で動作させる。Minecraftマルチプレイ用のサーバを構築する必要がある。

  • Minecraft 1.19.3
  • OpenJDK 17.0.4
  • Visual Studio 2022 17.4.3
  • .NET 6.0.307
  • MinecraftConnection 2.0.1

自動建築

setblockコマンドとC#の言語機能をうまく組み合わせて、設計図から建築物を作りたい。csvファイルなどが手っ取り早いのだろうが、Excelと違ってシートを複数枚持てない。書籍でも紹介されているように、Excelブックを読み込むことができるライブラリ「NPOI」を利用する。


画像のようにy座標ごとに輪切りにした建物の設計図を作る。

実装

Excelブックを読み込む実装はLoaderクラスとして分離した。

Loader.cs
using NPOI.SS.UserModel;

namespace MinecraftAutoBuild
{
    public class Loader
    {
        private string filePath;
        public readonly List<List<List<string>>> BlockId = new List<List<List<string>>>();

        public Loader(string filePath)
        {
            this.filePath = filePath;
            try
            {
                getData();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void getData()
        {
            var book = WorkbookFactory.Create(filePath);
            var sheetsCnt = book.NumberOfSheets;

            for(int i = 0; i < sheetsCnt; i++)
            {
                List<List<string>> rows = new List<List<string>>();
                var sheet = book.GetSheetAt(i);
                int rowsCnt = sheet.LastRowNum + 1;

                for(int j = 0; j < rowsCnt; j++)
                {
                    List<string> cols = new List<string>();
                    var row = sheet.GetRow(j);
                    int colsCnt = row.LastCellNum;

                    for(int k = 0; k < colsCnt; k++)
                    {
                        var cell = row.GetCell(k);
                        cols.Add(cell.StringCellValue);
                    }
                    rows.Add(cols);
                }
                BlockId.Add(rows);
            }
        }
    }
}

ブロックの配置をExcelブックから読み出して3次元リストに格納する。

Program.cs
using MinecraftAutoBuild;
using MinecraftConnection;

string address = "192.168.0.***";
ushort port = *****;
string pass = "*********";
MinecraftCommands command = new MinecraftCommands(address, port, pass);

string filePath = "../../../../tofu_house.xlsx";
var buildData = new Loader(filePath);
var TofuHouse = buildData.BlockId;

int x = -353;
int y = 81;
int z = -84;

for (int i = 0; i < TofuHouse.Count; i++)
{
    for (int j = 0; j < TofuHouse[i].Count; j++)
    {
        for (int k = 0; k < TofuHouse[i][j].Count; k++)
        {
            command.SendCommand($"setblock {x + j} {y + i} {z + k} {TofuHouse[i][j][k]}");
            command.Wait(10);
        }
    }
}

リストの中身を3重for文でsetblockしている。

動かしてみる

簡単な豆腐建築を作ってみる。

3Dプリンタみたいな動きをしている。

最後に

書けることがたくさんありそうだったのでとりあえず中編ということにしたが、後編に何を書くかは全く決まっていない。

4
2
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
4
2