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 [2019/02/19 14:50] mcrafterzztutorial:itemgroup [2023/10/01 03:21] (current) – Remove semicolon to nowhere haykam
Line 1: Line 1:
-====== Adding an item group ====== +====== Item Groups ======
-Do you want your blocks and items blocks to have their own group in the creative inventory? Then that can easly be fixed. The most common way to create one is to use //FabricItemGroupBuilder.create//. Like items and blocks this has to be done in //onInitialize// method to work correctly.+
  
-   @Override +//This is the 1.20+ version of this tutorialFor the 1.19 version, see [[tutorial:1.19:itemgroup|Creating an itemgroup (1.19)]].//
-   public void onInitialize() { +
-       ItemGroup itemGroup = FabricItemGroupBuilder.create("modid:name")})).icon(() -> new ItemStack   +
-       (block)).build(); +
-   }+
  
-The modid is the id of your modname is the name of the creative tabs (must be lowercase to not crash)The icon is should be an itemstackcreated with either an item or a block like in this case for example //Blocks.Grass//. To add blocks to your item group use //.itemGroup(itemGroup)// when creating it.+So faryou have used ''/give @s tutorial:custom_item'' to obtain your itemTo make obtaining your item easieryou can add it to item groups within the creative inventoryYou can also add your own item group. All items added to any group will also be searchable within the creative inventory.
  
-   public static final Item FABRIC_ITEM new Item(new Item.Settings().itemGroup(itemGroup));+==== Adding to Item Groups ====
  
-Creating an item group is as easy as that!+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. 
 + 
 +Next, you will have to create an event handler for modifying item groups. 
 + 
 +<yarncode java [enable_line_numbers="true"]> 
 +ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { 
 + content.add(CUSTOM_ITEM); 
 +}); 
 +</yarncode> 
 + 
 +The modification event also allows more fine-grained control such as placing your custom item in a specific location(eg. after of ''<yarn field_8691>'') 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: 
 + 
 +<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 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: 
 + 
 +<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> 
 + 
 +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. 
 + 
 +<yarncode java [enable_line_numbers="true"]> 
 +class_2378.method_10230(class_7923.field_44687, new class_2960("tutorial", "test_group"), ITEM_GROUP); 
 +</yarncode> 
 + 
 +{{:tutorial:item_group_append_items.png?nolink&400|}}
tutorial/itemgroup.1550587835.txt.gz · Last modified: 2019/02/19 14:50 by mcrafterzz