diff --git a/fabric-item-groups-v0/build.gradle b/fabric-item-groups-v0/build.gradle index b1c9feced..7ee5d22f2 100644 --- a/fabric-item-groups-v0/build.gradle +++ b/fabric-item-groups-v0/build.gradle @@ -1,5 +1,5 @@ archivesBaseName = "fabric-item-groups-v0" -version = getSubprojectVersion(project, "0.2.10") +version = getSubprojectVersion(project, "0.3.0") moduleDependencies(project, [ 'fabric-api-base', diff --git a/fabric-item-groups-v0/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java b/fabric-item-groups-v0/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java index dea868815..b32ff8cd0 100644 --- a/fabric-item-groups-v0/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java +++ b/fabric-item-groups-v0/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java @@ -17,9 +17,11 @@ package net.fabricmc.fabric.api.client.itemgroup; import java.util.List; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Supplier; +import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.util.collection.DefaultedList; @@ -30,7 +32,7 @@ import net.fabricmc.fabric.impl.item.group.ItemGroupExtensions; public final class FabricItemGroupBuilder { private Identifier identifier; private Supplier stackSupplier = () -> ItemStack.EMPTY; - private Consumer> stacksForDisplay; + private BiConsumer, ItemGroup> stacksForDisplay; private FabricItemGroupBuilder(Identifier identifier) { this.identifier = identifier; @@ -70,12 +72,25 @@ public final class FabricItemGroupBuilder { } /** - * This allows for a custom list of items to be displayed in a tab, this enabled tabs to be created with a custom set of items. + * Set the item stacks of this item group, by having the consumer add them to the passed list. + * This bypasses {@link Item#appendStacks}. If you want to append stacks from your items, consider using {@linkplain #appendItems(BiConsumer) the other overload}. * * @param stacksForDisplay Add ItemStack's to this list to show in the ItemGroup * @return a reference to the FabricItemGroupBuilder */ public FabricItemGroupBuilder appendItems(Consumer> stacksForDisplay) { + return appendItems((itemStacks, itemGroup) -> stacksForDisplay.accept(itemStacks)); + } + + /** + * Set the item stacks of this item group, by having the consumer add them to the passed list. + * Compared to the other overload, this one also passes the new ItemGroup. + * This allows you to call {@link Item#appendStacks} yourself if you want. + * + * @param stacksForDisplay Add ItemStack's to this list to show in the ItemGroup, and check if the item is in the ItemGroup + * @return a reference to the FabricItemGroupBuilder + */ + public FabricItemGroupBuilder appendItems(BiConsumer, ItemGroup> stacksForDisplay) { this.stacksForDisplay = stacksForDisplay; return this; } @@ -107,7 +122,7 @@ public final class FabricItemGroupBuilder { @Override public void appendStacks(DefaultedList stacks) { if (stacksForDisplay != null) { - stacksForDisplay.accept(stacks); + stacksForDisplay.accept(stacks, this); return; } diff --git a/fabric-item-groups-v0/src/testmod/java/net/fabricmc/fabric/test/item/group/ItemGroupTest.java b/fabric-item-groups-v0/src/testmod/java/net/fabricmc/fabric/test/item/group/ItemGroupTest.java index a8c6cdbc5..6d04ba705 100644 --- a/fabric-item-groups-v0/src/testmod/java/net/fabricmc/fabric/test/item/group/ItemGroupTest.java +++ b/fabric-item-groups-v0/src/testmod/java/net/fabricmc/fabric/test/item/group/ItemGroupTest.java @@ -16,6 +16,7 @@ package net.fabricmc.fabric.test.item.group; +import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; @@ -26,6 +27,8 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; public class ItemGroupTest implements ModInitializer { + private static Item TEST_ITEM; + //Adds an item group with all items in it private static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.create(new Identifier("fabric-item-groups-v0-testmod", "test_group")) .icon(() -> new ItemStack(Items.DIAMOND)) @@ -35,7 +38,18 @@ public class ItemGroupTest implements ModInitializer { .forEach(stacks::add) ).build(); + private static final ItemGroup ITEM_GROUP_2 = FabricItemGroupBuilder.create(new Identifier("fabric-item-groups-v0-testmod", "test_group_two")) + .icon(() -> new ItemStack(Items.REDSTONE)) + .appendItems((stacks, itemGroup) -> { + for (Item item : Registry.ITEM) { + if (item.getGroup() == ItemGroup.FOOD || item.getGroup() == itemGroup) { + stacks.add(new ItemStack(item)); + } + } + }).build(); + @Override public void onInitialize() { + TEST_ITEM = Registry.register(Registry.ITEM, new Identifier("fabric-item-groups-v0-testmod", "item_test_group"), new Item(new Item.Settings().group(ITEM_GROUP_2))); } }