zh_cn:tutorial:itemgroup
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| zh_cn:tutorial:itemgroup [2023/08/17 12:17] – [创建简单物品组] wjz_p | zh_cn:tutorial:itemgroup [2024/08/23 13:06] (current) – [创建自定义物品组] solidblock | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== 物品组 ====== | ====== 物品组 ====== | ||
| - | 现在,你可以使用命令 ''/ | + | //这是 1.20+ 版本的教程,对于 1.19 版本,请阅读[[tutorial:1.19: |
| - | ==== 添加物品组 ==== | + | 现在,你可以使用命令 ''/ |
| + | * 将你的物品添加到已存在的物品组 | ||
| + | * 创建你自己的物品组并添加物品 | ||
| + | |||
| + | 添加到任何物品组的物品都可以在创造模式物品栏中搜索到、 | ||
| + | |||
| + | ===== 将物品添加到已存在的物品组 | ||
| 首先,先决定需要将物品添加到哪个物品组。例如,添加到建筑方块物品组。原版物品组存储在 ''< | 首先,先决定需要将物品添加到哪个物品组。例如,添加到建筑方块物品组。原版物品组存储在 ''< | ||
| - | 然后,在你的 '' | + | 然后,需要为修改物品组创建事件处理器。 |
| + | |||
| + | < | ||
| + | public class ExampleMod implements ModInitializer { | ||
| + | @Override | ||
| + | public void onInitialize() { | ||
| + | ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { | ||
| + | content.add(TutorialItems.CUSTOM_ITEM); | ||
| + | }); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 这个 modification event 也能够进行更加精细化的控制,例如将你的自定义物品放在特定的位置(例如,在 ''< | ||
| 物品可以添加到一个相对于原版物品的位置。仔细思考你的模组的用户会期望物品出现在哪里。例如,如果你添加一种新的类型的木头,那么将其添加到已有木头的后面或许是最合理的。 | 物品可以添加到一个相对于原版物品的位置。仔细思考你的模组的用户会期望物品出现在哪里。例如,如果你添加一种新的类型的木头,那么将其添加到已有木头的后面或许是最合理的。 | ||
| Line 14: | Line 33: | ||
| < | < | ||
| - | ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { | + | public class ExampleMod implements ModInitializer { |
| - | content.addAfter(class_1802.field_8691, | + | @Override |
| - | }); | + | public void onInitialize() { |
| + | | ||
| + | content.addAfter(class_1802.field_8691, | ||
| + | }); | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| + | 在实践中,考虑到你注册的物品可能有很多,所以建议放在专门的方法中,而不是直接在你的 '' | ||
| + | < | ||
| + | public final class TutorialItems { | ||
| + | // [...] | ||
| + | | ||
| + | public static void registerToVanillaItemGroups() { | ||
| + | ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { | ||
| + | content.addAfter(class_1802.field_8691, | ||
| + | }); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | 然后,记得在你的 '' | ||
| + | < | ||
| + | public class ExampleMod implements ModInitializer { | ||
| + | @Override | ||
| + | public void onInitialize() { | ||
| + | TutorialItems.registerToVanillaItemGroups(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| - | ==== 创建简单物品组 ==== | + | ===== 创建自定义物品组 |
| 创建物品组之前,先决定是否有足够多的内容以至于需要自己的物品组。你的物品组会放置在单独的标签页中,影响可见性。 | 创建物品组之前,先决定是否有足够多的内容以至于需要自己的物品组。你的物品组会放置在单独的标签页中,影响可见性。 | ||
| Line 26: | Line 71: | ||
| 如果你认为需要自己的物品组,可以使用 '' | 如果你认为需要自己的物品组,可以使用 '' | ||
| < | < | ||
| - | private | + | public final class TutorialItemGroups { |
| - | .icon(() -> new class_1799(CUSTOM_ITEM)) | + | public |
| - | .build(); | + | .icon(() -> new class_1799(TutorialItems.CUSTOM_ITEM)) |
| + | .displayName(class_2561.method_43469("itemGroup.tutorial.test_group" | ||
| + | .entries((context, entries) -> { | ||
| + | entries.add(TutorialItems.CUSTOM_ITEM); | ||
| + | }) | ||
| + | .build(); | ||
| + | } | ||
| </ | </ | ||
| - | 你可以使用 | + | 你可以在 '' |
| + | |||
| + | 必须设置显示名称,否则会导致崩溃。 | ||
| + | |||
| + | 下一部是注册你的物品组。 | ||
| < | < | ||
| - | ItemGroupEvents.modifyEntriesEvent(ITEM_GROUP).register(content -> { | + | public final class TutorialItemGroups { |
| - | content.add(CUSTOM_ITEM); | + | // .... |
| - | }); | + | |
| + | public static void initialize() { | ||
| + | // 自从 1.21: | ||
| + | class_2378.method_10230(class_7923.field_44687, | ||
| + | |||
| + | // 在 1.21 之前: | ||
| + | class_2378.method_10230(class_7923.field_44687, | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| + | |||
| + | 当然,也可以在给字段赋值时,直接注册: | ||
| + | < | ||
| + | public final class TutorialItemGroups { | ||
| + | public static final class_1761 TEST_GROUP = class_2378.method_10230(class_7923.field_44687, | ||
| + | .icon(() -> new class_1799(CUSTOM_ITEM)) | ||
| + | .displayName(class_2561.method_43469(" | ||
| + | .entries((context, | ||
| + | entries.add(TutorialItems.CUSTOM_ITEM); | ||
| + | }) | ||
| + | .build()); | ||
| + | | ||
| + | public static void initialize() { | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 记得在你的 '' | ||
| + | < | ||
| + | public class ExampleMod implements ModInitializer { | ||
| + | @Override | ||
| + | public void onInitialize() { | ||
| + | TutorialItemGroups.initialize(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | :!: 下面的截图已过时。 | ||
| {{: | {{: | ||
zh_cn/tutorial/itemgroup.1692274671.txt.gz · Last modified: 2023/08/17 12:17 by wjz_p