🇨🇳 中文 (Chinese - China)
🇨🇳 中文 (Chinese - China)
外观
🇨🇳 中文 (Chinese - China)
🇨🇳 中文 (Chinese - China)
外观
本页面基于这个版本编写:
1.20.4
本页将介绍 Fabric 模组项目的结构以及项目中每个文件和文件夹的用途。
fabric.mod.json fabric.mod.json 是向 Fabric Loader 描述你的模组的主要文件, 包含模组的 ID、版本、依赖等信息。
fabric.mod.json 文件最重要的字段是:
id:模组的 ID,必须是独特的,不能和其他模组重复。name:模组的显示名称。environment:模组运行环境,可以是 client(仅客户端)、server(仅服务端)和 *(双端)。entrypoints:模组提供的入口点,例如 main 和 client 等。depends:模组的依赖模组/库。mixins:模组提供的 Mixin。下面看到的是示例的 fabric.mod.json 文件——这是该文档的开发参考项目的 fabric.mod.json 文件。
fabric.mod.json{
  "schemaVersion": 1,
  "id": "example-mod",
  "version": "1.0.0",
  "name": "Example Mod",
  "icon": "assets/example-mod/icon.png",
  "environment": "*",
  "entrypoints": {
    "main": [
      "com.example.docs.ExampleMod",
      "com.example.docs.event.ExampleModEvents",
      "com.example.docs.command.ExampleModCommands",
      "com.example.docs.effect.ExampleModEffects",
      "com.example.docs.potion.ExampleModPotions",
      "com.example.docs.sound.ExampleModSounds",
      "com.example.docs.damage.ExampleModDamageTypes",
      "com.example.docs.item.ExampleModItems",
      "com.example.docs.enchantment.ExampleModEnchantments",
      "com.example.docs.block.ExampleModBlocks",
      "com.example.docs.block.entity.ExampleModBlockEntities",
      "com.example.docs.component.ExampleModComponents",
      "com.example.docs.advancement.ExampleModDatagenAdvancement",
      "com.example.docs.networking.ExampleModNetworking",
      "com.example.docs.networking.basic.ExampleModNetworkingBasic",
      "com.example.docs.debug.ExampleModDebug"
    ],
    "client": [
      "com.example.docs.client.command.ExampleModClientCommands",
      "com.example.docs.ExampleModBlockEntityRenderer",
      "com.example.docs.ExampleModDynamicSound",
      "com.example.docs.ExampleModClient",
      "com.example.docs.rendering.CustomRenderPipeline",
      "com.example.docs.rendering.HudRenderingEntrypoint",
      "com.example.docs.rendering.RenderingConceptsEntrypoint",
      "com.example.docs.network.basic.ExampleModNetworkingBasicClient"
    ],
    "fabric-datagen": [
      "com.example.docs.datagen.ExampleModDataGenerator"
    ]
  },
  "mixins": [
    "example-mod.mixins.json",
    {
      "config": "example-mod.client.mixins.json",
      "environment": "client"
    }
  ],
  "depends": {}
}如前所述,fabric.mod.json 文件包含一个名为 entrypoints 的字段——该字段用于指定你的模组提供的入口点。
模组开发模板生成器默认创建 main 和 client 入口点——main 入口点用于双端共用部分,client 入口点用于客户端特定部分。 这些入口点将会在游戏启动时依次调用。
public class ExampleMod implements ModInitializer {
	// This logger is used to write text to the console and the log file.
	// It is considered best practice to use your mod id as the logger's name.
	// That way, it's clear which mod wrote info, warnings, and errors.
	public static final String MOD_ID = "example-mod";
	public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
	@Override
	public void onInitialize() {
		// This code runs as soon as Minecraft is in a mod-load-ready state.
		// However, some things (like resources) may still be uninitialized.
		// Proceed with mild caution.
		LOGGER.info("Hello Fabric world!");
	}
}上面是一个简单的 main 入口点的使用示例,会在游戏开始时记录一条消息到控制台。
src/main/resources src/main/resources 用于存储模组的资源文件,例如纹理、模型和声音。
它也是 fabric.mod.json 和模组使用的 Mixin 配置文件的存放位置。
资源文件存储在与资源包结构相似的结构中——例如,方块的纹理会存放在 assets/example-mod/textures/block/block.png 中。
src/client/resources src/client/resources 文件夹用于存储客户端特定的资源,例如仅在客户端使用的纹理、模型和音效。
src/main/java src/main/java 文件夹用于存储模组的 Java 源代码——在客户端和服务端环境中都存在。
src/client/java src/client/java 文件夹用于存储客户端专属的 Java 源代码,例如渲染代码或客户端逻辑——例如方块颜色提供程序。