标签:压缩包 文件结构 handler tco res one 字母 ace mdk
本文通过一个简单的Helll World Mod示例, 介绍了使用Forge MDK (Mod Development Kit)开发Minecraft Mod的基本环境设置和过程.
Java 1.8 JDK
Minecraft Java Edition 1.12.2
从Minecraft Forge官网下载Minecraft Forge - MC 1.12.2 installer和Mod Development Kit (MDK)包.
运行Forge installer安装Forge
将MDK压缩包解压的指定目录, 例如C:\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
编辑build.gradle
并修改version
, group
和archivesBaseName
version = "1.0.0"
group= "minecraftfun"
archivesBaseName = "helloWorldMod"
编辑 mcmod.info
以修改modid
, name
和description
.
"modid": "helloworldmod",
"name": "Hello World Mod",
"description": "Hello World",
提示: modid
只能使用小写字母.
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);
}
}
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事件的方法.
@EventHandler
public void init(FMLInitializationEvent event)
{
logger.info("Mod initlialised :" + NAME);
MinecraftForge.EVENT_BUS.register(new BlockPlaceHandler());
}
运行如下命令以编译Mod
gradlew build.
该命令在build/libs
目录下生成[archivesBaseName]-[version].jar
文件.
将编译生成的mod jar文件放入Minecraft mods目录(在Windows下为 %appdata%.minecraft\mods), 运行Minecraft.
当玩家放下一个Block时, 该mod会显示一条对话消息并输出放下的Block的名称.
对Minecraft Mod开发感兴趣的朋友不妨试一试, 所有源代码和mod jar文件也可以从这里下载.
我的世界Minecraft Mod开发学习笔记 - Mod开发环境设置和Hello World示例
标签:压缩包 文件结构 handler tco res one 字母 ace mdk
原文地址:https://www.cnblogs.com/programmingfun/p/9064236.html