From fefb2bc313ca56e37204dbea80055aa228ef8c5e Mon Sep 17 00:00:00 2001 From: Mason Hunt Date: Wed, 9 Oct 2024 14:24:39 +1100 Subject: [PATCH] Implement Automatic REI Grouping (#340) * Implement Automatic REI Grouping * Fix including base block in grouping --------- Co-authored-by: Adrian O.V <83074853+CodexAdrian@users.noreply.github.com> --- .../common/compat/rei/ChippedReiPlugin.java | 68 +++++++++++++++++++ fabric/build.gradle.kts | 5 +- gradle.properties | 2 +- neoforge/build.gradle.kts | 13 ++-- 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/earth/terrarium/chipped/common/compat/rei/ChippedReiPlugin.java b/common/src/main/java/earth/terrarium/chipped/common/compat/rei/ChippedReiPlugin.java index 0d0ade33d..753c04d22 100644 --- a/common/src/main/java/earth/terrarium/chipped/common/compat/rei/ChippedReiPlugin.java +++ b/common/src/main/java/earth/terrarium/chipped/common/compat/rei/ChippedReiPlugin.java @@ -4,13 +4,27 @@ import earth.terrarium.chipped.common.recipes.ChippedRecipe; import earth.terrarium.chipped.common.registry.ModBlocks; import earth.terrarium.chipped.common.registry.ModRecipeTypes; +import earth.terrarium.chipped.common.registry.base.ChippedPaletteRegistry; +import it.unimi.dsi.fastutil.objects.Object2IntArrayMap; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; +import me.shedaniel.rei.api.client.registry.entry.CollapsibleEntryRegistry; import me.shedaniel.rei.api.common.category.CategoryIdentifier; +import me.shedaniel.rei.api.common.util.EntryIngredients; import me.shedaniel.rei.api.common.util.EntryStacks; import net.minecraft.client.Minecraft; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.item.crafting.RecipeManager; +import net.minecraft.world.level.block.Block; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Objects; public class ChippedReiPlugin implements REIClientPlugin { @@ -38,4 +52,58 @@ public void registerDisplays(DisplayRegistry registry) { recipe.value().ingredients().forEach(ingredient -> registry.registerRecipeFiller(ChippedRecipe.class, ModRecipeTypes.WORKBENCH.get(), r -> new WorkbenchDisplay(ingredient)))); } + + private final List> paletteRegistries = new ArrayList<>(); + + @Override + @SuppressWarnings("UnstableApiUsage") + public void registerCollapsibleEntries(CollapsibleEntryRegistry registry) { + RecipeManager recipeManager = Objects.requireNonNull(Minecraft.getInstance().level).getRecipeManager(); + List recipes = recipeManager.getAllRecipesFor(ModRecipeTypes.WORKBENCH.get()).stream() + .map(recipe -> (ChippedRecipe) recipe.value()) + .toList(); + + // Any of the base item keys in here is guaranteed to have at least one palette (left of the pair). + // Any base item may also have further palettes (right of the pair). + Object2IntArrayMap palettes = new Object2IntArrayMap<>(); + + // For each recipe, iterate over the ingredients for their stack arrays (which are the palettes). + // Each ingredient's stack array is a palette, whose first item is the base item. + recipes + .stream() + .map(ChippedRecipe::ingredients) + .flatMap(List::stream) + .map(Ingredient::getItems) + .map(Arrays::asList) + .forEach(ingredientStacks -> { + // Get the base item from the first stack. + Item baseItem = ingredientStacks.getFirst().getItem(); + + // Get the description ID for the base item. + String baseItemDescriptionId = baseItem.getDescriptionId(); + + // Get the current palette index for this base item. + int paletteIndex = palettes.getInt(baseItemDescriptionId); + + // Increment the palette index for the next palette. + palettes.put(baseItemDescriptionId, paletteIndex + 1); + + // Importantly, we don't want the group to include the base item itself. + ingredientStacks.removeFirst(); + + // Register the palette group for this base item. + registry.group( + // Create a new resource location for this palette's REI group. + ResourceLocation.fromNamespaceAndPath(Chipped.MOD_ID, "palettes/" + + BuiltInRegistries.ITEM.getKey(baseItem).getPath() + + "/" + + paletteIndex + ), + // The display name for this palette group is the base item's display name with the palette index appended. + Component.translatable(baseItemDescriptionId).append(" [Palette: " + paletteIndex + "]"), + EntryIngredients.ofItemStacks(ingredientStacks) + ); + } + ); + } } diff --git a/fabric/build.gradle.kts b/fabric/build.gradle.kts index d3e31a748..4e15ce0f1 100644 --- a/fabric/build.gradle.kts +++ b/fabric/build.gradle.kts @@ -13,12 +13,13 @@ dependencies { val fabricLoaderVersion: String by project val fabricApiVersion: String by project val modMenuVersion: String by project - val reiVersion: String by project + // val reiVersion: String by project modImplementation(group = "net.fabricmc", name = "fabric-loader", version = fabricLoaderVersion) modApi(group = "net.fabricmc.fabric-api", name = "fabric-api", version = "$fabricApiVersion+$minecraftVersion") modApi(group = "com.terraformersmc", name = "modmenu", version = modMenuVersion) -// modLocalRuntime(group = "me.shedaniel", name = "RoughlyEnoughItems-fabric", version = reiVersion) + + // modLocalRuntime(group = "me.shedaniel", name = "RoughlyEnoughItems-fabric", version = reiVersion) common(project(":common", configuration = "namedElements")) { isTransitive = false diff --git a/gradle.properties b/gradle.properties index d22f35f4f..a8839b806 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ minecraftVersion=1.21.1 parchmentVersion=2024.07.28 modMenuVersion=11.0.2 -reiVersion=16.0.754 +reiVersion=16.0.777 # jei versions: https://github.com/mezz/JustEnoughItems#1211 jeiVersion=19.19.3.224 diff --git a/neoforge/build.gradle.kts b/neoforge/build.gradle.kts index ec4d415a1..4871b3873 100644 --- a/neoforge/build.gradle.kts +++ b/neoforge/build.gradle.kts @@ -22,13 +22,18 @@ val common: Configuration by configurations.creating { dependencies { val minecraftVersion: String by project val neoforgeVersion: String by project - val jeiVersion: String by project + // val jeiVersion: String by project + // val reiVersion: String by project neoForge(group = "net.neoforged", name = "neoforge", version = neoforgeVersion) -// modLocalRuntime(group = "mezz.jei", name = "jei-$minecraftVersion-neoforge", version = jeiVersion) { -// isTransitive = false -// } + // modLocalRuntime(group = "mezz.jei", name = "jei-$minecraftVersion-neoforge", version = jeiVersion) { + // isTransitive = false + // } + + // modLocalRuntime(group = "me.shedaniel", name = "RoughlyEnoughItems-neoforge", version = reiVersion) { + // isTransitive = false + // } common(project(":common", configuration = "namedElements")) { isTransitive = false