Skip to content

Commit

Permalink
Fix crash with assembler, closes #6093
Browse files Browse the repository at this point in the history
  • Loading branch information
malte0811 committed Nov 2, 2024
1 parent 807a794 commit 9cbcde8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import blusunrize.immersiveengineering.api.tool.assembler.AssemblerHandler;
import blusunrize.immersiveengineering.api.tool.assembler.RecipeQuery;
import blusunrize.immersiveengineering.common.util.InventoryCraftingFalse;
import blusunrize.immersiveengineering.common.util.Utils;
import net.minecraft.core.HolderLookup.Provider;
import net.minecraft.core.NonNullList;
Expand All @@ -36,7 +37,7 @@ public void recalculateOutput(@Nullable Level level)
{
if(level==null)
return;
CraftingInput invC = Utils.InventoryCraftingFalse.createFilledCraftingInventory(3, 3, inv).asCraftInput();
CraftingInput invC = InventoryCraftingFalse.createFilledCraftingInventory(3, 3, inv);
this.recipe = Utils.findCraftingRecipe(invC, level).map(RecipeHolder::value).orElse(null);
this.inv.set(9, recipe!=null?recipe.assemble(invC, level.registryAccess()): ItemStack.EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import blusunrize.immersiveengineering.common.config.IEServerConfig;
import blusunrize.immersiveengineering.common.fluids.ArrayFluidHandler;
import blusunrize.immersiveengineering.common.util.IESounds;
import blusunrize.immersiveengineering.common.util.InventoryCraftingFalse;
import blusunrize.immersiveengineering.common.util.Utils;
import blusunrize.immersiveengineering.common.util.inventory.SlotwiseItemHandler;
import blusunrize.immersiveengineering.common.util.inventory.SlotwiseItemHandler.IOConstraint;
Expand Down Expand Up @@ -148,7 +149,7 @@ private List<OutputBuffer> craftRecipes(IMultiblockContext<State> ctx)
this.consumeIngredients(state, queries, availableStacks, true, sources);

NonNullList<ItemStack> remainingItems = pattern.recipe.getRemainingItems(
Utils.InventoryCraftingFalse.createFilledCraftingInventory(3, 3, sources.gridItems).asCraftInput()
InventoryCraftingFalse.createFilledCraftingInventory(3, 3, sources.gridItems)
);
for(int i = 0; i < remainingItems.size(); i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import blusunrize.immersiveengineering.api.tool.assembler.AssemblerHandler.IRecipeAdapter;
import blusunrize.immersiveengineering.api.tool.assembler.RecipeQuery;
import blusunrize.immersiveengineering.common.util.FakePlayerUtil;
import blusunrize.immersiveengineering.common.util.Utils;
import blusunrize.immersiveengineering.common.util.InventoryCraftingFalse;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;
Expand All @@ -31,27 +31,25 @@ public class DefaultAssemblerAdapter implements IRecipeAdapter<Recipe<CraftingIn
public List<RecipeQuery> getQueriedInputs(Recipe<CraftingInput> recipe, NonNullList<ItemStack> input, Level world)
{
NonNullList<Ingredient> ingred = recipe.getIngredients();
CraftingInput craftingInput = InventoryCraftingFalse.createFilledCraftingInventory(3, 3, input);
// Check that the ingredients roughly match what the recipe actually requires.
// This is necessary to prevent infinite crafting for recipes like FireworkRocketRecipe which don't return
// meaningful values in getIngredients.
NonNullList<Ingredient> ingredientsForMatching = NonNullList.create();
List<ItemStack> inputList = input.subList(0, input.size()-1);
for(Ingredient i : ingred)
if(!i.isEmpty())
ingredientsForMatching.add(i);
while(ingredientsForMatching.size() < inputList.size())
while(ingredientsForMatching.size() < craftingInput.size())
ingredientsForMatching.add(Ingredient.EMPTY);
CommonHooks.setCraftingPlayer(FakePlayerUtil.getFakePlayer(world));
int[] ingredientAssignment = RecipeMatcher.findMatches(inputList, ingredientsForMatching);
int[] ingredientAssignment = RecipeMatcher.findMatches(craftingInput.items(), ingredientsForMatching);
CommonHooks.setCraftingPlayer(null);

// Collect remaining items
NonNullList<ItemStack> remains = recipe.getRemainingItems(
Utils.InventoryCraftingFalse.createFilledCraftingInventory(3, 3, input).asCraftInput()
);
NonNullList<ItemStack> remains = recipe.getRemainingItems(craftingInput);

List<RecipeQuery> queries = new ArrayList<>();
for(int i = 0; i < inputList.size(); i++)
for(int i = 0; i < craftingInput.size(); i++)
{
final RecipeQuery query;
if(ingredientAssignment!=null)
Expand All @@ -63,7 +61,7 @@ public List<RecipeQuery> getQueriedInputs(Recipe<CraftingInput> recipe, NonNullL
);
else
// Otherwise request the exact stacks used in the input
query = AssemblerHandler.createQueryFromItemStack(input.get(i), remains.get(i));
query = AssemblerHandler.createQueryFromItemStack(craftingInput.getItem(i), remains.get(i));
if(query!=null)
queries.add(query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import blusunrize.immersiveengineering.api.crafting.TagOutput;
import blusunrize.immersiveengineering.api.crafting.cache.CachedRecipeList;
import blusunrize.immersiveengineering.common.register.IEItems.Molds;
import blusunrize.immersiveengineering.common.util.Utils;
import blusunrize.immersiveengineering.common.util.InventoryCraftingFalse;
import com.mojang.datafixers.util.Pair;
import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess;
Expand Down Expand Up @@ -206,9 +206,9 @@ else if(UNPACK_ID.equals(id))

public static Pair<RecipeHolder<CraftingRecipe>, ItemStack> getPackedOutput(int gridSize, ItemStack stack, Level world)
{
CraftingInput invC = Utils.InventoryCraftingFalse.createFilledCraftingInventory(
CraftingInput invC = InventoryCraftingFalse.createFilledCraftingInventory(
gridSize, gridSize, NonNullList.withSize(gridSize*gridSize, stack.copy())
).asCraftInput();
);
return world.getRecipeManager()
.getRecipeFor(RecipeType.CRAFTING, invC, world)
.map(recipe -> Pair.of(recipe, recipe.value().assemble(invC, world.registryAccess())))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* BluSunrize
* Copyright (c) 2024
*
* This code is licensed under "Blu's License of Common Sense"
* Details can be found in the license file in the root folder of this project
*/

package blusunrize.immersiveengineering.common.util;

import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.TransientCraftingContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;

import javax.annotation.Nonnull;
import java.util.List;

public class InventoryCraftingFalse
{
private static final AbstractContainerMenu NULL_CONTAINER = new AbstractContainerMenu(MenuType.CRAFTING, 0)
{
@Override
public ItemStack quickMoveStack(Player p_38941_, int p_38942_)
{
return ItemStack.EMPTY;
}

@Override
public void slotsChanged(Container paramIInventory)
{
}

@Override
public boolean stillValid(@Nonnull Player playerIn)
{
return false;
}
};

public static CraftingInput createFilledCraftingInventory(int w, int h, List<ItemStack> stacks)
{
CraftingContainer invC = new TransientCraftingContainer(NULL_CONTAINER, w, h);
for(int j = 0; j < w*h; j++)
if(!stacks.get(j).isEmpty())
invC.setItem(j, stacks.get(j).copy());
return invC.asCraftInput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,12 @@
import net.minecraft.tags.TagKey;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.Container;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.TransientCraftingContainer;
import net.minecraft.world.item.*;
import net.minecraft.world.item.component.FireworkExplosion;
import net.minecraft.world.item.component.FireworkExplosion.Shape;
Expand Down Expand Up @@ -626,43 +621,6 @@ public static Vec3 withCoordinate(Vec3 vertex, Axis axis, double value)
return vertex;
}

public static class InventoryCraftingFalse extends TransientCraftingContainer
{
private static final AbstractContainerMenu nullContainer = new AbstractContainerMenu(MenuType.CRAFTING, 0)
{
@Override
public ItemStack quickMoveStack(Player p_38941_, int p_38942_)
{
return ItemStack.EMPTY;
}

@Override
public void slotsChanged(Container paramIInventory)
{
}

@Override
public boolean stillValid(@Nonnull Player playerIn)
{
return false;
}
};

public InventoryCraftingFalse(int w, int h)
{
super(nullContainer, w, h);
}

public static CraftingContainer createFilledCraftingInventory(int w, int h, List<ItemStack> stacks)
{
CraftingContainer invC = new Utils.InventoryCraftingFalse(w, h);
for(int j = 0; j < w*h; j++)
if(!stacks.get(j).isEmpty())
invC.setItem(j, stacks.get(j).copy());
return invC;
}
}

public static BlockPos rayTraceForFirst(Vec3 start, Vec3 end, Level w, Set<BlockPos> ignore)
{
Set<BlockPos> trace = Raytracer.rayTrace(start, end, w);
Expand Down

0 comments on commit 9cbcde8

Please sign in to comment.