LoginSignup
1
1

More than 3 years have passed since last update.

[Java]MinecraftのModを作成しよう 1.14.4【0. 基本ファイル】

Last updated at Posted at 2020-08-03

(この記事は一連の解説記事の一つになります)

先頭記事:入門編
前の記事:入門編
次の記事:1. アイテムの追加

基本ファイル

開発環境が整い、ようやくスタートラインに立てました。これからあなたがModdingを進めていくための下準備をしましょう。この辺りは今は深く考えず真似すればいいと思います。

GroupIdとArtifactId

まずはGroupIdおよびArtifactIdを設定します。これはMinecraftどうこうではなく、Javaの方の要求っぽいですが詳しくないので省略します。

パッケージの名前の付け方には一般的な決め方があります。

  • GroupId: 組織名のこと。一般的にはドメインを逆から書く形式
  • ArtifactId: プロジェクトの名前

とのことなので、以下のようにしました。

項目
GroupId jp.koteko
ArtifactId example_mod

適宜自分に合わせて変更してくださいね。これをもとにフォルダをリネームします。

変更前
D:\projects\mc_example_mod\src\main\java
  └ com
      └ example
          └ examplemod
              └ ExampleMod.java
変更後
D:\projects\mc_example_mod\src\main\java
  └ jp
     └ koteko
          └ example_mod
              └ ExampleMod.java

assetsフォルダ

次にテクスチャや効果音などのファイルを配置する assets\example_mod フォルダを作成しておきます。

D:\projects\mc_example_mod\src\main\resources
   ├ assets
   │  └ example_mod
   ├ META-INF
   │   └ mods.toml
   └ pack.mcmeta

pack.mcmeta

さて、すでに配置されているファイルは何なのでしょうか?一つずつ見ていきましょう。

pack.mcmeta
{
    "pack": {
        "description": "examplemod resources",
        "pack_format": 4,
        "_comment": "A pack_format of 4 requires json lang files. Note: we require v4 pack meta for all mods."
    }
}

pack.mcmeta ファイルはリソースパックの詳細を記載するファイルです。
詳しくはWikiを参照しましょう。description は自由に変え、 _comment の行は不要なので消してしまいましょう。

変更後
{
    "pack": {
        "description": "Example Mod resources",
        "pack_format": 4
    }
}

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).
# 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="[28,)" #mandatory (28 is current forge version)
# 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="examplemod" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
version="${file.jarVersion}" #mandatory
 # A display name for the mod
displayName="Example 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

Have some lorem ipsum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mollis lacinia magna. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed sagittis luctus odio eu tempus. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque volutpat ligula eget lacus auctor sagittis. In hac habitasse platea dictumst. Nunc gravida elit vitae sem vehicula efficitur. Donec mattis ipsum et arcu lobortis, eleifend sagittis sem rutrum. Cras pharetra quam eget posuere fermentum. Sed id tincidunt justo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
'''
# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional.
[[dependencies.examplemod]] #optional
    # the modid of the dependency
    modId="forge" #mandatory
    # Does this dependency have to exist - if not, ordering below must be specified
    mandatory=true #mandatory
    # The version range of the dependency
    versionRange="[28,)" #mandatory
    # An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory
    ordering="NONE"
    # Side this dependency is applied on - BOTH, CLIENT or SERVER
    side="BOTH"
# Here's another dependency
[[dependencies.examplemod]]
    modId="minecraft"
    mandatory=true
    versionRange="[1.14.4]"
    ordering="NONE"
    side="BOTH"

mods.toml ファイルはMod情報を記載するファイルです。
依存などの情報に加え、mod導入時の画面に表示される情報もここに含まれるため、必要に応じて変更しましょう。
キャプチャ.PNG

長いですが、各項目の説明がコメントで書かれているだけなので消しましょう(必要になった時参照できるようにはしておきましょう)。mandatory は必須、 optional は任意項目です。以下に適当に編集した例を示します。

変更後
modLoader="javafml"
loaderVersion="[28,)"
[[mods]]
modId="example_mod"
version="${file.jarVersion}"
displayName="Example Mod"
logoFile="logo.png"
description='''
ここに
    説明を
        入力
'''

[[dependencies.example_mod]]
    modId="forge"
    mandatory=true
    versionRange="[28,)"
    ordering="NONE"
    side="BOTH"

[[dependencies.example_mod]]
    modId="minecraft"
    mandatory=true
    versionRange="[1.14.4]"
    ordering="NONE"
    side="BOTH"

また、Modのロゴ画像として logo.pngresources 直下に配置しました。

D:\projects\mc_example_mod\src\main\resources
   ├ assets
   │  └ example_mod
   ├ META-INF
   │   └ mods.toml
   ├ logo.png
   └ pack.mcmeta

これによって画面表示は以下のように変わりました。
キャプチャ.PNG

メインファイル

ExampleMod.java
package jp.koteko.example_mod;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.stream.Collectors;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("example_mod")
public class ExampleMod
{
    // Directly reference a log4j logger.
    private static final Logger LOGGER = LogManager.getLogger();

    public ExampleMod() {
        // Register the setup method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        // Register the enqueueIMC method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
        // Register the processIMC method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
        // Register the doClientStuff method for modloading
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);

        // Register ourselves for server and other game events we are interested in
        MinecraftForge.EVENT_BUS.register(this);
    }

    private void setup(final FMLCommonSetupEvent event)
    {
        // some preinit code
        LOGGER.info("HELLO FROM PREINIT");
        LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
    }

    private void doClientStuff(final FMLClientSetupEvent event) {
        // do something that can only be done on the client
        LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings);
    }

    private void enqueueIMC(final InterModEnqueueEvent event)
    {
        // some example code to dispatch IMC to another mod
        InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
    }

    private void processIMC(final InterModProcessEvent event)
    {
        // some example code to receive and process InterModComms from other mods
        LOGGER.info("Got IMC {}", event.getIMCStream().
                map(m->m.getMessageSupplier().get()).
                collect(Collectors.toList()));
    }
    // You can use SubscribeEvent and let the Event Bus discover methods to call
    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
        // do something when the server starts
        LOGGER.info("HELLO from server starting");
    }

    // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
    // Event bus for receiving Registry Events)
    @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
    public static class RegistryEvents {
        @SubscribeEvent
        public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
            // register a new block here
            LOGGER.info("HELLO from Register Block");
        }
    }
}

これがmodのメインとなるファイルです。まずはそのまま使って、ある程度理解が進んできたら随時書き換えなど行えばよいと思います。
ここで注意したいのは、ファイル名(ExampleMod.java)とクラス名(public class ExampleMod)およびコンストラクタ(public ExampleMod())が一致しているかという点と、modIdの指定(@Mod("examplemod"))が mods.toml での指定と同じであるかという点です。違った場合修正しましょう。

参考

Minecraft 1.14.4 Forge Modの作成 その2 【基本的なファイルの配置】

次の記事

1. アイテムの追加

1
1
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
1
1