User Tools

Site Tools


tutorial:itemgroup
This page has been moved, the new location is 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 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 ItemGroups class.

Next, you will have to create an event handler for modifying item groups.

  1. public class ExampleMod implements ModInitializer {
  2. @Override
  3. public void onInitialize() {
  4. ItemGroupEvents.modifyEntriesEvent(ItemGroups.BUILDING_BLOCKS).register(content -> {
  5. content.add(TutorialItems.CUSTOM_ITEM);
  6. });
  7. }
  8. }

The modification event also allows more fine-grained control such as placing your custom item in a specific location(eg. after of OAK_DOOR) 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:

  1. public class ExampleMod implements ModInitializer {
  2. @Override
  3. public void onInitialize() {
  4. ItemGroupEvents.modifyEntriesEvent(ItemGroups.BUILDING_BLOCKS).register(content -> {
  5. content.addAfter(Items.OAK_DOOR, TutorialItems.CUSTOM_ITEM);
  6. });
  7. }
  8. }

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):

  1. public final class TutorialItems {
  2. // [...]
  3.  
  4. public static void registerToVanillaItemGroups() {
  5. ItemGroupEvents.modifyEntriesEvent(ItemGroups.BUILDING_BLOCKS).register(content -> {
  6. content.addAfter(Items.OAK_DOOR, CUSTOM_ITEM);
  7. });
  8. }
  9. }
Then, remember to refer to that method in your ModInitializer:
  1. public class ExampleMod implements ModInitializer {
  2. @Override
  3. public void onInitialize() {
  4. TutorialItems.registerToVanillaItemGroups();
  5. }
  6. }

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:

  1. public final class TutorialItemGroups {
  2. public static final ItemGroup TEST_GROUP = FabricItemGroup.builder()
  3. .icon(() -> new ItemStack(TutorialItems.CUSTOM_ITEM))
  4. .displayName(Text.translatable("itemGroup.tutorial.test_group"))
  5. .entries((context, entries) -> {
  6. entries.add(TutorialItems.CUSTOM_ITEM);
  7. })
  8. .build();
  9. }

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.

  1. public final class TutorialItemGroups {
  2. // ....
  3.  
  4. public static void initialize() {
  5. // Since 1.21:
  6. Registry.register(Registries.ITEM_GROUP, Identifier.of("tutorial", "test_group"), ITEM_GROUP);
  7.  
  8. // Below 1.21:
  9. Registry.register(Registries.ITEM_GROUP, new Identifier("tutorial", "test_group"), ITEM_GROUP);
  10. }
  11. }

Of course, you can directly register them when assigning the fields:

  1. public final class TutorialItemGroups {
  2. public static final ItemGroup TEST_GROUP = Registry.register(Registries.ITEM_GROUP, new Identifier("tutorial", "test_group"), FabricItemGroup.builder()
  3. .icon(() -> new ItemStack(CUSTOM_ITEM))
  4. .displayName(Text.translatable("itemGroup.tutorial.test_group"))
  5. .entries((context, entries) -> {
  6. entries.add(TutorialItems.CUSTOM_ITEM);
  7. })
  8. .build());
  9.  
  10. public static void initialize() {
  11. }
  12. }

Remember to statically load the class in your ModInitializer:

  1. public class ExampleMod implements ModInitializer {
  2. @Override
  3. public void onInitialize() {
  4. TutorialItemGroups.initialize();
  5. }
  6. }

:!: The screenshot below is outdated.

tutorial/itemgroup.txt · Last modified: 2024/08/23 13:03 by solidblock