User Tools

Site Tools


zh_cn:tutorial:items

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:items [2025/04/01 07:39] – [添加物品纹理] solidblockzh_cn:tutorial:items [2026/01/16 13:02] (current) peqb
Line 45: Line 45:
 ===== 注册物品的最佳实践(1.21.2 之前) ===== ===== 注册物品的最佳实践(1.21.2 之前) =====
  
-在上面的代码中,简直创建了//一个//物品。但是如果模组有//许多//物品,这样并不方,因为你每次都需要注册、创建一个 ''Identifier''。所以我们创建一个专门的类存储物品对象,例如 ''ModItems'' 或者 ''TutorialItems'',然后简单的 ''register'' 方法便捷地注册物品。这在实际模组开发中很常见。你也可以看看原版的 ''Items'' 类以了解 Minecraft 中如何以类似方式完成的。+在上面的代码中,创建了一个物品。但是如果模组有许多物品,这样并不方便,因为你每次都需要注册、创建一个 ''Identifier''。所以我们创建一个专门的类存储物品对象,例如 ''ModItems'' 或者 ''TutorialItems'',然后简单的 ''register'' 方法便捷地注册物品。这在实际模组开发中很常见。你也可以看看原版的 ''Items'' 类以了解 Minecraft 中如何以类似方式完成的。
  
 在这个例子中,创建一个 ''TutorialItems'' 类,并在 ''ModInitializer'' 中引用这个类。 在这个例子中,创建一个 ''TutorialItems'' 类,并在 ''ModInitializer'' 中引用这个类。
Line 110: Line 110:
 ===== 添加物品模型、纹理和模型映射 ===== ===== 添加物品模型、纹理和模型映射 =====
  
-如果第一步成功注册了你的物品,就可以输入命令 ''/give @s tutorial:custom_item'' 成功得到你的物品。你会发现纹理缺失,Minecraft 会像这样报错:+如果第一步成功注册了你的物品,就可以通过输入命令 ''/give @s tutorial:custom_item'' 得到你的物品。你会发现纹理缺失,Minecraft 会像这样报错:
  
     [Server-Worker-1/WARN]: Unable to load model: 'tutorial:custom_item#inventory' referenced from: tutorial:custom_item#inventory: java.io.FileNotFoundException: tutorial:models/item/custom_item.json     [Server-Worker-1/WARN]: Unable to load model: 'tutorial:custom_item#inventory' referenced from: tutorial:custom_item#inventory: java.io.FileNotFoundException: tutorial:models/item/custom_item.json
  
-这是因为我们还没有给物品提供纹理(texture)、烘焙模型(baked module,以下简称模型)以及相应的模型映射(自 1.21.4 开始)。所以,你需要定义物品模型并提供纹理图像。你会需要将这些添加到你的资源的目的下,直接路径如下:+这是因为我们还没有给物品提供**纹理**(texture)、**烘焙模型**(baked module,以下简称模型)以及相应的**模型映射**(自 1.21.4 开始)。这些文件分别位于以位置
  
-为物品注册纹理需要物品模型.json文件和纹理图像文件。 您将需要将它们添加到资源目录中。每个的直接路径是:+为物品注册纹理需要物品模型.json文件和纹理图像文件。 您将需要将它们添加到资源目录中。每个文件的直接路径是:
  
   * 物品模型:''.../resources/assets/tutorial/models/item/custom_item.json''   * 物品模型:''.../resources/assets/tutorial/models/item/custom_item.json''
   * 物品纹理:''.../resources/assets/tutorial/textures/item/custom_item.png''   * 物品纹理:''.../resources/assets/tutorial/textures/item/custom_item.png''
-  * 物品模型映射:''.../resources/assets/tutorial/items/custom_item.json''+  * 物品模型映射(自从 1.21.4):''.../resources/assets/tutorial/items/custom_item.json''
  
 我们将使用[[https://i.imgur.com/CqLSMEQ.png|这个示例纹理]]。 我们将使用[[https://i.imgur.com/CqLSMEQ.png|这个示例纹理]]。
  
 一个非常简单的物品模型长这个样子: 一个非常简单的物品模型长这个样子:
-<code JavaScript>+<code javascript /resources/assets/tutorial/models/item/custom_item.json>
 { {
   "parent": "item/generated",   "parent": "item/generated",
Line 133: Line 133:
 } }
 </code> </code>
-你的物品模型的 ''parent''改变了物品在手中以及在物品栏内等情形下的渲染。''item/generated'' 用于许多简单的物品。''item/handheld'' 用于手持其纹理左下角的物品。在 json 中,''textures/layer0'' 是图像文件的位置。+ 
 +你的物品模型的 ''parent'' 改变了物品在手中以及在物品栏内等情形下的渲染。''item/generated'' 用于许多简单的物品。''item/handheld'' 用于手持其纹理左下角的物品。在 json 中,''textures/layer0'' 是图像文件的位置。 
 + 
 +从 1.21.4 开始,还需要物品模型映射(1.21.4 之前不需要),其内容如下: 
 +<code javascript /resources/assets/tutorial/items/custom_item.json> 
 +
 +  "model":
 +    "type": "model", 
 +    "model": "tutorial:item/custom_item" 
 +  } 
 +
 +</code> 
 + 
 +该物品模型映射将指定物品使用对应的物品模型。 
 + 
 +> :!: 手动创建这些文件显然会很累。了解数据生成可查看 [[datagen_model]] 页面。 
  
 ===== 创建物品类 ===== ===== 创建物品类 =====
Line 185: Line 201:
 } }
 </yarncode> </yarncode>
-如果你正确执行了所有操作,使用该物品现在应该会播放声音。+如果你正确执行了所有操作,现在使用该物品应该会播放声音。
  
 ===== 物品组件 ===== ===== 物品组件 =====
Line 192: Line 208:
 这个例子中,物品默认不可破坏,并隐藏关于这一点的物品提示: 这个例子中,物品默认不可破坏,并隐藏关于这一点的物品提示:
 <yarncode java> <yarncode java>
-    // For versions below 1.21.2:+    // 对于 1.21.2 之前的版本:
     public static final CustomItem CUSTOM_ITEM = register("custom_item", new CustomItem(new class_1792.class_1793()     public static final CustomItem CUSTOM_ITEM = register("custom_item", new CustomItem(new class_1792.class_1793()
         .component(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(true))));         .component(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(true))));
-    // For versions since 1.21.2:+         
 +    // 对于从 1.21.2 及以后、1.21.4 之前的版本:
     public static final Item CUSTOM_ITEM = register("custom_item", CustomItem::new, new Item.Settings()     public static final Item CUSTOM_ITEM = register("custom_item", CustomItem::new, new Item.Settings()
         .component(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(true)));         .component(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(true)));
 +        
 +    // 对于从 1.21.4 及以后:
 +    public static final Item CUSTOM_ITEM = register("custom_item", CustomItem::new, new Item.Settings()
 +        .component(DataComponentTypes.UNBREAKABLE, Unit.INSTANCE));
 </yarncode> </yarncode>
  
Line 206: Line 227:
     // 我们新物品的实例,最大堆叠数为 16     // 我们新物品的实例,最大堆叠数为 16
          
-    // For versions below 1.21.2:+    // 对于 1.21.2 之前的版本:
     public static final CustomItem CUSTOM_ITEM = register("custom_item", new CustomItem(new class_1792.class_1793().maxCount(16)));     public static final CustomItem CUSTOM_ITEM = register("custom_item", new CustomItem(new class_1792.class_1793().maxCount(16)));
-    // For versions since 1.21.2:+     
 +    // 自从 1.21.2 开始:
     public static final Item CUSTOM_ITEM = register("custom_item", CustomItem::new, new Item.Settings().maxCount(16));     public static final Item CUSTOM_ITEM = register("custom_item", CustomItem::new, new Item.Settings().maxCount(16));
     [...]     [...]
Line 275: Line 297:
 类似地,你也可以使用 ''CompostingChanceRegistry'' 来让物品可以在堆肥桶中堆肥。 类似地,你也可以使用 ''CompostingChanceRegistry'' 来让物品可以在堆肥桶中堆肥。
 ===== 下一步 ===== ===== 下一步 =====
-试着[[zh_cn:tutorial:itemgroup|将你的物品添加到一个物品组中]]。+试着[[itemgroup|将你的物品添加到一个物品组中]]。你的物品还没有名字,所以还可以查看[[lang|如何创建语言文件]]。
zh_cn/tutorial/items.1743493149.txt.gz · Last modified: 2025/04/01 07:39 by solidblock