【Minecraft Forge】从零开始学习1.20.1模组开发 (零):配置开发环境

发布时间 2023-07-21 13:45:17作者: dudujerry

上帝说过,一切开源项目上的开发都要建立在官方文档的基础上。

官方文档Forge Documentation (minecraftforge.net)

也查了中文版本,可惜似乎只到1.15......

此系列的所有教程都会以该文档为骨架,辅以内部实现的理解,面向有一定Java基础或面向对象基础的同学(不会就先学一下~~)

anyway,让我们来进行第一步:配置开发环境吧!

0,任意位置新建一个workspace文件夹,在内部新建project文件夹(命名均随意,例如mcmod_workspace/MyModProject)

1,JDK17:https://adoptium.net/zh-CN/temurin/releases/?version=17 ,记住安装路径,配置环境变量Path(可选)

2,IDE,Forge官方可选Eclipse或IdeaIU,这里选择免费的Eclipse https://www.eclipse.org/downloads/ ,安装的时候如图选择

选第二个,因为这样会默认安装gradle构建工具

 Java17+VM注意选你刚才装的JDK17路径

最后打开选择workspace,直接选择你第0步新建的workspace文件夹即可。

3,安装gradle

打开Eclipse按照教程所示操作:

https://jingyan.baidu.com/article/86fae346ed733a7d48121a2c.html
这一步里如果发现Gradle已经是Installed状态,说明第2步已经默认安装了,可以选择进行更新

 

4,安装Forge开发包MDK

Downloads for Minecraft Forge for Minecraft 1.20.1

进去选择

 下载后解压到project文件夹里。

5,导入MDK。打开Eclipse。左上角File -> Import... -> Gradle -> Existing Gradle Project

接着在此处选择你的project文件夹

 别在意为什么我的Finish是灰色的。。。

导入后等一会即可。

 6,下载Forge Gradle

这一步只需要点一下(如果运气好的话)

做完第5步,(注意关闭最开始的默认界面,右上角有x)左侧过一会会出现Project Explorer

双击gradlew,等候下方控制台不再变化,此时gradlew已经配置完毕。

此时,可以尝试重启Eclipse(打开仍然选择你的workspace目录)然后点击上方的Run -> Run。若成功构建,则在项目文件夹里的/build/libs中会出现你的mod文件。

若出现问题,则打开控制台进入项目目录,输入gradlew build,等待进度条全部走完,窗口不再变化后再次尝试Run。

7,配置mods.toml文件,在项目文件夹里src/main/resources/META-INF/mods.toml,配置其中的项。

具体什么意思可以参考官方文档Introduction - Forge Documentation (minecraftforge.net)

不想看可以直接看我的

 1 # This is an example mods.toml file. It contains the data relating to the loading mods.
 2 # There are several mandatory fields (#mandatory), and many more that are optional (#optional).
 3 # The overall format is standard TOML format, v0.5.0.
 4 # Note that there are a couple of TOML lists in this file.
 5 # Find more information on toml format here:  https://github.com/toml-lang/toml
 6 # The name of the mod loader type to load - for regular FML @Mod mods it should be javafml
 7 modLoader="javafml" #mandatory
 8 # A version range to match for said mod loader - for regular FML @Mod it will be the forge version
 9 loaderVersion="[46,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions.
10 # The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties.
11 # Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here.
12 license="All Rights Reserved"
13 # A URL to refer people to when problems occur with this mod
14 #issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional
15 # A list of mods - how many allowed here is determined by the individual mod loader
16 [[mods]] #mandatory
17 # The modid of the mod
18 modId="my_mod" #mandatory
19 # The version number of the mod
20 version="1" #mandatory
21 # A display name for the mod
22 displayName="MyMod" #mandatory
23 # A URL to query for updates for this mod. See the JSON update specification https://docs.minecraftforge.net/en/latest/misc/updatechecker/
24 #updateJSONURL="https://change.me.example.invalid/updates.json" #optional
25 # A URL for the "homepage" for this mod, displayed in the mod UI
26 #displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional
27 # A file name (in the root of the mod JAR) containing a logo for display
28 #logoFile="examplemod.png" #optional
29 # A text field displayed in the mod UI
30 #credits="" #optional
31 # A text field displayed in the mod UI
32 authors="dudujerry" #optional
33 # Display Test controls the display for your mod in the server connection screen
34 # MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod.
35 # IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod.
36 # IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component.
37 # NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value.
38 # IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself.
39 #displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional)
40 
41 # The description text for the mod (multi line!) (#mandatory)
42 description='''Some words'''
43 # A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional.
44 [[dependencies.my_mod]] #optional
45     # the modid of the dependency
46     modId="forge" #mandatory
47     # Does this dependency have to exist - if not, ordering below must be specified
48     mandatory=true #mandatory
49     # The version range of the dependency
50     versionRange="(46,]" #mandatory
51     # An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory
52     # BEFORE - This mod is loaded BEFORE the dependency
53     # AFTER - This mod is loaded AFTER the dependency
54     ordering="NONE"
55     # Side this dependency is applied on - BOTH, CLIENT, or SERVER
56     side="BOTH"
57 # Here's another dependency
58 [[dependencies.my_mod]]
59     modId="minecraft"
60     mandatory=true
61     # This version range declares a minimum of the current minecraft version up to but not including the next major version
62     versionRange="[1.20,1.20.1]"
63     ordering="NONE"
64     side="BOTH"
65 
66 # Features are specific properties of the game environment, that you may want to declare you require. This example declares
67 # that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't
68 # stop your mod loading on the server for example.
69 #[features.${mod_id}]
70 #openGLVersion="[3.2,)"
mods.toml

只需修改18,22,32行中的内容,其中唯一影响代码的是modId,它是你mod主类的名字,也是标识你的mod的唯一标识符,贯穿整个开发过程。在代码中ModId这段字符串就指代的是你的mod,因此需要慎重选择。只允许小写字母和数字、下划线。

8,修改package结构,找到package explorer中你的包名,右键 -> Refactor -> Rename可改,建议改个清楚点的,也别和别人重复。

 9,重新Run,或者你也可以使用右侧Gradle菜单运行指定任务。

 runClient即为运行minecraft客户端测试。

 

build在这

运行Gradle任务均双击即可。

这样,开发环境就配置好了,下一篇开始进入真正的编码世界。

下一篇:【Minecraft Forge】从零开始学习1.20.1模组开发 (一):Forge的注册系统 - dudujerry - 博客园 (cnblogs.com)