Prevent update spam in tank and resource dataslots when they don't actually change (bdew/pressure#34)

This commit is contained in:
bdew committed 2015-02-26 14:29:04 +02:00
1 parent 64c0484c2d
commit 1003aa1d6a
3 files changed
+13 -4

No files matched your search

+4 -2
View File
@@ -50,17 +50,19 @@ abstract class DataSlotTankBase(sz: Int) extends FluidTank(sz) with IFluidTank w
if (sendCapacityOnUpdateKind.contains(k))
setCapacity(tag.getInteger("size"))
}
override def setFluid(fluid: FluidStack) = execWithChangeNotify(super.setFluid(fluid))
override def setCapacity(capacity: Int) = execWithChangeNotify(super.setCapacity(capacity))
override def drain(maxDrain: Int, doDrain: Boolean) =
if (doDrain)
execWithChangeNotify(super.drain(maxDrain, true))
execWithChangeNotifyConditional[FluidStack](super.drain(maxDrain, true), _ != null)
else
super.drain(maxDrain, false)
override def fill(resource: FluidStack, doFill: Boolean) =
if (doFill)
execWithChangeNotify(super.fill(resource, true))
execWithChangeNotifyConditional[Int](super.fill(resource, true), _ > 0)
else
super.fill(resource, false)
}
@@ -41,6 +41,13 @@ trait DataSlot {
v
}
def execWithChangeNotifyConditional[T](f: => T, condition: T => Boolean): T = {
val v = f
if (condition(v))
parent.dataSlotChanged(this)
v
}
def setUpdate(vals: UpdateKind.Value*): this.type = {
updateKind = vals.toSet
return this
@@ -36,7 +36,7 @@ class DataSlotResource(val name: String, val parent: TileDataSlots, initCapacity
val current = if (resource.isEmpty) 0D else resource.get.amount
var canFill = Misc.clamp(res.amount, 0D, capacity * res.kind.capacityMultiplier - current)
if (onlyRound) canFill = canFill.floor
if (doFill) execWithChangeNotify {
if (doFill && canFill > 0) execWithChangeNotify {
resource = Some(Resource(res.kind, current + canFill))
}
canFill
@@ -54,7 +54,7 @@ class DataSlotResource(val name: String, val parent: TileDataSlots, initCapacity
resource map { res =>
var canDrain = Misc.clamp(res.amount, 0D, maxDrain)
if (onlyRound) canDrain = canDrain.floor
if (doDrain) execWithChangeNotify {
if (doDrain && canDrain > 0) execWithChangeNotify {
resource =
if (res.amount - canDrain < 0.000001)
None