/* * Copyright (c) Forge Development LLC and contributors * SPDX-License-Identifier: LGPL-2.1-only */ package net.minecraftforge.client; import java.util.IdentityHashMap; import java.util.Map; import net.minecraft.client.Minecraft; import net.minecraft.client.searchtree.SearchRegistry; import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.CreativeModeTabs; import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.CreativeModeTabRegistry; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; public class CreativeModeTabSearchRegistry { private static final Map> NAME_SEARCH_KEYS = new IdentityHashMap<>(); private static final Map> TAG_SEARCH_KEYS = new IdentityHashMap<>(); public static Map> getNameSearchKeys() { Map> nameSearchKeys = new IdentityHashMap<>(); nameSearchKeys.put(CreativeModeTabs.searchTab(), getNameSearchKey(CreativeModeTabs.searchTab())); for (CreativeModeTab tab : CreativeModeTabRegistry.getSortedCreativeModeTabs()) { SearchRegistry.Key nameSearchKey = getNameSearchKey(tab); if (nameSearchKey != null) nameSearchKeys.put(tab, nameSearchKey); } return nameSearchKeys; } public static Map> getTagSearchKeys() { Map> tagSearchKeys = new IdentityHashMap<>(); tagSearchKeys.put(CreativeModeTabs.searchTab(), getTagSearchKey(CreativeModeTabs.searchTab())); for (CreativeModeTab tab : CreativeModeTabRegistry.getSortedCreativeModeTabs()) { SearchRegistry.Key tagSearchKey = getTagSearchKey(tab); if (tagSearchKey != null) tagSearchKeys.put(tab, tagSearchKey); } return tagSearchKeys; } @Nullable public static SearchRegistry.Key getNameSearchKey(CreativeModeTab tab) { if (tab == CreativeModeTabs.searchTab()) return SearchRegistry.CREATIVE_NAMES; if (!tab.hasSearchBar()) return null; return NAME_SEARCH_KEYS.computeIfAbsent(tab, k -> new SearchRegistry.Key<>()); } @Nullable public static SearchRegistry.Key getTagSearchKey(CreativeModeTab tab) { if (tab == CreativeModeTabs.searchTab()) return SearchRegistry.CREATIVE_TAGS; if (!tab.hasSearchBar()) return null; return TAG_SEARCH_KEYS.computeIfAbsent(tab, k -> new SearchRegistry.Key<>()); } // We do it this way to prevent unwanted classloading @ApiStatus.Internal public static void createSearchTrees() { Minecraft.getInstance().createSearchTrees(); } }