初心者modderの備忘録

マイクラのmodを作りたくて、初めて見たのですが難しくて忘れそうなので自分用の備忘録も兼ねてブログにしようと思います

1日目 Modメインファイル(Forge1.15.2)

前回(約1年前)の記事を書いたときは1.13.2がforgeの最新版だったので環境設定は1.13.2になっていますが1.15.2でも同様の手順で出来ました。

とりあえず今回からは現時点(2020/3/15)で最新の1.15.2に関して更新していこうと思います。
前回までで環境設定が終わったので、今回はModのメインファイルを作っていこうと思います。

まず/src/main/javaにデフォルトで入っているパッケージおよびファイルを削除して、そこに新たにパッケージを作成して、その中にメインとなるクラスを作成します。
今回の場合はtestmodというパッケージにTestMod.javaを作成しています。

f:id:json_fileman:20200315111210p:plain


続いて、TestMod.javaの中身を記述します。

package testmod;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

@Mod(TestMod.MOD_ID)
public class TestMod
{
	public static TestMod instance;
	public static final String MOD_ID = "testmod";
	public static final Logger LOGGER = LogManager.getLogger();

	public TestMod()
	{
		instance = this;

		FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
		FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientRegistries);

		MinecraftForge.EVENT_BUS.register(this);
	}

	private void setup(final FMLCommonSetupEvent event)
	{
		LOGGER.info("setup method");
	}

	private void clientRegistries(final FMLClientSetupEvent event)
	{
		LOGGER.info("clientRegistries method");
	}
}


1.12以前と1.13以降でこの辺りが結構変わってますね。
setupメソッドが以前のpreinitに当たるらしいです。で、clienetRegistriesがクライアント側のみの処理なので以前のproxy等が必要なくなったようです。

とは言っても、このへん詳しいことは正直よくわかっていません。
詳しくわかったら、また追記するかもしれませんが、それまでは"おまじない"状態です。



これでメインのクラスの最低限の記述は終わったのですが、このまま起動するとエラーが出ます。
/src/main/resources/META-INFに入っているmods.tomlにmodの情報を記載する必要があります。

# This is an example mods.toml file. It contains the data relating to the loading mods.
# There are several mandatory fields (#mandatory), and many more that are optional (#optional).
# The overall format is standard TOML format, v0.5.0.
# Note that there are a couple of TOML lists in this file.
# Find more information on toml format here:  https://github.com/toml-lang/toml
# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml
modLoader="javafml" #mandatory
# A version range to match for said mod loader - for regular FML @Mod it will be the forge version
loaderVersion="[31,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions.
# A URL to refer people to when problems occur with this mod
issueTrackerURL="http://my.issue.tracker/" #optional
# A list of mods - how many allowed here is determined by the individual mod loader
[[mods]] #mandatory
# The modid of the mod
modId="testmod" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
version="0.1" #mandatory
 # A display name for the mod
displayName="Test Mod" #mandatory
# A URL to query for updates for this mod. See the JSON update specification <here>
updateJSONURL="http://myurl.me/" #optional
# A URL for the "homepage" for this mod, displayed in the mod UI
displayURL="http://example.com/" #optional
# A file name (in the root of the mod JAR) containing a logo for display
logoFile="examplemod.png" #optional
# A text field displayed in the mod UI
credits="Thanks for this example mod goes to Java" #optional
# A text field displayed in the mod UI
authors="Love, Cheese and small house plants" #optional
# The description text for the mod (multi line!) (#mandatory)
description='''
This is a long form description of the mod. You can write whatever you want here

全部のせると長くなるので前半を抜粋しました。

とりあえず15行目のmodidだけ変更して自分のmodidと一致していればエラーは出ないと思います。今回はそのほかに17行目のバージョン、19行目の表示名を変更しました。
#から始まっている行はコメントなので消しても問題ありません。また行末に#optinalとなっている行も消して大丈夫です。


これで実行してみてmodの欄に自分のmodが表示されていればうまく動いてるということになります。


f:id:json_fileman:20200315114141p:plain


次回は無機能アイテムの追加をしたいと思います。