Skip to content

Commit

Permalink
Add charged variant items to creative tab again
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Jul 20, 2023
1 parent 7023df4 commit b71d2f2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gripe._90.megacells.definition;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;

import appeng.block.AEBaseBlock;
import appeng.block.AEBaseBlockItem;
import appeng.core.definitions.ItemDefinition;
import appeng.items.AEBaseItem;

import gripe._90.megacells.util.Utils;

public class MEGACreativeTab {
public static final CreativeModeTab TAB = Utils.PLATFORM.getCreativeTab(MEGACreativeTab::populateTab);
public static final ResourceLocation ID = Utils.makeId("tab");

private static void populateTab(CreativeModeTab.ItemDisplayParameters itemDisplayParameters,
CreativeModeTab.Output output) {
var itemDefs = new ArrayList<ItemDefinition<?>>();
var blacklist = List.of(MEGAItems.DECOMPRESSION_PATTERN);

itemDefs.addAll(MEGAItems.getItems());
itemDefs.addAll(MEGABlocks.getBlocks());
itemDefs.removeAll(blacklist);

for (var itemDef : itemDefs) {
var item = itemDef.asItem();

// For block items, the block controls the creative tab
if (item instanceof AEBaseBlockItem baseItem && baseItem.getBlock()instanceof AEBaseBlock baseBlock) {
baseBlock.addToMainCreativeTab(output);
} else if (item instanceof AEBaseItem baseItem) {
baseItem.addToMainCreativeTab(output);
} else {
output.accept(itemDef);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import java.util.List;
import java.util.function.Function;

import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;

import appeng.api.stacks.AEKeyType;
Expand Down Expand Up @@ -39,10 +35,6 @@ public static List<ItemDefinition<?>> getItems() {
}

// spotless:off
public static final CreativeModeTab CREATIVE_TAB = Utils.PLATFORM.getCreativeTab();
public static final ResourceLocation CREATIVE_TAB_ID = Utils.makeId("tab");
public static final ResourceKey<CreativeModeTab> CREATIVE_TAB_KEY = ResourceKey.create(Registries.CREATIVE_MODE_TAB, CREATIVE_TAB_ID);

public static final ItemDefinition<MaterialItem> MEGA_ITEM_CELL_HOUSING = item("MEGA Item Cell Housing", "mega_item_cell_housing", MaterialItem::new);
public static final ItemDefinition<MaterialItem> MEGA_FLUID_CELL_HOUSING = item("MEGA Fluid Cell Housing", "mega_fluid_cell_housing", MaterialItem::new);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.world.item.CreativeModeTab;

public interface Platform {
CreativeModeTab getCreativeTab();
CreativeModeTab getCreativeTab(CreativeModeTab.DisplayItemsGenerator display);

boolean isModLoaded(String modId);
}
11 changes: 3 additions & 8 deletions fabric/src/main/java/gripe/_90/megacells/MEGACells.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gripe._90.megacells;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;

Expand All @@ -14,6 +13,7 @@
import gripe._90.megacells.crafting.DecompressionPatternDecoder;
import gripe._90.megacells.definition.MEGABlockEntities;
import gripe._90.megacells.definition.MEGABlocks;
import gripe._90.megacells.definition.MEGACreativeTab;
import gripe._90.megacells.definition.MEGAItems;
import gripe._90.megacells.definition.MEGAParts;
import gripe._90.megacells.init.InitStorageCells;
Expand Down Expand Up @@ -47,8 +47,6 @@ private void initAll() {
}

private void registerAll() {
Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, MEGAItems.CREATIVE_TAB_ID, MEGAItems.CREATIVE_TAB);

for (var block : MEGABlocks.getBlocks()) {
Registry.register(BuiltInRegistries.BLOCK, block.id(), block.block());
Registry.register(BuiltInRegistries.ITEM, block.id(), block.asItem());
Expand All @@ -58,17 +56,14 @@ private void registerAll() {
Registry.register(BuiltInRegistries.ITEM, item.id(), item.asItem());
}

Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, MEGACreativeTab.ID, MEGACreativeTab.TAB);

for (var blockEntity : MEGABlockEntities.getBlockEntityTypes().entrySet()) {
Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, blockEntity.getKey(), blockEntity.getValue());
}

Registry.register(BuiltInRegistries.MENU, AppEng.makeId("mega_pattern_provider"),
MEGAPatternProviderBlock.MENU);

ItemGroupEvents.modifyEntriesEvent(MEGAItems.CREATIVE_TAB_KEY).register(content -> {
MEGAItems.getItems().stream().filter(i -> i != MEGAItems.DECOMPRESSION_PATTERN).forEach(content::accept);
MEGABlocks.getBlocks().forEach(content::accept);
});
}

private void initCompression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

public final class FabricPlatform implements Platform {
@Override
public CreativeModeTab getCreativeTab() {
public CreativeModeTab getCreativeTab(CreativeModeTab.DisplayItemsGenerator display) {
return FabricItemGroup.builder()
.title(MEGATranslations.CreativeTab.text())
.icon(() -> MEGAItems.ITEM_CELL_256M.stack(1))
.displayItems(display)
.build();
}

Expand Down
18 changes: 5 additions & 13 deletions forge/src/main/java/gripe/_90/megacells/forge/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -23,6 +22,7 @@
import gripe._90.megacells.crafting.DecompressionPatternDecoder;
import gripe._90.megacells.definition.MEGABlockEntities;
import gripe._90.megacells.definition.MEGABlocks;
import gripe._90.megacells.definition.MEGACreativeTab;
import gripe._90.megacells.definition.MEGAItems;
import gripe._90.megacells.definition.MEGAParts;
import gripe._90.megacells.init.InitStorageCells;
Expand All @@ -41,7 +41,6 @@ public MEGACells() {

var bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(this::registerAll);
bus.addListener(this::populateTab);
bus.addListener(this::initCells);

initCompression();
Expand All @@ -65,10 +64,6 @@ private void initAll() {
}

private void registerAll(RegisterEvent event) {
if (event.getRegistryKey().equals(Registries.CREATIVE_MODE_TAB)) {
Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, MEGAItems.CREATIVE_TAB_ID, MEGAItems.CREATIVE_TAB);
}

if (event.getRegistryKey().equals(Registries.BLOCK)) {
MEGABlocks.getBlocks().forEach(b -> {
ForgeRegistries.BLOCKS.register(b.id(), b.block());
Expand All @@ -80,6 +75,10 @@ private void registerAll(RegisterEvent event) {
MEGAItems.getItems().forEach(i -> ForgeRegistries.ITEMS.register(i.id(), i.asItem()));
}

if (event.getRegistryKey().equals(Registries.CREATIVE_MODE_TAB)) {
Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, MEGACreativeTab.ID, MEGACreativeTab.TAB);
}

if (event.getRegistryKey().equals(Registries.BLOCK_ENTITY_TYPE)) {
MEGABlockEntities.getBlockEntityTypes().forEach(ForgeRegistries.BLOCK_ENTITY_TYPES::register);
}
Expand All @@ -90,13 +89,6 @@ private void registerAll(RegisterEvent event) {
}
}

private void populateTab(BuildCreativeModeTabContentsEvent event) {
if (event.getTabKey() == MEGAItems.CREATIVE_TAB_KEY) {
MEGAItems.getItems().stream().filter(i -> i != MEGAItems.DECOMPRESSION_PATTERN).forEach(event::accept);
MEGABlocks.getBlocks().forEach(event::accept);
}
}

private void initCells(FMLCommonSetupEvent event) {
event.enqueueWork(InitStorageCells::init);
event.enqueueWork(InitUpgrades::init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

public final class ForgePlatform implements Platform {
@Override
public CreativeModeTab getCreativeTab() {
public CreativeModeTab getCreativeTab(CreativeModeTab.DisplayItemsGenerator display) {
return CreativeModeTab.builder()
.title(MEGATranslations.CreativeTab.text())
.icon(() -> MEGAItems.ITEM_CELL_256M.stack(1))
.displayItems(display)
.build();
}

Expand Down

0 comments on commit b71d2f2

Please sign in to comment.