tutorial:itemgroup
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:itemgroup [2019/09/18 10:32] – .group instead of .itemGroup yanis48 | tutorial:itemgroup [2024/08/23 13:03] (current) – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~REDIRECT> | ||
+ | |||
====== Item Groups ====== | ====== Item Groups ====== | ||
- | ==== Creating | + | |
- | To have your '' | + | //This is the 1.20+ version of this tutorial. For the 1.19 version, see [[tutorial: |
- | <code java [enable_line_numbers=" | + | |
- | public class ExampleMod implements ModInitializer | + | So far, you have used '' |
- | { | + | * adding your item into an existing item group |
- | // ... | + | * create your own item group and add items |
- | public | + | |
- | new Identifier(" | + | All items added to any group will also be searchable within |
- | () -> new ItemStack(Blocks.COBBLESTONE)); | + | |
- | + | ===== Add items into existing item groups ===== | |
- | public static final ItemGroup OTHER_GROUP = FabricItemGroupBuilder.create( | + | |
- | new Identifier(" | + | 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 in the '' |
- | .icon(() -> new ItemStack(Items.BOWL)) | + | |
- | .build(); | + | Next, you will have to create |
- | // ... | + | |
+ | <yarncode | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | | ||
+ | public | ||
+ | | ||
+ | | ||
+ | }); | ||
+ | } | ||
} | } | ||
- | </code> | + | </yarncode> |
- | Once '' | + | |
- | Make sure you replace the arguments | + | The modification event also allows more fine-grained control such as placing your custom item in a specific location(eg. after of '' |
- | === Adding your Items to your Item Group === | + | Items can be positioned relative |
- | When creating | + | |
- | <code java> | + | |
- | public static final Item YOUR_ITEM = new Item(new Item.Settings().group(ExampleMod.ITEM_GROUP)); | + | |
- | </ | + | |
- | ==== Making an Item Group display specific Items in a particular order ==== | + | For example, this event handler will place your mod's item after the oak door in the building blocks item group: |
- | Call '' | + | |
- | <code java [enable_line_numbers=" | + | <yarncode |
- | public class ExampleMod implements ModInitializer | + | public class ExampleMod implements ModInitializer { |
- | { | + | |
- | // ... | + | public |
- | public | + | |
- | new Identifier(" | + | |
- | () -> new ItemStack(Blocks.COBBLESTONE)); | + | }); |
- | + | } | |
- | public static final ItemGroup OTHER_GROUP = FabricItemGroupBuilder.create( | + | |
- | new Identifier(" | + | |
- | .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> | + | </yarncode> |
+ | |||
+ | 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, | ||
+ | |||
+ | If you think that your own item group is needed, you can use the '' | ||
+ | |||
+ | < | ||
+ | public final class TutorialItemGroups { | ||
+ | public static final class_1761 TEST_GROUP = FabricItemGroup.builder() | ||
+ | .icon(() -> new class_1799(TutorialItems.CUSTOM_ITEM)) | ||
+ | .displayName(class_2561.method_43469(" | ||
+ | .entries((context, | ||
+ | entries.add(TutorialItems.CUSTOM_ITEM); | ||
+ | }) | ||
+ | .build(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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. | ||
+ | |||
+ | < | ||
+ | public final class TutorialItemGroups { | ||
+ | // .... | ||
+ | |||
+ | 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.1568802722.txt.gz · Last modified: 2019/09/18 10:32 by yanis48