Minecraft Forge 环境搭建

Minecraft Forge 环境搭建

本文使用 Minecraft 1.19 Forge 编写 mod

下载 JDK

对于 Minecraft 1.19, 所需 JDK 版本应该是 openjdk-17

1
sudo pacman -Sy jdk17-openjdk

下载 开发工具包 MDK

搜索 minecraft forge,从官网下载

search with google

下载 minecraft 1.19 的推荐版本的 MDK (地址

image-20240708101603869

下载后解压

保留圈出来的文件,其余的 .txt 文件都可删除

image-20240708101943966

配置项目

手动构建

直接运行 gradlew 完成基本的构建

1
./gradlew

根据使用的开发工具,生成相应的 Runs(用于启动客户端、服务端)

1
2
3
4
# 任选其一即可
./gradlew genIntelljJRuns # 使用 IDEA 开发
./gradlew genVSCodeRuns # 使用 VSCode
./gradlew genEclipseRuns # 使用 Eclipse

我们使用 IDEA,所以直接运行相应的命令

生成 Runs 时,会自动下载所需的文件,显示 BUILD SUCCESSFUL 就是成功了。如果失败了的话,尝试更换网络环境之后,重新运行命令即可

image-20240708102650163

之后就可以使用所选用的开发工具打开这个文件夹了

自动构建

使用 IDEA 打开时,会自动运行一次构建程序,所以解压之后可以直接使用 IDEA 打开

image-20240708102944915

等到下方运行完成后,右边可以看到这个项目所有的任务

点击 forgegradle runs/genIntellijRuns 即可配置 IDEA 的 Runs,生成完毕后,在运行按钮处可以看到 runClientrunServer

修改基本信息

默认的 mod 是叫做 examplemod,添加了一个没有绑定材质的方块。为了方便起见,我们可以直接把 com.example.examplemod.ExampleMod 中相应内容删除,删除后的文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.example.examplemod;

import com.mojang.logging.LogUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
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.event.server.ServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.slf4j.Logger;

// The value here should match an entry in the META-INF/mods.toml file
@Mod(ExampleMod.MODID)
public class ExampleMod {
// Define mod id in a common place for everything to reference
public static final String MODID = "examplemod";
// Directly reference a slf4j logger
private static final Logger LOGGER = LogUtils.getLogger();

public ExampleMod() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

// Register the commonSetup method for modloading
modEventBus.addListener(this::commonSetup);
// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
}

private void commonSetup(final FMLCommonSetupEvent event) {
// Some common setup code
LOGGER.info("HELLO FROM COMMON SETUP");
LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));
}

// You can use SubscribeEvent and let the Event Bus discover methods to call
@SubscribeEvent
public void onServerStarting(ServerStartingEvent event) {
// Do something when the server starts
LOGGER.info("HELLO from server starting");
}

// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public static class ClientModEvents {
@SubscribeEvent
public static void onClientSetup(FMLClientSetupEvent event) {
// Some client setup code
LOGGER.info("HELLO FROM CLIENT SETUP");
LOGGER.info("MINECRAFT NAME >> {}", Minecraft.getInstance().getUser().getName());
}
}
}

下面开始修改基本信息

在以下几处进行修改

字段 修改后的值 含义 备注
com.example.examplemod.ExampleMod.MODID tutorialmod mod 的 ID 小写,数字,下划线
com.example.examplemod.ExampleMod 重构为cc.neolux.tutorialmod.TutorialMod 主类 重构成想要的包和类名,便于管理
build.gradle/group cc.neolux.tutorialmod
build.gradle/archivesBaseName tutorialmod
build.gradle 中 所有的 examplemod 修改为 tutorialmod 全文替换即可
resources/META-INF/mods.toml modId 修改为 tutorialmod
resources/META-INF/mods.toml 所有examplemod 修改为 tutorialmod 全文替换

之后运行 runClient,可以进入游戏就是配置成功,新建一个创造模式存档,用于测试。

image-20240708105834444