From 3a6f6fc734603ce2b5540908bb23d87ee8a59913 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 24 Oct 2023 00:26:03 +0800 Subject: Rewrite how creative mode tab items are collected --- .../client/forge/CreativeModeTabCollectorImpl.java | 67 ++++++++++++++++++++++ .../main/resources/META-INF/accesstransformer.cfg | 2 + 2 files changed, 69 insertions(+) create mode 100644 forge/src/main/java/me/shedaniel/rei/impl/client/forge/CreativeModeTabCollectorImpl.java (limited to 'forge') diff --git a/forge/src/main/java/me/shedaniel/rei/impl/client/forge/CreativeModeTabCollectorImpl.java b/forge/src/main/java/me/shedaniel/rei/impl/client/forge/CreativeModeTabCollectorImpl.java new file mode 100644 index 000000000..5cd817c82 --- /dev/null +++ b/forge/src/main/java/me/shedaniel/rei/impl/client/forge/CreativeModeTabCollectorImpl.java @@ -0,0 +1,67 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.client.forge; + +import me.shedaniel.rei.impl.common.InternalLogger; +import net.minecraft.core.RegistryAccess; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.flag.FeatureFlagSet; +import net.minecraft.world.flag.FeatureFlags; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.CreativeModeTabs; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.client.ForgeHooksClient; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +public class CreativeModeTabCollectorImpl { + public static Map> collectTabs() { + Map> map = new LinkedHashMap<>(); + FeatureFlagSet featureFlags = FeatureFlags.REGISTRY.allFlags(); + CreativeModeTab.ItemDisplayParameters parameters = new CreativeModeTab.ItemDisplayParameters(featureFlags, true, RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY)); + + for (CreativeModeTab tab : CreativeModeTabs.allTabs()) { + if (tab.getType() != CreativeModeTab.Type.HOTBAR && tab.getType() != CreativeModeTab.Type.INVENTORY) { + try { + CreativeModeTab.ItemDisplayBuilder builder = new CreativeModeTab.ItemDisplayBuilder(tab, featureFlags); + ResourceKey resourceKey = BuiltInRegistries.CREATIVE_MODE_TAB + .getResourceKey(tab) + .orElseThrow(() -> new IllegalStateException("Unregistered creative tab: " + tab)); + ForgeHooksClient.onCreativeModeTabBuildContents(tab, resourceKey, tab.displayItemsGenerator, parameters, (stack, visibility) -> { + if (visibility == CreativeModeTab.TabVisibility.SEARCH_TAB_ONLY) return; + builder.accept(stack, visibility); + }); + map.put(tab, builder.tabContents); + } catch (Throwable throwable) { + InternalLogger.getInstance().error("Failed to collect creative tab: " + tab, throwable); + } + } + } + + return map; + } +} diff --git a/forge/src/main/resources/META-INF/accesstransformer.cfg b/forge/src/main/resources/META-INF/accesstransformer.cfg index 17f10f568..082c7dea2 100644 --- a/forge/src/main/resources/META-INF/accesstransformer.cfg +++ b/forge/src/main/resources/META-INF/accesstransformer.cfg @@ -46,3 +46,5 @@ public net.minecraft.world.item.crafting.SmithingTrimRecipe f_266053_ # addition public-f net.minecraft.client.gui.font.CodepointMap f_283911_ # empty public-f net.minecraft.client.gui.font.CodepointMap f_283938_ # blockMap public-f net.minecraft.client.gui.font.CodepointMap f_283773_ # blockConstructor +public net.minecraft.world.item.CreativeModeTab f_256824_ # displayItemsGenerator +public net.minecraft.world.item.CreativeModeTab$ItemDisplayBuilder -- cgit