Minecraft 1.19.2 Forge模组开发 01.Idea开发环境配置

本文是Minecraft 1.19.2模组开发环境配置的教程。首先,从Forge官网或百度网盘下载MDK并解压,使用IntelliJ IDEA打开并配置SDK为Java 17。接着,修改build.gradle文件的group和archivesBaseName字段,然后重构项目主类并更新MOD_ID。最后,修改mods.toml中的modid,完成Gradle构建并运行客户端。

我们本次来进行Minecraft 1.19.2 模组开发环境配置教程的介绍。

1.首先我们需要下载模组开发包:

1.19.2Forge MDK下载官网

找到Mdk的按钮点击并下载即可。

在这里插入图片描述

你也可以通过百度网盘下载该Mdk,下载链接附文末。

2.下载后解压该开发包,并用Idea打开:

cr.png

之后等待系统自动构建环境:

cr1.png

出现Build Successful则说明项目成功构建了。
打开文件 -> 项目结构 -> 修改SDK版本为Java17 -> 应用

cr2.png

打开文件 -> 设置 -> 构建,执行,部署 -> 修改Gradle JVM

cr3.png

3.打开build.gradle文件,修改group字段和archivesBaseName字段:

plugins {
    id 'eclipse'
    id 'maven-publish'
    id 'net.minecraftforge.gradle' version '5.1.+'
}

version = '1.0'
//修改为com.名称.你的模组名称
group = 'com.joy187.re8joymod' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
//修改为你的模组名称,只允许小写
archivesBaseName = 're8joymod'

// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17.
java.toolchain.languageVersion = JavaLanguageVersion.of(17)

println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}"
minecraft {
    // The mappings can be changed at any time and must be in the following format.
    // Channel:   Version:
    // official   MCVersion             Official field/method names from Mojang mapping files
    // parchment  YYYY.MM.DD-MCVersion  Open community-sourced parameter names and javadocs layered on top of official
    //
    // You must be aware of the Mojang license when using the 'official' or 'parchment' mappings.
    // See more information here: https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md
    //
    // Parchment is an unofficial project maintained by ParchmentMC, separate from MinecraftForge
    // Additional setup is needed to use their mappings: https://github.com/ParchmentMC/Parchment/wiki/Getting-Started
    //
    // Use non-default mappings at your own risk. They may not always work.
    // Simply re-run your setup task after changing the mappings to update your workspace.
    mappings channel: 'official', version: '1.19.2'

    // accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') // Currently, this location cannot be changed from the default.

    // Default run configurations.
    // These can be tweaked, removed, or duplicated as needed.
    runs {
        client {
            workingDirectory project.file('run')

            // Recommended logging data for a userdev environment
            // The markers can be added/remove as needed separated by commas.
            // "SCAN": For mods scan.
            // "REGISTRIES": For firing of registry events.
            // "REGISTRYDUMP": For getting the contents of all registries.
            property 'forge.logging.markers', 'REGISTRIES'

            // Recommended logging level for the console
            // You can set various levels here.
            // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
            property 'forge.logging.console.level', 'debug'

            // Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
            property 'forge.enabledGameTestNamespaces', 're8joymod'

            mods {
                re8joymod {
                    source sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run')

            property 'forge.logging.markers', 'REGISTRIES'

            property 'forge.logging.console.level', 'debug'

            property 'forge.enabledGameTestNamespaces', 're8joymod'

            mods {
                re8joymod {
                    source sourceSets.main
                }
            }
        }

        // This run config launches GameTestServer and runs all registered gametests, then exits.
        // By default, the server will crash when no gametests are provided.
        // The gametest system is also enabled by default for other run configs under the /test command.
        gameTestServer {
            workingDirectory project.file('run')

            property 'forge.logging.markers', 'REGISTRIES'

            property 'forge.logging.co
Minecraft 1.19.2 中使用 Forge 进行模组开发时,实现 GUI(图形用户界面)和工作台效果通常涉及多个步骤,包括注册 GUI、创建容器类、绑定 GUI 与容器,并在客户端渲染 GUI。以下是实现的基本流程和关键代码示例。 ### 创建 GUI 的基本流程 1. **定义容器类(Container)** 容器类用于管理 GUI 中的物品槽位和玩家的交互逻辑。需要继承 `AbstractContainerMenu` 并实现其方法。 ```java public class CustomGuiContainer extends AbstractContainerMenu { private final ContainerLevelAccess access; private final int containerId; public CustomGuiContainer(int containerId, Inventory playerInventory, FriendlyByteBuf extraData) { this(containerId, playerInventory, ContainerLevelAccess.NULL); } public CustomGuiContainer(int containerId, Inventory playerInventory, ContainerLevelAccess access) { super(ModContainers.CUSTOM_GUI_CONTAINER.get(), containerId); this.containerId = containerId; this.access = access; // 添加 GUI 中的槽位 for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { this.addSlot(new Slot(new SimpleContainer(9), j + i * 3, 62 + j * 18, 17 + i * 18)); } } // 添加玩家背包槽位 for (int k = 0; k < 3; ++k) { for (int l = 0; l < 9; ++l) { this.addSlot(new Slot(playerInventory, l + k * 9 + 9, 8 + l * 18, 84 + k * 18)); } } // 添加热键槽位 for (int i1 = 0; i1 < 9; ++i1) { this.addSlot(new Slot(playerInventory, i1, 8 + i1 * 18, 142)); } } @Override public boolean stillValid(Player player) { return true; } @Override public ItemStack quickMoveStack(Player player, int index) { return ItemStack.EMPTY; } public int getContainerId() { return this.containerId; } } ``` 2. **注册容器类型(ContainerType)** 在模组的注册类中注册容器类型,以便 Minecraft 可以识别。 ```java public class ModContainers { public static final DeferredRegister<MenuType<?>> CONTAINERS = DeferredRegister.create(ForgeRegistries.CONTAINERS, "modid"); public static final RegistryObject<MenuType<CustomGuiContainer>> CUSTOM_GUI_CONTAINER = CONTAINERS.register("custom_gui", () -> IForgeContainerType.create(CustomGuiContainer::new)); } ``` 3. **创建 GUI 类(Screen)** GUI 类用于渲染界面。需要继承 `Screen` 并实现其绘制逻辑。 ```java public class CustomGuiScreen extends Screen { private final int containerId; public CustomGuiScreen(int containerId, Inventory playerInventory, Component title) { super(title); this.containerId = containerId; } @Override protected void init() { // 初始化 GUI 元素 } @Override public void render(PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) { this.renderBackground(matrixStack); super.render(matrixStack, mouseX, mouseY, partialTicks); this.drawCenteredString(matrixStack, this.font, "Custom GUI", this.width / 2, 20, 16777215); } @Override public boolean isPauseScreen() { return false; } } ``` 4. **注册 GUI 工厂** 使用 `ScreenManager` 注册 GUI 工厂,以便在打开 GUI 时能够正确创建。 ```java @Mod.EventBusSubscriber public class ClientSetup { @SubscribeEvent public static void onClientSetup(FMLClientSetupEvent event) { ScreenManager.register(ModContainers.CUSTOM_GUI_CONTAINER.get(), CustomGuiScreen::new); } } ``` 5. **打开 GUI** 在某个事件(如玩家右键点击方块)中打开 GUI。 ```java @SubscribeEvent public void onPlayerInteract(PlayerInteractEvent.RightClickBlock event) { if (event.getWorld().getBlockState(event.getPos()).getBlock() == ModBlocks.CUSTOM_BLOCK.get()) { NetworkHooks.openGui(event.getPlayer(), new SimpleMenuProvider((containerId, playerInventory, player) -> new CustomGuiContainer(containerId, playerInventory, ContainerLevelAccess.create(event.getWorld(), event.getPos())), new StringTextComponent("Custom GUI")), buffer -> buffer.writeBlockPos(event.getPos())); } } ``` ### 实现工作台效果 Minecraft 中的“工作台”效果通常指的是玩家在 GUI 中进行物品合成或处理的操作。为了实现类似效果,需要: - 使用 `Slot` 类管理每个物品槽位。 - 在容器类中实现 `quickMoveStack` 方法以支持物品移动。 - 在 GUI 类中绘制槽位和物品图标。 ###
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值