概述
本文使用Forge MDK (Mod Development Kit)实现一个简单的命令Mod用于显示用户输入.
运行环境
Java 1.8 JDK
Minecraft Java Edition 1.12.2
Forge and MDK 1.12.2
安装Forge和MDK
从Minecraft Forge官网下载Minecraft Forge - MC 1.12.2 installer和Mod Development Kit (MDK)包.
运行Forge installer安装Forge
将MDK压缩包解压的指定目录, 例如C:\mdk.
设置Mod项目
在MDK目录下, 运行gradlew setupDecompWorkspace命令以创建如下Mod项目结构
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, group和archivesBaseName
version = "1.0.0"
group= "minecraftfun"
archivesBaseName = "echo-cmd-mod"
修改 mod 信息
编辑 mcmod.info以修改modid, name和description.
"modid": "echocmdmod",
"name": "Echo Command Mod",
"description": "A command mod echoes user inputs",
提示: modid 只能使用小写字母.
创建command Java类
src/main/java/minecraftfun/EchoCommand.java
package minecraftfun;
import mandBase;
import mandException;
import mand.ICommandSender;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
public class EchoCommand extends CommandBase {
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] params) throws CommandException {
EchoCommandMod.logger.info("execute called");
if (params != null && params.length > 0) {
for (String param : params) {
String message = "Echo : " + param;
TextComponentString text = new TextComponentString(message);
text.getStyle().setColor(TextFormatting.RED);
sender.sendMessage(text);
}
}
}
@Override
public String getName() {
return "echo";
}
@Override
public String getUsage(ICommandSender sender) {
return "command.echo.usage";
}
}
getUsage方法的返回字符串对应语言文件中的键值.
创建mod Java类
src/main/java/minecraftfun/EchoCommandMod.java
package minecraftfun;
import org.apache.logging.log4j.Logger;
import net.mon.Mod;
import net.mon.Mod.EventHandler;
import net.mon.event.FMLPreInitializationEvent;
import net.mon.event.FMLServerStartingEvent;
@Mod(modid = EchoCommandMod.MODID, name = EchoCommandMod.NAME, version = EchoCommandMod.VERSION)
public class EchoCommandMod {
public static final String MODID = "echocmdmod";
public static final String NAME = "Echo Command Mod";
public static final String VERSION = "1.0.0";
public static Logger logger;
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
logger = event.getModLog();
}
@EventHandler
public void init(FMLServerStartingEvent event)
{
logger.info("initalise FMLServerStartingEvent :" + NAME);
event.registerServerCommand(new EchoCommand());
}
}
设置resource目录结构
在src/main/resources目录下, 创建如下目录和文件, 将MODID替换为EchoCommandMod.MODID
src/main/resources/assets/
└── /
└── lang/
└── en_us.lang
修改mod语言文件
src/main/resources/assets/echocmdmod/lang/en_us.lang
command.echo.usage=/echo
运行如下命令以编译Mod
gradlew build
该命令在build/libs目录下生成[archivesBaseName]-[version].jar文件.
测试Mod
将编译生成的mod jar文件放入Minecraft mods目录(在Windows下为 %appdata%.minecraft\mods), 运行Minecraft, 输入命令/echo some message here.
小结
对Minecraft Mod开发感兴趣的朋友不妨试一试, 所有源代码和mod jar文件也可以从这里下载.