User Tools

Site Tools


zh_cn:tutorial:primer

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
zh_cn:tutorial:primer [2023/01/11 15:44] – [Fabric 是什么?] tao0luzh_cn:tutorial:primer [2024/04/15 00:39] (current) – [Fabric 是什么?] solidblock
Line 7: Line 7:
 ===== Minecraft 模组编写的前提 ===== ===== Minecraft 模组编写的前提 =====
  
-你必须知道代码编写的基础。如果你从没接触过代码,你应该先知道如何使用代码。这里所说的基础,包括变量、函数、类和方法等,以及一些面向对象的概念,如继承、多态、类型强转。要是不知道,先搜索。 +  * 你必须知道代码编写的基础。如果你从没接触过代码,你应该先知道如何使用代码。这里所说的基础,包括变量、函数、类和方法等,以及一些面向对象的概念,如继承、多态、类型强转。要是不知道,先搜索。 
-你应该要有一些编程语言的经验,例如 Java、C、C++ 和 C#。Java 的语法和 C 系列的比较类似。 +  你应该要有一些编程语言的经验,例如 Java、C、C++ 和 C#。Java 的语法和 C 系列的比较类似。 
-熟悉 Java 的语法,例如 lambda 方法、泛型(你应该知道这些是什么),以及一些语法糖,这些语法应该会在学习的时候一并遇到。 +  熟悉 Java 的语法,例如 lambda 方法、泛型(你应该知道这些是什么),以及一些语法糖,这些语法应该会在学习的时候一并遇到。 
-你必须知道如何使用 Internet 查找问题与答案,以及如何在网络论坛或者群里提问求助。+  你必须知道如何使用 Internet 查找问题与答案,以及如何在网络论坛或者群里提问求助。
  
 ===== 模组编写是什么? ===== ===== 模组编写是什么? =====
Line 28: Line 28:
 为了更好的理解 Minecraft 的代码干了什么,当你使用 Fabric 进行模组编写时,你有机会接触到 Minecraft 的源代码。由于 Java 是编译型语言,我们需要反编译来获得我们能够读懂的代码。这一过程会将它从 Java 字节码转变为人类可读的 Java 源代码。然而,为了阻止盗版,Mojang  **混淆** 了 Minecraft 的代码。这意味着代码中所有的类、方法和字段的名称是随机的。你可以通过 zip 解压工具中打开 Minecraft 的 .jar 文件亲眼看到这一点 —— 所有的文件都会以类似 ''abc.class'' 的形式出现。此外,我们无法保证两个版本同一个对象拥有相同的名称 —— 可能上个版本还是 ''abc'',下个版本就是 ''adb'' 了。所以这使得在不知道对象名称的情况下修改游戏十分困难,因为我们无法知道不同变量的作用。为了解决这个问题,Fabric 使用了一套映射工具来给所有东西起一个人类可读的名称。 为了更好的理解 Minecraft 的代码干了什么,当你使用 Fabric 进行模组编写时,你有机会接触到 Minecraft 的源代码。由于 Java 是编译型语言,我们需要反编译来获得我们能够读懂的代码。这一过程会将它从 Java 字节码转变为人类可读的 Java 源代码。然而,为了阻止盗版,Mojang  **混淆** 了 Minecraft 的代码。这意味着代码中所有的类、方法和字段的名称是随机的。你可以通过 zip 解压工具中打开 Minecraft 的 .jar 文件亲眼看到这一点 —— 所有的文件都会以类似 ''abc.class'' 的形式出现。此外,我们无法保证两个版本同一个对象拥有相同的名称 —— 可能上个版本还是 ''abc'',下个版本就是 ''adb'' 了。所以这使得在不知道对象名称的情况下修改游戏十分困难,因为我们无法知道不同变量的作用。为了解决这个问题,Fabric 使用了一套映射工具来给所有东西起一个人类可读的名称。
  
-  * **中介**映射器是一个程序,它将会给予 Minecraft 源码中所有被混淆的对象一个名称,类似 “field_10832” 和 “method_12991()”。关键的是,这个程序将总是给予一个对象相同的名称,所以一个在不同版本之间没有变化的方法将总是会拥有相同的中介名称。+  * **intermediary **映射器是一个程序,它将会给予 Minecraft 源码中所有被混淆的对象一个名称,类似 “field_10832” 和 “method_12991()”。关键的是,这个程序将总是给予一个对象相同的名称,所以一个在不同版本之间没有变化的方法将总是会拥有相同的中介名称。 
 +  * **Yarn** 是反混淆过程中的最后一步。Yarn 是一个自由的、开源的社区驱动的 Minecraft 中所有方法和类的名称库。当你看 Minecraft 的源代码时,对于任何类、变量或方法,描述其作用的名称都是由 Yarn 编写的。社区中会有人分析并决定为其命名。每次有新的更新或快照出现时,社区就会开始工作,梳理代码,看看有哪些新的对象需要命名。((注意:虽然 Mojang 发布了所有 Minecraft 版本的官方映射,但在模组编写中使用这些映射的合法性在未来可能会改变。而 Yarn 映射是自由的,每个人都可以使用,并且足以满足大多数模组编写的目的,所以不鼓励你使用官方的映射。))
  
-  * **yarn** 是反混淆过程中的最后一步。Yarn 是一个自由的、开源的社区驱动的 Minecraft 中所有方法和类的名称库。当你看 Minecraft 的源代码时,对于任何类、变量或方法,描述其作用的名称都是由 Yarn 编写的。社区中会有人分析并决定为其命名。每次有新的更新或快照出现时,社区就会开始工作,梳理代码,看看有哪些新的对象需要命名。((注意:虽然 Mojang 发布了所有 Minecraft 版本的官方映射,但在模组编写中使用这些映射的合法性在未来可能会改变。而 Yarn 映射是自由的,每个人都可以使用,并且足以满足大多数模组编写的目的,所以不鼓励你使用官方的映射。)) +但是,在反编译的 Minecraft 代码库中,并不是所有的对象都被 Yarn 映射了 —— 有时你会看到一些变量仍然有中介名称。如果你弄清楚了它们的作用,你可以为 Yarn 贡献一个名称((关于官方映射的另一个说明:不要通过查询官方映射来帮助 Yarn 映射。这很可能违反了知识产权法 —— **所有的 Yarn 映射必须是原创的,并且是独立于 Mojang 的。**))。查看 [[mappings]] 页面,了解更多关于使用和贡献映射的信息。
- +
-但是,在反编译的 Minecraft 代码库中,并不是所有的对象都被 Yarn 映射了 —— 有时你会看到一些变量仍然有中介名称。如果你弄清楚了它们的作用,你可以为 Yarn 贡献一个名称((关于官方映射的另一个说明:不要官方映射咨询 Yarn 映射的帮助。这很可能违反了知识产权法 —— **所有的 Yarn 映射必须是原创的,并且是独立于 Mojang 的。**))。查看 [[mappings]] 页面,了解更多关于使用和贡献映射的信息。+
  
 ===== 代码结构 ===== ===== 代码结构 =====
  
-MinecraftJava Edition is a huge project, with years and years of code built on top of each other. It can seem chaotic (because it is), but there are a few key concepts that are (mostly) consistent across the board.+Minecraft Java 版 是一个巨大的项目,多年来的代码都是建立在彼此之上的。它可能看起来很混乱(因为它就是的),但有几个关键的概念是(大部分)一致的,贯穿始终。
  
 === 注册表 === === 注册表 ===
  
-Most "features" (things you might want to add) in the game (blocks, items, screens, entities, chunk generators, etc.) are loaded into Registries when the game is loaded. For example, each ''Item'' has a single static instance that is initialized when the game starts, and is put into the item registry. The game uses that instance to determine the properties that ''ItemStack''s (the memory representation of the items you can hold in your inventory) of that item have. **If you add something to the game, you probably have to register it.** There are exceptions to this rule, however: some features in Minecraft are **data-driven**, meaning that they can be defined purely in terms of data (as opposed to code). If you've ever wondered why datapacks can add some things but not others, this is why: datapacks can only add data-driven content. Some examples of data-driven content in Minecraft include biomes, textures, *somechunk generators, and crafting recipes. When a world with a datapack is loaded, the game automatically adds these features to a special transient registry only used while that world is loaded. If you've developed datapacks before, you can integrate datapacks into your mods very easily, which makes some content easier to create. You can even add your own data-drivable content that other people can make datapacks for using Codecs.+很多游戏中的(方块、物品、UI、实体、区块生成器等的)“特性”(你想添加的)在游戏被加载时候将会被加载进注册表。比如,每一个''物品''类型都有一个静态的实例,这些实例将在游戏开始的时候被初始化,接着就会被加载进物品类型的注册表。游戏使用这些实例去获取物品类型的 ''ItemStack''(所持有物品在物品栏中的内存表示)属性。**如果你要添加一些对象到游戏内,你可能需要注册它。**然而,这条规定也有例外:Minecraft 中的一些功能是**数据驱动的**,这意味着它们可以纯粹地用数据(而不是代码)来定义。如果你想知道为什么数据包可以添加一些对象而有些对象不行,这就是原因:数据包只能添加数据驱动的内容。例如在 Minecraft 中,数据驱动的内容包括生物群落、纹理、*一些*区块生成器和制作配方。当一个带有数据包的世界被加载时,游戏会自动将这些功能添加到一个特殊的暂存器中,只会在该世界被加载完毕后使用。如果你以前开发过数据包,你可以非常容易地将数据包整合到你的模组中,这使得一些内容更容易创建。你甚至可以添加你自己的数据驱动规则,从而其他人可以使用 Codecs 为其制作数据包。
  
 === 端 === === 端 ===
  
-Minecraft's processing is split between two threads, commonly called "sides": the server side, and the client side. The client always runs on a player's computer and handles rendering and input. The server can run either on its own as a dedicated server (what you probably think of as a "Minecraft server"), or, in singleplayer, on the player's computer along with the client as an integrated server. The server handles everything the client doesn't-- inventories, the world itself, et cetera.  +Minecraft 运行在两个线程上,通常称为 "":服务器端和客户端。客户端总是运行在玩家的电脑上,处理渲染和输入。服务器端可以在专用服务器上独立运行(你可能认为是“Minecraft 服务器”),或者在单人游戏中,与客户端一起在玩家的计算机上运行,作为一个集成服务器。服务器端处理客户端没有处理的一切 —— 物品栏、世界本身,等等。客户端和服务器端必须就某些事情达成一致:世界上有哪些区块,箱子里有什么,玩家的位置等等。由于这些都是由服务器端处理的,所以它向客户端规定这些值应该是什么,而客户端则向玩游戏的人展现这些值。任何由客户端处理的东西都不需要告诉服务器 —— 这包括区块和实体的样子(资源包),以及如何绘制世界(着色器)。这就是客户端模组(如着色器)、服务器端模组(如那些运行小游戏的模组)和双方模组(那些添加机器或新方块和物品的模组)之间的区别。重要的是,不要在服务器端上调用只针对客户端的代码(即与渲染有关),而在客户端调用只针对服务器端的代码。这就是你在 Minecraft 模组制作中经常看到的无处不在的 ''world.isClient()'' 检查的原因。
-The client and server must agree on certain things: what blocks are in the world, what is inside chests, player position, etc. Since these are handled by the server, it dictates to the client what these values should be, and the client displays them to the person playing the game. Anything handled by the client does not have to be told to the server at all-- this includes what blocks and entities look like (resource packs), and how to draw the world (shaders). This is the distinction between clientside mods (like shaders), serverside mods (like those that run minigames), and both-sided mods (those that add things like machines or new blocks and items). +
-It is important to not call client-only code (i.e., relating to rendering) on the server, and server-only code on the client. This is the purpose of the ubiquitous ''world.isClient()'' checks that you often see in Minecraft modding.+
  
 ===== 下一步 ===== ===== 下一步 =====
  
-Once you've achieved the prerequisites and have read this document, it's time to get started! Check out the [[introduction]] to learn about the ins and outs of Fabric as a platform, read the [[setup]] document to set up an IDE to mod Minecraft, and then head to the [[items]] tutorial to add your first item!+一旦你达到了先决条件并阅读了本文档,就可以开始了!查看 [[introduction]] 介绍以更深入了解 Fabric 这个平台,阅读 [[setup]] 文档以设置 IDE 来修改 Minecraft,然后前往 [[items]] 教程来添加第一个物品!
  
zh_cn/tutorial/primer.1673451851.txt.gz · Last modified: 2023/01/11 15:44 by tao0lu