User Tools

Site Tools


tutorial:itemgroup

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:itemgroup [2024/07/03 02:26] solidblocktutorial:itemgroup [2024/08/23 13:03] (current) solidblock
Line 1: Line 1:
 +~~REDIRECT>https://docs.fabricmc.net/develop/items/custom-item-groups~~
 +
 ====== Item Groups ====== ====== Item Groups ======
  
 //This is the 1.20+ version of this tutorial. For the 1.19 version, see [[tutorial:1.19:itemgroup|Creating an itemgroup (1.19)]].// //This is the 1.20+ version of this tutorial. For the 1.19 version, see [[tutorial:1.19:itemgroup|Creating an itemgroup (1.19)]].//
  
-So far, you have used ''/give @s tutorial:custom_item'' to obtain your item. However, unlike most vanilla items, it exists in non of the item group, and you cannot easily obtain it in Creative Mode! There are two ways of achieving this:+So far, you have used ''/give @s tutorial:custom_item'' to obtain your item. However, unlike most vanilla items, it exists in none of the item group, and you cannot easily obtain it in Creative Mode! There are two ways of achieving this:
   * adding your item into an existing item group   * adding your item into an existing item group
   * create your own item group and add items   * create your own item group and add items
Line 16: Line 18:
  
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
-ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { +public class ExampleMod implements ModInitializer { 
- content.add(CUSTOM_ITEM); +    @Override 
-});+    public void onInitialize() { 
 +        ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { 
 +            content.add(TutorialItems.CUSTOM_ITEM); 
 +        }); 
 +    } 
 +}
 </yarncode> </yarncode>
  
Line 28: Line 35:
  
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
-ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { +public class ExampleMod implements ModInitializer { 
- content.addAfter(class_1802.field_8691, CUSTOM_ITEM); +    @Override 
-});+    public void onInitialize() { 
 +        ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { 
 +            content.addAfter(class_1802.field_8691, TutorialItems.CUSTOM_ITEM); 
 +        }); 
 +    } 
 +
 +</yarncode> 
 + 
 +In practice, considering items you register may be in large quantities, it's recommended to place then in a particular method, instead of directly in your ''ModInitializer''. See the following example (if write like this, remember to undo the codes above): 
 + 
 +<yarncode java [enable_line_numbers="true"]> 
 +public final class TutorialItems { 
 +    // [...] 
 +     
 +    public static void registerToVanillaItemGroups() { 
 +        ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { 
 +            content.addAfter(class_1802.field_8691, CUSTOM_ITEM); 
 +        }); 
 +    } 
 +
 +</yarncode> 
 +Then, remember to refer to that method in your ''ModInitializer'': 
 +<yarncode java [enable_line_numbers="true"]> 
 +public class ExampleMod implements ModInitializer { 
 +    @Override 
 +    public void onInitialize() { 
 +        TutorialItems.registerToVanillaItemGroups(); 
 +    } 
 +}
 </yarncode> </yarncode>
  
Line 40: Line 75:
  
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
-private static final class_1761 ITEM_GROUP = FabricItemGroup.builder() +public final class TutorialItemGroups { 
- .icon(() -> new class_1799(CUSTOM_ITEM)) +    public static final class_1761 TEST_GROUP = FabricItemGroup.builder() 
- .displayName(class_2561.method_43469("itemGroup.tutorial.test_group"))+        .icon(() -> new class_1799(TutorialItems.CUSTOM_ITEM)) 
 +        .displayName(class_2561.method_43469("itemGroup.tutorial.test_group"))
         .entries((context, entries) -> {         .entries((context, entries) -> {
- entries.add(CUSTOM_ITEM); +            entries.add(TutorialItems.CUSTOM_ITEM); 
- }) +        }) 
- .build();+        .build(); 
 +}
 </yarncode> </yarncode>
  
Line 56: Line 93:
  
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
-// Since 1.21: +public final class TutorialItemGroups { 
-class_2378.method_10230(class_7923.field_44687, Identifier.of("tutorial", "test_group"), ITEM_GROUP);+    // .... 
 +     
 +    public static void initialize() { 
 +        // Since 1.21: 
 +        class_2378.method_10230(class_7923.field_44687, Identifier.of("tutorial", "test_group"), ITEM_GROUP); 
 +         
 +        // Below 1.21: 
 +        class_2378.method_10230(class_7923.field_44687, new class_2960("tutorial", "test_group"), ITEM_GROUP); 
 +    } 
 +
 +</yarncode>
  
-// Below 1.21+Of course, you can directly register them when assigning the fields
-class_2378.method_10230(class_7923.field_44687, new class_2960("tutorial", "test_group"), ITEM_GROUP);+<yarncode java [enable_line_numbers="true"]> 
 +public final class TutorialItemGroups { 
 +    public static final class_1761 TEST_GROUP = class_2378.method_10230(class_7923.field_44687, new class_2960("tutorial", "test_group"), FabricItemGroup.builder() 
 +        .icon(() -> new class_1799(CUSTOM_ITEM)) 
 +        .displayName(class_2561.method_43469("itemGroup.tutorial.test_group")) 
 +        .entries((context, entries) -> { 
 +            entries.add(TutorialItems.CUSTOM_ITEM); 
 +        }) 
 +        .build()); 
 +     
 +    public static void initialize() { 
 +    } 
 +}
 </yarncode> </yarncode>
 +
 +Remember to statically load the class in your ''ModInitializer'':
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleMod implements ModInitializer {
 +    @Override
 +    public void onInitialize() {
 +        TutorialItemGroups.initialize();
 +    }
 +}
 +</yarncode>
 +
 +:!: The screenshot below is outdated.
  
 {{:tutorial:item_group_append_items.png?nolink&400|}} {{:tutorial:item_group_append_items.png?nolink&400|}}
tutorial/itemgroup.1719973566.txt.gz · Last modified: 2024/07/03 02:26 by solidblock