diff --git a/src/main/kotlin/org/neoflock/neocomputers/entity/CaseBlockEntity.kt b/src/main/kotlin/org/neoflock/neocomputers/entity/CaseBlockEntity.kt index 6ce9ba5..4f145b5 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/entity/CaseBlockEntity.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/entity/CaseBlockEntity.kt @@ -31,13 +31,15 @@ import org.neoflock.neocomputers.sounds.ComputerRunningSoundInstance import org.neoflock.neocomputers.sounds.Sounds import org.neoflock.neocomputers.utils.GenericContainer import java.time.Duration +import kotlin.math.max import kotlin.math.min class CaseBlockEntity(blockPos: BlockPos, blockState: BlockState): NodeBlockEntity(BlockEntities.CASE_ENTITY.get(), blockPos, blockState), MachineEntity, GenericContainer, MenuProvider { val stacks: NonNullList = NonNullList.withSize(7, ItemStack.EMPTY) var isOn = false - var isDisking = false // TOOD: writing writers and reading readers + var diskActivityTime = 0 // TOOD: writing writers and reading readers + var networkActivityTime = 0 var err: String? = null var arch = "Lua 5.3" var soundInstance: SoundInstance? = null @@ -50,14 +52,16 @@ class CaseBlockEntity(blockPos: BlockPos, blockState: BlockState): NodeBlockEnti override fun encodeDownstreamData(packet: FriendlyByteBuf) { super.encodeDownstreamData(packet) packet.writeBoolean(isOn) - packet.writeBoolean(isDisking) + packet.writeVarInt(diskActivityTime) + packet.writeVarInt(networkActivityTime) packet.writeUtf(err ?: "") } override fun syncWithUpstream(packet: FriendlyByteBuf) { super.syncWithUpstream(packet) setRunning(packet.readBoolean()) - isDisking = packet.readBoolean() + diskActivityTime = packet.readVarInt() + networkActivityTime = packet.readVarInt() err = packet.readUtf().ifEmpty { null } } @@ -219,6 +223,14 @@ class CaseBlockEntity(blockPos: BlockPos, blockState: BlockState): NodeBlockEnti return true } + override fun signalDiskActivity(delay: Int) { + diskActivityTime = max(delay, diskActivityTime) + } + + override fun signalNetworkActivity(delay: Int) { + networkActivityTime = max(delay, networkActivityTime) + } + override fun getMachineMemoryTotal(): Long = stacks.mapNotNull { (it.item as? ComponentItem)?.getMemoryCapacity(it) }.sum().toLong() override fun getMachineMemoryUsed(): Long = 0 override fun getMachineComponentsUsed(): Long = node.getReachable().size.toLong() @@ -264,6 +276,8 @@ class CaseBlockEntity(blockPos: BlockPos, blockState: BlockState): NodeBlockEnti super.tickNode(level) if(!level.isClientSide) { if (isRunning()) { + if(diskActivityTime > 0) diskActivityTime-- + if(networkActivityTime > 0) networkActivityTime-- if(getMachineComponentsUsed() > getMachineComponentsTotal()) { crash("too many components") } diff --git a/src/main/kotlin/org/neoflock/neocomputers/entity/MachineEntity.kt b/src/main/kotlin/org/neoflock/neocomputers/entity/MachineEntity.kt index 9fbf922..87a54e4 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/entity/MachineEntity.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/entity/MachineEntity.kt @@ -28,6 +28,10 @@ interface MachineEntity { // the duration is doubled. // Architectures should only use short ones. fun beepAsync(pattern: String, frequency: Int = 1000, duration: Duration = Duration.ofMillis(200), volume: Double = 1.0): Boolean + // Signals that disk activity is happening for at least that delay + fun signalDiskActivity(delay: Int) + // signal network activity to a machine + fun signalNetworkActivity(delay: Int) fun isRunning(): Boolean fun start(): Boolean diff --git a/src/main/kotlin/org/neoflock/neocomputers/gui/render/CaseEntityRenderer.kt b/src/main/kotlin/org/neoflock/neocomputers/gui/render/CaseEntityRenderer.kt index 42a224e..6d5ba24 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/gui/render/CaseEntityRenderer.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/gui/render/CaseEntityRenderer.kt @@ -42,7 +42,7 @@ class CaseEntityRenderer(private val context: BlockEntityRendererProvider.Contex } else drawLED(buffer, mat.last(), 3F, OFF) mat.translate(6/16F, 0F, 0F) - drawLED(buffer, mat.last(), 2F, if (ent.isDisking) GREEN else OFF) + drawLED(buffer, mat.last(), 2F, if (ent.diskActivityTime > 0) GREEN else OFF) mat.popPose()