From 640aeabb0760f2295ec7ac8984bf3db6d4297822 Mon Sep 17 00:00:00 2001
From: DBotThePony <dbotthepony@yandex.ru>
Date: Sat, 29 Mar 2025 14:06:38 +0700
Subject: [PATCH] Invalidate recipe caches on resources reload

---
 .../kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt     | 5 +++++
 .../mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt | 2 ++
 .../block/entity/tech/AbstractPoweredFurnaceBlockEntity.kt   | 5 +++++
 .../mc/otm/block/entity/tech/PlatePressBlockEntity.kt        | 5 +++++
 4 files changed, 17 insertions(+)

diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt
index 0d99c1660..572532f27 100644
--- a/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt
+++ b/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt
@@ -14,6 +14,8 @@ import ru.dbotthepony.mc.otm.player.android.AndroidResearchResults
 import ru.dbotthepony.mc.otm.player.android.feature.EnderTeleporterFeature
 import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
 import ru.dbotthepony.mc.otm.block.entity.decorative.DevChestBlockEntity
+import ru.dbotthepony.mc.otm.block.entity.tech.AbstractPoweredFurnaceBlockEntity
+import ru.dbotthepony.mc.otm.block.entity.tech.PlatePressBlockEntity
 import ru.dbotthepony.mc.otm.player.MatteryPlayer
 import ru.dbotthepony.mc.otm.capability.drive.DrivePool
 import ru.dbotthepony.mc.otm.client.AndroidAbilityKeyMapping
@@ -244,6 +246,9 @@ object OverdriveThatMatters {
 
 		FORGE_BUS.addListener(EventPriority.NORMAL, MStructureTags::registerVillagerTrades)
 
+		FORGE_BUS.addListener(EventPriority.LOWEST, PlatePressBlockEntity::onReload)
+		FORGE_BUS.addListener(EventPriority.LOWEST, AbstractPoweredFurnaceBlockEntity.Companion::onReload)
+
 		if (isCuriosLoaded) {
 			FORGE_BUS.addListener(EventPriority.NORMAL, ::onCuriosSlotModifiersUpdated)
 		}
diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt
index 5dfb606a1..e0e4857ef 100644
--- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt
+++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt
@@ -92,6 +92,8 @@ class MatterEntanglerBlockEntity(blockPos: BlockPos, blockState: BlockState) : M
 	}
 
 	private inner class InputSlot(container: SlottedContainer, slot: Int) : FilteredContainerSlot(container, slot) {
+		// may get stalled on /reload command for up to a minute
+		// shouldn't cause major issues through, since /reload is not something you frequently be executing
 		val insertCache = SimpleCache<ItemStackKey, Boolean>(Duration.ofMinutes(1))
 
 		override fun canAutomationPlaceItem(itemStack: ItemStack): Boolean {
diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/AbstractPoweredFurnaceBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/AbstractPoweredFurnaceBlockEntity.kt
index 6ef977b1a..e92851a9f 100644
--- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/AbstractPoweredFurnaceBlockEntity.kt
+++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/AbstractPoweredFurnaceBlockEntity.kt
@@ -16,6 +16,7 @@ import net.minecraft.world.item.crafting.SmokingRecipe
 import net.minecraft.world.level.block.entity.BlockEntityType
 import net.minecraft.world.level.block.state.BlockState
 import net.neoforged.neoforge.capabilities.Capabilities
+import net.neoforged.neoforge.event.AddReloadListenerEvent
 import ru.dbotthepony.kommons.util.getValue
 import ru.dbotthepony.kommons.util.setValue
 import ru.dbotthepony.mc.otm.block.entity.ExperienceStorage
@@ -203,6 +204,10 @@ sealed class AbstractPoweredFurnaceBlockEntity<P : AbstractCookingRecipe, S : Ma
 	companion object {
 		// shared by all furnace instances, so cache should be large enough
 		private val acceptableItems = SimpleCache<ItemStackKey, Boolean>(Duration.ofMinutes(1))
+
+		internal fun onReload(event: AddReloadListenerEvent) {
+			acceptableItems.invalidateAll()
+		}
 	}
 }
 
diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/PlatePressBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/PlatePressBlockEntity.kt
index 59d6d166a..3f1c9ca45 100644
--- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/PlatePressBlockEntity.kt
+++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/PlatePressBlockEntity.kt
@@ -9,6 +9,7 @@ import net.minecraft.world.item.ItemStack
 import net.minecraft.world.item.crafting.SingleRecipeInput
 import net.minecraft.world.level.block.state.BlockState
 import net.neoforged.neoforge.capabilities.Capabilities
+import net.neoforged.neoforge.event.AddReloadListenerEvent
 import ru.dbotthepony.mc.otm.block.entity.ExperienceStorage
 import ru.dbotthepony.mc.otm.block.entity.JobContainer
 import ru.dbotthepony.mc.otm.block.entity.JobStatus
@@ -128,5 +129,9 @@ class PlatePressBlockEntity(
 
 	companion object {
 		private val cache = SimpleCache<ItemStackKey, Boolean>(Duration.ofMinutes(1L))
+
+		internal fun onReload(event: AddReloadListenerEvent) {
+			cache.invalidateAll()
+		}
 	}
 }