sapdon 3.2.2 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +255 -121
  2. package/doc/dev/architecture.md +418 -0
  3. package/doc/dev/cli.md +467 -0
  4. package/doc/dev/core.md +751 -0
  5. package/doc/dev/lr-paradigm.md +85 -0
  6. package/doc/dev/oc.md +582 -0
  7. package/doc/dev/workflow.md +257 -0
  8. package/doc/hello_sapdon/hello_sapdon.md +3 -3
  9. package/doc/user/api/biome.md +558 -0
  10. package/doc/user/api/block.md +1530 -0
  11. package/doc/user/api/entity.md +685 -0
  12. package/doc/user/api/extra.md +231 -0
  13. package/doc/user/api/item.md +1125 -0
  14. package/doc/user/api/neo-guidebook.md +409 -0
  15. package/doc/user/api/recipe.md +427 -0
  16. package/doc/user/api/sapdon-ui.md +185 -0
  17. package/doc/user/api/texture.md +181 -0
  18. package/doc/user/config/build-config.md +83 -0
  19. package/doc/user/config/mod-info.md +33 -0
  20. package/doc/user/faq.md +160 -0
  21. package/doc/user/quick-start.md +122 -0
  22. package/doc/user/tutorials/block.md +474 -0
  23. package/doc/user/tutorials/entity.md +356 -0
  24. package/doc/user/tutorials/item.md +449 -0
  25. package/doc/user/tutorials/neo-guidebook-experience.md +381 -0
  26. package/doc/user/tutorials/neo-guidebook.md +640 -0
  27. package/doc/user/tutorials/recipe.md +278 -0
  28. package/doc/user/tutorials/sapdon-ui.md +207 -0
  29. package/package.json +8 -3
  30. package/prod/cli/index.js +1 -1
  31. package/prod/cli/start.js +1 -1458
  32. package/prod/core/index.d.ts +4564 -2166
  33. package/prod/core/index.js +1 -59015
  34. package/prod/core/package.json +7 -0
  35. package/prod/oc/index.d.ts +350 -108
  36. package/prod/oc/index.js +1 -1
  37. package/prod/oc/package.json +7 -0
  38. package/prod/utils/index.d.ts +20 -9
  39. package/prod/utils/index.js +1 -1
  40. package/prod/utils/package.json +7 -0
  41. package/doc/BlockAPI.md +0 -145
  42. package/doc/api.md +0 -128
  43. package/doc/oc/index.md +0 -0
  44. package/doc/sapdon-ts.md +0 -64
  45. package/src/templates/js_sapdon/build.config +0 -23
  46. package/src/templates/js_sapdon/main.mjs +0 -4
  47. package/src/templates/js_sapdon/mod.info +0 -7
  48. package/src/templates/js_sapdon/pack_icon.png +0 -0
  49. package/src/templates/js_sapdon/package.json +0 -20
  50. package/src/templates/js_sapdon/res/animations/animation_item.animation.json +0 -34
  51. package/src/templates/js_sapdon/res/animations/large_item.animation.json +0 -27
  52. package/src/templates/js_sapdon/res/models/blocks/crop.geo.json +0 -48
  53. package/src/templates/js_sapdon/res/models/entity/animation/animation_item.geo.json +0 -26
  54. package/src/templates/js_sapdon/res/models/entity/animation/large_item.geo.json +0 -28
  55. package/src/templates/js_sapdon/res/textures/blocks/none.png +0 -0
  56. package/src/templates/js_sapdon/res/textures/blocks/test_log_oak.png +0 -0
  57. package/src/templates/js_sapdon/res/textures/blocks/test_log_top.png +0 -0
  58. package/src/templates/js_sapdon/res/textures/items/masterball.png +0 -0
  59. package/src/templates/js_sapdon/scripts/custom_components/cropComponent.js +0 -50
  60. package/src/templates/js_sapdon/scripts/custom_components/items/gui_book.js +0 -37
  61. package/src/templates/js_sapdon/scripts/custom_components/registry.js +0 -25
  62. package/src/templates/js_sapdon/scripts/index.js +0 -0
  63. package/src/templates/ts_sapdon/build.config +0 -23
  64. package/src/templates/ts_sapdon/main.ts +0 -11
  65. package/src/templates/ts_sapdon/mod.info +0 -7
  66. package/src/templates/ts_sapdon/pack_icon.png +0 -0
  67. package/src/templates/ts_sapdon/package.json +0 -20
  68. package/src/templates/ts_sapdon/res/models/blocks/crop.geo.json +0 -48
  69. package/src/templates/ts_sapdon/res/textures/blocks/test_log_oak.png +0 -0
  70. package/src/templates/ts_sapdon/res/textures/blocks/test_log_top.png +0 -0
  71. package/src/templates/ts_sapdon/res/textures/items/masterball.png +0 -0
  72. package/src/templates/ts_sapdon/scripts/components/cropComponent.ts +0 -44
  73. package/src/templates/ts_sapdon/scripts/components/items/guiBook.ts +0 -36
  74. package/src/templates/ts_sapdon/scripts/components/registry.ts +0 -24
  75. package/src/templates/ts_sapdon/scripts/index.ts +0 -7
  76. package/src/templates/ts_sapdon/tsconfig.json +0 -117
@@ -0,0 +1,1530 @@
1
+ # 方块系统 API 参考
2
+
3
+ ---
4
+
5
+ ## BlockAPI
6
+
7
+ 工厂函数集合,用于创建各类方块并自动注册到游戏系统。
8
+
9
+ ```typescript
10
+ import { BlockAPI, registry } from '@sapdon/core'
11
+ ```
12
+
13
+ ---
14
+
15
+ ### createBasicBlock
16
+
17
+ 创建基础六面方块。
18
+
19
+ ```typescript
20
+ BlockAPI.createBasicBlock(
21
+ identifier: string,
22
+ category: string,
23
+ textures_arr: string[],
24
+ options?: BasicBlockOptions
25
+ ): BasicBlock
26
+ ```
27
+
28
+ **参数**
29
+
30
+ | 参数 | 类型 | 说明 |
31
+ |------|------|------|
32
+ | `identifier` | `string` | 方块唯一标识符,格式 `命名空间:名称` |
33
+ | `category` | `string` | 创造栏分类:`construction` / `nature` / `equipment` / `items` / `none` |
34
+ | `textures_arr` | `string[]` | 6 纹理数组,顺序 `[down, up, north, south, west, east]` |
35
+ | `options.group` | `string` | 分组,默认 `"construction"` |
36
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
37
+
38
+ **示例**
39
+
40
+ ```typescript
41
+ const block = BlockAPI.createBasicBlock('demo:stone', 'nature', [
42
+ 'stone_down', 'stone_up', 'stone_north', 'stone_south', 'stone_west', 'stone_east'
43
+ ])
44
+ ```
45
+
46
+ ---
47
+
48
+ ### createBlock
49
+
50
+ 创建含变体状态的标准方块。
51
+
52
+ ```typescript
53
+ BlockAPI.createBlock(
54
+ identifier: string,
55
+ category: string,
56
+ variantDatas: VariantData[],
57
+ options?: BlockOptions
58
+ ): Block
59
+ ```
60
+
61
+ **参数**
62
+
63
+ | 参数 | 类型 | 说明 |
64
+ |------|------|------|
65
+ | `identifier` | `string` | 方块唯一标识符 |
66
+ | `category` | `string` | 创造栏分类 |
67
+ | `variantDatas` | `VariantData[]` | 变体数据数组 |
68
+ | `options.group` | `string` | 分组,默认 `"construction"` |
69
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
70
+ | `options.ambient_occlusion` | `boolean` | 环境光遮蔽,默认 `false` |
71
+ | `options.face_dimming` | `boolean` | 面亮度衰减,默认 `false` |
72
+ | `options.render_method` | `string` | 渲染模式:`opaque` / `blend` / `alpha_test` |
73
+
74
+ **VariantData**
75
+
76
+ ```typescript
77
+ interface VariantData {
78
+ stateTag: number // 状态标签值 (0-15)
79
+ textures: string[] // 6 纹理数组
80
+ }
81
+ ```
82
+
83
+ **示例**
84
+
85
+ ```typescript
86
+ const block = BlockAPI.createBlock('demo:varied', 'nature', [
87
+ { stateTag: 0, textures: ['tex_0', 'tex_0', 'tex_0', 'tex_0', 'tex_0', 'tex_0'] },
88
+ { stateTag: 1, textures: ['tex_1', 'tex_1', 'tex_1', 'tex_1', 'tex_1', 'tex_1'] }
89
+ ])
90
+ ```
91
+
92
+ ---
93
+
94
+ ### createRotatableBlock
95
+
96
+ 创建可旋转方块。
97
+
98
+ ```typescript
99
+ BlockAPI.createRotatableBlock(
100
+ identifier: string,
101
+ category: string,
102
+ textures_arr: string[],
103
+ options?: RotatableBlockOptions
104
+ ): RotatableBlock
105
+ ```
106
+
107
+ **参数**
108
+
109
+ | 参数 | 类型 | 说明 |
110
+ |------|------|------|
111
+ | `identifier` | `string` | 方块唯一标识符 |
112
+ | `category` | `string` | 创造栏分类 |
113
+ | `textures_arr` | `string[]` | 6 纹理数组 |
114
+ | `options.group` | `string` | 分组,默认 `"construction"` |
115
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
116
+ | `options.rotationType` | `RotationTypes` | 旋转类型,默认 `RotationTypes.CARDINAL` |
117
+ | `options.yRotationOffset` | `number` | Y轴旋转偏移,默认 `0` |
118
+
119
+ **示例**
120
+
121
+ ```typescript
122
+ const block = BlockAPI.createRotatableBlock('demo:facing', 'construction',
123
+ ['top', 'bottom', 'front', 'back', 'left', 'right'],
124
+ { rotationType: RotationTypes.FACING }
125
+ )
126
+ ```
127
+
128
+ ---
129
+
130
+ ### createGeometryBlock
131
+
132
+ 创建自定义几何模型的方块。
133
+
134
+ ```typescript
135
+ BlockAPI.createGeometryBlock(
136
+ identifier: string,
137
+ category: string,
138
+ geometry: string,
139
+ material_instances: Record<string, MaterialInstance>,
140
+ options?: GeometryBlockOptions
141
+ ): GeometryBlock
142
+ ```
143
+
144
+ **参数**
145
+
146
+ | 参数 | 类型 | 说明 |
147
+ |------|------|------|
148
+ | `identifier` | `string` | 方块唯一标识符 |
149
+ | `category` | `string` | 创造栏分类 |
150
+ | `geometry` | `string` | 几何模型标识符,如 `"geometry.chair"` |
151
+ | `material_instances` | `object` | 材质实例配置 |
152
+ | `options.group` | `string` | 分组,默认 `"construction"` |
153
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
154
+
155
+ **MaterialInstance**
156
+
157
+ ```typescript
158
+ interface MaterialInstance {
159
+ texture: string
160
+ render_method?: 'opaque' | 'double_sided' | 'blend' | 'alpha_test' | 'alpha_test_single_sided'
161
+ ambient_occlusion?: boolean | number
162
+ face_dimming?: boolean
163
+ tint_method?: string // 生物群系染色方法,如 "grass"
164
+ alpha_masked_tint?: boolean // 是否基于 alpha 通道应用染色
165
+ isotropic?: boolean // 是否随机旋转 UV
166
+ }
167
+ ```
168
+
169
+ **示例**
170
+
171
+ ```typescript
172
+ const block = BlockAPI.createGeometryBlock('demo:chair', 'construction',
173
+ 'geometry.chair',
174
+ { '*': { texture: 'chair_tex', render_method: 'opaque' } }
175
+ )
176
+ ```
177
+
178
+ ---
179
+
180
+ ### createCropBlock
181
+
182
+ 创建作物方块。
183
+
184
+ ```typescript
185
+ BlockAPI.createCropBlock(
186
+ identifier: string,
187
+ category: string,
188
+ variantDatas: VariantData[],
189
+ options?: CropBlockOptions
190
+ ): CropBlock
191
+ ```
192
+
193
+ **参数**
194
+
195
+ | 参数 | 类型 | 说明 |
196
+ |------|------|------|
197
+ | `identifier` | `string` | 方块唯一标识符 |
198
+ | `category` | `string` | 创造栏分类 |
199
+ | `variantDatas` | `VariantData[]` | 生长阶段变体数据 |
200
+ | `options.group` | `string` | 分组,默认 `"construction"` |
201
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
202
+ | `options.ambient_occlusion` | `boolean` | 环境光遮蔽,默认 `false` |
203
+ | `options.face_dimming` | `boolean` | 面亮度衰减,默认 `false` |
204
+ | `options.render_method` | `string` | 渲染模式,默认 `"alpha_test"` |
205
+
206
+ **自动添加的组件**
207
+ - `minecraft:collision_box` → `false`
208
+ - `minecraft:geometry` → `geometry.crop`
209
+ - `minecraft:placement_filter` → 仅允许放置在耕地上方
210
+ - 每个生长阶段自动调整选择框高度
211
+
212
+ **运行时依赖**
213
+
214
+ 作物方块的生长、骨粉交互等行为需要注册框架内置的运行时组件。在 `scripts/index.ts` 中添加:
215
+
216
+ ```typescript
217
+ import { registerBuiltinComponents } from '@sapdon/runtime'
218
+ registerBuiltinComponents()
219
+ ```
220
+
221
+ **示例**
222
+
223
+ ```typescript
224
+ import { BlockAPI, registry } from '@sapdon/core'
225
+
226
+ const crop = BlockAPI.createCropBlock('demo:tomato', 'nature', [
227
+ { stateTag: 0, textures: ['stage_0', 'stage_0', 'stage_0', 'stage_0', 'stage_0', 'stage_0'] },
228
+ { stateTag: 1, textures: ['stage_1', 'stage_1', 'stage_1', 'stage_1', 'stage_1', 'stage_1'] },
229
+ { stateTag: 2, textures: ['stage_2', 'stage_2', 'stage_2', 'stage_2', 'stage_2', 'stage_2'] },
230
+ { stateTag: 3, textures: ['stage_3', 'stage_3', 'stage_3', 'stage_3', 'stage_3', 'stage_3'] }
231
+ ])
232
+
233
+ registry.submit()
234
+ ```
235
+
236
+ ---
237
+
238
+ ### createOreBlock
239
+
240
+ 创建矿物方块(自动生成方块 + 矿脉特征 + 特征规则)。
241
+
242
+ ```typescript
243
+ BlockAPI.createOreBlock(
244
+ identifier: string,
245
+ category: string,
246
+ textures_arr: string[],
247
+ options?: OreBlockOptions
248
+ ): OreBlock
249
+ ```
250
+
251
+ **参数**
252
+
253
+ | 参数 | 类型 | 说明 |
254
+ |------|------|------|
255
+ | `identifier` | `string` | 方块唯一标识符 |
256
+ | `category` | `string` | 创造栏分类 |
257
+ | `textures_arr` | `string[]` | 6 纹理数组 |
258
+ | `options.group` | `string` | 分组,默认 `"construction"` |
259
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
260
+
261
+ **OreBlock 属性**
262
+
263
+ | 属性 | 类型 | 说明 |
264
+ |------|------|------|
265
+ | `.block` | `BasicBlock` | 矿物方块实例 |
266
+ | `.feature` | `OreFeature` | 矿脉特征实例 |
267
+ | `.feature_rules` | `FeatureRule` | 特征规则实例(Y 0-64,10 次/区块) |
268
+
269
+ **示例**
270
+
271
+ ```typescript
272
+ const ore = BlockAPI.createOreBlock('demo:ruby_ore', 'nature',
273
+ ['ruby_ore', 'ruby_ore', 'ruby_ore', 'ruby_ore', 'ruby_ore', 'ruby_ore']
274
+ )
275
+ // ore (OreBlock extends BasicBlock) — 方块本身
276
+ // ore.feature — 矿脉特征
277
+ // ore.feature_rules — 特征规则
278
+ ```
279
+
280
+ ---
281
+
282
+ ---
283
+
284
+ ### createGlassBlock
285
+
286
+ 创建透明玻璃方块。
287
+
288
+ ```typescript
289
+ BlockAPI.createGlassBlock(
290
+ identifier: string,
291
+ category: string,
292
+ texture: string,
293
+ options?: GlassBlockOptions
294
+ ): GlassBlock
295
+ ```
296
+
297
+ **参数**
298
+
299
+ | 参数 | 类型 | 说明 |
300
+ |------|------|------|
301
+ | `identifier` | `string` | 方块唯一标识符 |
302
+ | `category` | `string` | 创造栏分类 |
303
+ | `texture` | `string` | 纹理短名(6 面复用) |
304
+ | `options.group` | `string` | 分组,默认 `"construction"` |
305
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
306
+ | `options.geometry` | `string` | 自定义几何模型标识符,如 `"geometry.custom_glass"` |
307
+ | `options.culling` | `string` | 裁剪规则标识符,如 `"wiki:culling.custom_glass"` |
308
+
309
+ **自动添加的组件**
310
+ - `minecraft:material_instances` → 6 面材质实例,`render_method: "blend"`
311
+ - `minecraft:light_dampening` → `0`
312
+ - `minecraft:destructible_by_mining` → `{ seconds_to_destroy: 0.3 }`
313
+ - `minecraft:destructible_by_explosion` → `{ explosion_resistance: 0.3 }`
314
+ - 可选 `minecraft:geometry`(设 `geometry` 参数时自动添加)
315
+
316
+ **示例**
317
+
318
+ ```typescript
319
+ const glass = BlockAPI.createGlassBlock('wiki:glass', 'construction', 'glass_tex')
320
+ ```
321
+
322
+ ---
323
+
324
+ ### createFenceBlock
325
+
326
+ 创建栅栏方块。
327
+
328
+ ```typescript
329
+ BlockAPI.createFenceBlock(
330
+ identifier: string,
331
+ category: string,
332
+ textures_arr: string[],
333
+ options?: FenceBlockOptions
334
+ ): FenceBlock
335
+ ```
336
+
337
+ **参数**
338
+
339
+ | 参数 | 类型 | 说明 |
340
+ |------|------|------|
341
+ | `identifier` | `string` | 方块唯一标识符 |
342
+ | `category` | `string` | 创造栏分类 |
343
+ | `textures_arr` | `string[]` | 6 纹理数组 |
344
+ | `options.group` | `string` | 分组,默认 `"construction"` |
345
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
346
+ | `options.leashable` | `boolean` | 是否可被拴绳拴住,默认 `false` |
347
+
348
+ **自动添加的组件**
349
+ - `minecraft:collision_box` → 自定义栅栏柱碰撞箱
350
+ - `minecraft:selection_box` → 同上
351
+ - `minecraft:support` → `{ shape: "fence" }`
352
+ - `minecraft:connection_rule` → 接受所有方向连接
353
+ - 可选 `minecraft:leashable`(设 `leashable` 参数时自动添加)
354
+
355
+ **示例**
356
+
357
+ ```typescript
358
+ const fence = BlockAPI.createFenceBlock('wiki:fence', 'construction',
359
+ ['fence_tex', 'fence_tex', 'fence_tex', 'fence_tex', 'fence_tex', 'fence_tex']
360
+ )
361
+ ```
362
+
363
+ ---
364
+
365
+ ### createStairBlock
366
+
367
+ 创建楼梯方块(8 个 permutation:4 方向 × 2 上下)。
368
+
369
+ ```typescript
370
+ BlockAPI.createStairBlock(
371
+ identifier: string,
372
+ category: string,
373
+ textures_arr: string[],
374
+ options?: StairBlockOptions
375
+ ): StairBlock
376
+ ```
377
+
378
+ **参数**
379
+
380
+ | 参数 | 类型 | 说明 |
381
+ |------|------|------|
382
+ | `identifier` | `string` | 方块唯一标识符 |
383
+ | `category` | `string` | 创造栏分类 |
384
+ | `textures_arr` | `string[]` | 6 纹理数组 |
385
+ | `options.group` | `string` | 分组,默认 `"construction"` |
386
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
387
+
388
+ **自动添加的组件**
389
+ - `minecraft:destructible_by_mining` / `minecraft:destructible_by_explosion`
390
+ - `minecraft:support` → `{ shape: "stair" }`
391
+
392
+ **自动注册的 Traits**
393
+ - `minecraft:placement_direction` → 控制 `minecraft:cardinal_direction` 状态
394
+ - `minecraft:placement_position` → 控制 `minecraft:vertical_half` 状态
395
+
396
+ **自动生成的 Permutations(8 个)**
397
+
398
+ 底部 4 方向 + 顶部 4 方向,每个 permutation 设置碰撞箱、选择框和旋转变换。
399
+
400
+ **示例**
401
+
402
+ ```typescript
403
+ const stair = BlockAPI.createStairBlock('wiki:stair', 'construction',
404
+ ['stair_tex', 'stair_tex', 'stair_tex', 'stair_tex', 'stair_tex', 'stair_tex']
405
+ )
406
+ ```
407
+
408
+ ---
409
+
410
+ ### createTrapdoorBlock
411
+
412
+ 创建活板门方块(16 个 permutation:4 方向 × 2 上下 × 2 开闭)。
413
+
414
+ ```typescript
415
+ BlockAPI.createTrapdoorBlock(
416
+ identifier: string,
417
+ category: string,
418
+ texture: string,
419
+ options?: TrapdoorBlockOptions
420
+ ): TrapdoorBlock
421
+ ```
422
+
423
+ **参数**
424
+
425
+ | 参数 | 类型 | 说明 |
426
+ |------|------|------|
427
+ | `identifier` | `string` | 方块唯一标识符 |
428
+ | `category` | `string` | 创造栏分类 |
429
+ | `texture` | `string` | 纹理短名(6 面复用) |
430
+ | `options.group` | `string` | 分组,默认 `"construction"` |
431
+ | `options.hide_in_command` | `boolean` | 是否在命令中隐藏,默认 `false` |
432
+
433
+ **自动添加的组件**
434
+ - `minecraft:material_instances` → 6 面材质实例,`render_method: "blend"`
435
+ - `minecraft:destructible_by_mining` / `minecraft:destructible_by_explosion`
436
+
437
+ **自动注册的 Traits**
438
+ - `minecraft:placement_direction` → 控制 `minecraft:cardinal_direction` 状态
439
+ - `minecraft:placement_position` → 控制 `minecraft:vertical_half` 状态
440
+
441
+ **自动生成的 Permutations(16 个)**
442
+
443
+ 每个 permutation 根据方向、上下半、开闭状态设置碰撞箱。
444
+
445
+ **示例**
446
+
447
+ ```typescript
448
+ const trapdoor = BlockAPI.createTrapdoorBlock('wiki:trapdoor', 'construction', 'trapdoor_tex')
449
+ ```
450
+
451
+ ---
452
+
453
+ ## BasicBlock
454
+
455
+ 所有方块类型的基类。
456
+
457
+ ```typescript
458
+ import { BasicBlock, BlockComponent } from '@sapdon/core'
459
+ ```
460
+
461
+ ### 构造函数
462
+
463
+ ```typescript
464
+ new BasicBlock(
465
+ identifier: string,
466
+ category: string,
467
+ textures_arr: string[],
468
+ options?: {
469
+ group?: string
470
+ hide_in_command?: boolean
471
+ format_version?: string
472
+ }
473
+ )
474
+ ```
475
+
476
+ 构造函数自动调用 `BlockComponent.setMaterialInstances()` 设置 6 面纹理。
477
+
478
+ ### 方法
479
+
480
+ #### `addComponent(componentMap): this`
481
+
482
+ 添加组件。
483
+
484
+ ```typescript
485
+ block.addComponent(BlockComponent.setDisplayName('名称'))
486
+ ```
487
+
488
+ | 参数 | 类型 | 说明 |
489
+ |------|------|------|
490
+ | `componentMap` | `Map<string, any>` | 组件 Map,使用 `BlockComponent` 静态方法生成 |
491
+
492
+ #### `removeComponent(key): this`
493
+
494
+ 移除组件。
495
+
496
+ ```typescript
497
+ block.removeComponent('minecraft:display_name')
498
+ ```
499
+
500
+ | 参数 | 类型 | 说明 |
501
+ |------|------|------|
502
+ | `key` | `string` | 组件名称 |
503
+
504
+ #### `addPermutation(condition, componentMap): this`
505
+
506
+ 添加方块变体。
507
+
508
+ ```typescript
509
+ block.addPermutation(
510
+ "q.block_state('minecraft:cardinal_direction') == 'north'",
511
+ BlockComponent.setTransformation([0, 0, 0], [1, 1, 1], [0, 0, 0], [0, 0, 0])
512
+ )
513
+ ```
514
+
515
+ | 参数 | 类型 | 说明 |
516
+ |------|------|------|
517
+ | `condition` | `string` | MoLang 条件表达式 |
518
+ | `componentMap` | `Map<string, any>` | 该变体下的组件集合 |
519
+
520
+ #### `registerTrait(key, value): this`
521
+
522
+ 注册方块 trait。
523
+
524
+ ```typescript
525
+ block.registerTrait('minecraft:placement_direction', {
526
+ enabled_states: ['minecraft:cardinal_direction'],
527
+ y_rotation_offset: 0
528
+ })
529
+ ```
530
+
531
+ | 参数 | 类型 | 说明 |
532
+ |------|------|------|
533
+ | `key` | `string` | Trait 名称 |
534
+ | `value` | `any` | Trait 配置 |
535
+
536
+ #### `registerState(key, value): this`
537
+
538
+ 注册方块状态。
539
+
540
+ ```typescript
541
+ block.registerState('sapdon:block_variant_tag', { values: { min: 0, max: 3 } })
542
+ ```
543
+
544
+ | 参数 | 类型 | 说明 |
545
+ |------|------|------|
546
+ | `key` | `string` | 状态名称 |
547
+ | `value` | `any` | 状态定义(对象或数组) |
548
+
549
+ #### `getId(): string`
550
+
551
+ 返回方块标识符。
552
+
553
+ ```typescript
554
+ const id = block.getId() // "demo:stone"
555
+ ```
556
+
557
+ #### `toObject(): object`
558
+
559
+ 将方块转换为 JSON 格式对象(由 `@Serializer` 装饰器标记,框架自动调用)。
560
+
561
+ ---
562
+
563
+ ## Block
564
+
565
+ 继承自 `BasicBlock`,支持变体状态。
566
+
567
+ ```typescript
568
+ import { Block } from '@sapdon/core'
569
+ ```
570
+
571
+ ### 构造函数
572
+
573
+ ```typescript
574
+ new Block(
575
+ identifier: string,
576
+ category: string,
577
+ variantDatas: VariantData[],
578
+ options?: BlockOptions
579
+ )
580
+ ```
581
+
582
+ 额外注册状态 `sapdon:block_variant_tag`,为每个变体创建 `material_instances` permutation。
583
+
584
+ ### 方法
585
+
586
+ #### `addVariantComponent(variantIndex, componentMap): this`
587
+
588
+ 为指定变体添加组件。
589
+
590
+ ```typescript
591
+ block.addVariantComponent(0, BlockComponent.setLightEmission(10))
592
+ ```
593
+
594
+ | 参数 | 类型 | 说明 |
595
+ |------|------|------|
596
+ | `variantIndex` | `number` | 变体索引(从 0 开始) |
597
+ | `componentMap` | `Map` | 组件 Map |
598
+
599
+ 内部自动生成条件 `q.block_state('sapdon:block_variant_tag') == {index}`。
600
+
601
+ ---
602
+
603
+ ## RotatableBlock
604
+
605
+ 继承自 `BasicBlock`,支持方向旋转。
606
+
607
+ ```typescript
608
+ import { RotatableBlock, RotationTypes } from '@sapdon/core'
609
+ ```
610
+
611
+ ### 构造函数
612
+
613
+ ```typescript
614
+ new RotatableBlock(
615
+ identifier: string,
616
+ category: string,
617
+ textures_arr: string[],
618
+ options?: {
619
+ group?: string
620
+ hide_in_command?: boolean
621
+ rotationType?: RotationTypes
622
+ yRotationOffset?: number
623
+ }
624
+ )
625
+ ```
626
+
627
+ ### RotationTypes
628
+
629
+ ```typescript
630
+ enum RotationTypes {
631
+ CARDINAL = 'cardinal', // 北、南、东、西 (placement_direction)
632
+ FACING = 'facing', // 上、下、北、南、东、西 (placement_direction)
633
+ BLOCK_FACE = 'block_face', // 上、下、北、南、东、西 (placement_position)
634
+ LOG = 'log' // X/Y/Z 轴对齐原木旋转 (placement_position)
635
+ }
636
+ ```
637
+
638
+ | 类型 | 注册的 Trait | 状态值 | 方向数 |
639
+ |------|-------------|--------|--------|
640
+ | `CARDINAL` | `minecraft:placement_direction` | `minecraft:cardinal_direction` | 4 |
641
+ | `FACING` | `minecraft:placement_direction` | `minecraft:facing_direction` | 6 |
642
+ | `BLOCK_FACE` | `minecraft:placement_position` | `minecraft:block_face` | 6 |
643
+ | `LOG` | `minecraft:placement_position` | `minecraft:block_face` | 3 轴 |
644
+
645
+ ---
646
+
647
+ ## CropBlock
648
+
649
+ 继承自 `Block`,用于作物/植物方块。
650
+
651
+ ```typescript
652
+ import { CropBlock } from '@sapdon/core'
653
+ ```
654
+
655
+ ### 构造函数
656
+
657
+ ```typescript
658
+ new CropBlock(
659
+ identifier: string,
660
+ category: string,
661
+ variantDatas: VariantData[],
662
+ options?: CropBlockOptions
663
+ )
664
+ ```
665
+
666
+ 构造函数自动添加:
667
+ - `minecraft:collision_box` → `false`
668
+ - `minecraft:geometry` → `geometry.crop`
669
+ - `minecraft:placement_filter` → 仅限耕地上方
670
+ - 每生长阶段设置自定义选择框高度
671
+
672
+ **运行时依赖:** 需调用 `registerBuiltinComponents()` 注册内置的生长脚本组件。
673
+
674
+ ---
675
+
676
+ ## GeometryBlock
677
+
678
+ 继承自 `BasicBlock`,使用自定义几何模型。
679
+
680
+ ```typescript
681
+ import { GeometryBlock } from '@sapdon/core'
682
+ ```
683
+
684
+ ### 构造函数
685
+
686
+ ```typescript
687
+ new GeometryBlock(
688
+ identifier: string,
689
+ category: string,
690
+ geometry: string,
691
+ material_instances: Record<string, MaterialInstance>,
692
+ options?: GeometryBlockOptions
693
+ )
694
+ ```
695
+
696
+ 自动添加 `minecraft:geometry` 和 `minecraft:material_instances` 组件。
697
+
698
+ ---
699
+
700
+ ## OreBlock
701
+
702
+ 创建矿物方块及其世界生成规则。
703
+
704
+ ```typescript
705
+ import { OreBlock } from '@sapdon/core'
706
+ ```
707
+
708
+ ### 构造函数
709
+
710
+ ```typescript
711
+ new OreBlock(
712
+ identifier: string,
713
+ category: string,
714
+ textures_arr: string[],
715
+ options?: OreBlockOptions
716
+ )
717
+ ```
718
+
719
+ ### 属性(继承自 BasicBlock 之外的额外属性)
720
+
721
+ | 属性 | 类型 | 说明 |
722
+ |------|------|------|
723
+ | `feature` | `OreFeature` | 矿脉特征 |
724
+ | `feature_rules` | `FeatureRule` | 特征规则(默认 Y 0-64,10次/区块,主世界群系) |
725
+
726
+ ---
727
+
728
+ ## GlassBlock
729
+
730
+ 继承自 `BasicBlock`,透明玻璃方块。
731
+
732
+ ```typescript
733
+ import { GlassBlock } from '@sapdon/core'
734
+ ```
735
+
736
+ ### 构造函数
737
+
738
+ ```typescript
739
+ new GlassBlock(
740
+ identifier: string,
741
+ category: string,
742
+ texture: string,
743
+ options?: {
744
+ group?: string
745
+ hide_in_command?: boolean
746
+ geometry?: string
747
+ culling?: string
748
+ }
749
+ )
750
+ ```
751
+
752
+ | 参数 | 类型 | 说明 |
753
+ |------|------|------|
754
+ | `texture` | `string` | 单纹理短名,6 面复用 |
755
+
756
+ 构造函数自动添加 `light_dampening: 0`、`render_method: "blend"` 材质实例、挖掘/爆炸抗性。可选 `geometry` + `culling` 支持相邻面剔除。
757
+
758
+ ---
759
+
760
+ ## FenceBlock
761
+
762
+ 继承自 `BasicBlock`,栅栏方块。
763
+
764
+ ```typescript
765
+ import { FenceBlock } from '@sapdon/core'
766
+ ```
767
+
768
+ ### 构造函数
769
+
770
+ ```typescript
771
+ new FenceBlock(
772
+ identifier: string,
773
+ category: string,
774
+ textures_arr: string[],
775
+ options?: {
776
+ group?: string
777
+ hide_in_command?: boolean
778
+ leashable?: boolean
779
+ }
780
+ )
781
+ ```
782
+
783
+ 构造函数自动添加自定义碰撞箱、`support: "fence"`、`connection_rule: "all"`。
784
+
785
+ ---
786
+
787
+ ## StairBlock
788
+
789
+ 继承自 `BasicBlock`,楼梯方块。
790
+
791
+ ```typescript
792
+ import { StairBlock } from '@sapdon/core'
793
+ ```
794
+
795
+ ### 构造函数
796
+
797
+ ```typescript
798
+ new StairBlock(
799
+ identifier: string,
800
+ category: string,
801
+ textures_arr: string[],
802
+ options?: {
803
+ group?: string
804
+ hide_in_command?: boolean
805
+ }
806
+ )
807
+ ```
808
+
809
+ 构造函数自动注册 `minecraft:cardinal_direction` 和 `minecraft:vertical_half` 状态,通过 `placement_direction` 和 `placement_position` trait 控制玩家放置朝向,生成 8 个 permutation(含半砖碰撞箱 + 方向旋转变换)。
810
+
811
+ ---
812
+
813
+ ## TrapdoorBlock
814
+
815
+ 继承自 `BasicBlock`,活板门方块。
816
+
817
+ ```typescript
818
+ import { TrapdoorBlock } from '@sapdon/core'
819
+ ```
820
+
821
+ ### 构造函数
822
+
823
+ ```typescript
824
+ new TrapdoorBlock(
825
+ identifier: string,
826
+ category: string,
827
+ texture: string,
828
+ options?: {
829
+ group?: string
830
+ hide_in_command?: boolean
831
+ }
832
+ )
833
+ ```
834
+
835
+ 构造函数自动注册 `minecraft:cardinal_direction`、`minecraft:vertical_half`、`minecraft:open` 三个状态,生成 16 个 permutation。活板门关闭时为薄板碰撞箱(下半或上半),打开时根据方向生成竖立薄板碰撞箱。
836
+
837
+ ---
838
+
839
+ ## TileBlock
840
+
841
+ 方块与实体的组合,实现方块实体功能。
842
+
843
+ ```typescript
844
+ import { TileBlock } from '@sapdon/core'
845
+ ```
846
+
847
+ ### 构造函数
848
+
849
+ ```typescript
850
+ new TileBlock(
851
+ identifier: string,
852
+ category: string,
853
+ textures_arr: string[],
854
+ options?: TileBlockOptions
855
+ )
856
+ ```
857
+
858
+ ### 属性
859
+
860
+ | 属性 | 类型 | 说明 |
861
+ |------|------|------|
862
+ | `block` | `BasicBlock` | 方块实例(带有 `sapdon:block_or_entity` 状态) |
863
+ | `entity` | `Entity` | 实体实例(含容器、无敌、不可推动等组件) |
864
+
865
+ ### 方法
866
+
867
+ #### `setGeometry(geometry): void`
868
+
869
+ 同时设置方块和实体的几何模型。
870
+
871
+ ```typescript
872
+ tileBlock.setGeometry('geometry.chest')
873
+ ```
874
+
875
+ #### `addAnimation(name, animation): void`
876
+
877
+ 为客户端实体添加动画。
878
+
879
+ ```typescript
880
+ tileBlock.addAnimation('open', 'animation.chest.open')
881
+ ```
882
+
883
+ #### `setScript(key, value): void`
884
+
885
+ 为客户端实体设置脚本属性。
886
+
887
+ ```typescript
888
+ tileBlock.setScript('animate', 'open')
889
+ ```
890
+
891
+ ---
892
+
893
+ ## BlockComponent
894
+
895
+ 静态工具方法集合,用于生成组件 Map。每个方法返回 `Map<string, any>`。
896
+
897
+ ```typescript
898
+ import { BlockComponent } from '@sapdon/core'
899
+ ```
900
+
901
+ ### setMaterialInstances
902
+
903
+ ```typescript
904
+ static setMaterialInstances(instances: object): Map
905
+ ```
906
+
907
+ 设置材质实例。
908
+
909
+ ```typescript
910
+ BlockComponent.setMaterialInstances({
911
+ '*': { texture: 'tex', render_method: 'opaque' },
912
+ 'up': { texture: 'tex_top' }
913
+ })
914
+ ```
915
+
916
+ ### setGeometry
917
+
918
+ ```typescript
919
+ static setGeometry(identifier: string, bone_visibility?: object): Map
920
+ ```
921
+
922
+ 设置几何模型。
923
+
924
+ ```typescript
925
+ BlockComponent.setGeometry('geometry.cube', { bone1: true })
926
+ ```
927
+
928
+ ### setCollisionBoxEnabled
929
+
930
+ ```typescript
931
+ static setCollisionBoxEnabled(enabled: boolean): Map
932
+ ```
933
+
934
+ 启用/禁用碰撞箱。
935
+
936
+ ### setCollisionBoxCustom
937
+
938
+ ```typescript
939
+ static setCollisionBoxCustom(origin: number[], size: number[]): Map
940
+ ```
941
+
942
+ 设置自定义碰撞箱。origin 范围:`(-8, 0, -8)` ~ `(8, 16, 8)`。
943
+
944
+ ### setSelectionBoxEnabled
945
+
946
+ ```typescript
947
+ static setSelectionBoxEnabled(enabled: boolean): Map
948
+ ```
949
+
950
+ 启用/禁用选择框。
951
+
952
+ ### setSelectionBoxCustom
953
+
954
+ ```typescript
955
+ static setSelectionBoxCustom(origin: number[], size: number[]): Map
956
+ ```
957
+
958
+ 设置自定义选择框。origin 范围:`(-8, 0, -8)` ~ `(8, 16, 8)`。
959
+
960
+ ### setDestructibleByExplosionEnabled
961
+
962
+ ```typescript
963
+ static setDestructibleByExplosionEnabled(enabled: boolean): Map
964
+ ```
965
+
966
+ 启用/禁用爆炸破坏。
967
+
968
+ ### setDestructibleByExplosionCustom
969
+
970
+ ```typescript
971
+ static setDestructibleByExplosionCustom(explosionResistance: number): Map
972
+ ```
973
+
974
+ 设置自定义爆炸抗性。
975
+
976
+ ### setDestructibleByMiningEnabled
977
+
978
+ ```typescript
979
+ static setDestructibleByMiningEnabled(enabled: boolean): Map
980
+ ```
981
+
982
+ 启用/禁用挖掘破坏。
983
+
984
+ ### setDestructibleByMiningCustom
985
+
986
+ ```typescript
987
+ static setDestructibleByMiningCustom(
988
+ secondsToDestroy: number,
989
+ itemSpecificSpeeds?: Array<{ item: string; destroy_speed: number }>
990
+ ): Map
991
+ ```
992
+
993
+ 设置自定义挖掘时间及工具速度。
994
+
995
+ ### setDisplayName
996
+
997
+ ```typescript
998
+ static setDisplayName(displayName: string): Map
999
+ ```
1000
+
1001
+ 设置显示名称。
1002
+
1003
+ ### setFlammableEnabled
1004
+
1005
+ ```typescript
1006
+ static setFlammableEnabled(enabled: boolean): Map
1007
+ ```
1008
+
1009
+ 启用/禁用燃烧。
1010
+
1011
+ ### setFlammableCustom
1012
+
1013
+ ```typescript
1014
+ static setFlammableCustom(
1015
+ catchChanceModifier: number,
1016
+ destroyChanceModifier: number,
1017
+ lava_flammable?: 'always' | 'never'
1018
+ ): Map
1019
+ ```
1020
+
1021
+ 设置自定义燃烧概率(≥ 0)。可选 `lava_flammable` 参数控制岩浆能否点燃该方块,默认 `"never"`。
1022
+
1023
+ ### setFriction
1024
+
1025
+ ```typescript
1026
+ static setFriction(value: number): Map
1027
+ ```
1028
+
1029
+ 设置摩擦力(0.0 ~ 0.9)。
1030
+
1031
+ ### setLightDampening
1032
+
1033
+ ```typescript
1034
+ static setLightDampening(value: number): Map
1035
+ ```
1036
+
1037
+ 设置遮光值(0 ~ 15)。
1038
+
1039
+ ### setLightEmission
1040
+
1041
+ ```typescript
1042
+ static setLightEmission(value: number): Map
1043
+ ```
1044
+
1045
+ 设置发光值(0 ~ 15)。
1046
+
1047
+ ### setLoot
1048
+
1049
+ ```typescript
1050
+ static setLoot(path: string): Map
1051
+ ```
1052
+
1053
+ 设置战利品表路径(最大 256 字符)。
1054
+
1055
+ ### setMapColor
1056
+
1057
+ ```typescript
1058
+ static setMapColor(value: string | number[] | { color: string | number[], tint_method?: string }): Map
1059
+ ```
1060
+
1061
+ 设置地图颜色。支持:
1062
+ - 十六进制字符串 `"#RRGGBB"`
1063
+ - RGB 数组 `[255, 0, 0]`
1064
+ - 对象格式 `{ color: "#RRGGBB", tint_method: "grass" }` — 带生物群系染色
1065
+
1066
+ ### setTransformation
1067
+
1068
+ ```typescript
1069
+ static setTransformation(
1070
+ translation?: number[], // 默认 [0, 0, 0]
1071
+ scale?: number[], // 默认 [1, 1, 1]
1072
+ scale_pivot?: number[], // 默认 [0, 0, 0]
1073
+ rotation?: number[], // 默认 [0, 0, 0](角度)
1074
+ rotation_pivot?: number[] // 默认 [0, 0, 0]
1075
+ ): Map
1076
+ ```
1077
+
1078
+ 设置方块变换(平移、缩放、旋转)。
1079
+
1080
+ ### setPlacementFilter
1081
+
1082
+ ```typescript
1083
+ static setPlacementFilter(conditions: PlacementCondition[]): Map
1084
+ ```
1085
+
1086
+ 设置放置过滤条件(1 ~ 64 条)。
1087
+
1088
+ ```typescript
1089
+ interface PlacementCondition {
1090
+ allowed_faces?: ('up' | 'down' | 'north' | 'south' | 'east' | 'west' | 'side' | 'all')[]
1091
+ block_filter?: (string | BlockDescriptor)[]
1092
+ }
1093
+ interface BlockDescriptor {
1094
+ name?: string
1095
+ tags?: string
1096
+ states?: object
1097
+ }
1098
+ ```
1099
+
1100
+ ### setRedstoneConductivity
1101
+
1102
+ ```typescript
1103
+ static setRedstoneConductivity(allowsWireToStepDown: boolean, redstoneConductor: boolean): Map
1104
+ ```
1105
+
1106
+ 设置红石导电性。
1107
+
1108
+ ### setCraftingTable
1109
+
1110
+ ```typescript
1111
+ static setCraftingTable(craftingTags: string[], tableName?: string): Map
1112
+ ```
1113
+
1114
+ 设置合成台属性(最多 64 个标签)。
1115
+
1116
+ ### setTick
1117
+
1118
+ ```typescript
1119
+ static setTick(interval_range: number[], looping: boolean): Map
1120
+ ```
1121
+
1122
+ 设置 Tick 间隔。
1123
+
1124
+ ### setCustomComponents
1125
+
1126
+ ```typescript
1127
+ static setCustomComponents(custom_components: string[]): Map
1128
+ ```
1129
+
1130
+ 设置自定义组件(需要 Script API)。
1131
+
1132
+ ### setCustomComponentV2
1133
+
1134
+ ```typescript
1135
+ static setCustomComponentV2(component_id: string, params: object): Map
1136
+ ```
1137
+
1138
+ 设置自定义组件 V2(需要 Script API V2.0.0+)。
1139
+
1140
+ ### setDestructionParticles
1141
+
1142
+ ```typescript
1143
+ static setDestructionParticles(
1144
+ texture: string,
1145
+ options?: {
1146
+ particle_count?: number // 粒子数量 0-255,默认 100
1147
+ tint_method?: string // 染色方法,如 "grass"
1148
+ }
1149
+ ): Map
1150
+ ```
1151
+
1152
+ 设置破坏粒子纹理。可选 `particle_count` 控制粒子数量(0-255,默认 100)。
1153
+
1154
+ ### setItemVisual
1155
+
1156
+ ```typescript
1157
+ static setItemVisual(geometry: string, materialInstances: object): Map
1158
+ ```
1159
+
1160
+ 设置物品栏中的视觉属性。
1161
+
1162
+ ### setLiquidDetection
1163
+
1164
+ ```typescript
1165
+ // 选项对象格式(推荐,支持多检测规则)
1166
+ static setLiquidDetection(options: {
1167
+ detection_rules?: {
1168
+ liquid_type: string
1169
+ can_contain_liquid: boolean
1170
+ on_liquid_touches?: 'blocking' | 'broken' | 'popped' | 'no_reaction'
1171
+ stops_liquid_flowing_from_direction?: string[]
1172
+ use_liquid_clipping?: boolean
1173
+ }[]
1174
+ use_liquid_clipping?: boolean
1175
+ }): Map
1176
+
1177
+ // 旧版单规则格式(向后兼容)
1178
+ static setLiquidDetection(
1179
+ canContainLiquid: boolean,
1180
+ liquidType?: 'water',
1181
+ onLiquidTouches?: 'blocking' | 'broken' | 'popped' | 'no_reaction',
1182
+ stopsLiquidFlowingFromDirection?: string[]
1183
+ ): Map
1184
+ ```
1185
+
1186
+ 设置液体检测属性。使用对象参数时可设置 `detection_rules` 数组(支持水淹等行为)和 `use_liquid_clipping`。
1187
+
1188
+ ```typescript
1189
+ // 示例:水淹方块
1190
+ BlockComponent.setLiquidDetection({
1191
+ detection_rules: [{
1192
+ liquid_type: "water",
1193
+ can_contain_liquid: true,
1194
+ on_liquid_touches: "no_reaction"
1195
+ }]
1196
+ })
1197
+ ```
1198
+
1199
+ ### setBreathability
1200
+
1201
+ ```typescript
1202
+ static setBreathability(value: 'solid' | 'air'): Map
1203
+ ```
1204
+
1205
+ 设置呼吸性。
1206
+
1207
+ ### combineComponents
1208
+
1209
+ ```typescript
1210
+ static combineComponents(...componentMaps: Map[]): Map
1211
+ ```
1212
+
1213
+ 合并多个组件 Map。
1214
+
1215
+ ```typescript
1216
+ BlockComponent.combineComponents(
1217
+ BlockComponent.setDisplayName('测试'),
1218
+ BlockComponent.setLightEmission(5)
1219
+ )
1220
+ ```
1221
+
1222
+ ### toJSON
1223
+
1224
+ ```typescript
1225
+ static toJSON(components: Map): object
1226
+ ```
1227
+
1228
+ 将组件 Map 转换为普通 JSON 对象。
1229
+
1230
+ ---
1231
+
1232
+ ## BlockCustomComponentBuilder
1233
+
1234
+ 块自定义组件构建器,提供 TypeScript 类型安全的流式 API 定义块事件处理器,并可生成脚本端注册代码。
1235
+
1236
+ ```typescript
1237
+ import { BlockCustomComponentBuilder } from '@sapdon/core'
1238
+ ```
1239
+
1240
+ ### 构造函数
1241
+
1242
+ ```typescript
1243
+ new BlockCustomComponentBuilder(componentId: string)
1244
+ ```
1245
+
1246
+ | 参数 | 类型 | 说明 |
1247
+ |------|------|------|
1248
+ | `componentId` | `string` | 自定义组件标识符,格式 `"命名空间:组件名"` |
1249
+
1250
+ ### 方法
1251
+
1252
+ #### 事件绑定(均返回 `this`,可链式调用)
1253
+
1254
+ | 方法 | 对应事件 | Wiki 说明 |
1255
+ |------|---------|-----------|
1256
+ | `beforeOnPlayerPlace(handler)` | `beforeOnPlayerPlace` | 玩家放置前触发 |
1257
+ | `onBlockStateChange(handler)` | `onBlockStateChange` | 块状态改变时触发 |
1258
+ | `onBreak(handler)` | `onBreak` | 块被破坏时触发 |
1259
+ | `onEntity(handler)` | `onEntity` | 实体在块上执行事件时触发 |
1260
+ | `onEntityFallOn(handler)` | `onEntityFallOn` | 实体坠落在块上时触发 |
1261
+ | `onPlace(handler)` | `onPlace` | 块被放置时触发 |
1262
+ | `onPlayerBreak(handler)` | `onPlayerBreak` | 玩家破坏块时触发 |
1263
+ | `onPlayerInteract(handler)` | `onPlayerInteract` | 玩家与块交互时触发 |
1264
+ | `onRandomTick(handler)` | `onRandomTick` | 随机刻触发 |
1265
+ | `onRedstoneUpdate(handler)` | `onRedstoneUpdate` | 红石信号更新时触发 |
1266
+ | `onStepOff(handler)` | `onStepOff` | 实体离开块时触发 |
1267
+ | `onStepOn(handler)` | `onStepOn` | 实体踏上块时触发 |
1268
+ | `onTick(handler)` | `onTick` | 块计划刻触发 |
1269
+
1270
+ #### `id(): string`
1271
+
1272
+ 返回组件标识符,用于 `BlockComponent.setCustomComponents()`。
1273
+
1274
+ #### `build(): BlockCustomComponentHandlers`
1275
+
1276
+ 构建处理器对象,可用于 runtime 的 `registerCustomComponent`。
1277
+
1278
+ #### `handlerCount(): number`
1279
+
1280
+ 返回已注册的事件处理器数量。
1281
+
1282
+ ### 事件参数类型
1283
+
1284
+ | 接口名 | 属性 |
1285
+ |--------|------|
1286
+ | `BeforeOnPlayerPlaceEvent` | `block`, `cancel`, `dimension`, `face`, `permutationToPlace`, `player?` |
1287
+ | `OnBlockStateChangeEvent` | `block`, `dimension`, `previousPermutation` |
1288
+ | `OnBreakEvent` | `block`, `dimension`, `blockDestructionSource?`, `brokenBlockPermutation`, `entitySource?` |
1289
+ | `OnEntityEvent` | `block`, `blockPermutation`, `dimension`, `entitySource`, `name` |
1290
+ | `OnEntityFallOnEvent` | `block`, `dimension`, `entity?`, `fallDistance` |
1291
+ | `OnPlaceEvent` | `block`, `dimension`, `previousBlock` |
1292
+ | `OnPlayerBreakEvent` | `block`, `brokenBlockPermutation`, `dimension`, `player?` |
1293
+ | `OnPlayerInteractEvent` | `block`, `dimension`, `face`, `faceLocation`, `player?` |
1294
+ | `OnRandomTickEvent` | `block`, `dimension` |
1295
+ | `OnRedstoneUpdateEvent` | `block`, `dimension`, `power` |
1296
+ | `OnStepOffEvent` | `block`, `dimension`, `entity?` |
1297
+ | `OnStepOnEvent` | `block`, `dimension`, `entity?` |
1298
+ | `OnTickEvent` | `block`, `dimension` |
1299
+
1300
+ ### 示例
1301
+
1302
+ ```typescript
1303
+ import { BlockAPI, BlockComponent, BlockCustomComponentBuilder, registry } from '@sapdon/core'
1304
+
1305
+ // 定义块自定义组件事件处理器(构建时)
1306
+ const growComponent = new BlockCustomComponentBuilder('wiki:crop_grow')
1307
+ .onRandomTick(({ block }) => {
1308
+ const stage = block.permutation.getState('sapdon:block_variant_tag') as number
1309
+ if (stage < 3) {
1310
+ block.setPermutation(
1311
+ block.permutation.withState('sapdon:block_variant_tag', stage + 1)
1312
+ )
1313
+ }
1314
+ })
1315
+ .onPlayerInteract(({ block, player }) => {
1316
+ if (player?.getGameMode() === 'creative') {
1317
+ block.setPermutation(
1318
+ block.permutation.withState('sapdon:block_variant_tag', 3)
1319
+ )
1320
+ }
1321
+ })
1322
+
1323
+ // 创建方块并关联组件 ID
1324
+ const crop = BlockAPI.createCropBlock('wiki:tomato', 'nature', [
1325
+ { stateTag: 0, textures: ['stage_0', 'stage_0', 'stage_0', 'stage_0', 'stage_0', 'stage_0'] },
1326
+ { stateTag: 1, textures: ['stage_1', 'stage_1', 'stage_1', 'stage_1', 'stage_1', 'stage_1'] },
1327
+ { stateTag: 2, textures: ['stage_2', 'stage_2', 'stage_2', 'stage_2', 'stage_2', 'stage_2'] },
1328
+ { stateTag: 3, textures: ['stage_3', 'stage_3', 'stage_3', 'stage_3', 'stage_3', 'stage_3'] }
1329
+ ])
1330
+ crop.addComponent(BlockComponent.setCustomComponents([growComponent.id()]))
1331
+
1332
+ registry.submit()
1333
+ ```
1334
+
1335
+ ### 运行时脚本自动生成
1336
+
1337
+ `registry.submit()` 会自动收集所有 `BlockCustomComponentBuilder` 实例,并在构建时通过 `load.js` 生成对应的运行时脚本文件:
1338
+
1339
+ - **脚本文件** → `scripts/custom_components/{componentId}.js`(以 `:` 替换为 `_` 命名)
1340
+ - **索引文件** → `scripts/custom_components/index.js`(以正确 ID 注册所有组件)
1341
+ - 文件已存在时**跳过**,不会覆盖用户修改
1342
+
1343
+ 需要在项目入口文件 `scripts/index.js`(或 `scripts/index.ts`)中添加导入:
1344
+
1345
+ ```typescript
1346
+ import './custom_components/index.js'
1347
+ ```
1348
+
1349
+ 这样 `load.js` 生成的注册代码即可自动执行。
1350
+
1351
+ 内置 OC 运行时组件(如 `sapdon:head_rotation`)注册只需调用:
1352
+
1353
+ ```typescript
1354
+ import { registerBuiltinComponents } from '@sapdon/runtime'
1355
+ registerBuiltinComponents()
1356
+ ```
1357
+
1358
+ ---
1359
+
1360
+ ## Permutation
1361
+
1362
+ 方块变体,由条件和组件集合组成。
1363
+
1364
+ ```typescript
1365
+ interface Permutation {
1366
+ condition: string
1367
+ components: Record<string, any>
1368
+ }
1369
+ ```
1370
+
1371
+ 通过 `basicBlock.addPermutation(condition, componentMap)` 创建。
1372
+
1373
+ **condition** — MoLang 表达式,如:
1374
+
1375
+ ```
1376
+ "q.block_state('minecraft:cardinal_direction') == 'north'"
1377
+ "q.block_state('sapdon:block_variant_tag') == 0"
1378
+ ```
1379
+
1380
+ **componentMap** — `Map<string, any>`,由 `BlockComponent` 方法生成。
1381
+
1382
+ ---
1383
+
1384
+ ---
1385
+
1386
+ ## TintMethod
1387
+
1388
+ 生物群系染色方法常量,用于 `BlockComponent.setMaterialInstances()` 和 `BlockComponent.setMapColor()` 的 `tint_method` 参数。
1389
+
1390
+ ```typescript
1391
+ import { TintMethod } from '@sapdon/core'
1392
+ ```
1393
+
1394
+ ```typescript
1395
+ const TintMethod = {
1396
+ GRASS: 'grass', // 草地色
1397
+ WATER: 'water', // 水色
1398
+ DEFAULT_FOLIAGE: 'default_foliage', // 默认 foliage 色
1399
+ EVERGREEN_FOLIAGE: 'evergreen_foliage', // 常绿 foliage 色
1400
+ DRY_FOLIAGE: 'dry_foliage', // 干 foliage 色
1401
+ BIRCH_FOLIAGE: 'birch_foliage', // 白桦 foliage 色
1402
+ } as const
1403
+ ```
1404
+
1405
+ **示例**
1406
+
1407
+ ```typescript
1408
+ BlockComponent.setMaterialInstances({
1409
+ '*': { texture: 'grass_tex', render_method: 'opaque', tint_method: TintMethod.GRASS }
1410
+ })
1411
+ ```
1412
+
1413
+ ---
1414
+
1415
+ ## FlipbookTextureConfig
1416
+
1417
+ 翻转书纹理配置构建器,用于生成资源包 `flipbook_textures.json`。
1418
+
1419
+ ```typescript
1420
+ import { FlipbookTextureConfig } from '@sapdon/core'
1421
+ ```
1422
+
1423
+ ### 方法
1424
+
1425
+ | 方法 | 说明 |
1426
+ |------|------|
1427
+ | `addEntry(entry: FlipbookEntry)` | 添加一个翻转书纹理条目 |
1428
+ | `addEntries(entries: FlipbookEntry[])` | 批量添加多个条目 |
1429
+ | `toObject()` | 生成 `{ flipbook_textures: [...] }` 对象,可直接注册到资源包 |
1430
+
1431
+ **FlipbookEntry**
1432
+
1433
+ ```typescript
1434
+ interface FlipbookEntry {
1435
+ flipbook_texture: string // 纹理短名
1436
+ atlas_tile?: string // 图集 tile 名称(默认同 flipbook_texture)
1437
+ ticks_per_frame?: number // 每帧停留 tick 数(默认 2)
1438
+ blend_frames?: boolean // 是否混合帧(默认 false)
1439
+ replicate?: number // 复制次数(默认 1)
1440
+ }
1441
+ ```
1442
+
1443
+ **示例**
1444
+
1445
+ ```typescript
1446
+ const flipbook = new FlipbookTextureConfig()
1447
+ .addEntry({ flipbook_texture: 'water_flow', ticks_per_frame: 3 })
1448
+ .addEntry({ flipbook_texture: 'lava_flow', ticks_per_frame: 2, blend_frames: true })
1449
+ ```
1450
+
1451
+ ---
1452
+
1453
+ ## TextureVariationConfig
1454
+
1455
+ 纹理变体配置构建器,用于生成资源包 `terrain_texture.json`。
1456
+
1457
+ ```typescript
1458
+ import { TextureVariationConfig } from '@sapdon/core'
1459
+ ```
1460
+
1461
+ ### 方法
1462
+
1463
+ | 方法 | 说明 |
1464
+ |------|------|
1465
+ | `addTexture(name, path)` | 添加单纹理映射:短名 → 图片路径 |
1466
+ | `addTextureWithVariations(name, variations)` | 添加带权重的多变体纹理 |
1467
+ | `toObject()` | 生成 `{ texture_data: {...} }` 对象,可直接注册到资源包 |
1468
+
1469
+ **TextureVariation**
1470
+
1471
+ ```typescript
1472
+ interface TextureVariation {
1473
+ path: string // 纹理图片路径
1474
+ weight?: number // 随机权重(默认 1)
1475
+ }
1476
+ ```
1477
+
1478
+ **示例**
1479
+
1480
+ ```typescript
1481
+ const tex = new TextureVariationConfig()
1482
+ .addTexture('stone', 'textures/blocks/stone')
1483
+ .addTextureWithVariations('grass', [
1484
+ { path: 'textures/blocks/grass_1', weight: 3 },
1485
+ { path: 'textures/blocks/grass_2', weight: 1 }
1486
+ ])
1487
+ ```
1488
+
1489
+ ---
1490
+
1491
+ ## 类型汇总
1492
+
1493
+ ```typescript
1494
+ // 方块工厂
1495
+ BlockAPI.createBasicBlock(identifier, category, textures_arr, options?)
1496
+ BlockAPI.createBlock(identifier, category, variantDatas, options?)
1497
+ BlockAPI.createRotatableBlock(identifier, category, textures_arr, options?)
1498
+ BlockAPI.createGeometryBlock(identifier, category, geometry, material_instances, options?)
1499
+ BlockAPI.createCropBlock(identifier, category, variantDatas, options?)
1500
+ BlockAPI.createOreBlock(identifier, category, textures_arr, options?)
1501
+ BlockAPI.createGlassBlock(identifier, category, texture, options?)
1502
+ BlockAPI.createFenceBlock(identifier, category, textures_arr, options?)
1503
+ BlockAPI.createStairBlock(identifier, category, textures_arr, options?)
1504
+ BlockAPI.createTrapdoorBlock(identifier, category, texture, options?)
1505
+
1506
+ // 类
1507
+ class BasicBlock { ... }
1508
+ class Block extends BasicBlock { ... }
1509
+ class RotatableBlock extends BasicBlock { ... }
1510
+ class GeometryBlock extends BasicBlock { ... }
1511
+ class CropBlock extends Block { ... }
1512
+ class OreBlock extends BasicBlock { feature, feature_rules }
1513
+ class GlassBlock extends BasicBlock { ... }
1514
+ class FenceBlock extends BasicBlock { ... }
1515
+ class StairBlock extends BasicBlock { ... }
1516
+ class TrapdoorBlock extends BasicBlock { ... }
1517
+ class TileBlock { block, entity }
1518
+
1519
+ // 枚举
1520
+ RotationTypes = { CARDINAL, FACING, BLOCK_FACE, LOG }
1521
+ TintMethod = { GRASS, WATER, DEFAULT_FOLIAGE, EVERGREEN_FOLIAGE, DRY_FOLIAGE, BIRCH_FOLIAGE }
1522
+
1523
+ // 工具类
1524
+ BlockComponent = { setMaterialInstances, setGeometry, ... }
1525
+ class FlipbookTextureConfig { addEntry, addEntries, toObject }
1526
+ class TextureVariationConfig { addTexture, addTextureWithVariations, toObject }
1527
+
1528
+ // 块自定义组件构建器
1529
+ class BlockCustomComponentBuilder { constructor(componentId), id(), build(), handlerCount(), onTick(), ... }
1530
+ ```