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 [2019/02/21 13:17] mcrafterzztutorial:itemgroup [2019/06/17 18:56] – small refactoring + modid fixes draylar
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.+==== 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( 
 + 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.
  
-   public static final ItemGroup ITEM_GROUP; +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.
-   @Override +
-   public void onInitialize() { +
-       ITEM_GROUP = FabricItemGroupBuilder.create(new Identifier("modid", "name")})).icon(() -> new ItemStack(block)).build()+
-   }+
  
-The modid is the id of your mod, name 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.+=== Adding your Items to your Item Group === 
 +When creating a custom Itemcall ''Item.Settings#group'' on your settings and pass in your custom group
 +<code java> 
 +public static final Item YOUR_ITEM = new Item(new Item.Settings().itemGroup(ExampleMod.ITEM_GROUP)); 
 +</code>
  
-   public static final Item FABRIC_ITEM = new Item(new Item.Settings().itemGroup(ITEM_GROUP)); +==== 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. 
-Creating an item group is as easy as that!+<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/itemgroup.txt · Last modified: 2023/10/01 03:21 by haykam