Skip to content

Commit

Permalink
Clean up Creative Tab registration
Browse files Browse the repository at this point in the history
There's a choice between buffering up Item objects so that Creative tab filling event can
read it, or registering a new event handler for every single item.  I opted for the latter
as it more clearly attributes that memory usage to Botania.

Note that Forge floating flower rendering still crashes the creative tab so I can't
actually test it quite yet.
  • Loading branch information
williewillus committed Jul 17, 2023
1 parent 37db6ab commit 304d59b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
import net.minecraft.commands.Commands;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
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.server.MinecraftServer;
import net.minecraft.world.item.*;
Expand Down Expand Up @@ -112,23 +110,12 @@
import vazkii.botania.xplat.XplatAbstractions;
import vazkii.patchouli.api.PatchouliAPI;

import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.function.BiConsumer;

import static vazkii.botania.common.lib.ResourceLocationHelper.prefix;

public class FabricCommonInitializer implements ModInitializer {
// TODO 1.19.3 menu texture
private static final ResourceKey<CreativeModeTab> BOTANIA_TAB_KEY = ResourceKey.create(Registries.CREATIVE_MODE_TAB, prefix("botania"));
private static final CreativeModeTab BOTANIA_TAB = Registry.register(
BuiltInRegistries.CREATIVE_MODE_TAB,
BOTANIA_TAB_KEY,
FabricItemGroup.builder()
.title(Component.translatable("itemGroup.botania.botania"))
.icon(() -> new ItemStack(BotaniaItems.lexicon))
.build()
);
private static final Registry<Brew> BREW_REGISTRY = FabricRegistryBuilder.createDefaulted(BotaniaRegistries.BREWS, prefix("fallback")).buildAndRegister();

@Override
Expand Down Expand Up @@ -162,13 +149,13 @@ private void registryInit() {
// Core item/block/BE
BotaniaSounds.init(bind(BuiltInRegistries.SOUND_EVENT));
BotaniaBlocks.registerBlocks(bind(BuiltInRegistries.BLOCK));
BotaniaBlocks.registerItemBlocks(registerItemAndPutInTab);
BotaniaBlocks.registerItemBlocks(boundForItem);
BotaniaFluffBlocks.registerBlocks(bind(BuiltInRegistries.BLOCK));
BotaniaFluffBlocks.registerItemBlocks(registerItemAndPutInTab);
BotaniaFluffBlocks.registerItemBlocks(boundForItem);
BotaniaBlockEntities.registerTiles(bind(BuiltInRegistries.BLOCK_ENTITY_TYPE));
BotaniaItems.registerItems(registerItemAndPutInTab);
BotaniaItems.registerItems(boundForItem);
BotaniaFlowerBlocks.registerBlocks(bind(BuiltInRegistries.BLOCK));
BotaniaFlowerBlocks.registerItemBlocks(registerItemAndPutInTab);
BotaniaFlowerBlocks.registerItemBlocks(boundForItem);
BotaniaFlowerBlocks.registerTEs(bind(BuiltInRegistries.BLOCK_ENTITY_TYPE));
BotaniaBlocks.addDispenserBehaviours();
BotaniaBlocks.addAxeStripping();
Expand Down Expand Up @@ -221,6 +208,21 @@ private void registryInit() {
BotaniaLootModifiers.submitLootConditions(bind(BuiltInRegistries.LOOT_CONDITION_TYPE));
BotaniaLootModifiers.submitLootFunctions(bind(BuiltInRegistries.LOOT_FUNCTION_TYPE));
BotaniaStats.init();
Registry.register(
BuiltInRegistries.CREATIVE_MODE_TAB,
BotaniaRegistries.BOTANIA_TAB_KEY,
FabricItemGroup.builder()
.title(Component.translatable("itemGroup.botania.botania"))
.icon(() -> new ItemStack(BotaniaItems.lexicon))
.backgroundSuffix("botania.png")
.build()
);
ItemGroupEvents.modifyEntriesEvent(BotaniaRegistries.BOTANIA_TAB_KEY)
.register(entries -> {
for (Item item : this.itemsToAddToCreativeTab) {
entries.accept(item);
}
});
}

private void registerEvents() {
Expand Down Expand Up @@ -252,10 +254,12 @@ private static <T> BiConsumer<T, ResourceLocation> bind(Registry<? super T> regi
return (t, id) -> Registry.register(registry, id, t);
}

private static final BiConsumer<Item, ResourceLocation> registerItemAndPutInTab = (item, id) -> {
Registry.register(BuiltInRegistries.ITEM, id, item);
ItemGroupEvents.modifyEntriesEvent(BOTANIA_TAB_KEY).register(entries -> entries.accept(item));
};
private final Set<Item> itemsToAddToCreativeTab = new LinkedHashSet<>();
private final BiConsumer<Item, ResourceLocation> boundForItem =
(t, id) -> {
this.itemsToAddToCreativeTab.add(t);
Registry.register(BuiltInRegistries.ITEM, id, t);
};

private void registerCapabilities() {
FluidStorage.ITEM.registerForItems((stack, context) -> new FullItemFluidStorage(context, Items.BOWL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
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.server.MinecraftServer;
Expand All @@ -22,9 +23,7 @@
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.monster.EnderMan;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.*;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
Expand Down Expand Up @@ -124,8 +123,7 @@
import vazkii.botania.xplat.XplatAbstractions;
import vazkii.patchouli.api.PatchouliAPI;

import java.util.Arrays;
import java.util.Map;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -173,13 +171,13 @@ private void registryInit() {
// Core item/block/BE
bind(Registries.SOUND_EVENT, BotaniaSounds::init);
bind(Registries.BLOCK, BotaniaBlocks::registerBlocks);
bind(Registries.ITEM, BotaniaBlocks::registerItemBlocks);
bindForItems(BotaniaBlocks::registerItemBlocks);
bind(Registries.BLOCK, BotaniaFluffBlocks::registerBlocks);
bind(Registries.ITEM, BotaniaFluffBlocks::registerItemBlocks);
bindForItems(BotaniaFluffBlocks::registerItemBlocks);
bind(Registries.BLOCK_ENTITY_TYPE, BotaniaBlockEntities::registerTiles);
bind(Registries.ITEM, BotaniaItems::registerItems);
bindForItems(BotaniaItems::registerItems);
bind(Registries.BLOCK, BotaniaFlowerBlocks::registerBlocks);
bind(Registries.ITEM, BotaniaFlowerBlocks::registerItemBlocks);
bindForItems(BotaniaFlowerBlocks::registerItemBlocks);
bind(Registries.BLOCK_ENTITY_TYPE, BotaniaFlowerBlocks::registerTEs);

// GUI and Recipe
Expand Down Expand Up @@ -218,6 +216,23 @@ private void registryInit() {
BotaniaStats.init();
}
});
bind(Registries.CREATIVE_MODE_TAB, consumer -> {
consumer.accept(CreativeModeTab.builder()
.title(Component.translatable("itemGroup.botania.botania"))
.icon(() -> new ItemStack(BotaniaItems.lexicon))
.withTabsBefore(CreativeModeTabs.NATURAL_BLOCKS)
.backgroundSuffix("botania.png")
.withSearchBar()
.build(),
BotaniaRegistries.BOTANIA_TAB_KEY.location());
});
modBus.addListener((BuildCreativeModeTabContentsEvent e) -> {
if (e.getTabKey() == BotaniaRegistries.BOTANIA_TAB_KEY) {
for (Item item : this.itemsToAddToCreativeTab) {
e.accept(item);
}
}
});
}

private static <T> void bind(ResourceKey<Registry<T>> registry, Consumer<BiConsumer<T, ResourceLocation>> source) {
Expand All @@ -228,6 +243,19 @@ private static <T> void bind(ResourceKey<Registry<T>> registry, Consumer<BiConsu
});
}

private final Set<Item> itemsToAddToCreativeTab = new LinkedHashSet<>();

private void bindForItems(Consumer<BiConsumer<Item, ResourceLocation>> source) {
FMLJavaModLoadingContext.get().getModEventBus().addListener((RegisterEvent event) -> {
if (event.getRegistryKey().equals(Registries.ITEM)) {
source.accept((t, rl) -> {
itemsToAddToCreativeTab.add(t);
event.register(Registries.ITEM, rl, () -> t);
});
}
});
}

private void registerEvents() {
IEventBus bus = MinecraftForge.EVENT_BUS;
registerBlockLookasides();
Expand Down
8 changes: 8 additions & 0 deletions Xplat/src/main/java/vazkii/botania/api/BotaniaRegistries.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package vazkii.botania.api;

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

import vazkii.botania.api.brew.Brew;

public class BotaniaRegistries {
public static final ResourceKey<Registry<Brew>> BREWS =
ResourceKey.createRegistryKey(new ResourceLocation(BotaniaAPI.MODID, "brews"));

/**
* The ID of Botania's Creative Tab
*/
public static final ResourceKey<CreativeModeTab> BOTANIA_TAB_KEY = ResourceKey.create(Registries.CREATIVE_MODE_TAB,
new ResourceLocation(BotaniaAPI.MODID, "botania"));
}

1 comment on commit 304d59b

@williewillus
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the former.

Please sign in to comment.