From 2f369fb6ee78daa3696fec63a8d17b685ff49b0f Mon Sep 17 00:00:00 2001 From: plastered_crab Date: Sun, 29 Dec 2024 14:24:31 -0800 Subject: [PATCH] More work trying to get the Cooking Pot to behave --- .../block/entity/CampfireBlockEntity.kt | 23 +++++++--- .../gui/cookingpot/CookingPotContainer.kt | 23 +++++++--- .../client/gui/cookingpot/CookingPotMenu.kt | 2 +- .../client/gui/cookingpot/CookingPotRecipe.kt | 43 ++++++++++++++++++- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/block/entity/CampfireBlockEntity.kt b/common/src/main/kotlin/com/cobblemon/mod/common/block/entity/CampfireBlockEntity.kt index 3f507daf15..47f00735c4 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/block/entity/CampfireBlockEntity.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/block/entity/CampfireBlockEntity.kt @@ -43,18 +43,27 @@ class CampfireBlockEntity : BaseContainerBlockEntity, WorldlyContainer, RecipeCr fun serverTick(level: Level, pos: BlockPos, state: BlockState, campfireBlockEntity: CampfireBlockEntity) { if (!level.isClientSide) { var itemStack = ItemStack.EMPTY - campfireBlockEntity.quickCheck.getRecipeFor(CraftingInput.of(3, 3, campfireBlockEntity.items), level).ifPresent { cookingPotRecipe -> + + // Validate items before creating the crafting input + val validItems = campfireBlockEntity.items.filterNotNull().take(9) + if (validItems.size < 9) { + println("Not enough items in crafting grid for recipe matching.") + return + } + + campfireBlockEntity.quickCheck.getRecipeFor(CraftingInput.of(3, 3, validItems), level).ifPresent { cookingPotRecipe -> val recipeHolder = cookingPotRecipe as RecipeHolder<*> recipeHolder.value.getResultItem(level.registryAccess()).let { itemStack = it } if (!itemStack.isEmpty) { campfireBlockEntity.recipeUsed = recipeHolder println(itemStack) - campfireBlockEntity.items[9] = itemStack.copy() + campfireBlockEntity.items[0] = itemStack.copy() } } + } } } - } + private var cookingProgress : Int = 0 private var cookingTotalTime : Int = 0 @@ -64,7 +73,7 @@ class CampfireBlockEntity : BaseContainerBlockEntity, WorldlyContainer, RecipeCr private val quickCheck: RecipeManager.CachedCheck constructor(pos: BlockPos, state: BlockState) : super(CobblemonBlockEntities.CAMPFIRE, pos, state) { - this.items = NonNullList.withSize(11, ItemStack.EMPTY) + this.items = NonNullList.withSize(10, ItemStack.EMPTY) this.recipesUsed = Object2IntOpenHashMap() this.quickCheck = RecipeManager.createCheck(CobblemonRecipeTypes.COOKING_POT_COOKING) @@ -192,18 +201,18 @@ class CampfireBlockEntity : BaseContainerBlockEntity, WorldlyContainer, RecipeCr } } - fun getItemStack(): ItemStack = items[10] + fun getItemStack(): ItemStack = items[0] fun setItemStack(itemStack: ItemStack) { if (level != null) { - items[10] = itemStack + items[0] = itemStack onItemUpdate(level!!) } } fun removeItemStack(): ItemStack { if (level != null) { - val itemStack = ContainerHelper.removeItem(items, 10, 1) + val itemStack = ContainerHelper.removeItem(items, 0, 1) onItemUpdate(level!!) return itemStack } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotContainer.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotContainer.kt index 1d2479834e..a94b7a31f1 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotContainer.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotContainer.kt @@ -11,12 +11,13 @@ import java.util.List; class CookingPotContainer : TransientCraftingContainer { - val menu : CookingPotMenu + val menu : CookingPotMenu? val items : NonNullList + var outputSlot: ItemStack = ItemStack.EMPTY constructor(menu : CookingPotMenu, width: Int, height: Int) : super(menu, width, height) { this.menu = menu - this.items = NonNullList.withSize(11, ItemStack.EMPTY) + this.items = NonNullList.withSize(10, ItemStack.EMPTY) } override fun getContainerSize(): Int { @@ -33,9 +34,15 @@ class CookingPotContainer : TransientCraftingContainer { } override fun getItem(slot: Int): ItemStack { - return if (slot >= this.getContainerSize()) ItemStack.EMPTY else this.items[slot] + return when (slot) { + 0 -> outputSlot // Result slot + in 1..9 -> if (slot - 1 < items.size) this.items[slot - 1] else ItemStack.EMPTY + else -> ItemStack.EMPTY // Invalid slot + } } + + override fun removeItemNoUpdate(slot: Int): ItemStack { return ContainerHelper.takeItem(this.items, slot) } @@ -43,16 +50,20 @@ class CookingPotContainer : TransientCraftingContainer { override fun removeItem(slot: Int, amount: Int): ItemStack { val itemStack = ContainerHelper.removeItem(this.items, slot, amount) if (!itemStack.isEmpty) { - this.menu.slotsChanged(this) + this.menu?.slotsChanged(this) } return itemStack } override fun setItem(slot: Int, stack: ItemStack) { - this.items[slot] = stack - this.menu.slotsChanged(this) + when (slot) { + 0 -> outputSlot = stack // Result slot + in 1..9 -> this.items[slot - 1] = stack // Adjust for crafting slots + else -> println("Invalid slot index: $slot") + } } + override fun setChanged() {} override fun stillValid(player: Player): Boolean { diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotMenu.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotMenu.kt index f7525e195b..daea979397 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotMenu.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotMenu.kt @@ -66,7 +66,7 @@ class CookingPotMenu : RecipeBookMenu, Containe val craftingOutputOffsetX = 16 val craftingOutputOffsetY = 10 - addSlot(CookingPotResultSlot(playerInventory.player, resultContainer, 0, 124 + craftingOutputOffsetX, 35 + craftingOutputOffsetY)) + addSlot(CookingPotResultSlot(playerInventory.player, resultContainer, RESULT_SLOT, 124 + craftingOutputOffsetX, 35 + craftingOutputOffsetY)) println("Initialized result slot at index 0") for (i in 0..2) { diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotRecipe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotRecipe.kt index 8995cc460a..835ce33382 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotRecipe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/cookingpot/CookingPotRecipe.kt @@ -8,6 +8,7 @@ package com.cobblemon.mod.common.client.gui.cookingpot +import com.cobblemon.mod.common.CobblemonItems import com.cobblemon.mod.common.CobblemonRecipeSerializers import com.cobblemon.mod.common.CobblemonRecipeTypes import com.mojang.serialization.Codec @@ -33,11 +34,51 @@ class CookingPotRecipe( return groupName } - override fun matches(input: CraftingInput, level: Level): Boolean { + /*override fun matches(input: CraftingInput, level: Level): Boolean { val matches = this.pattern.matches(input) + return matches + }*/ + + /*override fun matches(input: CraftingInput, level: Level): Boolean { + val craftingItems = (1..9).map { input.getItem(it) } + val filteredInput = CraftingInput.of(3, 3, craftingItems) + return this.pattern.matches(filteredInput) + }*/ + + override fun matches(input: CraftingInput, level: Level): Boolean { + println("Validating recipe match in CookingPotRecipe...") + + // Create a filtered CraftingInput with only slots 1-9 + val filteredItems = (1..9).mapNotNull { index -> + if (index < input.size()) input.getItem(index) else ItemStack.EMPTY + } + val filteredInput = CraftingInput.of(3, 3, filteredItems) + + // Debugging: Log filtered crafting grid contents + for (i in 0 until filteredInput.size()) { + val itemStack = filteredInput.getItem(i) + println("Filtered crafting slot $i: ${itemStack.item} (${itemStack.count})") + } + + // Perform pattern matching on the filtered input + val matches = this.pattern.matches(filteredInput) + println("Pattern match result: $matches") + + // Additional logic for specific recipes + if (this.pattern.width() == 3 && this.pattern.height() == 3) { + if (this.result.item == CobblemonItems.DAWN_STONE_BLOCK.asItem()) { + println("Special case for Dawn Stone Block recipe.") + } + } + + if (matches == true) { + val test = 1 + } + return matches } + override fun assemble( input: CraftingInput, registries: HolderLookup.Provider