User Tools

Site Tools


zh_cn:tutorial:itemgroup

This is an old revision of the document!


物品组

创建简单物品组

要使你的ItemGroup属性显示在创造模式菜单中,请使用FabricItemGroupBuilder创建它们:

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

调用FabricItemGroupBuilder#build后,您的组将被添加到创造模式菜单中的物品组列表中。

请确保将您传递到Identifier构造器的参数1)替换为您的实际模组ID和你需要给予您的物品组用于稍后本地化的翻译键(translation key)2)

将物品添加到物品组

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

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

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

调用FabricItemGroupBuilder#appendItems,并传递任何Consumer<List<ItemStack>>。然后,您可以按顺序将将你想要添加的堆栈添加到特定列表中。ItemStack.EMPTY可用于在组中放置空格。

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

1)
请记住,传递给Identifier构造函数的参数只能包含某些字符。
两个参数(命名空间namespace和路径path)都可以包含小写字母、数字、下划线、点和横杠。[a-z0-9_.-]
第二个参数(path)还可以包含斜杠。[a-z0-9/_.-]
避免使用其他符号,否则将引发InvalidIdentifierException
2)
第一个示例item group的完整翻译键将是ItemGroup.mod_ID.general
zh_cn/tutorial/itemgroup.1605106888.txt.gz · Last modified: 2020/11/11 15:01 by solidblock