Path: blob/1.21.x/src/test/java/net/minecraftforge/debug/creativetabs/CreativeModeTabTest.java
7172 views
/*1* Copyright (c) Forge Development LLC and contributors2* SPDX-License-Identifier: LGPL-2.1-only3*/45package net.minecraftforge.debug.creativetabs;67import java.util.List;8import net.minecraft.core.registries.Registries;9import net.minecraft.network.chat.Component;10import net.minecraft.resources.ResourceKey;11import net.minecraft.resources.ResourceLocation;12import net.minecraft.world.item.CreativeModeTab;13import net.minecraft.world.item.CreativeModeTab.TabVisibility;14import net.minecraft.world.item.DyeColor;15import net.minecraft.world.item.DyeItem;16import net.minecraft.world.item.ItemStack;17import net.minecraft.world.item.Items;18import net.minecraft.world.level.ItemLike;19import net.minecraft.world.level.block.Block;20import net.minecraft.world.level.block.Blocks;21import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;22import net.minecraftforge.fml.common.Mod;23import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;24import net.minecraftforge.registries.RegisterEvent;2526@Mod(CreativeModeTabTest.MOD_ID)27public class CreativeModeTabTest {28public static final String MOD_ID = "creative_mode_tab_test";29private static final boolean ENABLED = true;3031private static final ResourceKey<CreativeModeTab> LOGS = ResourceKey.create(Registries.CREATIVE_MODE_TAB, ResourceLocation.fromNamespaceAndPath(MOD_ID, "logs"));32private static final ResourceKey<CreativeModeTab> STONE = ResourceKey.create(Registries.CREATIVE_MODE_TAB, ResourceLocation.fromNamespaceAndPath(MOD_ID, "stone"));33private static final ResourceKey<CreativeModeTab> COLORS = ResourceKey.create(Registries.CREATIVE_MODE_TAB, ResourceLocation.fromNamespaceAndPath(MOD_ID, "colors"));34private static final ResourceKey<CreativeModeTab> SEARCH = ResourceKey.create(Registries.CREATIVE_MODE_TAB, ResourceLocation.fromNamespaceAndPath(MOD_ID, "search"));3536public CreativeModeTabTest(FMLJavaModLoadingContext context) {37if (!ENABLED)38return;3940var modBus = context.getModBusGroup();4142RegisterEvent.getBus(modBus).addListener(CreativeModeTabTest::onCreativeModeTabRegister);43BuildCreativeModeTabContentsEvent.getBus(modBus).addListener(CreativeModeTabTest::onCreativeModeTabBuildContents);44}4546private static void onCreativeModeTabRegister(RegisterEvent event) {47event.register(Registries.CREATIVE_MODE_TAB, helper -> {48helper.register(LOGS, CreativeModeTab.builder().icon(() -> new ItemStack(Blocks.ACACIA_LOG))49.title(Component.literal("Logs"))50.withLabelColor(0x00FF00)51.displayItems((params, output) -> {52output.accept(new ItemStack(Blocks.ACACIA_LOG));53output.accept(new ItemStack(Blocks.BIRCH_LOG));54output.accept(new ItemStack(Blocks.DARK_OAK_LOG));55output.accept(new ItemStack(Blocks.JUNGLE_LOG));56output.accept(new ItemStack(Blocks.OAK_LOG));57output.accept(new ItemStack(Blocks.SPRUCE_LOG));58})59.build());6061helper.register(STONE, CreativeModeTab.builder()62.icon(() -> new ItemStack(Blocks.STONE))63.title(Component.literal("Stone"))64.withLabelColor(0x0000FF)65.displayItems((params, output) -> {66output.accept(new ItemStack(Blocks.STONE));67output.accept(new ItemStack(Blocks.GRANITE));68output.accept(new ItemStack(Blocks.DIORITE));69output.accept(new ItemStack(Blocks.ANDESITE));70})71.withTabsBefore(LOGS)72.build());7374helper.register(COLORS, CreativeModeTab.builder()75.title(Component.literal("Colors"))76.displayItems((params, output) -> {77for (DyeColor color : DyeColor.values())78output.accept(DyeItem.byColor(color));79})80.withTabFactory(CreativeModeColorTab::new)81.withTabsBefore(STONE)82.build()83);8485helper.register(SEARCH, CreativeModeTab.builder()86.title(Component.literal("Search"))87.icon(() -> new ItemStack(Items.BOOKSHELF))88.displayItems((params, output) -> {89for (DyeColor color : DyeColor.values())90output.accept(DyeItem.byColor(color));91})92.withTabsBefore(COLORS)93.withSearchBar()94.build()95);9697List<Block> blocks = List.of(Blocks.GRANITE, Blocks.DIORITE, Blocks.ANDESITE, Blocks.COBBLESTONE);98for (int i = 0; i < blocks.size(); i++) {99Block block = blocks.get(i);100helper.register(ResourceLocation.fromNamespaceAndPath(MOD_ID, "dummy" + i), CreativeModeTab.builder()101.title(Component.literal("Dummy " + i))102.icon(() -> new ItemStack(block))103.displayItems((params, output) -> output.accept(block))104.build()105);106}107108//final ResourceLocation custom_tabs_image = new ResourceLocation(MOD_ID, "textures/gui/container/creative_inventory/custom_tabs.png");109helper.register(ResourceLocation.fromNamespaceAndPath(MOD_ID, "with_tabs_image"), CreativeModeTab.builder()110.title(Component.translatable("itemGroup.with_tabs_image"))111.icon(() -> new ItemStack(Blocks.BRICKS))112.displayItems((params, output) -> output.accept(Blocks.BRICKS))113//.withTabsImage(custom_tabs_image)114.build());115});116}117118private static ItemStack i(ItemLike item) { return new ItemStack(item); }119120private static void onCreativeModeTabBuildContents(BuildCreativeModeTabContentsEvent event) {121var entries = event.getEntries();122var vis = TabVisibility.PARENT_AND_SEARCH_TABS;123if (event.getTabKey() == LOGS) {124entries.putAfter(i(Blocks.ACACIA_LOG), i(Blocks.STRIPPED_ACACIA_LOG), vis);125entries.putAfter(i(Blocks.BIRCH_LOG), i(Blocks.STRIPPED_BIRCH_LOG), vis);126entries.putAfter(i(Blocks.DARK_OAK_LOG), i(Blocks.STRIPPED_DARK_OAK_LOG), vis);127entries.putAfter(i(Blocks.JUNGLE_LOG), i(Blocks.STRIPPED_JUNGLE_LOG), vis);128entries.putAfter(i(Blocks.OAK_LOG), i(Blocks.STRIPPED_OAK_LOG), vis);129entries.putAfter(i(Blocks.SPRUCE_LOG), i(Blocks.STRIPPED_SPRUCE_LOG), vis);130}131132if (event.getTabKey() == STONE) {133entries.putBefore(i(Blocks.STONE), i(Blocks.SMOOTH_STONE), vis);134entries.putBefore(i(Blocks.GRANITE), i(Blocks.POLISHED_GRANITE), vis);135entries.putBefore(i(Blocks.DIORITE), i(Blocks.POLISHED_DIORITE), vis);136entries.putBefore(i(Blocks.ANDESITE), i(Blocks.POLISHED_ANDESITE), vis);137}138}139140private static class CreativeModeColorTab extends CreativeModeTab {141private final ItemStack[] iconItems;142143public CreativeModeColorTab(CreativeModeTab.Builder builder) {144super(builder);145146DyeColor[] colors = DyeColor.values();147iconItems = new ItemStack[colors.length];148for (int i = 0; i < colors.length; i++)149iconItems[i] = new ItemStack(DyeItem.byColor(colors[i]));150}151152@Override153public ItemStack getIconItem() {154int idx = (int)(System.currentTimeMillis() / 1200) % iconItems.length;155return iconItems[idx];156}157}158}159160161