Minecraft Mod development environment construction (IntelliJ IDEA + Minecraft Forge 1.15.2) + Hello World Mod creation

Overview

--Build a Minecraft Mod development environment in IntelliJ IDEA and create a simple Hello World Mod

environment

Build a development environment in IntelliJ IDEA

Java 8 installation

Java 11 or later is not supported in this development environment, so Java 8 must be installed in advance.

Reference: Install Java 8 \ (OpenJDK: AdoptOpenJDK ) on macOS with Homebrew -Qiita

Download Mdk (Mod Development Kit)

Download Mdk (Mod Development Kit) of Minecraft Forge 1.15.2-31.1.0 which is the recommended environment at the moment (as of February 16, 2020) from Minecraft Forge To do.

minecraft-forge-01.png

Extract the downloaded forge-1.15.2-31.1.0-mdk.zip.

$ unzip forge-1.15.2-31.1.0-mdk.zip 
Archive:  forge-1.15.2-31.1.0-mdk.zip
  inflating: gradlew                 
  inflating: gradlew.bat             
  inflating: CREDITS.txt             
  inflating: LICENSE.txt             
  inflating: changelog.txt           
   creating: gradle/
   creating: gradle/wrapper/
  inflating: gradle/wrapper/gradle-wrapper.properties  
  inflating: gradle/wrapper/gradle-wrapper.jar  
  inflating: .gitignore              
   creating: src/
   creating: src/main/
   creating: src/main/java/
   creating: src/main/java/com/
   creating: src/main/java/com/example/
   creating: src/main/java/com/example/examplemod/
  inflating: src/main/java/com/example/examplemod/ExampleMod.java  
   creating: src/main/resources/
  inflating: src/main/resources/pack.mcmeta  
   creating: src/main/resources/META-INF/
  inflating: src/main/resources/META-INF/mods.toml  
  inflating: build.gradle            
  inflating: README.txt              
  inflating: gradle.properties       

The setup method is described in README.txt.

If you prefer to use IntelliJ:

  1. Open IDEA, and import project.
  2. Select your build.gradle file and have it import.
  3. Run the following command: "gradlew genIntellijRuns" (./gradlew genIntellijRuns if you are on Mac/Linux)
  4. Refresh the Gradle Project in IDEA if required.

Reference: MinecraftForge / mdk at 1 \ .15 \ .x · MinecraftForge / MinecraftForge · GitHub

Import Project in IntelliJ IDEA

Create a project by specifying the folder extracted by Import Project in IntelliJ IDEA.

minecraft-forge-02.png

If the following message is output, the appropriate Java version has not been set.

> Failed to apply plugin [id 'net.minecraftforge.gradle']
   > Found java version null. Minimum required is 1.8.0_101. Versions 11.0.0 and newer are not supported yet.

IntelliJ Settings Preferences> Build, Execution, Deployment> Build Tools> Gradle> Gradle JVM requires Java 8 or later to be specified.

./gradlew genIntellijRuns

Run ./gradlew genIntellijRuns in IntelliJ IDEA Terminal etc.

$ ./gradlew genIntellijRuns

Since Java 8 or later is used here as well, set it as necessary. Reference: Install Java 8 \ (OpenJDK: AdoptOpenJDK ) on macOS with Homebrew -Qiita

Launch Minecraft Forge for Development

Run Tasks> fg_runs> runClient from the Gradle Tool Window in the upper right corner of IntelliJ IDEA, or run ./gradlew runClient from Terminal etc. to start Minecraft.

minecraft-forge-03.png

If you click "Mods", you can see that the Example Mod included in Mdk is loaded.

minecraft-forge-04.png

About Example Mod

The source code of Example Mod is included in Mdk as a sample of mod development.

Source code list.

You can also check the contents of the source code at MinecraftForge / mdk at 1 \ .15 \ .x · MinecraftForge / MinecraftForge · GitHub Possible.

Creating a Hello World Mod

Create a mod that realizes a simple Hello World on the built development environment.

Source code list

src/main/java/com/example/HelloWorldMod.java

Write a Java class that executes the main processing of the mod.

package com.example;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod("helloworldmod") // META-INF/mods.modId described in toml
public class HelloWorldMod {

  public HelloWorldMod() {
    // MinecraftForge.EVENT_Object with handler method for events to be processed by BUS(this)Register
    MinecraftForge.EVENT_BUS.register(this);
  }

  /**
   *An event handler method that runs when the player logs in.
   *The method name can be anything.
   *
   * @param event PlayerLoggedInEvent object
   */
  @SubscribeEvent
  public void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
    PlayerEntity player = event.getPlayer();
    BlockPos pos = player.getPosition();
    String message =
      "Hello, World!\n"
        + "[name]=[" + player.getName().getFormattedText() + "]\n"
        + "[pos]=[" + pos.getX() + "," + pos.getY() + "," + pos.getZ() + "]";
    ITextComponent text = new StringTextComponent(message);
    player.sendMessage(text);
  }
}

src/main/resources/pack.mcmeta

File for describing resource pack information. Without this file, an error such as "failed to load a valid ResourcePackInfo" will occur. Only the minimum contents are described here.

{
  "pack": {
    "description": "Hello World mod resources",
    "pack_format": 5
  }
}

src/main/resources/META-INF/mods.toml

Mod information description file. Only the minimum contents are described here.

modLoader="javafml"
loaderVersion="[31,)"

[[mods]]
modId="helloworldmod"
version="1.2.3"
displayName="Hello World Mod"
description='''
This is the ...
Hello World mod.
'''

build.gradle

Build configuration file for Gradle. Only the minimum contents are described here.

buildscript {
  repositories {
    maven { url = 'https://files.minecraftforge.net/maven' }
    jcenter()
    mavenCentral()
  }
  dependencies {
    classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true
  }
}
apply plugin: 'net.minecraftforge.gradle'

version = '1.2.3'
group = 'com.example.helloworld'
archivesBaseName = 'helloworldmod'

sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'

minecraft {
  mappings channel: 'snapshot', version: '20190719-1.14.3'
  runs {
    client {
      workingDirectory project.file('run')
      property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
      property 'forge.logging.console.level', 'debug'
      mods {
        helloworldmod {
          source sourceSets.main
        }
      }
    }
  }
}

dependencies {
  minecraft 'net.minecraftforge:forge:1.15.2-31.1.0'
}

Generate a jar file for distribution

Run Tasks> build> build from the Gradle Tool Window in the upper right corner of IntelliJ IDEA, or run ./gradlew build from Terminal etc.

$ ./gradlew build

The jar file for distribution is output to the build / libs directory.

$ ls build/libs/
helloworldmod-1.2.3.jar

Run in Minecraft Forge

Launch the regular Minecraft Java Edition Minecraft Launcher and configure the configuration for Minecraft Forge 1.15.2-31.1.0.

minecraft-forge-05.png

Place the created jar file in the mods directory of the game directory with the configuration you set, and start Minecraft Forge 1.15.2-31.1.0.

minecraft-forge-08.png

You can see that the Hello World Mod is loaded.

minecraft-forge-06.png

When you enter the world, Hello World is displayed in the chat column.

minecraft-forge-07.png

Reference material

Recommended Posts

Minecraft Mod development environment construction (IntelliJ IDEA + Minecraft Forge 1.15.2) + Hello World Mod creation
Development environment construction using IntelliJ IDEA + Maven + Tomcat 9
Try modding with Minecraft Forge 1.15.1 ① [Building development environment] [Multiple versions supported] [IntelliJ IDEA]
Minimal Java environment construction and Hello World
Error summary when creating Minecraft MOD development environment
Introduction to Slay the Spire Mod Development (2) Development Environment Construction
java development environment construction
JavaFX application development with IntelliJ IDEA and Gradle ~ From environment construction to sample code ~
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Rails6 development environment construction [Mac]
Error summary when creating Minecraft MOD development environment
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
Introduction to Slay the Spire Mod Development (3) Original Card Definition
Minecraft Mod development environment construction (IntelliJ IDEA + Minecraft Forge 1.15.2) + Hello World Mod creation
Development of Flink using DataStream API
[Rails 6] API development using GraphQL (Query)
minecraft1.14.4 MOD development memorandum 1 [Original recipe]
Error summary when creating Minecraft MOD development environment
DSL development using ANTLR 4.7.1
Environment construction for Servlet application development
[Unity] Android development environment construction procedure
(Intellij) Hello World with Spring Boot
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation
Spring Boot2 Web application development with Visual Studio Code Hello World creation