Actually fix matter bottler bottling when transferring very little amount of matter

This commit is contained in:
DBotThePony 2025-04-05 00:11:45 +07:00
parent 2453c60d76
commit 9a6cc53189
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -190,23 +190,32 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
if (matter.storedMatter < rate) {
val toExtract = matter.missingMatter
.coerceAtMost(rate * 200)
.coerceAtMost((capability.missingMatter - matter.storedMatter).coerceAtLeast(Decimal.ONE))
.coerceAtMost(capability.missingMatter - matter.storedMatter)
matter.receiveMatter(matterNode.graph.extractMatter(toExtract, false), false)
}
if (matter.storedMatter.isPositive) {
val energyRatio = if (energyRate <= Decimal.ZERO) Decimal.ONE else energy.extractEnergy(energyRate, true) / energyRate
val matterRatio = matter.extractMatter(capability.receiveMatter(rate.coerceAtMost(matter.storedMatter), true), true) / rate
val matterTransferred = matter.extractMatter(capability.receiveMatter(rate.coerceAtMost(matter.storedMatter), true), true)
val matterRatio = matterTransferred / rate
val minRatio = minOf(matterRatio, energyRatio)
if (minRatio > Decimal.ZERO) {
if (matterRatio == Decimal.ZERO && matterTransferred > Decimal.ZERO) {
// transferring very little matter, transfer this time for free
isWorking = true
energy.extractEnergy(energyRate * minRatio, false)
matter.extractMatter(capability.receiveMatter(rate * minRatio, false), false)
matter.extractMatter(capability.receiveMatter(matter.storedMatter, false), false)
workProgress = ((capability.storedMatter - initialCapacity!!) / capability.maxStoredMatter).toFloat()
slot.setChanged()
} else {
val minRatio = minOf(matterRatio, energyRatio)
if (minRatio > Decimal.ZERO) {
isWorking = true
energy.extractEnergy(energyRate * minRatio, false)
matter.extractMatter(capability.receiveMatter(rate * minRatio, false), false)
workProgress = ((capability.storedMatter - initialCapacity!!) / capability.maxStoredMatter).toFloat()
slot.setChanged()
}
}
} else {
if (spitItemsWhenCantWork) {
@ -266,16 +275,24 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
val energyRate = MachinesConfig.MatterBottler.VALUES.energyConsumption * (1.0 + upgrades.speedBonus)
val energyRatio = if (energyRate <= Decimal.ZERO) Decimal.ONE else energy.extractEnergy(energyRate, true) / energyRate
val matterRatio = matter.receiveMatter(it.extractMatterChecked(rate, true), true) / rate
val matterExtracted = matter.receiveMatter(it.extractMatterChecked(rate, true), true)
val matterRatio = matterExtracted / rate
val minRatio = minOf(energyRatio, matterRatio)
if (minRatio > Decimal.ZERO) {
if (matterRatio == Decimal.ZERO && matterExtracted > Decimal.ZERO) {
// very little matter extracted, extract this one for free
any = true
energy.extractEnergy(energyRate * energyRatio, false)
matter.receiveMatter(it.extractMatterChecked(rate * minRatio, false), false)
matter.receiveMatter(it.extractMatterChecked(matterExtracted, false), false)
workProgress = 1f - (it.storedMatter / initialCapacity!!).toFloat()
} else {
val minRatio = minOf(energyRatio, matterRatio)
if (minRatio > Decimal.ZERO) {
any = true
energy.extractEnergy(energyRate * energyRatio, false)
matter.receiveMatter(it.extractMatterChecked(rate * minRatio, false), false)
workProgress = 1f - (it.storedMatter / initialCapacity!!).toFloat()
}
}
break