~~REDIRECT>https://docs.fabricmc.net/develop/items/custom-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)]].// 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 * create your own item group and add items All items added to any group will also be searchable within the creative inventory. ===== Add items into existing item groups ===== First, choose the item group that the item should be added to. For this example, that item group will be the building blocks group. The registry keys of vanilla item groups are stored in the '''' class. Next, you will have to create an event handler for modifying item groups. public class ExampleMod implements ModInitializer { @Override public void onInitialize() { ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { content.add(TutorialItems.CUSTOM_ITEM); }); } } The modification event also allows more fine-grained control such as placing your custom item in a specific location(eg. after of '''') or other advanced modification, you can register the event handler for each item group that you would like to modify in your ''onInitialize'' method. Each item group that you would like to modify requires its own event handler, but the same event handler can be used to add multiple items to one item group. Items can be positioned relative to the existing vanilla items. Think carefully about where your mod's users would expect the item to be. For example, if you are adding a new type of wood, placing your item after the existing types of wood would make the most sense. For example, this event handler will place your mod's item after the oak door in the building blocks item group: public class ExampleMod implements ModInitializer { @Override public void onInitialize() { ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { content.addAfter(class_1802.field_8691, TutorialItems.CUSTOM_ITEM); }); } } 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): public final class TutorialItems { // [...] public static void registerToVanillaItemGroups() { ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { content.addAfter(class_1802.field_8691, CUSTOM_ITEM); }); } } Then, remember to refer to that method in your ''ModInitializer'': public class ExampleMod implements ModInitializer { @Override public void onInitialize() { TutorialItems.registerToVanillaItemGroups(); } } ===== Create your own item group ===== Before you create an item group, determine whether it would have enough content to warrant its own group. Your item group will be placed on a separate page of tabs, impacting its discoverability, and users may be confused if the item is not where similar items are in the creative inventory. If you think that your own item group is needed, you can use the ''FabricItemGroup.builder'' method to create a builder for an item group. Make sure to call the ''build'' method as well: public final class TutorialItemGroups { public static final class_1761 TEST_GROUP = FabricItemGroup.builder() .icon(() -> new class_1799(TutorialItems.CUSTOM_ITEM)) .displayName(class_2561.method_43469("itemGroup.tutorial.test_group")) .entries((context, entries) -> { entries.add(TutorialItems.CUSTOM_ITEM); }) .build(); } You can add entries to your item group within the ''entries'' callback method. Note that unlike vanilla item groups, where you can add items relative to existing items, you must add items to your own item group in order since there are no vanilla items to position your items relative to. It is important to set the display name, otherwise it will cause a crash. The next step is to register your item group. public final class TutorialItemGroups { // .... 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); } } Of course, you can directly register them when assigning the fields: 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() { } } Remember to statically load the class in your ''ModInitializer'': public class ExampleMod implements ModInitializer { @Override public void onInitialize() { TutorialItemGroups.initialize(); } } :!: The screenshot below is outdated. {{:tutorial:item_group_append_items.png?nolink&400|}}