From d0cbdaec7d80bc667d21e1a9a8805b075296abef Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 16 Apr 2024 02:13:21 +0900 Subject: [PATCH] Properly display smithing trim recipes --- .../plugin/client/DefaultClientPlugin.java | 5 +- .../displays/DefaultSmithingDisplay.java | 81 +++++++++---------- 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java index 2f902826c..8db9c6837 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java @@ -27,10 +27,7 @@ import dev.architectury.event.EventResult; import dev.architectury.networking.NetworkManager; import dev.architectury.platform.Platform; -import it.unimi.dsi.fastutil.objects.Object2FloatMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; -import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; -import it.unimi.dsi.fastutil.objects.ReferenceSet; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.favorites.FavoriteEntry; import me.shedaniel.rei.api.client.favorites.FavoriteEntryType; @@ -309,7 +306,7 @@ public void registerDisplays(DisplayRegistry registry) { registry.registerRecipeFiller(StonecutterRecipe.class, RecipeType.STONECUTTING, DefaultStoneCuttingDisplay::new); registry.registerRecipeFiller(LegacyUpgradeRecipe.class, RecipeType.SMITHING, DefaultSmithingDisplay::new); registry.registerRecipeFiller(SmithingTransformRecipe.class, RecipeType.SMITHING, DefaultSmithingDisplay::new); - registry.registerRecipeFiller(SmithingTrimRecipe.class, RecipeType.SMITHING, DefaultSmithingDisplay::new); + registry.registerRecipesFiller(SmithingTrimRecipe.class, RecipeType.SMITHING, DefaultSmithingDisplay::from); registry.registerFiller(AnvilRecipe.class, DefaultAnvilDisplay::new); registry.registerFiller(BrewingRecipe.class, DefaultBrewingDisplay::new); registry.registerFiller(TagKey.class, tagKey -> { diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/DefaultSmithingDisplay.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/DefaultSmithingDisplay.java index 67210568b..5e2b2e5fc 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/DefaultSmithingDisplay.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/DefaultSmithingDisplay.java @@ -27,11 +27,11 @@ import me.shedaniel.rei.api.common.display.basic.BasicDisplay; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.util.EntryIngredients; +import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.plugin.common.BuiltinPlugin; import net.minecraft.core.Holder; import net.minecraft.core.RegistryAccess; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.armortrim.*; import net.minecraft.world.item.crafting.LegacyUpgradeRecipe; @@ -40,9 +40,9 @@ import net.minecraft.world.item.crafting.SmithingTrimRecipe; import org.jetbrains.annotations.ApiStatus; +import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.function.Supplier; public class DefaultSmithingDisplay extends BasicDisplay { public DefaultSmithingDisplay(LegacyUpgradeRecipe recipe) { @@ -67,7 +67,7 @@ public DefaultSmithingDisplay(SmithingTransformRecipe recipe) { ); } - @ApiStatus.Experimental + @Deprecated(forRemoval = true) public DefaultSmithingDisplay(SmithingTrimRecipe recipe) { this( recipe, @@ -79,44 +79,43 @@ public DefaultSmithingDisplay(SmithingTrimRecipe recipe) { ); } -// @ApiStatus.Experimental -// public DefaultSmithingDisplay(SmithingTrimRecipe recipe) { -// this( -// List.of( -// EntryIngredients.ofIngredient(recipe.template), -// EntryIngredients.ofIngredient(recipe.base), -// EntryIngredients.ofIngredient(recipe.addition) -// ), -// List.of(EntryIngredients.ofItemStacks(((Supplier>) () -> { -// RegistryAccess registryAccess = BasicDisplay.registryAccess(); -// ItemStack[] templateItems = recipe.template.getItems(); -// ItemStack[] baseItems = recipe.base.getItems(); -// if (templateItems.length != 0) { -// Holder.Reference trimPattern = TrimPatterns.getFromTemplate(registryAccess, templateItems[0]) -// .orElse(null); -// if (trimPattern != null) { -// for (ItemStack additionItem : recipe.addition.getItems()) { -// Holder.Reference trimMaterial = TrimMaterials.getFromIngredient(registryAccess, additionItem) -// .orElse(null); -// if (trimMaterial != null) { -// Optional trim = ArmorTrim.getTrim(registryAccess, itemStack); -// if (trim.isEmpty() || !trim.get().hasPatternAndMaterial(trimPattern, trimMaterial)) { -// ItemStack itemStack2 = itemStack.copy(); -// itemStack2.setCount(1); -// ArmorTrim armorTrim = new ArmorTrim((Holder) optional.get(), (Holder) optional2.get()); -// if (ArmorTrim.setTrim(registryAccess, itemStack2, armorTrim)) { -// return itemStack2; -// } -// } -// } -// } -// } -// } -// return List.of(recipe.getResultItem(registryAccess)); -// }).get())), -// Optional.ofNullable(recipe.getId()) -// ); -// } + @ApiStatus.Experimental + public static List from(SmithingTrimRecipe recipe) { + RegistryAccess registryAccess = BasicDisplay.registryAccess(); + List displays = new ArrayList<>(); + for (ItemStack templateItem : recipe.template.getItems()) { + Holder.Reference trimPattern = TrimPatterns.getFromTemplate(registryAccess, templateItem) + .orElse(null); + if (trimPattern == null) continue; + + for (ItemStack additionStack : recipe.addition.getItems()) { + Holder.Reference trimMaterial = TrimMaterials.getFromIngredient(registryAccess, additionStack) + .orElse(null); + if (trimMaterial == null) continue; + + ArmorTrim armorTrim = new ArmorTrim(trimMaterial, trimPattern); + EntryIngredient.Builder baseItems = EntryIngredient.builder(), outputItems = EntryIngredient.builder(); + for (ItemStack item : recipe.base.getItems()) { + Optional trim = ArmorTrim.getTrim(registryAccess, item); + if (trim.isEmpty() || !trim.get().hasPatternAndMaterial(trimPattern, trimMaterial)) { + ItemStack newItem = item.copy(); + newItem.setCount(1); + if (ArmorTrim.setTrim(registryAccess, newItem, armorTrim)) { + baseItems.add(EntryStacks.of(item.copy())); + outputItems.add(EntryStacks.of(newItem)); + } + } + } + displays.add(new DefaultSmithingDisplay(List.of( + EntryIngredients.of(templateItem), + baseItems.build(), + EntryIngredients.of(additionStack) + ), List.of(outputItems.build()), + Optional.ofNullable(recipe.getId()))); + } + } + return displays; + } public DefaultSmithingDisplay(SmithingRecipe recipe, List inputs) { this(