我的世界Minecraft Mod开发学习笔记 - Mod开发环境设置和Hello World示例

时间:2022-08-16 20:08:50

概述

本文通过一个简单的Helll World Mod示例, 介绍了使用Forge MDK (Mod Development Kit)开发Minecraft Mod的基本环境设置和过程.

运行环境

  • Java 1.8 JDK

  • Minecraft Java Edition 1.12.2

安装Forge和MDK

  • Minecraft Forge官网下载Minecraft Forge - MC 1.12.2 installer和Mod Development Kit (MDK)包.

  • 运行Forge installer安装Forge

  • 将MDK压缩包解压的指定目录, 例如C:\mdk.

设置MDK

在MDK目录下, 运行如下命令

gradlew setupDecompWorkspace

项目文件结构

MDK设置完毕后会生成以下的项目文件结构

    mdk/
    ├── build/
    ├── gradle/
    │   └── wrapper/
    │       ├── gradle-wrapper.jar
    │       └── gradle-wrapper.properties
    ├── src/
    │   ├── main/
    │       ├── java/
    │       └── resources/
    │           ├── mcmod.info
    │           └── pack.mcmeta
    ├── build.gradle
    ├── gradlew
    └── gradlew.bat

设置Gradle build文件

编辑build.gradle并修改version, grouparchivesBaseName

  version = "1.0.0"
  group= "minecraftfun"
  archivesBaseName = "helloWorldMod"

修改 {mcmod.info}

编辑 mcmod.info以修改modid, namedescription.

  "modid": "helloworldmod",
  "name": "Hello World Mod",
  "description": "Hello World",

提示: modid 只能使用小写字母.

创建mod Java类

src/main/java/minecraftfun/HelloWorldMod.java

package minecraftfun;

import org.apache.logging.log4j.Logger;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

@Mod(modid = HelloWorldMod.MODID, name = HelloWorldMod.NAME, version = HelloWorldMod.VERSION)
public class HelloWorldMod {

    public static final String MODID = "helloworldmod";
    public static final String NAME = "Hello World Mod";
    public static final String VERSION = "1.0.0";

    private static Logger logger;

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        logger = event.getModLog();
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        logger.info("Mod initlialised :" + NAME);
    }
}

创建一个Java类用于处理放下Block的事件

src/main/java/minecraftfun/BlockPlaceHandler.java

package minecraftfun;

import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.event.world.BlockEvent.PlaceEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class BlockPlaceHandler {

  @SubscribeEvent
  public void onPlaceEvent(PlaceEvent event) {
    event.getPlayer().sendMessage(
        new TextComponentString("You placed a block : " + event.getPlacedBlock().getBlock().getLocalizedName()));
  }
}

使用@SubscribeEvent标记处理放下Block事件的方法.

在Mod类的init方法中注册事件处理类.

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        logger.info("Mod initlialised :" + NAME);
        MinecraftForge.EVENT_BUS.register(new BlockPlaceHandler());
    }

编译Mod

运行如下命令以编译Mod

  gradlew build. 

该命令在build/libs目录下生成[archivesBaseName]-[version].jar文件.

测试Mod

将编译生成的mod jar文件放入Minecraft mods目录(在Windows下为 %appdata%.minecraft\mods), 运行Minecraft.

当玩家放下一个Block时, 该mod会显示一条对话消息并输出放下的Block的名称.

小结

对Minecraft Mod开发感兴趣的朋友不妨试一试, 所有源代码和mod jar文件也可以从这里下载.