Skip to content

Commit

Permalink
fix: project bench not crafting from grid once storage is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed May 1, 2024
1 parent 5f123e9 commit d397681
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void loadRecipe() {

public void loadOutput() {

result = craftFromStorage(true);
result = craftFromStorageOrMatrix(true);
}

public boolean hasRecipe() {
Expand All @@ -109,15 +109,14 @@ public int getMissingIngredientMask() {
public boolean onCraftedByPlayer(Player player, boolean leaveRemainingInGrid) {
if (recipe == null) return false;

CraftingResult result = craftFromStorage(false);
CraftingResult result = craftFromStorageOrMatrix(false);

if (!result.isCraftable()) {
return false;
}

// Re-obtain remaining items in case "setCraftingPlayer" changes remaining items
ForgeHooks.setCraftingPlayer(player);
// NonNullList<ItemStack> remainingStacks = player.level.getRecipeManager().getRemainingItemsFor(IRecipeType.CRAFTING, craftingInventory, player.level);
NonNullList<ItemStack> remainingStacks = recipe.getRemainingItems(craftingInventory); // Skip re-searching for recipe, should be ok
ForgeHooks.setCraftingPlayer(null);

Expand Down Expand Up @@ -155,7 +154,21 @@ public boolean onCraftedIntoStorage() {
return true;
}

private CraftingResult craftFromStorageOrMatrix(boolean simulate) {
CraftingResult result = craftFromStorage(simulate);
if (!result.isCraftable() && inputSource.canConsumeFromCraftingMatrix()) {
// TODO maybe merge the missingIngredientMasks of these two results?
result = craftFromSource(inputSource.getCraftingMatrix(), simulate);
}
// TODO Hybrid craft that consumes from both sources instead of one or the other?
return result;
}

private CraftingResult craftFromStorage(boolean simulate) {
return craftFromSource(inputSource.getStorage(), simulate);
}

private CraftingResult craftFromSource(Container source, boolean simulate) {

if (recipe == null) return CraftingResult.EMPTY;

Expand All @@ -164,9 +177,8 @@ private CraftingResult craftFromStorage(boolean simulate) {
ItemStack result = recipe.assemble(craftingInventory);
if (result.isEmpty()) return CraftingResult.EMPTY;

Container storage = inputSource.getStorage();
if (simulate) {
storage = copyInventory(storage);
source = copyInventory(source);
}

// Try to consume all ingredients
Expand All @@ -176,7 +188,7 @@ private CraftingResult craftFromStorage(boolean simulate) {
ItemStack previousInput = craftingInventory.getItem(slot);
if (previousInput.isEmpty()) continue;

boolean isPresent = consumeIngredient(storage, 0, input -> {
boolean isPresent = consumeIngredient(source, 0, input -> {
// Candidate ingredient must be same item
if (!input.sameItemStackIgnoreDurability(previousInput)) return false;

Expand All @@ -199,31 +211,9 @@ private CraftingResult craftFromStorage(boolean simulate) {
return CraftingResult.missingIngredients(missingIngredientMask);
}

return new CraftingResult(result, recipe.getRemainingItems(craftingInventory), 0, simulate ? storage : copyInventory(storage));
return new CraftingResult(result, recipe.getRemainingItems(craftingInventory), 0, simulate ? source : copyInventory(source));
}

// private boolean insertResultsIntoInventory(NonNullList inventory, boolean simulate) {
// if (result.outputStack.isEmpty() || result.missingIngredientMask != 0) {
// return false;
// }
//
// IInventory storage = copyInventory(result.remainingStorage);
//
// // Try to insert into remaining storage
// ItemStack output = outputStack.copy();
// InventoryLib.injectItemStack(storage, output, true);
// if (!output.isEmpty()) return false;
//
// // Try to insert remaining items
// for (ItemStack stack : remainingItems) {
// ItemStack remaining = stack.copy();
// InventoryLib.injectItemStack(storage, remaining, true);
// if (!remaining.isEmpty()) return false;
// }
//
// return true;
// }

private boolean consumeIngredient(Container storage, int startIndex, Predicate<ItemStack> matchFunc) {

int i = startIndex;
Expand All @@ -250,7 +240,7 @@ private static Container copyInventory(Container inventory) {
return copy;
}

private static class CraftingResult {
private static final class CraftingResult {

private static final CraftingResult EMPTY = new CraftingResult(ItemStack.EMPTY, NonNullList.create(), 0, null);

Expand Down Expand Up @@ -294,10 +284,6 @@ private boolean canFitResultsIntoStorage() {
return InventoryLib.injectAllItemStacks(storage, getCopyOfAllResults(), true);
}

public static CraftingResult empty() {
return EMPTY;
}

public static CraftingResult missingIngredients(int missingIngredientMask) {
return new CraftingResult(ItemStack.EMPTY, NonNullList.create(), missingIngredientMask, null);
}
Expand All @@ -309,6 +295,10 @@ public interface InventorySource {

Container getStorage();

default boolean canConsumeFromCraftingMatrix() {
return false;
}

Level getWorld(); // Required for recipe lookup
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -77,10 +78,9 @@ public void drawBack(PoseStack stack, Point mouse, float partialFrame) {
blit(stack, x, y, 0, 0, getFrame().width(), getFrame().height());

ProjectBenchTile tile = getMenu().getProjectBenchTile();
ItemStack plan = tile.getPlanInventory().getItem(0);
if (!plan.isEmpty() && tile.isPlanRecipe()) {
ItemStack[] inputs = RecipePlanItem.loadPlanInputs(plan);
int missingMask = getMenu().getProjectBenchTile().getCraftingHelper().getMissingIngredientMask();
if (tile.isPlanRecipe()) {
int missingMask = tile.getCraftingHelper().getMissingIngredientMask();
Container inputs = tile.getCraftingHelper().getCraftingInventory();
drawPlanIngredientsOverlay(stack, inputs, missingMask, x + 48, y + 18);
}
}
Expand All @@ -93,16 +93,16 @@ public void drawFront(PoseStack stack, Point mouse, float partialFrame) {
}
}

private void drawPlanIngredientsOverlay(PoseStack mStack, ItemStack[] ingredients, int missingMask, int xPos, int yPos) {
private void drawPlanIngredientsOverlay(PoseStack mStack, Container ingredients, int missingMask, int xPos, int yPos) {

for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
int drawPosX = xPos + (x * 18);
int drawPosY = yPos + (y * 18);

int index = (y * 3) + x;
ItemStack ingredient = ingredients[index];
int colour = (missingMask & 1 << index) != 0 ? EnumColour.RED.argb() : EnumColour.GRAY.argb();
ItemStack ingredient = ingredients.getItem(index);
int colour = (missingMask & 1 << index) != 0 ? EnumColour.RED.argb(0x77) : EnumColour.GRAY.argb(0x77);

if (!ingredient.isEmpty()) {
fillGradient(mStack, drawPosX, drawPosY, drawPosX + 16, drawPosY + 16, colour, colour);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ public Container getStorage() {
return storageInventory;
}

@Override
public boolean canConsumeFromCraftingMatrix() {
return !isPlanRecipe;
}

@Override
public Level getWorld() {
return getLevel();
Expand Down

0 comments on commit d397681

Please sign in to comment.