ItemGroup API: Remove return and call super so all others ItemStack's get appended (#1617)

* Remove return and call super so all others ItemStack's get appended

* Add method to FabricItemGroupBuilder to pass ItemGroup with appendItems and change stacksForDisplay to a BiConsumer

* Update ItemGroupBuilder javadoc and add example to testmod

* Remove leftover aw

* Update fabric-item-groups-v0/src/main/java/net/fabricmc/fabric/api/client/itemgroup/FabricItemGroupBuilder.java

Co-authored-by: liach <7806504+liach@users.noreply.github.com>

* Update fabric-item-groups-v0/src/testmod/java/net/fabricmc/fabric/test/item/group/ItemGroupTest.java

Co-authored-by: Technici4n <13494793+Technici4n@users.noreply.github.com>

* Remove used import in ItemGroupTest

* Clean up

* Fix damn checkstyle

* Change version to 0.3.0

Co-authored-by: liach <7806504+liach@users.noreply.github.com>
Co-authored-by: Technici4n <13494793+Technici4n@users.noreply.github.com>
This commit is contained in:
AlphaMode 2021-10-31 05:48:22 -05:00 committed by modmuss50
parent edf35c6176
commit fe763b7016
3 changed files with 33 additions and 4 deletions

View file

@ -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',

View file

@ -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<ItemStack> stackSupplier = () -> ItemStack.EMPTY;
private Consumer<List<ItemStack>> stacksForDisplay;
private BiConsumer<List<ItemStack>, 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<List<ItemStack>> 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<List<ItemStack>, ItemGroup> stacksForDisplay) {
this.stacksForDisplay = stacksForDisplay;
return this;
}
@ -107,7 +122,7 @@ public final class FabricItemGroupBuilder {
@Override
public void appendStacks(DefaultedList<ItemStack> stacks) {
if (stacksForDisplay != null) {
stacksForDisplay.accept(stacks);
stacksForDisplay.accept(stacks, this);
return;
}

View file

@ -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)));
}
}