LoginSignup
2
3

More than 1 year has passed since last update.

Eclipse+Forgeでマイクラのmod開発~最小限の作業で空のmodを作る

Last updated at Posted at 2021-05-05

image.png
デバッグ環境を整えるところまでは以前の記事と同じです。以前の記事ではMdkにもともと含まれているサンプルのmodを動かしただけなので、今回はオリジナルのmodを作ります。といっても、modに色々させようと思うと学習しないとダメなことが大量にあるので今回は空っぽのmodを最少限度の作業で作ってみましょ。

環境

  • OS: Windows10
  • Jdk: openjdk version "15.0.2" 2021-01-19
    ※16.xでもいいかもだけどgradleやらなにやら色々変わってしまって心配だったので今回は15で。
  • IDE: Eclipse IDE for Java Developers (Version 4.19.0)
  • Buildship(Eclipseのgradleプラグイン): 3.0
  • Minecraft: Java版 1.16.5
  • Forge: 1.16.5

modの大元になるクラスの作成。

まずはサンプルコードのsrc\main\java\com\example\examplemodを削除します。comディレクトリ事バッサリ削除してOKです。

空のクラスを一個作ります。クラス名、パッケージなどなどなんでもOK。サンプルコードを載せておきます。

EmptyMod.java
package jp.munecraft.mod.test;

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

import net.minecraftforge.fml.common.Mod;

// ★詳細後述
@Mod("emptymod")
public class EmptyMod {

    // forgeがもともとlog4jを使っているので気楽にLoggerが使えます。
    private static final Logger LOGGER = LogManager.getLogger();

    public EmptyMod() {
        // 今回はログを吐くだけ。
        LOGGER.info("Welcome to munecraft mod!!!");
    }   
}

空っぽです。コンストラクタが呼ばれたらログを吐くだけ。ポイントは@Modアノテーションですね。このアノテーションを付けたクラスがmodのメインのクラスと認識されます。引数の文字列はmodIdと呼ばれるもので、後でも出てくるので間違えないように同じものを使ってください。
ちなみにmodIdに使える文字や長さには制限があり、forgeのソースによると

Required to be lowercased in the english locale for compatibility. Will be truncated to 64 characters long.

だそうです。試してみましたが、確かに大文字や_といった記号を使うと起動時にエラーになりました。

modの設定ファイルの作成

いじるファイルはsrc/main/resources/META-INF/mods.tomlだけです。中身を見てみよう。

mods.toml
# 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).

# (中略)

# A URL to refer people to when problems occur with this mod
#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional
# A list of mods - how many allowed here is determined by the individual mod loader
[[mods]] #mandatory
# The modid of the mod
modId="emptymod" #mandatory ★ここ!
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
# ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata
# see the associated build.gradle script for how to populate this completely automatically during a build
version="${file.jarVersion}" #mandatory
 # A display name for the mod
displayName="Empty mod by munecraft" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/
#updateJSONURL="https://change.me.example.invalid/updates.json" #optional

# (後略)

最低限いじらないとダメなのは[[mods]]セクションのmodIdだけです。ここを先ほど@Modアノテーションで指定したmodIdにそろえなくてはなりません。

動かしてみる

以前の記事でビルドスクリプトをインポート済みであれば、EclipseからrunClientが簡単にできるようになっているはず。で、実行。。

[17:06:16] [Render thread/DEBUG] [ne.mi.fm.ja.FMLModContainer/LOADING]: Creating FMLModContainer instance for jp.munecraft.mod.test.EmptyMod with classLoader cpw.mods.modlauncher.TransformingClassLoader@4b039c6d & cpw.mods.modlauncher.TransformingClassLoader@4b039c6d
[17:06:16] [modloading-worker-1/DEBUG] [ne.mi.ve.fo.ForgeVersion/CORE]: Forge Version package package net.minecraftforge.versions.forge, Forge, version 36.1 from cpw.mods.modlauncher.TransformingClassLoader@4b039c6d
[17:06:16] [modloading-worker-1/DEBUG] [ne.mi.ve.fo.ForgeVersion/CORE]: Found Forge version 36.1.0
[17:06:16] [modloading-worker-1/DEBUG] [ne.mi.ve.fo.ForgeVersion/CORE]: Found Forge spec 36.1
[17:06:16] [modloading-worker-1/DEBUG] [ne.mi.ve.fo.ForgeVersion/CORE]: Found Forge group net.minecraftforge
[17:06:16] [modloading-worker-2/INFO] [jp.mu.mo.te.EmptyMod/]: Welcome to munecraft mod!!!
[17:06:16] [modloading-worker-1/DEBUG] [ne.mi.ve.mc.MCPVersion/CORE]: Found MC version information 1.16.5
[17:06:16] [modloading-worker-1/DEBUG] [ne.mi.ve.mc.MCPVersion/CORE]: Found MCP version information 20210115.111550
[17:06:16] [modloading-worker-1/INFO] [ne.mi.co.ForgeMod/FORGEMOD]: Forge mod loading, version 36.1.0, for MC 1.16.5 with MCP 20210115.111550

無事にWelcome to munecraft mod!!!がコンソールに表示されました!ね?簡単でしょ?本当はこの記事でいじった以外にもいっぱい設定がありますが、それはまた後々。。

2
3
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
2
3