User Tools

Site Tools


zh_cn:tutorial:reading_mc_code

This is an old revision of the document!


阅读 Minecraft 源代码

阅读 Minecraft 源代码时编写模组时的重要技巧,这是因为:

  • Minecraft 的很多东西都没有文档
  • 文档和教程很容易过时

阅读 Minecraft 源代码,你首先需要生成源代码。请参考建立模组开发环境中的“生成 Minecraft 源代码”段落。

搜索 Minecraft 类

搜索一个 Minecraft 类,有以下步骤:

Intellij IDEA

  1. Ctrl + N 打开搜索面板
  2. 将搜索范围设为 项目和库 或者更大范围
  3. 在搜索框中输入类名称

Visual Studio Code

  1. Ctrl + P 打开搜索面板
  2. 输入前缀 # 再输入类名称

在 Visual Studio Code,你也可以使用 Ctrl + T 来直接打开类搜索。

查找你想要看到的类

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 or Type hierarchy.
  • Take a look at open source mods which are similar to what you want to make.
  • If you are adding a new content, there is a good chance you need to access registries defined in net.minecraft.util.registry.Registry.
  • Keep in mind that everything starts from one main class (net.minecraft.client.main.Main for client, net.minecraft.server.Main for dedicated server).

阅读字节码

有时必须要阅读字节码,这是因为

  • Mixin 是对字节码进行操作的,而非对源代码。编写复杂的 mixin 时,必须要理解字节码。
  • 反编译器(FernFlower)并不完美,有时会产生一些无效的 Java 代码,比如 int i = true;

如果你不熟悉 Java 字节码,请参考 JVM 规范

查看字节码有以下步骤:

Intellij IDEA

  1. 打开你需要查看字节码的类
  2. 从主菜单中选择 视图(View)
  3. 选择 显示字节码(Show Bytecode)

外部工具

如果你的 IDE 不支持字节码查看,可以使用外部工具,如https://github.com/Konloch/bytecode-viewer

常见问题

生成的源代码在哪里?

在用户 gradle 缓存中(~/.gradle/caches/fabric-loom)。你可以在 IDE 之外使用源代码 jar。

zh_cn/tutorial/reading_mc_code.1645240246.txt.gz · Last modified: 2022/02/19 03:10 by xtexchooser