User Tools

Site Tools


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
tutorial:items [2024/10/27 14:07] solidblocktutorial:items [2024/10/27 14:45] (current) – [Item components] solidblock
Line 204: Line 204:
  
 ===== Item components ===== ===== Item components =====
-Sometimes you may need to add some default components for the item, such as max stack size or fire durability. This can be done by calling ''component'' method in ''Item.Settings''. Detailed information about item components can be found in [[https://docs.fabricmc.net/zh_cn/develop/items/custom-data-components|the tutorial in Fabric docs]].+Sometimes you may need to add some default components for the item, such as max stack size or fire durability. This can be done by calling ''component'' method in ''Item.Settings''. Detailed information about item components can be found in [[https://docs.fabricmc.net/develop/items/custom-data-components|the tutorial in Fabric docs]].
  
 In this example, the item will be unbreakable by default, while hiding tooltips about it. In this example, the item will be unbreakable by default, while hiding tooltips about it.
 <yarncode java> <yarncode java>
-    public static final CustomItem CUSTOM_ITEM = new CustomItem(new class_1792.class_1793().component(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(true)));+    // For versions below 1.21.2: 
 +    public static final CustomItem CUSTOM_ITEM = register("custom_item", new CustomItem(new class_1792.class_1793() 
 +        .component(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(true)))); 
 +    // For versions since 1.21.2: 
 +    public static final Item CUSTOM_ITEM = register("custom_item", CustomItem::new, new Item.Settings() 
 +        .component(DataComponentTypes.UNBREAKABLE, new UnbreakableComponent(true)));
 </yarncode> </yarncode>
  
-Specifically, max stack size can be simply set by calling ''maxCount'' method (which is valid also before 1.20.5). Note that if your item is damageable you cannot specify a maximum stack size or the game will throw a RuntimeException.+Specifically, max stack size can be simply set by calling ''maxCount'' method (which is valid also before 1.20.5). Note that if your item is damageable you cannot specify a maximum stack size or the game will throw a ''RuntimeException''. 
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
- 
     // An instance of our new item, where the maximum stack size is 16     // An instance of our new item, where the maximum stack size is 16
-    public static final CustomItem CUSTOM_ITEM = new CustomItem(new class_1792.class_1793().maxCount(16));+     
 +    // For versions below 1.21.2: 
 +    public static final CustomItem CUSTOM_ITEM = register("custom_item", new CustomItem(new class_1792.class_1793().maxCount(16))); 
 +    // For versions since 1.21.2: 
 +    public static final Item CUSTOM_ITEM = register("custom_item", CustomItem::new, new Item.Settings().maxCount(16));
     [...]     [...]
 } }
Line 228: Line 237:
     [...]     [...]
          
-    // For versions below 1.21.3+    // For versions below 1.21.2
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
         [...]         [...]
-        FuelRegistry.INSTANCE.add(TutorialItems.CUSTOM_ITEM, 300)+        FuelRegistry.INSTANCE.add(TutorialItems.CUSTOM_ITEM, 300);
     }     }
 } }
 </code> </code>
  
-In practice, when you have many items to register, as registering quantities of items may be effort-consuming and messy, you can consider placing them in a separate method.+However, in practice, when you have many items to register, as registering quantities of items may be effort-consuming and messy, you can consider placing the codes in a separate method, instead of writing like above.
  
-In versions below 1.21.3, you need to use Fabric API's ''FuelRegistry.INSTANCE''.+In versions below 1.21.2, you need to use Fabric API's ''FuelRegistry.INSTANCE''.
 <code java> <code java>
 public final class TutorialItems { public final class TutorialItems {
     [...]     [...]
          
-    // For versions below 1.21.3+    // For versions below 1.21.2
     public static void registerFuels() {     public static void registerFuels() {
-        FuelRegistry.INSTANCE.add(CUSTOM_ITEM, 300)+        FuelRegistry.INSTANCE.add(CUSTOM_ITEM, 300);
     }     }
 } }
 </code> </code>
  
-In versions since 1.21.3, use Fabric API's ''FuelRegistryEvents'':+In versions since 1.21.2, use Fabric API's ''FuelRegistryEvents'':
 <code java> <code java>
 public final class TutorialItems { public final class TutorialItems {
     [...]     [...]
          
-    // For versions since 1.21.3+    // For versions since 1.21.2
     public static void registerFuels() {     public static void registerFuels() {
         FuelRegistryEvents.BUILD.register((builder, context) -> {         FuelRegistryEvents.BUILD.register((builder, context) -> {
tutorial/items.1730038052.txt.gz · Last modified: 2024/10/27 14:07 by solidblock