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

Next revision
Previous revision
zh_cn:tutorial:itemgroup [2019/12/18 11:52] – created lightcolourzh_cn:tutorial:itemgroup [2023/11/18 08:09] (current) solidblock
Line 1: Line 1:
-====== Item Groups ====== +====== 物品组 ======
-==== 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.+
  
-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.+//这是 1.20+ 版本的教程,对于 1.19 版本,请阅读[[1.19:itemgroup|创建物品组(1.19)]]。//
  
-=== Adding your Items to your Item Group === +现在,你可以使用命令 ''/give @s tutorial:custom_item'' 来获得你的物品。要让物品更容易获得,你需要将其添加到创造模式物品栏的物品组中。你也可以添加自己的物品组。所有添加到了物品组中的物品都可以在创造模式物品栏中搜索。 
-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().group(ExampleMod.ITEM_GROUP)); + 
-</code>+首先,先决定需要将物品添加到哪个物品组。例如,添加到建筑方块物品组。原版物品组存储在 ''<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_8691CUSTOM_ITEM); 
 +}); 
 +</yarncode> 
 + 
 + 
 +==== 创建简单物品组 ==== 
 + 
 +创建物品组之前,先决定是否有足够多的内容以至于需要自己的物品组。你的物品组会放置在单独的标签页中,影响可见性。 
 + 
 +如果你认为需要自己的物品组,可以使用 ''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"]> 
 +class_2378.method_10230(class_7923.field_44687, new class_2960("tutorial", "test_group"), ITEM_GROUP); 
 +</yarncode>
  
-==== 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. 
-<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.1576669926.txt.gz · Last modified: 2019/12/18 11:52 by lightcolour