Specify "id" of job event loop

This commit is contained in:
DBotThePony 2023-06-23 00:04:10 +07:00
parent 97d3a07065
commit f4c36cc728
Signed by: DBot
GPG Key ID: DCC23B5715498507
6 changed files with 27 additions and 27 deletions

View File

@ -32,7 +32,7 @@ abstract class MatteryWorkerBlockEntity<JobType : IMachineJob>(
val jobDeserializer: (tag: CompoundTag) -> JobType?, val jobDeserializer: (tag: CompoundTag) -> JobType?,
maxJobs: Int = 1 maxJobs: Int = 1
) : MatteryPoweredBlockEntity(type, blockPos, blockState) { ) : MatteryPoweredBlockEntity(type, blockPos, blockState) {
val jobEventLoops: ImmutableList<MachineJobEventLoop<JobType>> = immutableList(maxJobs) { val jobEventLoops: ImmutableList<MachineJobEventLoop<JobType>> = immutableList(maxJobs) { id ->
object : MachineJobEventLoop<JobType>() { object : MachineJobEventLoop<JobType>() {
override val energy: IMatteryEnergyStorage? override val energy: IMatteryEnergyStorage?
get() = matteryEnergy get() = matteryEnergy
@ -44,28 +44,28 @@ abstract class MatteryWorkerBlockEntity<JobType : IMachineJob>(
} }
override fun onJobFinish(job: JobType): JobStatus { override fun onJobFinish(job: JobType): JobStatus {
return this@MatteryWorkerBlockEntity.onJobFinish(job) return this@MatteryWorkerBlockEntity.onJobFinish(job, id)
} }
override fun computeNextJob(): JobContainer<JobType> { override fun computeNextJob(): JobContainer<JobType> {
return this@MatteryWorkerBlockEntity.computeNextJob() return this@MatteryWorkerBlockEntity.computeNextJob(id)
} }
override fun jobUpdated(new: JobType?, old: JobType?) { override fun jobUpdated(new: JobType?, old: JobType?) {
this@MatteryWorkerBlockEntity.jobUpdated(new, old) this@MatteryWorkerBlockEntity.jobUpdated(new, old, id)
} }
override fun onWorkTick(requiredPower: Decimal, extractedPower: Decimal, ticksAdvanced: Double, job: JobType): JobStatus { override fun onWorkTick(requiredPower: Decimal, extractedPower: Decimal, ticksAdvanced: Double, job: JobType): JobStatus {
return this@MatteryWorkerBlockEntity.onWorkTick(requiredPower, extractedPower, ticksAdvanced, job) return this@MatteryWorkerBlockEntity.onWorkTick(requiredPower, extractedPower, ticksAdvanced, job, id)
} }
} }
} }
protected open fun jobUpdated(new: JobType?, old: JobType?) {} protected open fun jobUpdated(new: JobType?, old: JobType?, id: Int) {}
protected abstract fun onJobFinish(job: JobType): JobStatus protected abstract fun onJobFinish(job: JobType, id: Int): JobStatus
protected abstract fun computeNextJob(): JobContainer<JobType> protected abstract fun computeNextJob(id: Int): JobContainer<JobType>
protected open fun onWorkTick(requiredPower: Decimal, extractedPower: Decimal, ticksAdvanced: Double, job: JobType): JobStatus { protected open fun onWorkTick(requiredPower: Decimal, extractedPower: Decimal, ticksAdvanced: Double, job: JobType, id: Int): JobStatus {
return JobStatus.SUCCESS return JobStatus.SUCCESS
} }

View File

@ -165,7 +165,7 @@ class MatterDecomposerBlockEntity(pos: BlockPos, state: BlockState)
return MatterDecomposerMenu(containerID, inventory, this) return MatterDecomposerMenu(containerID, inventory, this)
} }
override fun onJobFinish(job: DecomposerJob): JobStatus { override fun onJobFinish(job: DecomposerJob, id: Int): JobStatus {
if (job.toDust) { if (job.toDust) {
job.matterValue = moveMatterAsDustIntoContainer(job.matterValue, outputContainer, 0, 1) job.matterValue = moveMatterAsDustIntoContainer(job.matterValue, outputContainer, 0, 1)
@ -183,7 +183,7 @@ class MatterDecomposerBlockEntity(pos: BlockPos, state: BlockState)
return JobStatus.SUCCESS return JobStatus.SUCCESS
} }
override fun computeNextJob(): JobContainer<DecomposerJob> { override fun computeNextJob(id: Int): JobContainer<DecomposerJob> {
val stack = inputContainer[0] val stack = inputContainer[0]
if (!stack.isEmpty) { if (!stack.isEmpty) {

View File

@ -107,12 +107,12 @@ class MatterRecyclerBlockEntity(blockPos: BlockPos, blockState: BlockState)
return MatterRecyclerMenu(containerID, inventory, this) return MatterRecyclerMenu(containerID, inventory, this)
} }
override fun onJobFinish(job: RecyclerJob): JobStatus { override fun onJobFinish(job: RecyclerJob, id: Int): JobStatus {
// вся логика в onWorkTick // вся логика в onWorkTick
return JobStatus.SUCCESS return JobStatus.SUCCESS
} }
override fun computeNextJob(): JobContainer<RecyclerJob> { override fun computeNextJob(id: Int): JobContainer<RecyclerJob> {
if (matter.missingMatter.isZero) if (matter.missingMatter.isZero)
return JobContainer.noMatter() return JobContainer.noMatter()
@ -127,7 +127,7 @@ class MatterRecyclerBlockEntity(blockPos: BlockPos, blockState: BlockState)
return JobContainer.success(RecyclerJob(dustMatter.matter.toDouble() * TICKS_PER_MATTER, POWER_CONSUMPTION, dustMatter.matter * (0.4 + level!!.random.nextDouble() * 0.6))) return JobContainer.success(RecyclerJob(dustMatter.matter.toDouble() * TICKS_PER_MATTER, POWER_CONSUMPTION, dustMatter.matter * (0.4 + level!!.random.nextDouble() * 0.6)))
} }
override fun onWorkTick(requiredPower: Decimal, extractedPower: Decimal, ticksAdvanced: Double, job: RecyclerJob): JobStatus { override fun onWorkTick(requiredPower: Decimal, extractedPower: Decimal, ticksAdvanced: Double, job: RecyclerJob, id: Int): JobStatus {
val receive = job.totalMatter / job.ticks val receive = job.totalMatter / job.ticks
if (receive.isZero) if (receive.isZero)

View File

@ -118,15 +118,15 @@ class MatterReplicatorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
} }
override fun onMatterTaskCreated(task: IReplicationTask<*>) { override fun onMatterTaskCreated(task: IReplicationTask<*>) {
jobEventLoop.notify(MachineJobEventLoop.IdleReason.OBSERVING) jobEventLoops[0].notify(MachineJobEventLoop.IdleReason.OBSERVING)
} }
override fun <T : IReplicationTask<*>> onMatterTaskUpdated(newState: T, oldState: T) { override fun <T : IReplicationTask<*>> onMatterTaskUpdated(newState: T, oldState: T) {
jobEventLoop.notify(MachineJobEventLoop.IdleReason.OBSERVING) jobEventLoops[0].notify(MachineJobEventLoop.IdleReason.OBSERVING)
} }
override fun onPatternAdded(state: IPatternState) { override fun onPatternAdded(state: IPatternState) {
jobEventLoop.notify(MachineJobEventLoop.IdleReason.OBSERVING) jobEventLoops[0].notify(MachineJobEventLoop.IdleReason.OBSERVING)
} }
} }
@ -143,7 +143,7 @@ class MatterReplicatorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
return MatterReplicatorMenu(containerID, inventory, this) return MatterReplicatorMenu(containerID, inventory, this)
} }
override fun onJobFinish(job: ReplicatorJob): JobStatus { override fun onJobFinish(job: ReplicatorJob, id: Int): JobStatus {
if (job.asDust) { if (job.asDust) {
job.matterValue = moveMatterAsDustIntoContainer(job.matterValue, container, OUTPUT_DUST_MAIN, OUTPUT_DUST_STACKING) job.matterValue = moveMatterAsDustIntoContainer(job.matterValue, container, OUTPUT_DUST_MAIN, OUTPUT_DUST_STACKING)
@ -173,7 +173,7 @@ class MatterReplicatorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
matterNode.discover(this) matterNode.discover(this)
} }
override fun jobUpdated(new: ReplicatorJob?, old: ReplicatorJob?) { override fun jobUpdated(new: ReplicatorJob?, old: ReplicatorJob?, id: Int) {
visualItemStack = new?.itemStack ?: ItemStack.EMPTY visualItemStack = new?.itemStack ?: ItemStack.EMPTY
visualProgress = 0f visualProgress = 0f
} }
@ -188,7 +188,7 @@ class MatterReplicatorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
var lastRender = 0L var lastRender = 0L
var particleRenderScore = 0L var particleRenderScore = 0L
override fun computeNextJob(): JobContainer<ReplicatorJob> { override fun computeNextJob(id: Int): JobContainer<ReplicatorJob> {
if (energy.batteryLevel < BASE_CONSUMPTION) { if (energy.batteryLevel < BASE_CONSUMPTION) {
return JobContainer.noEnergy() return JobContainer.noEnergy()
} }
@ -213,7 +213,7 @@ class MatterReplicatorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
)) ))
} }
override fun onWorkTick(requiredPower: Decimal, extractedPower: Decimal, ticksAdvanced: Double, job: ReplicatorJob): JobStatus { override fun onWorkTick(requiredPower: Decimal, extractedPower: Decimal, ticksAdvanced: Double, job: ReplicatorJob, id: Int): JobStatus {
val drainPerTick = job.matterPerTick * ticksAdvanced val drainPerTick = job.matterPerTick * ticksAdvanced
if (matter.extractMatter(drainPerTick, true) < drainPerTick) { if (matter.extractMatter(drainPerTick, true) < drainPerTick) {
@ -237,7 +237,7 @@ class MatterReplicatorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
// в тик требуется меньше материи, чем её может хранить репликатор // в тик требуется меньше материи, чем её может хранить репликатор
// примем из сети недостающее количество бака материи, или 200 тиков репликации, что меньше // примем из сети недостающее количество бака материи, или 200 тиков репликации, что меньше
val drain = matterNode.graph.extractMatter((drainPerTick * DRAIN_MULT) val drain = matterNode.graph.extractMatter((drainPerTick * DRAIN_MULT)
.coerceAtMost(job.matterPerTick * (job.ticks - jobEventLoop.workTicks - ticksAdvanced)) .coerceAtMost(job.matterPerTick * (job.ticks - jobEventLoops[0].workTicks - ticksAdvanced))
.coerceAtLeast(Decimal.ONE) .coerceAtLeast(Decimal.ONE)
.coerceAtMost(matter.missingMatter), false) .coerceAtMost(matter.missingMatter), false)
@ -261,7 +261,7 @@ class MatterReplicatorBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
// в машине достаточно материи // в машине достаточно материи
matter.extractMatter(drainPerTick, false) matter.extractMatter(drainPerTick, false)
visualProgress = jobEventLoop.workProgress visualProgress = jobEventLoops[0].workProgress
return JobStatus.SUCCESS return JobStatus.SUCCESS
} }

View File

@ -36,7 +36,7 @@ class CobblerBlockEntity(blockPos: BlockPos, blockState: BlockState)
savetable(::container, INVENTORY_KEY) savetable(::container, INVENTORY_KEY)
} }
override fun onJobFinish(job: MachineItemJob): JobStatus { override fun onJobFinish(job: MachineItemJob, id: Int): JobStatus {
if (!container.fullyAddItem(job.itemStack)) { if (!container.fullyAddItem(job.itemStack)) {
return JobStatus.FAILURE_ITEM return JobStatus.FAILURE_ITEM
} }
@ -44,7 +44,7 @@ class CobblerBlockEntity(blockPos: BlockPos, blockState: BlockState)
return JobStatus.SUCCESS return JobStatus.SUCCESS
} }
override fun computeNextJob(): JobContainer<MachineItemJob> { override fun computeNextJob(id: Int): JobContainer<MachineItemJob> {
return JobContainer.success(MachineItemJob(ItemStack(Items.COBBLESTONE), 40.0)) return JobContainer.success(MachineItemJob(ItemStack(Items.COBBLESTONE), 40.0))
} }

View File

@ -58,7 +58,7 @@ class PlatePressBlockEntity(
return PlatePressMenu(containerID, inventory, this) return PlatePressMenu(containerID, inventory, this)
} }
override fun onJobFinish(job: MachineItemJob): JobStatus { override fun onJobFinish(job: MachineItemJob, id: Int): JobStatus {
if (job.itemStack.isEmpty) if (job.itemStack.isEmpty)
return JobStatus.SUCCESS return JobStatus.SUCCESS
@ -69,7 +69,7 @@ class PlatePressBlockEntity(
return JobStatus.SUCCESS return JobStatus.SUCCESS
} }
override fun computeNextJob(): JobContainer<MachineItemJob> { override fun computeNextJob(id: Int): JobContainer<MachineItemJob> {
if (energy.batteryLevel.isZero) { if (energy.batteryLevel.isZero) {
return JobContainer.noEnergy() return JobContainer.noEnergy()
} }