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 [2022/04/13 08:04] – map2fabricyarn daomephstatutorial:itemgroup [2023/10/01 03:21] (current) – Remove semicolon to nowhere haykam
Line 1: Line 1:
 ====== Item Groups ====== ====== Item Groups ======
-==== Creating a simple Item Group ==== + 
-To have your ''<yarn class_1761>'' properly show up in the creative menuuse the ''FabricItemGroupBuilder'' to create them:+//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. To make obtaining your item easier, you can add it to item groups within the creative inventory. You can also add your own item group. All items added to any group will also be searchable within the creative inventory. 
 + 
 +==== Adding to 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 ''<yarn class_7706>'' class. 
 + 
 +Nextyou will have to create an event handler for modifying item groups. 
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
-public class ExampleMod implements ModInitializer {+ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { 
 + content.add(CUSTOM_ITEM); 
 +}); 
 +</yarncode>
  
-    public static final class_1761 ITEM_GROUP = FabricItemGroupBuilder.build( +The modification event also allows more fine-grained control such as placing your custom item in a specific location(egafter of ''<yarn field_8691>''or other advanced modificationyou 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.
-        new class_2960("tutorial", "general"), +
-        () -> new class_1799(class_2246.field_10445));+
  
-    public static final class_1761 OTHER_GROUP = FabricItemGroupBuilder.create( +Items can be positioned relative to the existing vanilla itemsThink carefully about where your mod's users would expect the item to be. For example, if you are adding a new type of woodplacing your item after the existing types of wood would make the most sense. 
-        new class_2960("tutorial", "other")) + 
-        .icon(() -> new class_1799(class_1802.field_8428)+For example, this event handler will place your mod's item after the oak door in the building blocks item group: 
-        .build(); + 
-    // ... +<yarncode java [enable_line_numbers="true"]> 
-}+ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> 
 + content.addAfter(class_1802.field_8691, CUSTOM_ITEM); 
 +});
 </yarncode> </yarncode>
-Once ''FabricItemGroupBuilder#build'' is called, your group will be added to the list of item groups in the creative menu. 
  
-Make sure you replace the arguments ((Remember that the arguments you pass to the ''<yarn class_2960>'' constructor can only contain certain characters.\\ Both arguments (the ''namespace'' & ''path'') can contain //lowercase letters//, //numbers//, //underscores//, //periods//, or //dashes//. ''[a-z0-9_.-]''\\ The second argument (the ''path'') can also include //slashes//. ''[a-z0-9/._-]''\\ Avoid using other symbols, else an ''<yarn class_151>'' would be thrown!)) you pass to the ''<yarn class_2960>'' constructor with your actual mod ID and the translation key you want to give your item group for localization ((The full translation key for the first example ''<yarn class_1761>'' would be ''itemGroup.mod_id.general'')) later on.+==== Creating an Item Group ====
  
-=== Adding your Items to your 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 separate page of tabsimpacting its discoverability, and users may be confused if the item is not where similar items are in the creative inventory. 
-When creating custom Itemcall ''<yarn class_1792.class_1793>#<yarn method_7892>'' on your settings and pass in your custom group: + 
-<yarncode java> +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 static final class_1792 YOUR_ITEM new class_1792(new class_1792.class_1793().method_7892(ExampleMod.ITEM_GROUP));+ 
 +<yarncode java [enable_line_numbers="true"]
 +private static final class_1761 ITEM_GROUP FabricItemGroup.builder(
 + .icon(() -> new class_1799(CUSTOM_ITEM)) 
 + .displayName(class_2561.method_43469("itemGroup.tutorial.test_group")
 +        .entries((context, entries) -> { 
 + entries.add(CUSTOM_ITEM)
 + }) 
 + .build();
 </yarncode> </yarncode>
  
-==== Making an Item Group display specific Items in a particular order ==== +You can add entries to your item group within the ''entries'' callback methodNote 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.
-Call ''FabricItemGroupBuilder#appendItems'' and pass any ''Consumer<List%%<%%<yarn class_1799>//>//>''You can then add whatever stacks you want to the given list in some order. ''<yarn class_1799.field_8037>'' can be used to place empty spaces in your group. +
-<yarncode java [enable_line_numbers="true",highlight_lines_extra="11,12,13,14,15,16,17,18"]> +
-public class ExampleMod implements ModInitializer {+
  
-    public static final class_1761 ITEM_GROUP = FabricItemGroupBuilder.build( +It is important to set the display nameotherwise it will cause a crash.
-        new class_2960("tutorial", "general"), +
-        () -> new class_1799(class_2246.field_10445));+
  
-    public static final class_1761 OTHER_GROUP FabricItemGroupBuilder.create( +The next step is to register your item group. 
-        new class_2960("tutorial", "other")) + 
-        .icon(() -> new class_1799(class_1802.field_8428)) +<yarncode java [enable_line_numbers="true"]> 
-        .appendItems(stacks -> { +class_2378.method_10230(class_7923.field_44687, new class_2960("tutorial", "test_group"), ITEM_GROUP);
-            stacks.add(new class_1799(class_2246.field_10166)); +
-            stacks.add(new class_1799(class_1802.field_8279)); +
-            stacks.add(class_1844.method_8061(new class_1799(class_1802.field_8574), class_1847.field_8991)); +
-            stacks.add(class_1799.field_8037); +
-            stacks.add(new class_1799(class_1802.field_8699)); +
-        }) +
-        .build(); +
-    // ... +
-}+
 </yarncode> </yarncode>
 +
 {{:tutorial:item_group_append_items.png?nolink&400|}} {{:tutorial:item_group_append_items.png?nolink&400|}}
tutorial/itemgroup.1649837059.txt.gz · Last modified: 2022/04/13 08:04 by daomephsta