package net.shadowfacts.phycon.component import net.minecraft.block.entity.BlockEntity import net.shadowfacts.phycon.packet.RemoteActivationPacket import net.shadowfacts.phycon.util.ActivationMode import net.shadowfacts.phycon.util.ClientConfigurableDevice /** * @author shadowfacts */ class ActivationController( private val sleepInterval: Long, private val device: T ) where T: ActivationController.ActivatableDevice, T: BlockEntity { var activationMode = ActivationMode.AUTOMATIC set(value) { field = value // when the activation mode changes, reset the remote enabled status remotelyEnabled = false } private var lastActivation = -sleepInterval private var remotelyEnabled = false fun tick() { if (activationMode == ActivationMode.AUTOMATIC || remotelyEnabled) { tryActivate() } } fun handleRemoteActivation(packet: RemoteActivationPacket) { if (activationMode != ActivationMode.MANAGED) { return } when (packet.mode) { RemoteActivationPacket.Mode.SINGLE -> tryActivate() RemoteActivationPacket.Mode.ENABLE -> remotelyEnabled = true RemoteActivationPacket.Mode.DISABLE -> remotelyEnabled = false } } private fun tryActivate() { assert(!device.world!!.isClient) if ((device.counter - lastActivation) < sleepInterval) { return } if (device.activate()) { lastActivation = device.counter } } interface ActivatableDevice: ClientConfigurableDevice { val controller: ActivationController<*> val counter: Long fun activate(): Boolean fun canConfigureActivationController(): Boolean { return true } } }