0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Minecraft 1.20.x forgeでMOD開発 サーバー側でコマンド実行

Last updated at Posted at 2023-06-19

はじめに

Minecraftで夜になったら色々するイベントを作りたくてMOD制作に足を踏み入れたんですが、想像以上に難しかったので記事にしていきます。
基本的にサーバー側の実装のみで、プレイヤーはバニラ環境を想定しています。
Minecraftはたまに遊ぶ程度で、forgeについてはHello Worldレベルなのでコードの細かい説明は省きます。(できない)

今回やりたいこと

以下のコマンドをサーバー側で実行させたい。

/title @a title {"text":"Chapter I","bold":true}

なぜサーバー側で?

マルチでログインしているプレイヤーにパーミッションを与えたくないから。

コード

プレイヤーがサーバーにログイン時に上記コマンドをサーバー側で実行させるコード

    @SubscribeEvent
    public void OnPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
        // Playerのインスタンス
        Player player = event.getEntity();
        // コマンド
        String text = "title @a title {\"text\":\"Chapter I\",\"bold\":true}";
        // 実行する際のパーミッション?
        CommandSourceStack commandSourceStack = player.createCommandSourceStack().withSuppressedOutput().withPermission(4);

        // コマンドの実行権生成
        CommandDispatcher<CommandSourceStack> commanddispatcher = player.getServer().getCommands().getDispatcher();
        // コマンド文字列の解析
        ParseResults<CommandSourceStack> results = commanddispatcher.parse(text, commandSourceStack);
        // 実行 成功なら1 失敗なら0を返す
        if (serverPlayer.getServer().getCommands().performCommand(results, text) != 1){
            LOGGER.debug("-------- Title Command Failure ---------");
        }
    }

コメントは推測で書いています。

commanddispatcher.parse(text, commandSourceStack);

中を追っていくとparseの段階でそれぞれのコマンドのクラスからオブジェクトを生成しているみたい?
この段階でコマンドが間違っていると空のオブジェクトを返しているのでここで判定したほうがいいかもしれません。

Minecraftの実行基盤のJavaは8なことが普通みたいなので型推論とかは使わない方がいいと思います。。。

結果

Minecraft 1.20.1 2023_06_20 0_38_04.png

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?