Class FabricItemGroupBuilder

java.lang.Object
net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder

public final class FabricItemGroupBuilder extends Object
A builder for ItemGroup. Item groups are used to group items in the creative inventory.

Example of creating an empty group (recommended for modded item-only group):


 MY_GROUP = FabricItemGroupBuilder.build(
        new Identifier("my_mod", "example_group"),
        () -> new ItemStack(MY_ITEM);
 );
 // Use item settings to assign a group. Otherwise, it won't appear on creative inventory
 // search result.
 MY_ITEM = new Item(new Item.Settings().group(MY_GROUP));
 

Example of creating a group with vanilla item stacks:


 ItemGroup myGroup = FabricItemGroupBuilder.create(new Identifier("my_mod", "group_2"))
        .icon(Items.STONE::getDefaultStack)
        .appendItems((stacks) -> {
           stacks.add(new ItemStack(Items.STONE));
           stacks.add(new ItemStack(Items.COBBLESTONE));
        })
        .build();
 

Creative inventory search is implemented as a special item group. Adding items with the builder by default does not add them to the search result; to make the item searchable, set the item group via Item.Settings.group(ItemGroup) as well. If there are multiple searchable item stacks of the item (such as colored variants or ones with different NBT), override Item.appendStacks(ItemGroup, DefaultedList) on your item as well. This should be called by appendItems(BiConsumer) to actually add items to your item group.

  • Method Details

    • create

      public static FabricItemGroupBuilder create(net.minecraft.util.Identifier identifier)
      Creates a new item group builder.
      Parameters:
      identifier - the id of the ItemGroup, to be used as the translation key
      Returns:
      a new builder
    • icon

      public FabricItemGroupBuilder icon(Supplier<net.minecraft.item.ItemStack> stackSupplier)
      Sets an icon for the group. This is displayed on the creative inventory tab.
      Parameters:
      stackSupplier - the supplier that returns the item stack to be used as an icon
      Returns:
      this builder
    • stacksForDisplay

      @Deprecated public FabricItemGroupBuilder stacksForDisplay(Consumer<List<net.minecraft.item.ItemStack>> appender)
      Deprecated.
      This allows for a custom list of items to be displayed in a tab, this enabled tabs to be created with a custom set of items.
      Parameters:
      appender - Add ItemStack's to this list to show in the ItemGroup
      Returns:
      a reference to the FabricItemGroupBuilder
    • appendItems

      public FabricItemGroupBuilder appendItems(Consumer<List<net.minecraft.item.ItemStack>> stacksForDisplay)
      Sets the item stacks of this item group, by having the consumer add them to the passed list. This bypasses Item.appendStacks(net.minecraft.item.ItemGroup, net.minecraft.util.collection.DefaultedList<net.minecraft.item.ItemStack>), and by default, does not append the stack to the search result. To add modded items, consider setting the group via item groups in addition to this, as that adds the item stack to the search result. See the creative inventory searching section for details.

      Calling this multiple times overwrites the previously set stacks.

      Parameters:
      stacksForDisplay - a callback that adds item stacks to the passed list
      Returns:
      this builder
    • appendItems

      public FabricItemGroupBuilder appendItems(BiConsumer<List<net.minecraft.item.ItemStack>,net.minecraft.item.ItemGroup> stacksForDisplay)
      Sets the item stacks of this item group, by having the consumer add them to the passed list. This bypasses Item.appendStacks(net.minecraft.item.ItemGroup, net.minecraft.util.collection.DefaultedList<net.minecraft.item.ItemStack>), and by default, does not append the stack to the search result. To add modded items, consider setting the group via item groups in addition to this, as that adds the item stack to the search result. See the creative inventory searching section for details.

      Compared to the other overload, this one also passes the new ItemGroup. This allows you to call Item.appendStacks(net.minecraft.item.ItemGroup, net.minecraft.util.collection.DefaultedList<net.minecraft.item.ItemStack>) yourself if you want.

      Calling this multiple times overwrites the previously set stacks.

      Parameters:
      stacksForDisplay - a callback that adds item stacks to the passed list
      Returns:
      this builder
    • build

      public static net.minecraft.item.ItemGroup build(net.minecraft.util.Identifier identifier, Supplier<net.minecraft.item.ItemStack> stackSupplier)
      This is a single method that makes creating an empty ItemGroup with an icon one call. Items should be added using item settings. Useful for modded item-only group.
      Parameters:
      identifier - the id of the ItemGroup, to be used as the translation key
      stackSupplier - the supplier that returns the item stack to be used as an icon
      Returns:
      an instance of the built ItemGroup
    • build

      public net.minecraft.item.ItemGroup build()
      Creates an instance of the ItemGroup.
      Returns:
      an instance of the built ItemGroup