【MinecraftConnection 1.1.0】ライブラリメモ #6

自作ライブラリのメモその6です。下書きのままで公開するのを忘れていたので、今更ですが公開します。現在、2.0.0 に向けて開発を進めているので、古い情報にはなってしまいますが 1.1.0 で進める場合はこれを利用できます。

エンチャント本を作る

名前空間 : MinecraftConnection.Items

エンチャント本作成のためのクラス : EnchantedBook
コンストラクタ : Dictionary<Enchantments, int>

エンチャント本を作成するには、エンチャント効果とそのレベルが必要なので、Dictionary を使用してみました。

var Enchant = new Dictionary<Enchantments, int>();
Enchant.Add(Enchantments.AquaAffinity, 1);
Enchant.Add(Enchantments.FireProtection, 2);
Enchant.Add(Enchantments.FeatherFalling, 1);
Enchant.Add(Enchantments.Fortune, 3);

上から順番に水中採掘、火炎耐性、落下耐性、幸運のエンチャントとそのレベルを記述しています。これを EnchantedBook のコンストラクタに投げることで、これらの効果をもつエンチャント本のインスタンスが作れます。ちなみに、エンチャントの種類は列挙型で定義しており、こんな感じになっています。

これでエンチャントのIDが分からなくてもインテリコードで補完できます。

(余談ですが、<para> を使うと説明を改行できるんですね...。)

EnchantedBook enchantedBook = new EnchantedBook(Enchant);

これで上記の効果をもつエンチャント本インスタンスができました。

エンチャント本を与える例

プレイヤーにエンチャント本を与えるには GiveEnchantedBook() メソッドを使用します。

  • GiveEnchantedBook(string PlayerName, EnchantedBook Book, int Count)

引数はプレイヤー名、エンチャント本のインスタンス、本を渡す数量です。

class Program
{
    private static IPAddress Address = IPAddress.Parse("127.0.0.1");
    private static ushort Port = 25575;
    private static string Pass = "minecraft";
    private static MinecraftCommands Command = new MinecraftCommands(Address, Port, Pass);

    static void Main(string[] args)
    {
        string PlayerName = "takunology";

        var Enchant = new Dictionary<Enchantments, int>();
        Enchant.Add(Enchantments.AquaAffinity, 1);
        Enchant.Add(Enchantments.FireProtection, 2);
        Enchant.Add(Enchantments.FeatherFalling, 1);
        Enchant.Add(Enchantments.Fortune, 3);

        EnchantedBook enchantedBook = new EnchantedBook(Enchant);
        Command.GiveEnchantedBook(PlayerName, enchantedBook, 1);
    }
}

実行してみると、Dictionary で定義したエンチャントとそのレベルが付与された本が手に入ります。

ちなみに、このエンチャント本を与えるコマンドはこのようになっています。

/give <PlayerName> enchanted_book{StoredEnchantments:[{id:fire_protection,lvl:2},{id:feather_falling,lvl:1},{id:aqua_affinity,lvl:1},{id:fortune,lvl:3}]} 1

これを手で打つのは非常に面倒ですが、エンチャントとレベルを指定した本のインスタンスを引数にした GiveEnchantedBook メソッドを呼び出すだけで楽できますね。

イベントのお知らせ

2022年6月19日(日)に MS Tech Camp #16 学生によるLT大会!!を開催します。Microsoft に関するテーマであれば自由で、プログラミング関係以外にも例えば Office 関係や Windows の便利機能などであれば何でもOKです!

mspjp.connpass.com

当日は Youtube ライブ配信にてお送りします。LT 枠もまだまだ募集中ですので、ぜひご参加ください~