Skip to content

Commit

Permalink
Fix various issues with the formulaic assemblicator:
Browse files Browse the repository at this point in the history
- Remove no longer needed brute force repair item recipe variant as the RepairItemRecipe matches just fine and ensures that curses don't get removed
- Fix formulas for fireworks or other special recipes not properly working depending on how the matches is implemented on the recipe #7262
- Fix the slot targets we define for transferring recipes in the formulaic assemblicator from JEI not working properly/lining up
- Fix using passing the wrong dummy inventory to recipes when crafting in auto mode on the formulaic assemblicator #7022
  • Loading branch information
pupnewfster committed Jul 9, 2021
1 parent 2baa56f commit 9fe5371
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 58 deletions.
54 changes: 54 additions & 0 deletions src/main/java/mekanism/client/jei/FormulaicRecipeTransferInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package mekanism.client.jei;

import java.util.ArrayList;
import java.util.List;
import mekanism.common.inventory.container.slot.InventoryContainerSlot;
import mekanism.common.inventory.container.tile.FormulaicAssemblicatorContainer;
import mekanism.common.inventory.slot.FormulaicCraftingSlot;
import mekanism.common.inventory.slot.InputInventorySlot;
import mezz.jei.api.constants.VanillaRecipeCategoryUid;
import mezz.jei.api.recipe.transfer.IRecipeTransferInfo;
import net.minecraft.inventory.container.Slot;
import net.minecraft.util.ResourceLocation;

public class FormulaicRecipeTransferInfo implements IRecipeTransferInfo<FormulaicAssemblicatorContainer> {

@Override
public Class<FormulaicAssemblicatorContainer> getContainerClass() {
return FormulaicAssemblicatorContainer.class;
}

@Override
public ResourceLocation getRecipeCategoryUid() {
return VanillaRecipeCategoryUid.CRAFTING;
}

@Override
public boolean canHandle(FormulaicAssemblicatorContainer container) {
return true;
}

@Override
public List<Slot> getRecipeSlots(FormulaicAssemblicatorContainer container) {
List<Slot> slots = new ArrayList<>();
for (InventoryContainerSlot slot : container.getInventoryContainerSlots()) {
if (slot.getInventorySlot() instanceof FormulaicCraftingSlot) {
slots.add(slot);
}
}
return slots;
}

@Override
public List<Slot> getInventorySlots(FormulaicAssemblicatorContainer container) {
List<Slot> slots = new ArrayList<>();
slots.addAll(container.getMainInventorySlots());
slots.addAll(container.getHotBarSlots());
for (InventoryContainerSlot slot : container.getInventoryContainerSlots()) {
if (slot.getInventorySlot() instanceof InputInventorySlot) {
slots.add(slot);
}
}
return slots;
}
}
4 changes: 1 addition & 3 deletions src/main/java/mekanism/client/jei/MekanismJEI.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import mekanism.common.integration.crafttweaker.jeitweaker.JEITweakerHelper;
import mekanism.common.inventory.container.entity.robit.CraftingRobitContainer;
import mekanism.common.inventory.container.item.PortableQIODashboardContainer;
import mekanism.common.inventory.container.tile.FormulaicAssemblicatorContainer;
import mekanism.common.inventory.container.tile.QIODashboardContainer;
import mekanism.common.recipe.MekanismRecipeType;
import mekanism.common.registries.MekanismBlocks;
Expand Down Expand Up @@ -368,8 +367,7 @@ public void registerRecipeTransferHandlers(IRecipeTransferRegistration registry)
IRecipeTransferHandlerHelper transferHelper = registry.getTransferHelper();
IStackHelper stackHelper = registry.getJeiHelpers().getStackHelper();
registry.addRecipeTransferHandler(CraftingRobitContainer.class, VanillaRecipeCategoryUid.CRAFTING, 1, 9, 10, 36);
//TODO - 10.1: Validate we are properly allowing for searching all slots in the formulaic assemblicator and the player's inventory
registry.addRecipeTransferHandler(FormulaicAssemblicatorContainer.class, VanillaRecipeCategoryUid.CRAFTING, 19, 9, 35, 36);
registry.addRecipeTransferHandler(new FormulaicRecipeTransferInfo());
registry.addRecipeTransferHandler(new QIOCraftingTransferHandler<>(transferHelper, stackHelper, QIODashboardContainer.class), VanillaRecipeCategoryUid.CRAFTING);
registry.addRecipeTransferHandler(new QIOCraftingTransferHandler<>(transferHelper, stackHelper, PortableQIODashboardContainer.class), VanillaRecipeCategoryUid.CRAFTING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,24 @@ public boolean matches(World world, List<IInventorySlot> craftingGridSlots) {
return recipe.matches(dummy, world);
}

//Must have matches be called before this and be true as it assumes that the dummy inventory was set by it
public ItemStack assemble() {
return recipe == null ? ItemStack.EMPTY : recipe.assemble(dummy);
}

//Must have matches be called before this and be true as it assumes that the dummy inventory was set by it
public NonNullList<ItemStack> getRemainingItems() {
//Should never be null given the assumption matches is called first
return recipe == null ? NonNullList.create() : recipe.getRemainingItems(dummy);
}

public boolean isIngredientInPos(World world, ItemStack stack, int i) {
if (recipe == null) {
return false;
} else if (stack.isEmpty() && !input.get(i).isEmpty()) {
//If the stack being checked is empty but the input isn't expected to be empty,
// mark it as not being correct for the position
return false;
}
resetToRecipe();
dummy.setItem(i, stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ protected HotBarSlot createHotBarSlot(@Nonnull PlayerInventory inv, int index, i
protected void addSlots() {
}

public List<InventoryContainerSlot> getInventoryContainerSlots() {
return Collections.unmodifiableList(inventoryContainerSlots);
}

public List<MainInventorySlot> getMainInventorySlots() {
return Collections.unmodifiableList(mainInventorySlots);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,24 +279,22 @@ private void recalculateRecipe() {
for (int i = 0; i < craftingGridSlots.size(); i++) {
dummyInv.setItem(i, StackUtils.size(craftingGridSlots.get(i).getStack(), 1));
}

lastRemainingItems = EMPTY_LIST;

if (cachedRecipe == null || !cachedRecipe.matches(dummyInv, level)) {
cachedRecipe = level.getRecipeManager().getRecipeFor(IRecipeType.CRAFTING, dummyInv, level).orElse(null);
}
if (cachedRecipe != null) {
if (cachedRecipe == null) {
lastOutputStack = ItemStack.EMPTY;
} else {
lastOutputStack = cachedRecipe.assemble(dummyInv);
lastRemainingItems = cachedRecipe.getRemainingItems(dummyInv);
} else {
lastOutputStack = MekanismUtils.findRepairRecipe(dummyInv, level);
}
isRecipe = !lastOutputStack.isEmpty();
} else {
isRecipe = formula.matches(level, craftingGridSlots);
if (isRecipe) {
lastOutputStack = formula.recipe.assemble(dummyInv);
lastRemainingItems = formula.recipe.getRemainingItems(dummyInv);
lastOutputStack = formula.assemble();
lastRemainingItems = formula.getRemainingItems();
} else {
lastOutputStack = ItemStack.EMPTY;
}
Expand All @@ -306,12 +304,7 @@ private void recalculateRecipe() {
}

private boolean doSingleCraft() {
//Should always be 9 for the size
for (int i = 0; i < craftingGridSlots.size(); i++) {
dummyInv.setItem(i, StackUtils.size(craftingGridSlots.get(i).getStack(), 1));
}
recalculateRecipe();

ItemStack output = lastOutputStack;
if (!output.isEmpty() && tryMoveToOutput(output, Action.SIMULATE) &&
(lastRemainingItems.isEmpty() || lastRemainingItems.stream().allMatch(it -> it.isEmpty() || tryMoveToOutput(it, Action.SIMULATE)))) {
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/mekanism/common/util/MekanismUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.MathHelper;
Expand All @@ -61,7 +60,6 @@
import net.minecraft.util.math.RayTraceContext.FluidMode;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeMod;
import net.minecraftforge.common.UsernameCache;
import net.minecraftforge.common.util.Constants.NBT;
Expand Down Expand Up @@ -456,47 +454,6 @@ public boolean stillValid(@Nonnull PlayerEntity player) {
return new CraftingInventory(tempContainer, 3, 3);
}

/**
* Finds the output of a brute forced repairing action
*
* @param inv - InventoryCrafting to check
* @param world - world reference
*
* @return output ItemStack
*/
public static ItemStack findRepairRecipe(CraftingInventory inv, World world) {
NonNullList<ItemStack> dmgItems = NonNullList.withSize(2, ItemStack.EMPTY);
ItemStack leftStack = dmgItems.get(0);
for (int i = 0; i < inv.getContainerSize(); i++) {
if (!inv.getItem(i).isEmpty()) {
if (leftStack.isEmpty()) {
dmgItems.set(0, leftStack = inv.getItem(i));
} else {
dmgItems.set(1, inv.getItem(i));
break;
}
}
}

if (leftStack.isEmpty()) {
return ItemStack.EMPTY;
}

ItemStack rightStack = dmgItems.get(1);
if (!rightStack.isEmpty() && leftStack.getItem() == rightStack.getItem() && leftStack.getCount() == 1 && rightStack.getCount() == 1 &&
leftStack.getItem().isRepairable(leftStack)) {
Item theItem = leftStack.getItem();
int dmgDiff0 = theItem.getMaxDamage(leftStack) - leftStack.getDamageValue();
int dmgDiff1 = theItem.getMaxDamage(leftStack) - rightStack.getDamageValue();
int value = dmgDiff0 + dmgDiff1 + theItem.getMaxDamage(leftStack) * 5 / 100;
int solve = Math.max(0, theItem.getMaxDamage(leftStack) - value);
ItemStack repaired = new ItemStack(leftStack.getItem());
repaired.setDamageValue(solve);
return repaired;
}
return ItemStack.EMPTY;
}

/**
* Gets the wrench if the item is an IMekWrench, or a generic implementation if the item is in the forge wrenches tag
*/
Expand Down

0 comments on commit 9fe5371

Please sign in to comment.