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
zh_cn:tutorial:itemgroup [2019/12/18 11:57] lightcolourzh_cn:tutorial:itemgroup [2023/11/18 08:09] (current) solidblock
Line 1: Line 1:
 ====== 物品组 ====== ====== 物品组 ======
 +
 +//这是 1.20+ 版本的教程,对于 1.19 版本,请阅读[[1.19:itemgroup|创建物品组(1.19)]]。//
 +
 +现在,你可以使用命令 ''/give @s tutorial:custom_item'' 来获得你的物品。要让物品更容易获得,你需要将其添加到创造模式物品栏的物品组中。你也可以添加自己的物品组。所有添加到了物品组中的物品都可以在创造模式物品栏中搜索。
 +
 +==== 添加物品组 ====
 +
 +首先,先决定需要将物品添加到哪个物品组。例如,添加到建筑方块物品组。原版物品组存储在 ''<yarn class_7706>'' 类中。
 +
 +然后,在你的 ''onInitialize'' 方法中,为这个物品组添加事件处理器。每个需要修改的物品组都需要一个自己的监听器,但可以使用同一个监听器来将多个物品添加到同一个物品组中。
 +<yarncode java [enable_line_numbers="true"]>
 +ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> {
 + content.add(CUSTOM_ITEM);
 +});
 +</yarncode>
 +
 +这个 modification event 也能够进行更加精细化的控制,例如将你的自定义物品放在特定的位置(例如,在 ''<yarn field_8691>'' 的后面)或者其他的高级修改。每个你需要修改的物品组都需要自己的事件处理器,当然,同一个事件处理器可以用于给一个物品组添加多个物品。
 +
 +物品可以添加到一个相对于原版物品的位置。仔细思考你的模组的用户会期望物品出现在哪里。例如,如果你添加一种新的类型的木头,那么将其添加到已有木头的后面或许是最合理的。
 +
 +例如,以下这个事件监听器会将你的模组中的物品放在建筑方块物品组中的橡木门后面:
 +
 +<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'' 方法来完成: 
 +<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> 
 + 
 +你可以在 ''entries'' 方法中,将物品添加到你的物品组中。注意,不像原版的物品组,当你添加与已存在的物品相对位置的物品时,必须往你自己的物品组添加物品,因为没原版物品可用来做相对位置。 
 + 
 +必须设置显示名称,否则会导致崩溃。 
 + 
 +部是注册你物品组
  
-=== 将物品添加到物品组 === +<yarncode java [enable_line_numbers="true"]
-创建自定义项时,请在设置上调用''Item.Settings\group'',然后传入自定义组: +class_2378.method_10230(class_7923.field_44687, new class_2960("tutorial", "test_group")ITEM_GROUP); 
-<code java> +</yarncode>
-public static final Item YOUR_ITEM = new Item(new Item.Settings().group(ExampleMod.ITEM_GROUP)); +
-</code>+
  
-==== 使物品组按特定顺序显示特定项目 ==== 
-调用''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.1576670264.txt.gz · Last modified: 2019/12/18 11:57 by lightcolour