[Java] [Java]Let’s create a Minecraft Mod 1.14.4 [0. Basic files]

6 minute read

(This article is one of a series of commentary articles)

First article: Introduction Previous article: Introduction Next article: 1. Add Items

Basic file

With the development environment in place, we are finally at the starting line. Let’s prepare for your future Modding. I think that you can imitate this area without thinking about it now.

GroupId and ArtifactId

First, set GroupId and ArtifactId. This is a request from Java rather than Minecraft, but I am not familiar with it, so I will omit it.

There is a general convention for naming packages.

-GroupId: Organization name. Generally the domain is written in reverse. -ArtifactId: Name of the project

So I did the following.

Item Value
GroupId jp.koteko
ArtifactId example_mod

Please change according to yourself. Rename the folder based on this.

Before change


D:\projects\mc_example_mod\src\main\java
  └ com
      └ example
└ examplemod
└ ExampleMod.java

After change


D:\projects\mc_example_mod\src\main\java
  └ jp
     └ koteko
          └ example_mod
              └ ExampleMod.java

assets folder

Next, create an assets\example_mod folder in which you will place files such as textures and sound effects.

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

pack.mcmeta

So what are the files already placed? Let’s look at each one.

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."
    }
}

The pack.mcmeta file is a file that describes the details of the resource pack. Please refer to Wiki for details. Feel free to change the description and delete the _comment line as it is unnecessary.

After change


{
    "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 ipsumbus primut. 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 tinremdsum condount justo 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"

```The `mods.toml` file is a file that describes Mod information.
In addition to information such as dependencies, information displayed on the screen when installing mod is also included here, so change it if necessary.
![Capture.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/256627/854c9ed8-bf65-c282-1109-c34d8ba42236.png)

It's a long time, but it's just a comment that explains each item, so let's delete it (let's refer to it when necessary). `mandatory` is required, and `optional` is optional item. Below is an example of editing appropriately.


#### **` after change`**
```toml

modLoader="javafml"
loaderVersion="[28,)"
[[mods]]
modId="example_mod"
version="${file.jarVersion}"
displayName="Example Mod"
logoFile="logo.png"
description='''
here
    Explain
        input
'''

[[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"

Also, I placed logo.png directly under resources as the logo image of the mod.

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

This changed the screen display as follows. Capture.PNG

Main file

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");
        }
    }
}

This is the main mod file. First of all, I think that you can use it as it is, and rewrite it as needed if you understand it to some extent.It is important to note here that the file name (ExampleMod.java) matches the class name (public class ExampleMod) and the constructor (public ExampleMod()), and the modId specification ( Is @Mod("examplemod")) the same as the one specified in mods.toml? If it is different, fix it.

Reference

Minecraft 1.14.4 Forge Mod Part 2 [Basic File Placement]

Next article

1. Add Item