Skip to content

Commit

Permalink
fix 1.19.3 mod custom creative tab
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPlacentino committed Jan 1, 2023
1 parent 142b72e commit dbbfefd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
36 changes: 27 additions & 9 deletions src/main/java/com/obsilab/mcsc/MCSC.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.mojang.logging.LogUtils;
import com.obsilab.mcsc.block.ModBlocks;
import com.obsilab.mcsc.block.custom.CrystalIngotBlock;
import com.obsilab.mcsc.event.CreativeTabEvents;
import com.obsilab.mcsc.item.ModItems;
import com.obsilab.mcsc.networking.ModMessages;
import com.obsilab.mcsc.world.feature.ModConfiguredFeatures;
import com.obsilab.mcsc.world.feature.ModPlacedFeatures;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -91,8 +93,12 @@ public MCSC()

// Register the commonSetup method for modloading
modEventBus.addListener(this::commonSetup);
// Register the custom creative tab ("MCSC") event
modEventBus.addListener(CreativeTabEvents::onCreativeTabEvent);
// old: Register the item to a creative tab
// modEventBus.addListener(this::addCreative);

/*
/* see ModBlocks.java and ModItems.java
// Register the Deferred Register to the mod event bus so blocks get registered
BLOCKS.register(modEventBus);
// Register the Deferred Register to the mod event bus so items get registered
Expand All @@ -102,8 +108,6 @@ public MCSC()
// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);

// Register the item to a creative tab
modEventBus.addListener(this::addCreative);

/*
CreativeModeTabs.tabs().add(new CreativeModeTabs("mcsc") {
Expand All @@ -120,10 +124,19 @@ private void commonSetup(final FMLCommonSetupEvent event)
// Some common setup code
LOGGER.info("HELLO FROM COMMON SETUP");
LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));

event.enqueueWork(() -> {
ModMessages.register();
// ModVillagers.registerPOIs(); // to be implemented?
});

}

/* replaced by CreativeTabEvents.java (custom tab)
private void addCreative(CreativeModeTabEvent.BuildContents event) {
Collection<RegistryObject<Item>> ModItemsList = ModItems.ITEMS.getEntries();
Collection<RegistryObject<Block>> ModBlocksList = ModBlocks.BLOCKS.getEntries();
if (event.getTab() == CreativeModeTabs.SEARCH) {
Expand All @@ -144,14 +157,17 @@ private void addCreative(CreativeModeTabEvent.BuildContents event) {
//event.accept(ModItems.EMPTY_WAFER_ITEM.get());
}
/*
if (event.getTab() == CreativeModeTabs.MCSC) {
event.accept(ModItems.EMPTY_WAFER_ITEM.get());
}
*/
//if (event.getTab() == CreativeModeTabs.MCSC) {
// event.accept(ModItems.EMPTY_WAFER_ITEM.get());
//}
//
}
//! BELOW NOT WORKING RN
*/

/*
@SubscribeEvent
public void buildContents(CreativeModeTabEvent.Register event) {
event.registerCreativeModeTab(new ResourceLocation(MOD_ID, "mcsc"), builder ->
Expand All @@ -167,6 +183,8 @@ public void buildContents(CreativeModeTabEvent.Register event) {
);
}
*/

// You can use SubscribeEvent and let the Event Bus discover methods to call
@SubscribeEvent
public void onServerStarting(ServerStartingEvent event)
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/obsilab/mcsc/event/CreativeTabEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.obsilab.mcsc.event;

import com.obsilab.mcsc.MCSC;
import com.obsilab.mcsc.block.ModBlocks;
import com.obsilab.mcsc.block.custom.CrystalIngotBlock;
import com.obsilab.mcsc.item.ModItems;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.event.CreativeModeTabEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.registries.RegistryObject;

import java.util.Collection;

public class CreativeTabEvents {

public static CreativeModeTab MCSC_TAB;

@SubscribeEvent
public static void onCreativeTabEvent(CreativeModeTabEvent.Register event) {
MCSC_TAB = event.registerCreativeModeTab(new ResourceLocation(MCSC.MOD_ID, "mcsc_tab"),
builder -> builder
.icon(() -> new ItemStack(ModItems.ETCHED_WAFER_ITEM.get()))
.title(Component.literal("item_group." + MCSC.MOD_ID + ".tab"))
.displayItems((enabledFlags, populator, hasPermissions) -> {
Collection<RegistryObject<Item>> ModItemsList = ModItems.ITEMS.getEntries();
Collection<RegistryObject<Block>> ModBlocksList = ModBlocks.BLOCKS.getEntries();
for (RegistryObject<Item> mod_item : ModItemsList) {
populator.accept(new ItemStack(mod_item.get()));
}
for (RegistryObject<Block> mod_block : ModBlocksList) {
//check if the block is a crop block
if (mod_block.get() instanceof CrystalIngotBlock) { // if(mod_block.get().getName().equals("crystal_ingot"))
//continue; // remove crop block, prevents crashing
} else {
populator.accept(new ItemStack(mod_block.get()));
}

//event.accept(mod_block.get());
}
})
.build()
);
}

}
2 changes: 1 addition & 1 deletion src/main/resources/assets/mcsc/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
"block.mcsc.borax_ore": "Borax Ore",
"block.mcsc.phosphate_ore": "Phosphate Ore",

"item_group.mcsc": "MCSC"
"item_group.mcsc.tab": "MCSC"
}

0 comments on commit dbbfefd

Please # to comment.