Fix issues with energy counter history

This commit is contained in:
DBotThePony 2022-01-26 18:21:34 +07:00
parent a859084b6f
commit 154e3679df
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -55,6 +55,7 @@ data class EnergyCounterPacket(val pos: BlockPos, val thisTick: ImpreciseFractio
tile.lastTick = thisTick tile.lastTick = thisTick
tile.passed = total tile.passed = total
tile[index] = value tile[index] = value
tile.historyTick = index
} }
} }
@ -75,7 +76,8 @@ class BlockEntityEnergyCounter(p_155229_: BlockPos, p_155230_: BlockState) : Blo
internal set internal set
private val history = Array(10 * 20) { ImpreciseFraction.ZERO } private val history = Array(10 * 20) { ImpreciseFraction.ZERO }
private var historyTick = 0 var historyTick = 0
internal set
fun size() = history.size fun size() = history.size
operator fun get(i: Int) = history[i] operator fun get(i: Int) = history[i]
@ -93,7 +95,7 @@ class BlockEntityEnergyCounter(p_155229_: BlockPos, p_155230_: BlockState) : Blo
for (i in 0 until ticks) { for (i in 0 until ticks) {
var index = (historyTick - i) % this.history.size var index = (historyTick - i) % this.history.size
if (index < 0) index += this.history.size if (index < 0) index = this.history.size - 1
history[i] = this.history[index] history[i] = this.history[index]
} }
@ -111,7 +113,7 @@ class BlockEntityEnergyCounter(p_155229_: BlockPos, p_155230_: BlockState) : Blo
for (i in 0 until ticks) { for (i in 0 until ticks) {
var index = (historyTick - i) % history.size var index = (historyTick - i) % history.size
if (index < 0) index += history.size if (index < 0) index = history.size - 1
value += history[index] value += history[index]
} }
@ -422,8 +424,8 @@ class BlockEntityEnergyCounter(p_155229_: BlockPos, p_155230_: BlockState) : Blo
fun tick() { fun tick() {
lastTick = history[historyTick] lastTick = history[historyTick]
val index = historyTick val index = historyTick
historyTick = (historyTick + 1) % history.size
val accumulated = history[historyTick] val accumulated = history[historyTick]
historyTick = (historyTick + 1) % history.size
history[historyTick] = ImpreciseFraction.ZERO history[historyTick] = ImpreciseFraction.ZERO
MatteryNetworking.CHANNEL.send(PacketDistributor.NEAR.with(this::distributor), EnergyCounterPacket(blockPos, lastTick, passed, index, accumulated)) MatteryNetworking.CHANNEL.send(PacketDistributor.NEAR.with(this::distributor), EnergyCounterPacket(blockPos, lastTick, passed, index, accumulated))