User Tools

Site Tools


zh_cn: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
Last revisionBoth sides next revision
zh_cn:tutorial:itemgroup [2019/12/18 11:57] lightcolourzh_cn:tutorial:itemgroup [2023/08/17 13:02] – [创建简单物品组] wjz_p
Line 1: Line 1:
 ====== 物品组 ====== ====== 物品组 ======
 +
 +现在,你可以使用命令 ''/give @s tutorial:custom_item'' 来获得你的物品。要让物品更容易获得,你需要将其添加到创造模式物品栏的物品组中。你也可以添加自己的物品组。所有添加到了物品组中的物品都可以在创造模式物品栏中搜索。
 +
 +==== 添加物品组 ====
 +
 +首先,先决定需要将物品添加到哪个物品组。例如,添加到建筑方块物品组。原版物品组存储在 ''<yarn class_7706>'' 类中。
 +
 +然后,在你的 ''onInitialize'' 方法中,为这个物品组添加事件处理器。每个需要修改的物品组都需要一个自己的监听器,但可以使用同一个监听器来将多个物品添加到同一个物品组中。
 +
 +物品可以添加到一个相对于原版物品的位置。仔细思考你的模组的用户会期望物品出现在哪里。例如,如果你添加一种新的类型的木头,那么将其添加到已有木头的后面或许是最合理的。
 +
 +例如,以下这个事件监听器会将你的模组中的物品放在建筑方块物品组中的橡木门后面:
 +
 +<yarncode java [enable_line_numbers="true"]>
 +ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> {
 + content.addAfter(class_1802.field_8691, CUSTOM_ITEM);
 +});
 +</yarncode>
 +
 +
 ==== 创建简单物品组 ==== ==== 创建简单物品组 ====
-要使“项目组’‘正确显示在“创造性”菜单中,请使用''FabricItemGroupBuilder''创建它们: 
-<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> 
-调用''FabricItemGroupBuilder#build''后,您的组将被添加到creative菜单中的项目组列表中。 
  
-请确保替换参数(请记住传递给''Identifier''造函数的参数只能包含某些字符)。[两个参数命名空间和路径)都可包含//lowercase letters////numbers//,//划线//,//periods//,或//dashes//.'[a-z0-9_.-]''\\第二个参数(“路径”)也可以包含//slashes//.'[a-z0-9_.-]''\\避免使用其他符号,否则将引发"InvalidIdentifierException!")您将传递给具有实际mod ID的''Identifier''构造函数,以及稍后要本地化提供项组的转换密钥(第一个示例''item group''的完整转换密钥将是''ItemGroup.mod_ID.general'')。+创建物品组之前先决定是否有足够多的内容以至于需要自己的物品组。你的物品组会放置在单独的标签页中,影响可见性。 
 + 
 +如果你认为需要自己的物品组,可以使用 ''FabricItemGroup.builder'' 方法来创建物品组的建器,并调用 ''build'' 方法来完成:注意,1.19.4及更新的版本中,下面所示的builder方法不需要任何参数,且代码略有区别请参考英文原版https://fabricmc.net/wiki/tutorial:itemgroup) 
 +<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> 
 + 
 +可以使用 ''ITEM_GROUP'' 来修改内容注意必须依次添加,因为没有原版物品可相对位置:
  
-=== 将物品添加到物品组 === +<yarncode java [enable_line_numbers="true"]
-创建自定义项时,请在设置上调用''Item.Settings\group'',然后传入自定义组: +ItemGroupEvents.modifyEntriesEvent(ITEM_GROUP).register(content -> { 
-<code java> + content.add(CUSTOM_ITEM)
-public static final Item YOUR_ITEM = new Item(new Item.Settings().group(ExampleMod.ITEM_GROUP)); +}); 
-</code>+</yarncode>
  
-==== 使物品组按特定顺序显示特定项目 ==== 
-调用''FabricItemGroupBuilder#appendItems'',并传递任何''Consumer<List<ItemStack//>//>''。然后,您可以按顺序将任何堆栈添加到给定列表中ItemStack.EMPTY“”可用于在组中放置空格。 
-<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|}}
zh_cn/tutorial/itemgroup.txt · Last modified: 2023/11/18 08:09 by solidblock