Skip to content

Commit

Permalink
Add items to the appropriate and logical vanilla creative tabs as well
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Jul 3, 2023
1 parent 578aef6 commit 04d67f7
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,63 @@
package mekanism.additions.common.registries;

import java.util.Map;
import mekanism.additions.common.AdditionsLang;
import mekanism.additions.common.MekanismAdditions;
import mekanism.api.providers.IBlockProvider;
import mekanism.api.text.EnumColor;
import mekanism.common.registration.impl.CreativeTabDeferredRegister;
import mekanism.common.registration.impl.CreativeTabRegistryObject;
import mekanism.common.registries.MekanismCreativeTabs;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;

public class AdditionsCreativeTabs {

public static final CreativeTabDeferredRegister CREATIVE_TABS = new CreativeTabDeferredRegister(MekanismAdditions.MODID);
public static final CreativeTabDeferredRegister CREATIVE_TABS = new CreativeTabDeferredRegister(MekanismAdditions.MODID, AdditionsCreativeTabs::addToExistingTabs);

public static final CreativeTabRegistryObject ADDITIONS = CREATIVE_TABS.registerMain(AdditionsLang.MEKANISM_ADDITIONS, AdditionsBlocks.OBSIDIAN_TNT, builder ->
builder.withBackgroundLocation(MekanismAdditions.rl("textures/gui/creative_tab.png"))
.withSearchBar(65)//Allow our tabs to be searchable for convenience purposes
.withTabsBefore(MekanismCreativeTabs.MEKANISM.key())
.displayItems((displayParameters, output) -> {
CreativeTabDeferredRegister.addToDisplay(AdditionsItems.ITEMS, output);
CreativeTabDeferredRegister.addToDisplay(AdditionsBlocks.BLOCKS, output);
})
public static final CreativeTabRegistryObject ADDITIONS = CREATIVE_TABS.registerMain(AdditionsLang.MEKANISM_ADDITIONS,
AdditionsItems.BALLOONS.get(EnumColor.BRIGHT_GREEN), builder ->
builder.withBackgroundLocation(MekanismAdditions.rl("textures/gui/creative_tab.png"))
.withSearchBar(65)//Allow our tabs to be searchable for convenience purposes
.withTabsBefore(MekanismCreativeTabs.MEKANISM.key())
.displayItems((displayParameters, output) -> {
CreativeTabDeferredRegister.addToDisplay(AdditionsItems.ITEMS, output);
CreativeTabDeferredRegister.addToDisplay(AdditionsBlocks.BLOCKS, output);
})
);

private static void addToExistingTabs(BuildCreativeModeTabContentsEvent event) {
ResourceKey<CreativeModeTab> tabKey = event.getTabKey();
if (tabKey == CreativeModeTabs.BUILDING_BLOCKS) {
addToDisplay(event, AdditionsBlocks.PLASTIC_BLOCKS, AdditionsBlocks.SLICK_PLASTIC_BLOCKS, AdditionsBlocks.PLASTIC_GLOW_BLOCKS,
AdditionsBlocks.REINFORCED_PLASTIC_BLOCKS, AdditionsBlocks.PLASTIC_ROADS, AdditionsBlocks.TRANSPARENT_PLASTIC_BLOCKS, AdditionsBlocks.PLASTIC_STAIRS,
AdditionsBlocks.PLASTIC_SLABS, AdditionsBlocks.PLASTIC_FENCES, AdditionsBlocks.PLASTIC_FENCE_GATES, AdditionsBlocks.PLASTIC_GLOW_STAIRS,
AdditionsBlocks.PLASTIC_GLOW_SLABS, AdditionsBlocks.TRANSPARENT_PLASTIC_STAIRS, AdditionsBlocks.TRANSPARENT_PLASTIC_SLABS);
} else if (tabKey == CreativeModeTabs.COLORED_BLOCKS) {
addToDisplay(event, AdditionsBlocks.GLOW_PANELS, AdditionsBlocks.PLASTIC_BLOCKS, AdditionsBlocks.SLICK_PLASTIC_BLOCKS, AdditionsBlocks.PLASTIC_GLOW_BLOCKS,
AdditionsBlocks.REINFORCED_PLASTIC_BLOCKS, AdditionsBlocks.PLASTIC_ROADS, AdditionsBlocks.TRANSPARENT_PLASTIC_BLOCKS, AdditionsBlocks.PLASTIC_STAIRS,
AdditionsBlocks.PLASTIC_SLABS, AdditionsBlocks.PLASTIC_FENCES, AdditionsBlocks.PLASTIC_FENCE_GATES, AdditionsBlocks.PLASTIC_GLOW_STAIRS,
AdditionsBlocks.PLASTIC_GLOW_SLABS, AdditionsBlocks.TRANSPARENT_PLASTIC_STAIRS, AdditionsBlocks.TRANSPARENT_PLASTIC_SLABS);
} else if (tabKey == CreativeModeTabs.FUNCTIONAL_BLOCKS) {
addToDisplay(event, AdditionsBlocks.GLOW_PANELS, AdditionsBlocks.PLASTIC_GLOW_BLOCKS, AdditionsBlocks.PLASTIC_GLOW_STAIRS, AdditionsBlocks.PLASTIC_GLOW_SLABS);
} else if (tabKey == CreativeModeTabs.REDSTONE_BLOCKS) {
CreativeTabDeferredRegister.addToDisplay(event, AdditionsBlocks.OBSIDIAN_TNT);
} else if (tabKey == CreativeModeTabs.COMBAT) {
CreativeTabDeferredRegister.addToDisplay(event, AdditionsBlocks.OBSIDIAN_TNT);
} else if (tabKey == CreativeModeTabs.SPAWN_EGGS) {
CreativeTabDeferredRegister.addToDisplay(event, AdditionsItems.BABY_CREEPER_SPAWN_EGG, AdditionsItems.BABY_ENDERMAN_SPAWN_EGG,
AdditionsItems.BABY_SKELETON_SPAWN_EGG, AdditionsItems.BABY_STRAY_SPAWN_EGG, AdditionsItems.BABY_WITHER_SKELETON_SPAWN_EGG);
}
}

@SafeVarargs
private static void addToDisplay(CreativeModeTab.Output output, Map<EnumColor, ? extends IBlockProvider>... blocks) {
for (Map<EnumColor, ? extends IBlockProvider> blockMap : blocks) {
for (IBlockProvider block : blockMap.values()) {
CreativeTabDeferredRegister.addToDisplay(output, block);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package mekanism.generators.common.registries;

import mekanism.api.providers.IBlockProvider;
import mekanism.common.block.attribute.Attribute;
import mekanism.common.block.attribute.Attributes.AttributeComparator;
import mekanism.common.registration.impl.CreativeTabDeferredRegister;
import mekanism.common.registration.impl.CreativeTabRegistryObject;
import mekanism.common.registries.MekanismCreativeTabs;
import mekanism.generators.common.GeneratorsLang;
import mekanism.generators.common.MekanismGenerators;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;

public class GeneratorsCreativeTabs {

public static final CreativeTabDeferredRegister CREATIVE_TABS = new CreativeTabDeferredRegister(MekanismGenerators.MODID);
public static final CreativeTabDeferredRegister CREATIVE_TABS = new CreativeTabDeferredRegister(MekanismGenerators.MODID, GeneratorsCreativeTabs::addToExistingTabs);

public static final CreativeTabRegistryObject GENERATORS = CREATIVE_TABS.registerMain(GeneratorsLang.MEKANISM_GENERATORS, GeneratorsBlocks.HEAT_GENERATOR, builder ->
builder.withBackgroundLocation(MekanismGenerators.rl("textures/gui/creative_tab.png"))
Expand All @@ -20,4 +27,22 @@ public class GeneratorsCreativeTabs {
CreativeTabDeferredRegister.addToDisplay(GeneratorsFluids.FLUIDS, output);
})
);

private static void addToExistingTabs(BuildCreativeModeTabContentsEvent event) {
ResourceKey<CreativeModeTab> tabKey = event.getTabKey();
if (tabKey == CreativeModeTabs.FUNCTIONAL_BLOCKS) {
CreativeTabDeferredRegister.addToDisplay(event, GeneratorsBlocks.HEAT_GENERATOR, GeneratorsBlocks.SOLAR_GENERATOR, GeneratorsBlocks.ADVANCED_SOLAR_GENERATOR,
GeneratorsBlocks.WIND_GENERATOR, GeneratorsBlocks.BIO_GENERATOR, GeneratorsBlocks.GAS_BURNING_GENERATOR);
} else if (tabKey == CreativeModeTabs.REDSTONE_BLOCKS) {
for (IBlockProvider block : GeneratorsBlocks.BLOCKS.getAllBlocks()) {
if (Attribute.has(block.getBlock(), AttributeComparator.class)) {
CreativeTabDeferredRegister.addToDisplay(event, block);
}
}
} else if (tabKey == CreativeModeTabs.TOOLS_AND_UTILITIES) {
CreativeTabDeferredRegister.addToDisplay(GeneratorsFluids.FLUIDS, event);
} else if (tabKey == CreativeModeTabs.INGREDIENTS) {
CreativeTabDeferredRegister.addToDisplay(event, GeneratorsItems.HOHLRAUM, GeneratorsItems.SOLAR_PANEL);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mekanism.common.registration.impl;

import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import mekanism.api.providers.IBlockProvider;
import mekanism.api.providers.IItemProvider;
Expand All @@ -11,15 +12,29 @@
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.eventbus.api.IEventBus;

//TODO - 1.20: Maybe add some things to vanilla tabs/groups? At the very least things like mek additions' spawn eggs to the spawn egg tab
public class CreativeTabDeferredRegister extends WrappedDeferredRegister<CreativeModeTab> {

private final Consumer<BuildCreativeModeTabContentsEvent> addToExistingTabs;
private final String modid;

public CreativeTabDeferredRegister(String modid) {
this(modid, event -> {
});
}

public CreativeTabDeferredRegister(String modid, Consumer<BuildCreativeModeTabContentsEvent> addToExistingTabs) {
super(modid, Registries.CREATIVE_MODE_TAB);
this.modid = modid;
this.addToExistingTabs = addToExistingTabs;
}

@Override
public void register(IEventBus bus) {
super.register(bus);
bus.addListener(addToExistingTabs);
}

/**
Expand All @@ -46,7 +61,13 @@ public CreativeTabRegistryObject register(String name, ILangEntry title, IItemPr
}, CreativeTabRegistryObject::new);
}

private static void addToDisplay(ItemLike itemLike, CreativeModeTab.Output output) {
public static void addToDisplay(CreativeModeTab.Output output, ItemLike... items) {
for (ItemLike item : items) {
addToDisplay(output, item);
}
}

public static void addToDisplay(CreativeModeTab.Output output, ItemLike itemLike) {
if (itemLike.asItem() instanceof ICustomCreativeTabContents contents) {
if (contents.addDefault()) {
output.accept(itemLike);
Expand All @@ -59,26 +80,25 @@ private static void addToDisplay(ItemLike itemLike, CreativeModeTab.Output outpu

public static void addToDisplay(ItemDeferredRegister register, CreativeModeTab.Output output) {
for (IItemProvider itemProvider : register.getAllItems()) {
addToDisplay(itemProvider, output);
addToDisplay(output, itemProvider);
}
}

public static void addToDisplay(BlockDeferredRegister register, CreativeModeTab.Output output) {
for (IBlockProvider itemProvider : register.getAllBlocks()) {
//Don't add bounding blocks to the creative tab
if (!(itemProvider.getBlock() instanceof BlockBounding)) {
addToDisplay(itemProvider, output);
addToDisplay(output, itemProvider);
}
}
}

public static void addToDisplay(FluidDeferredRegister register, CreativeModeTab.Output output) {
for (FluidRegistryObject<?, ?, ?, ?, ?> fluidRO : register.getAllFluids()) {
addToDisplay(fluidRO.getBucket(), output);
addToDisplay(output, fluidRO.getBucket());
}
}

//TODO - 1.20: Re-evaluate this and maybe inline the stuff into their respective creative tabs
public interface ICustomCreativeTabContents {

void addItems(CreativeModeTab.Output tabOutput);
Expand Down
Loading

0 comments on commit 04d67f7

Please sign in to comment.