このシリーズのまとめはこちら:
umagame.hatenablog.jp
どうも。
はじめに
今回はコマンドの追加をやります。
進捗とか作物ブロックの追加を一緒にこの記事で説明しようと思っていたのですが、時間かかりそうだったのでやめました。
なのでこの記事は短めです。
前回:
umagame.hatenablog.jp
コマンドの追加
コマンドのクラス
まずはコマンドのクラスをcommand/implに作ります。

今回は/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に作ります。

以前と同じように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"
と追加します。(細かい詳細を書こうとしたのですが、はめんどくさかったのでやめました。)
とりあえずこれで完成です。


source ->のあとの{}の中が、コマンドの内容なのでここをいじれば好きなようにコマンドを作れます。
さいごに
今回はここまでです。
次回はディメンションかも。