User Tools

Site Tools


zh_cn:tutorial:itemgroup

This is an old revision of the document!


物品组

创建简单物品组

要使“项目组’‘正确显示在“创造性”菜单中,请使用FabricItemGroupBuilder创建它们:

  1. public class ExampleMod implements ModInitializer
  2. {
  3. // ...
  4. public static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.build(
  5. new Identifier("tutorial", "general"),
  6. () -> new ItemStack(Blocks.COBBLESTONE));
  7.  
  8. public static final ItemGroup OTHER_GROUP = FabricItemGroupBuilder.create(
  9. new Identifier("tutorial", "other"))
  10. .icon(() -> new ItemStack(Items.BOWL))
  11. .build();
  12. // ...
  13. }

调用FabricItemGroupBuilder#build后,您的组将被添加到creative菜单中的项目组列表中。 Make sure you replace the arguments 1) you pass to the Identifier constructor with your actual mod ID and the translation key you want to give your item group for localization 2) later on.

将物品添加到物品组

创建自定义项时,请在设置上调用Item.Settings\group,然后传入自定义组:

public static final Item YOUR_ITEM = new Item(new Item.Settings().group(ExampleMod.ITEM_GROUP));

使物品组按特定顺序显示特定项目

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.

  1. public class ExampleMod implements ModInitializer
  2. {
  3. // ...
  4. public static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.build(
  5. new Identifier("tutorial", "general"),
  6. () -> new ItemStack(Blocks.COBBLESTONE));
  7.  
  8. public static final ItemGroup OTHER_GROUP = FabricItemGroupBuilder.create(
  9. new Identifier("tutorial", "other"))
  10. .icon(() -> new ItemStack(Items.BOWL))
  11. .appendItems(stacks ->
  12. {
  13. stacks.add(new ItemStack(Blocks.BONE_BLOCK));
  14. stacks.add(new ItemStack(Items.APPLE));
  15. stacks.add(PotionUtil.setPotion(new ItemStack(Items.POTION), Potions.WATER));
  16. stacks.add(ItemStack.EMPTY);
  17. stacks.add(new ItemStack(Items.IRON_SHOVEL));
  18. })
  19. .build();
  20. // ...
  21. }

1)
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!
2)
The full translation key for the first example ItemGroup would be itemGroup.mod_id.general
zh_cn/tutorial/itemgroup.1576670093.txt.gz · Last modified: 2019/12/18 11:54 by lightcolour