Files
MinecraftForge/src/test/java/net/minecraftforge/debug/CreativeModeTabTest.java
T
d8c84e2949 Forge 1.20
- Creative mode tabs are now a registry; the `BuildContents` event was renamed to `BuildCreativeModeTabContentsEvent` and moved it to its own class
- The pack format is now 15 for both resource packs and data packs
- `ScreenUtils` was deprecated in favor of a `GuiGraphics` extension
- Forge and the MDK were updated to Gradle 8 and FG6
- The Forge common config file was removed (it only contained the deprecated old fields for resource caching, which was removed in 1.19.3)
- Registry dummy entries were removed
- `RemappingVertexPipeline` was fixed to forward the `endVertex()` call
- Forge tool tags were removed in favor of vanilla ones

Co-authored-by: ChampionAsh5357 <[email protected]>
Co-authored-by: coehlrich <[email protected]>
Co-authored-by: Dennis C <[email protected]>
Co-authored-by: Matyrobbrt <[email protected]>
2023-06-07 20:55:09 -05:00

137 lines
5.8 KiB
Java

/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTab.TabVisibility;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.DyeItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Blocks;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.RegisterEvent;
@Mod(CreativeModeTabTest.MOD_ID)
public class CreativeModeTabTest
{
public static final String MOD_ID = "creative_mode_tab_test";
private static final boolean ENABLED = true;
private static final ResourceKey<CreativeModeTab> LOGS = ResourceKey.create(Registries.CREATIVE_MODE_TAB, new ResourceLocation(MOD_ID, "logs"));
private static final ResourceKey<CreativeModeTab> STONE = ResourceKey.create(Registries.CREATIVE_MODE_TAB, new ResourceLocation(MOD_ID, "stone"));
public CreativeModeTabTest()
{
if (!ENABLED)
return;
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.addListener(CreativeModeTabTest::onCreativeModeTabRegister);
modEventBus.addListener(CreativeModeTabTest::onCreativeModeTabBuildContents);
}
private static void onCreativeModeTabRegister(RegisterEvent event)
{
event.register(Registries.CREATIVE_MODE_TAB, helper -> {
helper.register(LOGS, CreativeModeTab.builder().icon(() -> new ItemStack(Blocks.ACACIA_LOG))
.title(Component.literal("Logs"))
.withLabelColor(0x00FF00)
.displayItems((params, output) -> {
output.accept(new ItemStack(Blocks.ACACIA_LOG));
output.accept(new ItemStack(Blocks.BIRCH_LOG));
output.accept(new ItemStack(Blocks.DARK_OAK_LOG));
output.accept(new ItemStack(Blocks.JUNGLE_LOG));
output.accept(new ItemStack(Blocks.OAK_LOG));
output.accept(new ItemStack(Blocks.SPRUCE_LOG));
})
.build());
helper.register(STONE, CreativeModeTab.builder().icon(() -> new ItemStack(Blocks.STONE))
.title(Component.literal("Stone"))
.withLabelColor(0x0000FF)
.displayItems((params, output) -> {
output.accept(new ItemStack(Blocks.STONE));
output.accept(new ItemStack(Blocks.GRANITE));
output.accept(new ItemStack(Blocks.DIORITE));
output.accept(new ItemStack(Blocks.ANDESITE));
})
.withTabsAfter(CreativeModeTabs.BUILDING_BLOCKS)
.build());
helper.register(new ResourceLocation(MOD_ID, "colors"), CreativeModeTab.builder().title(Component.literal("Colors"))
.displayItems((params, output) ->
{
for (DyeColor color : DyeColor.values())
{
output.accept(DyeItem.byColor(color));
}
})
.withTabFactory(CreativeModeColorTab::new)
.withTabsBefore(CreativeModeTabs.COLORED_BLOCKS)
.build()
);
});
}
private static ItemStack i(ItemLike item) { return new ItemStack(item); }
private static void onCreativeModeTabBuildContents(BuildCreativeModeTabContentsEvent event)
{
var entries = event.getEntries();
var vis = TabVisibility.PARENT_AND_SEARCH_TABS;
if (event.getTabKey() == LOGS)
{
entries.putAfter(i(Blocks.ACACIA_LOG), i(Blocks.STRIPPED_ACACIA_LOG), vis);
entries.putAfter(i(Blocks.BIRCH_LOG), i(Blocks.STRIPPED_BIRCH_LOG), vis);
entries.putAfter(i(Blocks.DARK_OAK_LOG), i(Blocks.STRIPPED_DARK_OAK_LOG), vis);
entries.putAfter(i(Blocks.JUNGLE_LOG), i(Blocks.STRIPPED_JUNGLE_LOG), vis);
entries.putAfter(i(Blocks.OAK_LOG), i(Blocks.STRIPPED_OAK_LOG), vis);
entries.putAfter(i(Blocks.SPRUCE_LOG), i(Blocks.STRIPPED_SPRUCE_LOG), vis);
}
if (event.getTabKey() == STONE)
{
entries.putBefore(i(Blocks.STONE), i(Blocks.SMOOTH_STONE), vis);
entries.putBefore(i(Blocks.GRANITE), i(Blocks.POLISHED_GRANITE), vis);
entries.putBefore(i(Blocks.DIORITE), i(Blocks.POLISHED_DIORITE), vis);
entries.putBefore(i(Blocks.ANDESITE), i(Blocks.POLISHED_ANDESITE), vis);
}
}
private static class CreativeModeColorTab extends CreativeModeTab
{
private final ItemStack[] iconItems;
public CreativeModeColorTab(CreativeModeTab.Builder builder)
{
super(builder);
DyeColor[] colors = DyeColor.values();
iconItems = new ItemStack[colors.length];
for (int i = 0; i < colors.length; i++)
{
iconItems[i] = new ItemStack(DyeItem.byColor(colors[i]));
}
}
@Override
public ItemStack getIconItem()
{
int idx = (int)(System.currentTimeMillis() / 1200) % iconItems.length;
return iconItems[idx];
}
}
}