tutorial:itemgroup
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:itemgroup [2022/12/11 22:06] – Update to 1.19.3 item group API haykam | tutorial:itemgroup [2024/08/23 13:03] (current) – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~REDIRECT> | ||
+ | |||
====== Item Groups ====== | ====== Item Groups ====== | ||
- | So far, you have used '' | + | //This is the 1.20+ version of this tutorial. |
- | ==== Adding | + | So far, you have used ''/ |
+ | * adding your item into an existing item group | ||
+ | * create your own item group and add items | ||
- | First, choose the item group that the item should be added to. For this example, that item group will be the building blocks group. The vanilla item groups are stored in the ''< | + | All items added to any group will also be searchable within |
- | Next, in your '' | + | ===== Add items into existing item groups ===== |
+ | |||
+ | First, choose the item group that the item should be added to. For this example, that item group will be the building blocks group. The registry keys of vanilla item groups are stored | ||
+ | |||
+ | Next, you will have to create an event handler for modifying item groups. | ||
+ | |||
+ | < | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { | ||
+ | content.add(TutorialItems.CUSTOM_ITEM); | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The modification event also allows more fine-grained control such as placing your custom item in a specific location(eg. after of ''< | ||
Items can be positioned relative to the existing vanilla items. Think carefully about where your mod's users would expect the item to be. For example, if you are adding a new type of wood, placing your item after the existing types of wood would make the most sense. | Items can be positioned relative to the existing vanilla items. Think carefully about where your mod's users would expect the item to be. For example, if you are adding a new type of wood, placing your item after the existing types of wood would make the most sense. | ||
Line 14: | Line 35: | ||
< | < | ||
- | 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, | ||
+ | }); | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | ==== Creating an Item Group ==== | + | In practice, considering items you register may be in large quantities, it's recommended to place then in a particular method, instead of directly in your '' |
+ | |||
+ | < | ||
+ | public final class TutorialItems { | ||
+ | // [...] | ||
+ | |||
+ | public static void registerToVanillaItemGroups() { | ||
+ | ItemGroupEvents.modifyEntriesEvent(class_7706.field_40195).register(content -> { | ||
+ | content.addAfter(class_1802.field_8691, | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Then, remember to refer to that method in your '' | ||
+ | < | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | TutorialItems.registerToVanillaItemGroups(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Create your own item group ===== | ||
Before you create an item group, determine whether it would have enough content to warrant its own group. Your item group will be placed on a separate page of tabs, impacting its discoverability, | Before you create an item group, determine whether it would have enough content to warrant its own group. Your item group will be placed on a separate page of tabs, impacting its discoverability, | ||
Line 26: | Line 75: | ||
< | < | ||
- | 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(); | ||
+ | } | ||
</ | </ | ||
- | You can use '' | + | You can add entries to your item group within the '' |
+ | |||
+ | It is important to set the display name, otherwise it will cause a crash. | ||
+ | |||
+ | The next step is to register your item group. | ||
< | < | ||
- | ItemGroupEvents.modifyEntriesEvent(ITEM_GROUP).register(content -> { | + | public final class TutorialItemGroups { |
- | content.add(CUSTOM_ITEM); | + | // .... |
- | }); | + | |
+ | public static void initialize() { | ||
+ | // Since 1.21: | ||
+ | class_2378.method_10230(class_7923.field_44687, | ||
+ | |||
+ | // Below 1.21: | ||
+ | class_2378.method_10230(class_7923.field_44687, | ||
+ | } | ||
+ | } | ||
</ | </ | ||
+ | |||
+ | Of course, you can directly register them when assigning the fields: | ||
+ | < | ||
+ | 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() { | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Remember to statically load the class in your '' | ||
+ | < | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | TutorialItemGroups.initialize(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | :!: The screenshot below is outdated. | ||
{{: | {{: |
tutorial/itemgroup.1670796401.txt.gz · Last modified: 2022/12/11 22:06 by haykam