Skip to content

Commit

Permalink
add creative tab filler
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Feb 28, 2024
1 parent 47ba4ed commit 54b76ec
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,40 @@
import dev.upcraft.sparkweave.api.platform.Services;
import dev.upcraft.sparkweave.api.platform.services.SparkweaveHelperService;
import dev.upcraft.sparkweave.api.registry.RegistryHandler;
import dev.upcraft.sparkweave.api.registry.item.CreativeTabFiller;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.level.ItemLike;

public class CreativeTabHelper {

private static final SparkweaveHelperService HELPER = Services.getService(SparkweaveHelperService.class);
private static final RandomSource RANDOM_SOURCE = RandomSource.create();

public static <T extends ItemLike> void addRegistryEntries(CreativeModeTab.Output collector, RegistryHandler<T> itemProvider) {
itemProvider.stream().forEach(supplier -> collector.accept(supplier.get()));
}
public static <T extends ItemLike> void addRegistryEntries(CreativeModeTab.ItemDisplayParameters displayParameters, CreativeModeTab.Output collector, RegistryHandler<T> itemProvider) {
itemProvider.stream().forEach(supplier -> {

var registryObject = supplier.get();

if (registryObject instanceof CreativeTabFiller filler) {
filler.addItemsToTab(displayParameters, collector, RANDOM_SOURCE);
} else {
collector.accept(registryObject);
}
});
}

public static RandomSource getRandom() {
return RANDOM_SOURCE;
}

/**
* @deprecated Use {@link #addRegistryEntries(CreativeModeTab.ItemDisplayParameters, CreativeModeTab.Output, RegistryHandler)} instead
*/
@Deprecated(since = "0.105.0", forRemoval = true)
public static <T extends ItemLike> void addRegistryEntries(CreativeModeTab.Output collector, RegistryHandler<T> itemProvider) {
itemProvider.stream().forEach(supplier -> collector.accept(supplier.get()));
}

public static CreativeModeTab.Builder newBuilder() {
return HELPER.newCreativeTabBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.upcraft.sparkweave.api.registry.item;

import net.minecraft.util.RandomSource;
import net.minecraft.world.item.CreativeModeTab;

public interface CreativeTabFiller {

void addItemsToTab(CreativeModeTab.ItemDisplayParameters displayParameters, CreativeModeTab.Output collector, RandomSource random);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TestCreativeTabs {
public static final RegistrySupplier<CreativeModeTab> ITEMS = TABS.register("items", () -> CreativeTabHelper.newBuilder()
.title(Component.translatable("itemGroup.sparkweave_testmod.items"))
.icon(() -> TestItems.TEST_ITEM.get().getDefaultInstance())
.displayItems((itemDisplayParameters, output) -> CreativeTabHelper.addRegistryEntries(output, TestItems.ITEMS))
.displayItems((itemDisplayParameters, output) -> CreativeTabHelper.addRegistryEntries(itemDisplayParameters, output, TestItems.ITEMS))
.build()
);
}

0 comments on commit 54b76ec

Please sign in to comment.