うまげーむさん

ゲームの情報を主に投稿します。

【マインクラフト Modding】1.15対応 自作MODの作り方 #11 コマンド

このシリーズのまとめはこちら:

umagame.hatenablog.jp

どうも。

はじめに

今回はコマンドの追加をやります。

進捗とか作物ブロックの追加を一緒にこの記事で説明しようと思っていたのですが、時間かかりそうだったのでやめました。

なのでこの記事は短めです。

前回:

umagame.hatenablog.jp

コマンドの追加

コマンドのクラス

まずはコマンドのクラスをcommand/implに作ります。

f:id:Umagame:20200325215410p:plain

今回は/dirtmodと入力すると、Modの詳細がチャットに出るようにしたいと思います。

package com.umagame.dirtmod.command.impl;

import com.mojang.brigadier.CommandDispatcher;

import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.util.text.TranslationTextComponent;

public class CommandDirtMod {
    public static void register(CommandDispatcher<CommandSource> dispatcher) {
        dispatcher.register(Commands.literal("dirtmod").executes(source -> {
            source.getSource().sendFeedback(new TranslationTextComponent("command.dirtmod"), true);
            return 1;
        }));
    }
}

今回は、以前の記事と同じようにTranslationTextComponentを使って、言語によってテキストが変わるようにします。

"dirtmod"のところがコマンドになります。

コマンドのクラスはこれでOKです。

コマンドの登録

アイテムやブロックと同じように、コマンドもゲームに登録する必要があります。

登録用のクラスをinitに作ります。

f:id:Umagame:20200325220826p:plain

以前と同じようにSubscribeEventで登録します。

package com.umagame.dirtmod.init; 

import com.umagame.dirtmod.command.impl.CommandDirtMod; 
import com.umagame.dirtmod.main.DirtMod; 
import net.minecraftforge.eventbus.api.SubscribeEvent; 
import net.minecraftforge.fml.common.Mod; 
import net.minecraftforge.fml.event.server.FMLServerStartingEvent; 

@Mod.EventBusSubscriber(modid = DirtMod.MOD_ID) 
public class DirtModCommands { 
    @SubscribeEvent 
    public static void onServerStarting(final FMLServerStartingEvent event) { 
        CommandDirtMod.register(event.getCommandDispatcher());
    } 
}

langファイル

TranslationTextComponentを使っているので、langファイルをいじります。

ja_jpに

"command.dirtmod": "DirtMod バージョン1.0"

en_usに

"command.dirtmod": "DirtMod Version1.0"

と追加します。(細かい詳細を書こうとしたのですが、はめんどくさかったのでやめました。)

とりあえずこれで完成です。

f:id:Umagame:20200325221544p:plain

f:id:Umagame:20200325221555p:plain

source ->のあとの{}の中が、コマンドの内容なのでここをいじれば好きなようにコマンドを作れます。

さいごに

今回はここまでです。

次回はディメンションかも。