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:50] – [添加物品模型、纹理和模型映射] solidblockzh_cn:tutorial:items [2025/07/22 15:18] (current) – fix typo [注册物品的最佳实践(1.21.2 之前)] solidblock
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 147: Line 147:
  
 该物品模型映射将指定物品使用对应的物品模型。 该物品模型映射将指定物品使用对应的物品模型。
 +
 +> :!: 手动创建这些文件显然会很累。了解数据生成可看看 [[datagen_model]] 页面。
 +
  
 ===== 创建物品类 ===== ===== 创建物品类 =====
Line 205: 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 219: 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 288: Line 297:
 类似地,你也可以使用 ''CompostingChanceRegistry'' 来让物品可以在堆肥桶中堆肥。 类似地,你也可以使用 ''CompostingChanceRegistry'' 来让物品可以在堆肥桶中堆肥。
 ===== 下一步 ===== ===== 下一步 =====
-试着[[zh_cn:tutorial:itemgroup|将你的物品添加到一个物品组中]]。+试着[[itemgroup|将你的物品添加到一个物品组中]]。你的物品还没有名字,所以还可以看看[[lang|如何创建语言文件]]。
zh_cn/tutorial/items.1743493827.txt.gz · Last modified: 2025/04/01 07:50 by solidblock