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
Next revisionBoth sides next revision
tutorial:itemgroup [2020/06/09 23:44] – formatting changes (newline -> same line brackets) draylartutorial:itemgroup [2022/12/11 22:06] – Update to 1.19.3 item group API haykam
Line 1: Line 1:
 ====== Item Groups ====== ====== Item Groups ======
-==== Creating a simple Item Group ==== 
-To have your ''ItemGroup'' properly show up in the creative menu, use the ''FabricItemGroupBuilder'' to create them: 
-<code java [enable_line_numbers="true"]> 
-public class ExampleMod implements ModInitializer { 
  
- public static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.build( +So faryou have used ''/give @s tutorial:custom_item'' to obtain your item. To make obtaining your item easieryou 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.
- new Identifier("tutorial""general"), +
- () -> new ItemStack(Blocks.COBBLESTONE)); +
-  +
- public static final ItemGroup OTHER_GROUP = FabricItemGroupBuilder.create( +
- new Identifier("tutorial", "other")) +
- .icon(() -> new ItemStack(Items.BOWL)) +
- .build(); +
- // ... +
-+
-</code> +
-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 ''Identifier'' 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 ''InvalidIdentifierException'' would be thrown!)) you pass to the ''Identifier'' 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 ''ItemGroup'' would be ''itemGroup.mod_id.general'')) later on.+==== Adding to Item Groups ====
  
-=== Adding your Items to your Item Group === +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 vanilla item groups are stored in the ''<yarn class_7706>'' class. 
-When creating a custom Itemcall ''Item.Settings#group'' on your settings and pass in your custom group: + 
-<code java> +Next, in your ''onInitialize'' method, add the event handler for that item group. 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. 
-public static final Item YOUR_ITEM new Item(new Item.Settings().group(ExampleMod.ITEM_GROUP)); + 
-</code>+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: 
 + 
 +<yarncode java [enable_line_numbers="true"]> 
 +ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { 
 + content.addAfter(class_1802.field_8691, CUSTOM_ITEM); 
 +}); 
 +</yarncode> 
 + 
 +==== Creating an Item Group ==== 
 + 
 +Before you create an item groupdetermine whether it would have enough content to warrant its own groupYour 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: 
 + 
 +<yarncode java [enable_line_numbers="true"]
 +private static final class_1761 ITEM_GROUP FabricItemGroup.builder(new class_2960("tutorial", "test_group")) 
 + .icon(() -> new class_1799(CUSTOM_ITEM)) 
 + .build(); 
 +</yarncode> 
 + 
 +You can use ''ITEM_GROUP'' to modify entries just like a vanilla item groupNote that you must add the items in order since there are no vanilla items to position them relative to: 
 + 
 +<yarncode java [enable_line_numbers="true"]> 
 +ItemGroupEvents.modifyEntriesEvent(ITEM_GROUP).register(content -> { 
 + content.add(CUSTOM_ITEM); 
 +}); 
 +</yarncode>
  
-==== Making an Item Group display specific Items in a particular order ==== 
-Call ''FabricItemGroupBuilder#appendItems'' and pass any ''Consumer<List<ItemStack//>//>''. You can then add whatever stacks you want to the given list in some order. ''ItemStack.EMPTY'' can be used to place empty spaces in your group. 
-<code java [enable_line_numbers="true",highlight_lines_extra="11,12,13,14,15,16,17,18"]> 
-public class ExampleMod implements ModInitializer { 
-  
- public static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.build( 
- new Identifier("tutorial", "general"), 
- () -> new ItemStack(Blocks.COBBLESTONE)); 
-  
- public static final ItemGroup OTHER_GROUP = FabricItemGroupBuilder.create( 
- new Identifier("tutorial", "other")) 
- .icon(() -> new ItemStack(Items.BOWL)) 
- .appendItems(stacks -> { 
- stacks.add(new ItemStack(Blocks.BONE_BLOCK)); 
- stacks.add(new ItemStack(Items.APPLE)); 
- stacks.add(PotionUtil.setPotion(new ItemStack(Items.POTION), Potions.WATER)); 
- stacks.add(ItemStack.EMPTY); 
- stacks.add(new ItemStack(Items.IRON_SHOVEL)); 
- }) 
- .build(); 
- // ... 
-} 
-</code> 
 {{:tutorial:item_group_append_items.png?nolink&400|}} {{:tutorial:item_group_append_items.png?nolink&400|}}
tutorial/itemgroup.txt · Last modified: 2024/06/17 12:49 by solidblock