From 3ed935001b4f56892997af2ae50060b9cda076cd Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 1 Mar 2025 19:12:05 +0700 Subject: [PATCH] Move Painter to SlottedContainer --- .../entity/decorative/PainterBlockEntity.kt | 65 ++++++++++--------- .../mc/otm/menu/decorative/PainterMenu.kt | 18 ++--- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/PainterBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/PainterBlockEntity.kt index 94311cead..fafd2c8fc 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/PainterBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/PainterBlockEntity.kt @@ -20,6 +20,8 @@ import net.neoforged.neoforge.fluids.capability.IFluidHandler import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity import ru.dbotthepony.mc.otm.container.HandlerFilter import ru.dbotthepony.mc.otm.container.MatteryContainer +import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot +import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer import ru.dbotthepony.mc.otm.core.immutableList import ru.dbotthepony.mc.otm.core.immutableMap import ru.dbotthepony.mc.otm.core.isNotEmpty @@ -34,7 +36,39 @@ class PainterBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryDe return PainterMenu(containerID, inventory, this) } - val dyeInput = MatteryContainer(this::markDirtyFast, 1) + private inner class InputSlot(container: SlottedContainer, slot: Int) : ContainerSlot(container, slot) { + override fun canAutomationPlaceItem(itemStack: ItemStack): Boolean { + if (!super.canAutomationPlaceItem(itemStack)) + return false + + if (waterStored() < MAX_WATER_STORAGE) { + itemStack.getCapability(Capabilities.FluidHandler.ITEM)?.let { + val drain = it.drain(FluidStack(Fluids.WATER, MAX_WATER_STORAGE - waterStored()), IFluidHandler.FluidAction.SIMULATE) + + if (drain.isNotEmpty) { + return true + } + } + } + + val dye = DyeColor.getColor(itemStack) ?: return false + return dyeStored(dye) + HUE_PER_ITEM <= MAX_STORAGE + } + + override fun canAutomationTakeItem(desired: Int): Boolean { + return false + } + + override fun modifyAutomationPlaceCount(itemStack: ItemStack): Int { + if (itemStack.getCapability(Capabilities.FluidHandler.ITEM) != null) + return 1 + + val dye = DyeColor.getColor(itemStack) ?: return 0 + return itemStack.count.coerceAtMost((MAX_STORAGE - dyeStored(dye)) / HUE_PER_ITEM - item.count) + } + } + + val dyeInput = SlottedContainer.simple(1, ::InputSlot, ::markDirtyFast) // null - water val dyeStored = Object2IntArrayMap() @@ -108,34 +142,7 @@ class PainterBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryDe markDirtyFast() } - val config = ConfigurableItemHandler(input = dyeInput.handler(object : HandlerFilter { - override fun canInsert(slot: Int, stack: ItemStack): Boolean { - if (waterStored() < MAX_WATER_STORAGE) { - stack.getCapability(Capabilities.FluidHandler.ITEM)?.let { - val drain = it.drain(FluidStack(Fluids.WATER, MAX_WATER_STORAGE - waterStored()), IFluidHandler.FluidAction.SIMULATE) - - if (drain.isNotEmpty) { - return true - } - } - } - - val dye = DyeColor.entries.firstOrNull { stack.`is`(it.tag) } ?: return false - return dyeStored(dye) + HUE_PER_ITEM <= MAX_STORAGE - } - - override fun modifyInsertCount(slot: Int, stack: ItemStack, existing: ItemStack, simulate: Boolean): Int { - if (!ItemStack.isSameItemSameComponents(stack, existing)) - return super.modifyInsertCount(slot, stack, existing, simulate) - - val dye = DyeColor.entries.firstOrNull { stack.`is`(it.tag) } ?: return 0 - return stack.count.coerceAtMost((MAX_STORAGE - dyeStored(dye)) / HUE_PER_ITEM - existing.count) - } - - override fun canExtract(slot: Int, amount: Int, stack: ItemStack): Boolean { - return false - } - })) + val config = ConfigurableItemHandler(input = dyeInput) fun waterStored(): Int { return dyeStored.getInt(null) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/decorative/PainterMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/decorative/PainterMenu.kt index 924e73fdd..0c5b269c0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/decorative/PainterMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/decorative/PainterMenu.kt @@ -17,6 +17,8 @@ import ru.dbotthepony.kommons.util.setValue import ru.dbotthepony.mc.otm.block.entity.decorative.PainterBlockEntity import ru.dbotthepony.mc.otm.capability.matteryPlayer import ru.dbotthepony.mc.otm.container.MatteryContainer +import ru.dbotthepony.mc.otm.container.set +import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer import ru.dbotthepony.mc.otm.core.addAll import ru.dbotthepony.mc.otm.core.collect.SupplierMap import ru.dbotthepony.mc.otm.core.collect.filter @@ -45,8 +47,8 @@ class PainterMenu( val dyeStoredDirect = SupplierMap(dyeStored) val itemConfig = ItemConfigPlayerInput(this, tile?.config) - val inputContainer = MatteryContainer(::rescan, 1) - val outputContainer = MatteryContainer(1) + val inputContainer = SlottedContainer.simple(1, ::rescan) + val outputContainer = SlottedContainer.simple(1) private var lastRecipe: RecipeHolder? = null var selectedRecipe by mSynchronizer.add(ListenableDelegate.Box(null), StreamCodecs.RESOURCE_LOCATION.nullable()).also { it.addListener(Runnable { rescan() }) } @@ -102,13 +104,13 @@ class PainterMenu( } } - val dyeSlot = object : MatteryMenuSlot(tile?.dyeInput ?: SimpleContainer(1), 0) { + val dyeSlot = object : MatteryMenuSlot(tile?.dyeInput ?: SlottedContainer.simple(1), 0) { override fun mayPlace(itemStack: ItemStack): Boolean { - return super.mayPlace(itemStack) && (( - itemStack.getCapability(Capabilities.FluidHandler.ITEM)?.let { - dyeStoredDirect[null]!! < PainterBlockEntity.MAX_WATER_STORAGE && it.drain(FluidStack(Fluids.WATER, PainterBlockEntity.MAX_WATER_STORAGE - dyeStoredDirect[null]!!), IFluidHandler.FluidAction.SIMULATE).isNotEmpty - } ?: false - ) || (DyeColor.getColor(itemStack)?.let { dyeStoredDirect[it]!! + PainterBlockEntity.HUE_PER_ITEM <= PainterBlockEntity.MAX_STORAGE } ?: false)) + return super.mayPlace(itemStack) && ( + itemStack.getCapability(Capabilities.FluidHandler.ITEM) + ?.drain(FluidStack(Fluids.WATER, PainterBlockEntity.MAX_WATER_STORAGE - dyeStoredDirect[null]!!), IFluidHandler.FluidAction.SIMULATE)?.isNotEmpty + ?: false + || DyeColor.getColor(itemStack) != null) } }