Update Matter Reconstructor to use slotted container

This commit is contained in:
DBotThePony 2025-03-06 20:00:26 +07:00
parent bc2cfccffe
commit f8e9a67994
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 38 additions and 38 deletions

View File

@ -25,8 +25,9 @@ import ru.dbotthepony.mc.otm.capability.matter.MatterStorageImpl
import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
import ru.dbotthepony.mc.otm.config.MachinesConfig
import ru.dbotthepony.mc.otm.container.HandlerFilter
import ru.dbotthepony.mc.otm.container.MatteryContainer
import ru.dbotthepony.mc.otm.container.UpgradeContainer
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.otmRandom
import ru.dbotthepony.mc.otm.core.registryName
@ -37,9 +38,35 @@ import ru.dbotthepony.mc.otm.menu.matter.MatterReconstructorMenu
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities
import java.util.function.BooleanSupplier
class MatterReconstructorBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryPoweredBlockEntity(
MBlockEntities.MATTER_RECONSTRUCTOR, blockPos, blockState) {
val repairContainer = MatteryContainer(::containerChanged, 1).also(::addDroppableContainer)
class MatterReconstructorBlockEntity(blockPos: BlockPos, blockState: BlockState) : MatteryPoweredBlockEntity(MBlockEntities.MATTER_RECONSTRUCTOR, blockPos, blockState) {
private inner class Slot(container: SlottedContainer, slot: Int) : FilteredContainerSlot(container, slot) {
override fun canAutomationPlaceItem(itemStack: ItemStack): Boolean {
if (!super.canAutomationPlaceItem(itemStack) || !itemStack.isRepairable || !itemStack.isDamaged) {
return false
}
if (MachinesConfig.MatterReconstructor.ALLOW_TO_SKIP_ANVIL && !MachinesConfig.MatterReconstructor.ONLY_ANVIL && MatterManager.get(itemStack.item).hasMatterValue) {
return true
}
return matterNode.graph
.patterns
.filter { itemStack.item.isValidRepairItem(itemStack, ItemStack(it.item, 1)) }
.findFirst().orElse(null).let {
if (it == null) {
IMatterValue.ZERO
} else {
MatterManager.get(it.item)
}
}.hasMatterValue
}
override fun canAutomationTakeItem(desired: Int): Boolean {
return super.canAutomationTakeItem(desired) && (progressPerTick <= 0.0 || matterPerTick <= Decimal.ZERO)
}
}
val repairContainer = SlottedContainer.simple(1, ::Slot, ::rescan).also(::addDroppableContainer)
private var matterPerTick = Decimal.ZERO
private var progressPerTick = 0.0
@ -67,15 +94,15 @@ class MatterReconstructorBlockEntity(blockPos: BlockPos, blockState: BlockState)
}
override fun onPatternAdded(state: PatternState) {
containerChanged()
rescan()
}
override fun onPatternRemoved(state: PatternState) {
containerChanged()
rescan()
}
override fun onPatternUpdated(newState: PatternState, oldState: PatternState) {
containerChanged()
rescan()
}
}
@ -99,34 +126,7 @@ class MatterReconstructorBlockEntity(blockPos: BlockPos, blockState: BlockState)
val energyConfig = ConfigurableEnergy(energy)
val itemConfig = ConfigurableItemHandler(
inputOutput = repairContainer.handler(object : HandlerFilter {
override fun canInsert(slot: Int, stack: ItemStack): Boolean {
if (!stack.isRepairable || !stack.isDamaged) {
return false
}
if (MachinesConfig.MatterReconstructor.ALLOW_TO_SKIP_ANVIL && !MachinesConfig.MatterReconstructor.ONLY_ANVIL) {
if (MatterManager.get(stack.item).hasMatterValue) {
return true
}
}
return matterNode.graph
.patterns
.filter { stack.item.isValidRepairItem(stack, ItemStack(it.item, 1)) }
.findFirst().orElse(null).let {
if (it == null) {
IMatterValue.ZERO
} else {
MatterManager.get(it.item)
}
}.hasMatterValue
}
override fun canExtract(slot: Int, amount: Int, stack: ItemStack): Boolean {
return progressPerTick <= 0.0 || matterPerTick <= Decimal.ZERO
}
})
inputOutput = repairContainer
)
override fun createMenu(containerID: Int, inventory: Inventory, ply: Player): AbstractContainerMenu {
@ -145,7 +145,7 @@ class MatterReconstructorBlockEntity(blockPos: BlockPos, blockState: BlockState)
private var changeset = 0
private fun containerChanged() {
private fun rescan() {
matterPerTick = Decimal.ZERO
progressPerTick = 0.0
@ -196,7 +196,6 @@ class MatterReconstructorBlockEntity(blockPos: BlockPos, blockState: BlockState)
}
}
@Suppress("name_shadowing")
val matter = MatterManager.get(found.item) * 2
progressPerTick = (item.maxDamage / matter.complexity) / MachinesConfig.MatterReconstructor.DIVISOR

View File

@ -4,6 +4,7 @@ import net.minecraft.world.SimpleContainer
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.mc.otm.block.entity.matter.MatterReconstructorBlockEntity
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu
import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot
import ru.dbotthepony.mc.otm.menu.input.EnergyConfigPlayerInput
@ -20,7 +21,7 @@ class MatterReconstructorMenu(
tile: MatterReconstructorBlockEntity? = null
) : MatteryPoweredMenu(MMenus.ITEM_REPAIER, containerId, inventory, tile) {
val matterWidget = ProfiledLevelGaugeWidget(this, tile?.matter, LevelGaugeWidget(this, tile?.matter))
val slot = object : MatteryMenuSlot(tile?.repairContainer ?: SimpleContainer(1), 0) {
val slot = object : MatteryMenuSlot(tile?.repairContainer ?: SlottedContainer.filtered(1), 0) {
override fun mayPlace(itemStack: ItemStack): Boolean {
return itemStack.isRepairable && itemStack.isDamaged && super.mayPlace(itemStack)
}