From 736c62b6073c2ad88afaa10f1dee62d914bf73c3 Mon Sep 17 00:00:00 2001 From: Eugene Date: Sat, 10 Aug 2024 22:38:03 +0300 Subject: [PATCH] craft fix --- .../block/entity/BrewingKegBlockEntity.java | 80 ++++++++++++++----- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/megatrex4/ukrainian_dlight/block/entity/BrewingKegBlockEntity.java b/src/main/java/com/megatrex4/ukrainian_dlight/block/entity/BrewingKegBlockEntity.java index 2d50d2a..a700b21 100644 --- a/src/main/java/com/megatrex4/ukrainian_dlight/block/entity/BrewingKegBlockEntity.java +++ b/src/main/java/com/megatrex4/ukrainian_dlight/block/entity/BrewingKegBlockEntity.java @@ -39,6 +39,7 @@ import net.minecraft.network.PacketByteBuf; import net.minecraft.recipe.Ingredient; import net.minecraft.recipe.Recipe; +import net.minecraft.recipe.RecipeType; import net.minecraft.registry.tag.ItemTags; import net.minecraft.screen.PropertyDelegate; import net.minecraft.screen.ScreenHandler; @@ -52,6 +53,9 @@ import net.minecraft.util.math.*; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; +import vectorwing.farmersdelight.common.crafting.CookingPotRecipe; +import vectorwing.farmersdelight.common.mixin.accessor.RecipeManagerAccessor; +import vectorwing.farmersdelight.common.registry.ModRecipeTypes; import java.util.*; @@ -289,6 +293,7 @@ public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, Pla public void tick(World world, BlockPos pos, BlockState state, BrewingKegBlockEntity blockEntity) { boolean dirty = false; + boolean didInventoryChange = false; boolean isPowered = world.isReceivingRedstonePower(pos); if (isPowered) { @@ -300,7 +305,7 @@ public void tick(World world, BlockPos pos, BlockState state, BrewingKegBlockEnt if (blockEntity.hasInput() && enoughFluid) { Optional match = getCurrentRecipe(); - if (match.isPresent()) { + if (match.isPresent() && blockEntity.canCook((BrewingRecipe) match.get())) { BrewingRecipe recipe = match.get(); if (blockEntity.canCook(recipe)) { dirty = blockEntity.processBrewing(recipe); @@ -336,6 +341,7 @@ private boolean hasInput() { return true; } } + return false; } @@ -344,36 +350,66 @@ private void playSoundBrewing() { world.playSound(null, getPos(), SoundEvents.BLOCK_BREWING_STAND_BREW, SoundCategory.BLOCKS, 0.5F, randompitch); } - protected boolean canCook(Recipe recipeIn) { - if (hasInput() && recipeIn != null) { - ItemStack recipeOutput = recipeIn.getOutput(world.getRegistryManager()); - if (recipeOutput.isEmpty()) { - return false; - } + protected boolean canCook(BrewingRecipe recipe) { + if (!hasInput() || recipe == null) { + return false; + } + + // Get the output stack of the recipe + ItemStack recipeOutput = recipe.getOutput(world.getRegistryManager()); + if (recipeOutput.isEmpty()) { + return false; + } + + // Create a list to track the remaining required ingredients + List remainingIngredients = new ArrayList<>(recipe.getIngredients()); + + // Iterate over all ingredient slots + for (int i = 0; i < INGREDIENT_SLOTS.length; i++) { + ItemStack stack = getStack(INGREDIENT_SLOTS[i]); + + // If the slot is not empty, try to match it with one of the remaining ingredients + if (!stack.isEmpty()) { + boolean matched = false; + + for (Iterator iterator = remainingIngredients.iterator(); iterator.hasNext(); ) { + Ingredient ingredient = iterator.next(); + + if (ingredient.test(stack)) { + // If a match is found, remove the ingredient from the list and mark it as matched + iterator.remove(); + matched = true; + break; + } + } - for (int i = 0; i < DRINKS_DISPLAY_SLOT; ++i) { - if (i == WATER_SLOT || i == CONTAINER_SLOT) continue; - ItemStack stack = getStack(i); - if (!stack.isEmpty() && !recipeIn.getIngredients().stream().anyMatch(ingredient -> ingredient.test(stack))) { + // If no match is found for the stack, return false + if (!matched) { return false; } } + } - ItemStack currentOutput = getStack(DRINKS_DISPLAY_SLOT); - if (currentOutput.isEmpty()) { - return true; - } else if (!ItemStack.areItemsEqual(currentOutput, recipeOutput)) { - return false; - } else if (currentOutput.getCount() + recipeOutput.getCount() <= getMaxCountPerStack()) { - return true; - } else { - return currentOutput.getCount() + recipeOutput.getCount() <= recipeOutput.getMaxCount(); - } - } else { + // After iterating through all the slots, ensure that there are no remaining required ingredients + if (!remainingIngredients.isEmpty()) { return false; } + + // Check if the output slot can accommodate the result + ItemStack currentOutput = getStack(DRINKS_DISPLAY_SLOT); + if (currentOutput.isEmpty()) { + return true; + } else if (!ItemStack.areItemsEqual(currentOutput, recipeOutput)) { + return false; + } else if (currentOutput.getCount() + recipeOutput.getCount() <= getMaxCountPerStack()) { + return true; + } else { + return currentOutput.getCount() + recipeOutput.getCount() <= recipeOutput.getMaxCount(); + } } + + private boolean processBrewing(BrewingRecipe recipe) { if (world == null || recipe == null) return false;