【Java】让我们来创建Minecraft的Mod吧!1.14.4【0. 基本文件】

这篇文章是一系列解释性文章中的一篇。

标题:入门篇
上一篇:入门篇
下一篇:1. 添加物品

基本文件

经过开发环境的准备,你终于站在了起跑线上。现在让我们为您推进Modding做好一些准备工作。我认为在这个阶段,您只需要模仿,不需要深入思考。

项目组ID和项目ID

首先要设置GroupId和ArtifactId。这与Minecraft无关,看起来更像是Java方面的要求,但由于不太了解,我将省略此部分。

在给包命名时有一般的惯例。

GroupId:指代组织名称。通常以反向顺序写明域名形式。
ArtifactId:指代项目名称。

因此,我根据情况做了以下处理。

項目値GroupIdjp.kotekoArtifactIdexample_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\example_mod 的文件夹,用于放置纹理和音效文件。

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

pack.mcmeta 可以被翻译为 完整的包文件.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文件是用于记录资源包详细信息的文件。
详细信息请参考维基百科。可以自由地更改描述,不需要_comment行,可以删除。

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

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"

キャプチャ.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.png 安放在 resources 目錄下。

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

主文件

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. 添加物品

广告
将在 10 秒后关闭
bannerAds