diff --git a/CHANGELOG.md b/CHANGELOG.md index 37242ed..713e0c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added + +- Recipe transfer support for the Pattern Grid. + +### Fixed + +- Recipe transfer not prioritizing displayed item. + ## [0.3.2] - 2024-07-26 ### Fixed diff --git a/README.md b/README.md index e9b4d5b..98beac1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Adds support for: - Exclusion zones - Ghost ingredient dragging - Using the R/U keys on Grid slots and filtering slots -- Recipe transfer integration for the Crafting Grid +- Recipe transfer integration for the Crafting Grid and Pattern Grid - Grid / JEI search box synchronization ## Links diff --git a/gradle.properties b/gradle.properties index 4a715b6..f6bb0dd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ refinedarchitectVersion=0.16.9 -refinedstorageVersion=2.0.0-milestone.4.5 +refinedstorageVersion=2.0.0-milestone.4.6 jeiVersion=19.4.0.28 minecraftVersion=1.21 # Gradle diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/CraftingGridRecipeTransferHandler.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/CraftingGridRecipeTransferHandler.java index f97c64c..9dc9e8f 100644 --- a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/CraftingGridRecipeTransferHandler.java +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/CraftingGridRecipeTransferHandler.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import javax.annotation.Nullable; import mezz.jei.api.constants.RecipeTypes; @@ -58,7 +57,7 @@ public IRecipeTransferError transferRecipe(final CraftingGridContainerMenu conta } private void doTransfer(final IRecipeSlotsView recipeSlots, final CraftingGridContainerMenu containerMenu) { - final List> inputs = getInputs(recipeSlots); + final List> inputs = SlotUtil.getItems(recipeSlots, RecipeIngredientRole.INPUT); containerMenu.transferRecipe(inputs); } @@ -81,23 +80,4 @@ private boolean isAvailable(final ResourceList available, final IRecipeSlotView } return false; } - - private List> getInputs(final IRecipeSlotsView recipeSlots) { - return recipeSlots.getSlotViews(RecipeIngredientRole.INPUT).stream().map(slotView -> { - final List stacks = slotView.getItemStacks().collect(Collectors.toList()); - prioritizeDisplayedStack(slotView, stacks); - return stacks.stream().map(ItemResource::ofItemStack).collect(Collectors.toList()); - }).toList(); - } - - private void prioritizeDisplayedStack(final IRecipeSlotView slotView, final List stacks) { - slotView.getDisplayedItemStack().ifPresent(displayed -> { - final int index = stacks.indexOf(displayed); - if (index > 0) { - return; - } - stacks.remove(index); - stacks.add(0, displayed); - }); - } } diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/GhostIngredientHandler.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/GhostIngredientHandler.java index cc5e6aa..dea9af5 100644 --- a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/GhostIngredientHandler.java +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/GhostIngredientHandler.java @@ -1,9 +1,10 @@ package com.refinedmods.refinedstorage.jei.common; import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; -import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; +import com.refinedmods.refinedstorage.common.support.AbstractBaseContainerMenu; import com.refinedmods.refinedstorage.common.support.AbstractBaseScreen; import com.refinedmods.refinedstorage.common.support.containermenu.AbstractResourceContainerMenu; +import com.refinedmods.refinedstorage.common.support.containermenu.FilterSlot; import com.refinedmods.refinedstorage.common.support.containermenu.ResourceSlot; import com.refinedmods.refinedstorage.common.support.packet.c2s.C2SPackets; @@ -14,6 +15,8 @@ import mezz.jei.api.gui.handlers.IGhostIngredientHandler; import mezz.jei.api.ingredients.ITypedIngredient; import net.minecraft.client.renderer.Rect2i; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; @SuppressWarnings("rawtypes") class GhostIngredientHandler implements IGhostIngredientHandler { @@ -21,7 +24,7 @@ class GhostIngredientHandler implements IGhostIngredientHandler List> getTargetsTyped(final AbstractBaseScreen screen, final ITypedIngredient ingredient, final boolean doStart) { - if (screen.getMenu() instanceof AbstractResourceContainerMenu menu) { + if (screen.getMenu() instanceof AbstractBaseContainerMenu menu) { return getTargets(screen, ingredient.getIngredient(), menu); } return Collections.emptyList(); @@ -29,20 +32,44 @@ public List> getTargetsTyped(final AbstractBaseScreen screen, private List> getTargets(final AbstractBaseScreen screen, final I ingredient, - final AbstractResourceContainerMenu menu) { + final AbstractBaseContainerMenu menu) { final List> targets = new ArrayList<>(); - RefinedStorageApi.INSTANCE.getIngredientConverter().convertToResource(ingredient).ifPresent(resource -> { - for (final ResourceSlot slot : menu.getResourceSlots()) { - if (slot.isFilter() && slot.isValid(resource)) { - final Rect2i bounds = getBounds(screen, slot); - targets.add(new TargetImpl<>(bounds, slot.index)); + addResourceTargets(screen, ingredient, menu, targets); + addFilterTargets(screen, ingredient, menu, targets); + return targets; + } + + private void addResourceTargets(final AbstractBaseScreen screen, + final I ingredient, + final AbstractBaseContainerMenu menu, + final List> targets) { + if (menu instanceof AbstractResourceContainerMenu resourceMenu) { + RefinedStorageApi.INSTANCE.getIngredientConverter().convertToResource(ingredient).ifPresent(resource -> { + for (final ResourceSlot slot : resourceMenu.getResourceSlots()) { + if (slot.isActive() && slot.isFilter() && slot.isValid(resource)) { + final Rect2i bounds = getBounds(screen, slot); + targets.add(new TargetImpl<>(bounds, slot.index, true)); + } + } + }); + } + } + + private void addFilterTargets(final AbstractBaseScreen screen, + final I ingredient, + final AbstractBaseContainerMenu menu, + final List> targets) { + if (ingredient instanceof ItemStack stack) { + for (final Slot slot : menu.slots) { + if (slot instanceof FilterSlot filterSlot && filterSlot.isActive() && filterSlot.mayPlace(stack)) { + final Rect2i bounds = getBounds(screen, filterSlot); + targets.add(new TargetImpl<>(bounds, filterSlot.index, false)); } } - }); - return targets; + } } - private Rect2i getBounds(final AbstractBaseScreen screen, final ResourceSlot slot) { + private Rect2i getBounds(final AbstractBaseScreen screen, final Slot slot) { return new Rect2i(screen.getLeftPos() + slot.x, screen.getTopPos() + slot.y, 17, 17); } @@ -54,10 +81,12 @@ public void onComplete() { private static class TargetImpl implements Target { private final Rect2i area; private final int slotIndex; + private final boolean resource; - TargetImpl(final Rect2i area, final int slotIndex) { + TargetImpl(final Rect2i area, final int slotIndex, final boolean resource) { this.area = area; this.slotIndex = slotIndex; + this.resource = resource; } @Override @@ -67,11 +96,13 @@ public Rect2i getArea() { @Override public void accept(final I ingredient) { - RefinedStorageApi.INSTANCE.getIngredientConverter().convertToResource(ingredient).ifPresent(this::accept); - } - - private void accept(final PlatformResourceKey resource) { - C2SPackets.sendResourceFilterSlotChange(resource, slotIndex); + if (resource) { + RefinedStorageApi.INSTANCE.getIngredientConverter().convertToResource(ingredient).ifPresent( + convertedResource -> C2SPackets.sendResourceFilterSlotChange(convertedResource, slotIndex) + ); + } else if (ingredient instanceof ItemStack stack) { + C2SPackets.sendFilterSlotChange(stack, slotIndex); + } } } } diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/GridGuiContainerHandler.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/GridGuiContainerHandler.java index d17ed3c..79d1247 100644 --- a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/GridGuiContainerHandler.java +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/GridGuiContainerHandler.java @@ -30,7 +30,7 @@ public Optional> getClickableIngredientUnderMouse( if (resource == null) { return Optional.empty(); } - final PlatformResourceKey underlyingResource = resource.getUnderlyingResource(); + final PlatformResourceKey underlyingResource = resource.getResourceForRecipeMods(); if (underlyingResource == null) { return Optional.empty(); } diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/JeiRecipeModIngredientConverter.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/JeiRecipeModIngredientConverter.java index 36764e3..c6fc3e3 100644 --- a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/JeiRecipeModIngredientConverter.java +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/JeiRecipeModIngredientConverter.java @@ -1,5 +1,6 @@ package com.refinedmods.refinedstorage.jei.common; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; import com.refinedmods.refinedstorage.common.api.support.resource.RecipeModIngredientConverter; import com.refinedmods.refinedstorage.common.support.resource.FluidResource; @@ -24,6 +25,18 @@ public Optional convertToResource(final Object ingredient) return Optional.empty(); } + @Override + public Optional convertToResourceAmount(final Object ingredient) { + final var fluid = Common.getPlatform().convertJeiIngredientToFluidAmount(ingredient); + if (fluid.isPresent()) { + return fluid; + } + if (ingredient instanceof ItemStack itemStack) { + return Optional.of(new ResourceAmount(ItemResource.ofItemStack(itemStack), itemStack.getCount())); + } + return Optional.empty(); + } + @Override @SuppressWarnings("deprecation") public Optional convertToIngredient(final PlatformResourceKey resource) { diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridCraftingRecipeTransferHandler.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridCraftingRecipeTransferHandler.java new file mode 100644 index 0000000..1bd766e --- /dev/null +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridCraftingRecipeTransferHandler.java @@ -0,0 +1,53 @@ +package com.refinedmods.refinedstorage.jei.common; + +import com.refinedmods.refinedstorage.common.autocrafting.PatternGridContainerMenu; +import com.refinedmods.refinedstorage.common.content.Menus; +import com.refinedmods.refinedstorage.common.support.resource.ItemResource; + +import java.util.List; +import java.util.Optional; +import javax.annotation.Nullable; + +import mezz.jei.api.constants.RecipeTypes; +import mezz.jei.api.gui.ingredient.IRecipeSlotsView; +import mezz.jei.api.recipe.RecipeIngredientRole; +import mezz.jei.api.recipe.RecipeType; +import mezz.jei.api.recipe.transfer.IRecipeTransferError; +import mezz.jei.api.recipe.transfer.IRecipeTransferHandler; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.item.crafting.CraftingRecipe; +import net.minecraft.world.item.crafting.RecipeHolder; + +class PatternGridCraftingRecipeTransferHandler implements + IRecipeTransferHandler> { + @Override + public Class getContainerClass() { + return PatternGridContainerMenu.class; + } + + @Override + public Optional> getMenuType() { + return Optional.of(Menus.INSTANCE.getPatternGrid()); + } + + @Override + public RecipeType> getRecipeType() { + return RecipeTypes.CRAFTING; + } + + @Override + @Nullable + public IRecipeTransferError transferRecipe(final PatternGridContainerMenu containerMenu, + final RecipeHolder recipe, + final IRecipeSlotsView recipeSlots, + final Player player, + final boolean maxTransfer, + final boolean doTransfer) { + if (doTransfer) { + final List> inputs = SlotUtil.getItems(recipeSlots, RecipeIngredientRole.INPUT); + containerMenu.transferCraftingRecipe(inputs); + } + return null; + } +} diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridProcessingRecipeTransferHandler.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridProcessingRecipeTransferHandler.java new file mode 100644 index 0000000..7a959e7 --- /dev/null +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridProcessingRecipeTransferHandler.java @@ -0,0 +1,53 @@ +package com.refinedmods.refinedstorage.jei.common; + +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; +import com.refinedmods.refinedstorage.common.autocrafting.PatternGridContainerMenu; +import com.refinedmods.refinedstorage.common.content.Menus; + +import java.util.List; +import java.util.Optional; +import javax.annotation.Nullable; + +import mezz.jei.api.gui.ingredient.IRecipeSlotsView; +import mezz.jei.api.recipe.RecipeIngredientRole; +import mezz.jei.api.recipe.RecipeType; +import mezz.jei.api.recipe.transfer.IRecipeTransferError; +import mezz.jei.api.recipe.transfer.IRecipeTransferHandler; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.item.crafting.RecipeHolder; + +class PatternGridProcessingRecipeTransferHandler implements + IRecipeTransferHandler> { + @Override + public Class getContainerClass() { + return PatternGridContainerMenu.class; + } + + @Override + public Optional> getMenuType() { + return Optional.of(Menus.INSTANCE.getPatternGrid()); + } + + @Override + @SuppressWarnings({"DataFlowIssue"}) + public RecipeType> getRecipeType() { + return null; // universal transfer handler makes this null safe + } + + @Override + @Nullable + public IRecipeTransferError transferRecipe(final PatternGridContainerMenu containerMenu, + final RecipeHolder recipe, + final IRecipeSlotsView recipeSlots, + final Player player, + final boolean maxTransfer, + final boolean doTransfer) { + if (doTransfer) { + final List> inputs = SlotUtil.getResources(recipeSlots, RecipeIngredientRole.INPUT); + final List> outputs = SlotUtil.getResources(recipeSlots, RecipeIngredientRole.OUTPUT); + containerMenu.transferProcessingRecipe(inputs, outputs); + } + return null; + } +} diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridSmithingTableRecipeTransferHandler.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridSmithingTableRecipeTransferHandler.java new file mode 100644 index 0000000..4ae65eb --- /dev/null +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridSmithingTableRecipeTransferHandler.java @@ -0,0 +1,60 @@ +package com.refinedmods.refinedstorage.jei.common; + +import com.refinedmods.refinedstorage.common.autocrafting.PatternGridContainerMenu; +import com.refinedmods.refinedstorage.common.content.Menus; +import com.refinedmods.refinedstorage.common.support.resource.ItemResource; + +import java.util.List; +import java.util.Optional; +import javax.annotation.Nullable; + +import mezz.jei.api.constants.RecipeTypes; +import mezz.jei.api.gui.ingredient.IRecipeSlotsView; +import mezz.jei.api.recipe.RecipeIngredientRole; +import mezz.jei.api.recipe.RecipeType; +import mezz.jei.api.recipe.transfer.IRecipeTransferError; +import mezz.jei.api.recipe.transfer.IRecipeTransferHandler; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.item.crafting.RecipeHolder; +import net.minecraft.world.item.crafting.SmithingRecipe; + +class PatternGridSmithingTableRecipeTransferHandler implements + IRecipeTransferHandler> { + @Override + public Class getContainerClass() { + return PatternGridContainerMenu.class; + } + + @Override + public Optional> getMenuType() { + return Optional.of(Menus.INSTANCE.getPatternGrid()); + } + + @Override + public RecipeType> getRecipeType() { + return RecipeTypes.SMITHING; + } + + @Override + @Nullable + public IRecipeTransferError transferRecipe(final PatternGridContainerMenu containerMenu, + final RecipeHolder recipe, + final IRecipeSlotsView recipeSlots, + final Player player, + final boolean maxTransfer, + final boolean doTransfer) { + if (doTransfer) { + final List> inputSlots = SlotUtil.getItems(recipeSlots, RecipeIngredientRole.INPUT); + if (inputSlots.size() != 3) { + return null; + } + containerMenu.transferSmithingTableRecipe( + inputSlots.getFirst(), + inputSlots.get(1), + inputSlots.get(2) + ); + } + return null; + } +} diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridStonecutterRecipeTransferHandler.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridStonecutterRecipeTransferHandler.java new file mode 100644 index 0000000..54aea02 --- /dev/null +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/PatternGridStonecutterRecipeTransferHandler.java @@ -0,0 +1,61 @@ +package com.refinedmods.refinedstorage.jei.common; + +import com.refinedmods.refinedstorage.common.autocrafting.PatternGridContainerMenu; +import com.refinedmods.refinedstorage.common.content.Menus; + +import java.util.Optional; +import javax.annotation.Nullable; + +import mezz.jei.api.constants.RecipeTypes; +import mezz.jei.api.gui.ingredient.IRecipeSlotsView; +import mezz.jei.api.recipe.RecipeIngredientRole; +import mezz.jei.api.recipe.RecipeType; +import mezz.jei.api.recipe.transfer.IRecipeTransferError; +import mezz.jei.api.recipe.transfer.IRecipeTransferHandler; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.item.crafting.RecipeHolder; +import net.minecraft.world.item.crafting.StonecutterRecipe; + +class PatternGridStonecutterRecipeTransferHandler implements + IRecipeTransferHandler> { + @Override + public Class getContainerClass() { + return PatternGridContainerMenu.class; + } + + @Override + public Optional> getMenuType() { + return Optional.of(Menus.INSTANCE.getPatternGrid()); + } + + @Override + public RecipeType> getRecipeType() { + return RecipeTypes.STONECUTTING; + } + + @Override + @Nullable + public IRecipeTransferError transferRecipe(final PatternGridContainerMenu containerMenu, + final RecipeHolder recipe, + final IRecipeSlotsView recipeSlots, + final Player player, + final boolean maxTransfer, + final boolean doTransfer) { + if (doTransfer) { + final var inputStacks = SlotUtil.getItems(recipeSlots, RecipeIngredientRole.INPUT); + final var outputStacks = SlotUtil.getItems(recipeSlots, RecipeIngredientRole.OUTPUT); + if (inputStacks.isEmpty() + || outputStacks.isEmpty() + || inputStacks.getFirst().isEmpty() + || outputStacks.getFirst().isEmpty()) { + return null; + } + containerMenu.transferStonecutterRecipe( + inputStacks.getFirst().getFirst(), + outputStacks.getFirst().getFirst() + ); + } + return null; + } +} diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/Platform.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/Platform.java index d9bb0f5..81df024 100644 --- a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/Platform.java +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/Platform.java @@ -1,9 +1,12 @@ package com.refinedmods.refinedstorage.jei.common; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.common.support.resource.FluidResource; import java.util.Optional; public interface Platform { Optional convertJeiIngredientToFluid(Object ingredient); + + Optional convertJeiIngredientToFluidAmount(Object ingredient); } diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/RefinedStorageModPlugin.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/RefinedStorageModPlugin.java index c7db60c..b7079f7 100644 --- a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/RefinedStorageModPlugin.java +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/RefinedStorageModPlugin.java @@ -27,6 +27,16 @@ public ResourceLocation getPluginUid() { @Override public void registerRecipeTransferHandlers(final IRecipeTransferRegistration registration) { registration.addRecipeTransferHandler(new CraftingGridRecipeTransferHandler(), RecipeTypes.CRAFTING); + registration.addRecipeTransferHandler(new PatternGridCraftingRecipeTransferHandler(), RecipeTypes.CRAFTING); + registration.addRecipeTransferHandler( + new PatternGridStonecutterRecipeTransferHandler(), + RecipeTypes.STONECUTTING + ); + registration.addRecipeTransferHandler( + new PatternGridSmithingTableRecipeTransferHandler(), + RecipeTypes.SMITHING + ); + registration.addUniversalRecipeTransferHandler(new PatternGridProcessingRecipeTransferHandler()); } @Override diff --git a/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/SlotUtil.java b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/SlotUtil.java new file mode 100644 index 0000000..bf5ca76 --- /dev/null +++ b/refinedstorage-jei-integration-common/src/main/java/com/refinedmods/refinedstorage/jei/common/SlotUtil.java @@ -0,0 +1,69 @@ +package com.refinedmods.refinedstorage.jei.common; + +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; +import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; +import com.refinedmods.refinedstorage.common.support.resource.ItemResource; + +import java.util.List; +import java.util.stream.Collectors; + +import mezz.jei.api.gui.ingredient.IRecipeSlotView; +import mezz.jei.api.gui.ingredient.IRecipeSlotsView; +import mezz.jei.api.recipe.RecipeIngredientRole; +import net.minecraft.world.item.ItemStack; + +final class SlotUtil { + private SlotUtil() { + } + + static List> getItems(final IRecipeSlotsView recipeSlots, + final RecipeIngredientRole role) { + return recipeSlots.getSlotViews(role).stream().map(SlotUtil::getItems).toList(); + } + + private static List getItems(final IRecipeSlotView slotView) { + final List stacks = slotView.getItemStacks().collect(Collectors.toList()); + prioritizeDisplayedStack(slotView, stacks); + return stacks.stream().map(ItemResource::ofItemStack).collect(Collectors.toList()); + } + + private static void prioritizeDisplayedStack(final IRecipeSlotView slotView, final List stacks) { + slotView.getDisplayedItemStack().ifPresent(displayed -> { + final int index = stacks.indexOf(displayed); + if (index < 0) { + return; + } + stacks.remove(index); + stacks.addFirst(displayed); + }); + } + + static List> getResources(final IRecipeSlotsView recipeSlots, + final RecipeIngredientRole role) { + return recipeSlots.getSlotViews(role).stream().map(SlotUtil::getResources).toList(); + } + + private static List getResources(final IRecipeSlotView slotView) { + final List resources = slotView.getAllIngredients() + .flatMap(ingredient -> RefinedStorageApi.INSTANCE.getIngredientConverter().convertToResourceAmount( + ingredient.getIngredient()).stream()) + .collect(Collectors.toList()); + prioritizeDisplayedIngredient(slotView, resources); + return resources; + } + + private static void prioritizeDisplayedIngredient(final IRecipeSlotView slotView, + final List resources) { + slotView.getDisplayedIngredient() + .flatMap(displayed -> RefinedStorageApi.INSTANCE.getIngredientConverter() + .convertToResourceAmount(displayed)) + .ifPresent(resourceAmount -> { + final int index = resources.indexOf(resourceAmount); + if (index < 0) { + return; + } + resources.remove(index); + resources.addFirst(resourceAmount); + }); + } +} diff --git a/refinedstorage-jei-integration-fabric/src/main/java/com/refinedmods/refinedstorage/jei/fabric/FabricPlatform.java b/refinedstorage-jei-integration-fabric/src/main/java/com/refinedmods/refinedstorage/jei/fabric/FabricPlatform.java index b542740..acbeff6 100644 --- a/refinedstorage-jei-integration-fabric/src/main/java/com/refinedmods/refinedstorage/jei/fabric/FabricPlatform.java +++ b/refinedstorage-jei-integration-fabric/src/main/java/com/refinedmods/refinedstorage/jei/fabric/FabricPlatform.java @@ -1,5 +1,6 @@ package com.refinedmods.refinedstorage.jei.fabric; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.common.support.resource.FluidResource; import com.refinedmods.refinedstorage.jei.common.Platform; @@ -17,4 +18,15 @@ public Optional convertJeiIngredientToFluid(final Object ingredie } return Optional.empty(); } + + @Override + public Optional convertJeiIngredientToFluidAmount(final Object ingredient) { + if (ingredient instanceof IJeiFluidIngredient fluidIngredient) { + return Optional.of(new ResourceAmount( + ofFluidVariant(fluidIngredient.getFluidVariant()), + fluidIngredient.getAmount() + )); + } + return Optional.empty(); + } } diff --git a/refinedstorage-jei-integration-neoforge/src/main/java/com/refinedmods/refinedstorage/jei/forge/ForgePlatform.java b/refinedstorage-jei-integration-neoforge/src/main/java/com/refinedmods/refinedstorage/jei/forge/ForgePlatform.java index 9f3bc34..291f11a 100644 --- a/refinedstorage-jei-integration-neoforge/src/main/java/com/refinedmods/refinedstorage/jei/forge/ForgePlatform.java +++ b/refinedstorage-jei-integration-neoforge/src/main/java/com/refinedmods/refinedstorage/jei/forge/ForgePlatform.java @@ -1,5 +1,6 @@ package com.refinedmods.refinedstorage.jei.forge; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.common.support.resource.FluidResource; import com.refinedmods.refinedstorage.jei.common.Platform; @@ -17,4 +18,12 @@ public Optional convertJeiIngredientToFluid(final Object ingredie } return Optional.empty(); } + + @Override + public Optional convertJeiIngredientToFluidAmount(final Object ingredient) { + if (ingredient instanceof FluidStack fluidStack) { + return Optional.of(new ResourceAmount(ofFluidStack(fluidStack), fluidStack.getAmount())); + } + return Optional.empty(); + } }