Skip to content

Commit

Permalink
Implement Automatic REI Grouping (#340)
Browse files Browse the repository at this point in the history
* Implement Automatic REI Grouping

* Fix including base block in grouping

---------

Co-authored-by: Adrian O.V <[email protected]>
  • Loading branch information
Brittank88 and CodexAdrian authored Oct 9, 2024
1 parent 591bd90 commit fefb2bc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<ChippedPaletteRegistry<Block>> paletteRegistries = new ArrayList<>();

@Override
@SuppressWarnings("UnstableApiUsage")
public void registerCollapsibleEntries(CollapsibleEntryRegistry registry) {
RecipeManager recipeManager = Objects.requireNonNull(Minecraft.getInstance().level).getRecipeManager();
List<ChippedRecipe> 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<String> 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)
);
}
);
}
}
5 changes: 3 additions & 2 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions neoforge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fefb2bc

Please sign in to comment.