这是用户在 2024-4-30 13:08 为 https://fabricmc.net/wiki/tutorial:reading_mc_code 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

User Tools

Site Tools


tutorial:reading_mc_code 教程:读取麦克代码

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

  1. Open the search pane with or on mac
    使用 或 Mac 上的 打开搜索窗格
  2. Set the search scope to Project and Libraries or wider
    将搜索范围设置为 Project and Libraries 或更宽
  3. Type the class name in the search box
    在搜索框中键入班级名称

Visual Studio Code Visual Studio 代码

  1. Open the search pane with Ctrl + P
    Ctrl + P 打开搜索窗格
  2. Type the class name with prefix #
    键入带前缀 # 的类名

Eclipse 日蚀

  1. 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 or Type hierarchy.
    充分利用集成开发环境查找相关代码的功能,如 Call hierarchyMethod hierarchyType 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

  1. Open the class of which you want to see the bytecode
    打开要查看字节码的类
  2. Select View from the main menu
    从主菜单中选择 View
  3. 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。

tutorial/reading_mc_code.txt · Last modified: 2023/11/17 19:14 by cph101