本頁將介紹 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
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"
],
"client": [
"com.example.docs.ExampleModClient",
"com.example.docs.client.command.ExampleModClientCommands",
"com.example.docs.ExampleModDynamicSound",
"com.example.docs.ExampleModBlockEntityRenderer"
],
"fabric-datagen": [
"com.example.docs.datagen.ExampleModDataGenerator"
]
},
"mixins": [
"example-mod.mixins.json",
{
"config": "example-mod.client.mixins.json",
"environment": "client"
}
],
"depends": {}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Entrypoints
如前所述,fabric.mod.json 檔案包含 —— 個名為 entrypoints 的欄位 - 這個欄位用來指定你的模組提供的進入點。
模板模組生成器預設會創建一個 main 和一個 client 進入點 —— main 進入點用於共用的程式碼,而 client 進入點則用於客戶端特定的程式碼。 這些進入點會在遊戲啟動時分別被調用。
java
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!");
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
上面是一個簡單的 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 原始碼 —— 例如渲染程式碼或客戶端邏輯,如方塊顏色提供程式。

