−Table of Contents 目录
Reading the Minecraft source
阅读威廉与源代码
Reading the Minecraft source is an important skill in modding. This is mainly because
阅读 Minecraft 源代码是制作 MOD 的一项重要技能。这主要是因为
- Most of the Minecraft things are not documented
大多数威廉与事物都没有记录 - Documentations/tutorials get outdated fairly quickly
文档/教程很快就会过时
To read the Minecraft source, you need to generate sources first. See the “Generating Minecraft Sources” section in Setting up a mod development environment.
要读取 Minecraft 源代码,首先需要生成源代码。请参阅设置 MOD 开发环境中的 "生成 Minecraft 源代码 "部分。
Searching for a Minecraft class
搜索 Minecraft 课程
To search a Minecraft class, follow these steps.
要搜索 Minecraft 课程,请按照以下步骤操作。
Intellij IDEA
- Set the search scope to
Project and Libraries
or wider
将搜索范围设置为Project and Libraries
或更宽 - Type the class name in the search box
在搜索框中键入班级名称
Visual Studio Code Visual Studio 代码
- Open the search pane with
Ctrl + P
用Ctrl + P
打开搜索窗格 - Type the class name with prefix
#
键入带前缀#
的类名
Eclipse 日蚀
- Open the search pane with
Ctrl + Shift + T
用Ctrl + Shift + T
打开搜索窗格
In Visual Studio Code, you can also use Ctrl + T
to open the class search directly.
在 Visual Studio 代码中,也可以使用 Ctrl + T
直接打开类搜索。
Finding the class you want to see
找到您想看的课程
Locating the part of code you want to see is a challenging task. Here are some tips.
找到您想要查看的代码部分是一项具有挑战性的任务。下面是一些提示。
- Take full advantage of IDE's ability to find the relevant code such as
Call hierarchy
,Method hierarchy
orType hierarchy
.
充分利用集成开发环境查找相关代码的功能,如Call hierarchy
、Method hierarchy
或Type hierarchy
。 - Take a look at open source mods which are similar to what you want to make.
看看与您想制作的内容类似的开源 MOD。 - If you are adding a new content, there is a good chance you need to access registries defined in
net.minecraft.util.registry.Registries
.
如果要添加新内容,很可能需要访问net.minecraft.util.registry.Registries
中定义的注册表。 - Keep in mind that everything starts from one main class (
net.minecraft.client.main.Main
for client,net.minecraft.server.Main
for dedicated server).
请记住,一切都从一个主类开始(客户端为net.minecraft.client.main.Main
,专用服务器为net.minecraft.server.Main
)。 - Search Minecraft code for translation keys.
assets/assets/minecraft/lang/en_us.json
contains all keys used in vanilla.
搜索 Minecraft 代码中的翻译密钥。assets/assets/minecraft/lang/en_us.json
包含 vanilla 中使用的所有密钥。
Reading the bytecode 读取字节码
Reading the bytecode is occasionally required because
有时需要读取字节码,因为
- Mixin operates on bytecode, not the source code. Understanding the bytecode is essential when you write a complex mixin.
Mixin 对字节码而非源代码进行操作。在编写复杂的 mixin 时,理解字节码至关重要。 - Decompiler (FernFlower) is not perfect. It sometimes produces an invalid java code such as
int i = true;
.
反编译器(FernFlower)并不完美。它有时会生成无效的 Java 代码,如int i = true;
。
If you are not familiar with the java bytecode, reference the JVM specification.
如果您不熟悉 Java 字节码,请参考 JVM 规范。
To see the bytecode, follow these steps.
要查看字节码,请按以下步骤操作。
Intellij IDEA
- Open the class of which you want to see the bytecode
打开要查看字节码的类 - Select
View
from the main menu
从主菜单中选择View
- Select
Show Bytecode
选择Show Bytecode
External Tools 外部工具
If your IDE doesn't support bytecode viewing, use external bytecode viewers such as https://github.com/Konloch/bytecode-viewer.
如果集成开发环境不支持字节码查看,可使用外部字节码查看器,如 https://github.com/Konloch/bytecode-viewer。
FAQ
Where is the generated sources?
生成的资源在哪里?
It's either in the user gradle cache (~/.gradle/caches/fabric-loom
) or in the project gradle cache (./.gradle/loom-cache
).
You can use the sources jar outside of your IDE.
它要么在用户 gradle 缓存( ~/.gradle/caches/fabric-loom
)中,要么在项目 gradle 缓存( ./.gradle/loom-cache
)中。您可以在集成开发环境之外使用源代码 jar。