Skip to content

Commit

Permalink
Rewrite how creative mode tab items are collected
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Oct 23, 2023
1 parent 120e6ea commit 3a6f6fc
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry;
import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry;
import me.shedaniel.rei.api.client.registry.transfer.simple.SimpleTransferHandler;
import me.shedaniel.rei.api.common.display.basic.BasicDisplay;
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.entry.EntryStack;
import me.shedaniel.rei.api.common.util.EntryIngredients;
Expand Down Expand Up @@ -105,6 +104,7 @@
import net.minecraft.world.level.material.FluidState;
import org.jetbrains.annotations.ApiStatus;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
Expand All @@ -129,28 +129,19 @@ public void registerInformation(EntryIngredient ingredient, Component name, Unar

@Override
public void registerEntries(EntryRegistry registry) {
if (Minecraft.getInstance().player == null || Minecraft.getInstance().player.connection == null)
return;
Minecraft.getInstance().executeBlocking(() -> {
CreativeModeTabs.tryRebuildTabContents(Minecraft.getInstance().player.connection.enabledFeatures(),
Minecraft.getInstance().options.operatorItemsTab().get() && Minecraft.getInstance().player.canUseGameMasterBlocks(),
BasicDisplay.registryAccess());
});
Multimap<Item, EntryStack<ItemStack>> items = Multimaps.newListMultimap(new Reference2ObjectOpenHashMap<>()
, ArrayList::new);

for (CreativeModeTab tab : CreativeModeTabs.allTabs()) {
if (tab.getType() != CreativeModeTab.Type.HOTBAR && tab.getType() != CreativeModeTab.Type.INVENTORY) {
try {
for (ItemStack stack : tab.getDisplayItems()) {
try {
items.put(stack.getItem(), EntryStacks.of(stack));
} catch (Exception ignore) {
}
for (Map.Entry<CreativeModeTab, Collection<ItemStack>> entry : collectTabs().entrySet()) {
try {
for (ItemStack stack : entry.getValue()) {
try {
items.put(stack.getItem(), EntryStacks.of(stack));
} catch (Exception ignore) {
}
} catch (Exception exception) {
exception.printStackTrace();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}

Expand All @@ -175,6 +166,17 @@ public void registerEntries(EntryRegistry registry) {
}
}

private static Map<CreativeModeTab, Collection<ItemStack>> collectTabs() {
try {
return (Map<CreativeModeTab, Collection<ItemStack>>) Class.forName(Platform.isForge() ? "me.shedaniel.rei.impl.client.forge.CreativeModeTabCollectorImpl"
: "me.shedaniel.rei.impl.client.fabric.CreativeModeTabCollectorImpl")
.getDeclaredMethod("collectTabs")
.invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

@Override
public void registerCategories(CategoryRegistry registry) {
registry.add(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.fabric;

import me.shedaniel.rei.impl.common.InternalLogger;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroupEntries;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
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 java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

public class CreativeModeTabCollectorImpl {
public static Map<CreativeModeTab, Collection<ItemStack>> collectTabs() {
Map<CreativeModeTab, Collection<ItemStack>> 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<CreativeModeTab> resourceKey = BuiltInRegistries.CREATIVE_MODE_TAB
.getResourceKey(tab)
.orElseThrow(() -> new IllegalStateException("Unregistered creative tab: " + tab));
tab.displayItemsGenerator.accept(parameters, builder);
map.put(tab, postFabricEvents(tab, parameters, resourceKey, builder.tabContents));
} catch (Throwable throwable) {
InternalLogger.getInstance().error("Failed to collect creative tab: " + tab, throwable);
}
}
}

return map;
}

@SuppressWarnings("UnstableApiUsage")
private static Collection<ItemStack> postFabricEvents(CreativeModeTab tab, CreativeModeTab.ItemDisplayParameters parameters, ResourceKey<CreativeModeTab> resourceKey, Collection<ItemStack> tabContents) {
try {
// Sorry!
FabricItemGroupEntries entries = new FabricItemGroupEntries(parameters, new LinkedList<>(tabContents), new LinkedList<>());
ItemGroupEvents.modifyEntriesEvent(resourceKey).invoker().modifyEntries(entries);
ItemGroupEvents.MODIFY_ENTRIES_ALL.invoker().modifyEntries(tab, entries);
return entries.getDisplayStacks();
} catch (Throwable throwable) {
InternalLogger.getInstance().error("Failed to collect fabric's creative tab: " + tab, throwable);
return tabContents;
}
}
}
4 changes: 3 additions & 1 deletion fabric/src/main/resources/roughlyenoughitems.accessWidener
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ accessible field net/minecraft/world/item/crafting/SmithingTransformRecipe base
accessible field net/minecraft/world/item/crafting/SmithingTransformRecipe addition Lnet/minecraft/world/item/crafting/Ingredient;
accessible field net/minecraft/world/item/crafting/SmithingTrimRecipe template Lnet/minecraft/world/item/crafting/Ingredient;
accessible field net/minecraft/world/item/crafting/SmithingTrimRecipe base Lnet/minecraft/world/item/crafting/Ingredient;
accessible field net/minecraft/world/item/crafting/SmithingTrimRecipe addition Lnet/minecraft/world/item/crafting/Ingredient;
accessible field net/minecraft/world/item/crafting/SmithingTrimRecipe addition Lnet/minecraft/world/item/crafting/Ingredient;
accessible field net/minecraft/world/item/CreativeModeTab displayItemsGenerator Lnet/minecraft/world/item/CreativeModeTab$DisplayItemsGenerator;
accessible class net/minecraft/world/item/CreativeModeTab$ItemDisplayBuilder
Original file line number Diff line number Diff line change
@@ -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<CreativeModeTab, Collection<ItemStack>> collectTabs() {
Map<CreativeModeTab, Collection<ItemStack>> 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<CreativeModeTab> 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;
}
}
2 changes: 2 additions & 0 deletions forge/src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public EntryRegistryImpl() {

@Override
public void acceptPlugin(REIClientPlugin plugin) {
int size = size();
plugin.registerEntries(this);
InternalLogger.getInstance().trace("Registered %d entries from plugin %s", size() - size, plugin.getPluginProviderName());
}

@Override
Expand Down

0 comments on commit 3a6f6fc

Please sign in to comment.