More work trying to get the Cooking Pot to behave

This commit is contained in:
plastered_crab
2024-12-29 14:24:31 -08:00
parent c1d16f5185
commit 2f369fb6ee
4 changed files with 76 additions and 15 deletions
@@ -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<CraftingInput, *>
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
}
@@ -11,12 +11,13 @@ import java.util.List;
class CookingPotContainer : TransientCraftingContainer {
val menu : CookingPotMenu
val menu : CookingPotMenu?
val items : NonNullList<ItemStack>
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 {
@@ -66,7 +66,7 @@ class CookingPotMenu : RecipeBookMenu<CraftingInput, CookingPotRecipe>, 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) {
@@ -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