diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/Cables.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/Cables.kt index 9301f3efc..670dcbc81 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/Cables.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/Cables.kt @@ -11,25 +11,38 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.graph.matter.MatterNode import ru.dbotthepony.mc.otm.graph.storage.StorageGraph import ru.dbotthepony.mc.otm.graph.storage.StorageNode +import ru.dbotthepony.mc.otm.once import ru.dbotthepony.mc.otm.registry.MBlockEntities class MatterCableBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : MatteryBlockEntity(MBlockEntities.MATTER_CABLE, p_155229_, p_155230_) { val matterNode = object : MatterNode() { override fun onNeighbour(link: Link) { if (link is DirectionLink) { - val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[link.direction]!!, true) + if (!SERVER_IS_LIVE) return + val level = level - if (newState !== blockState && SERVER_IS_LIVE) - level?.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + level?.once { + if (!isValid) return@once + val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[link.direction]!!, true) + + if (newState !== blockState && SERVER_IS_LIVE) + level.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + } } } override fun onUnNeighbour(link: Link) { if (link is DirectionLink) { - val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[link.direction]!!, false) + if (!SERVER_IS_LIVE) return + val level = level - if (newState !== blockState && SERVER_IS_LIVE) - level?.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + level?.once { + if (!isValid) return@once + val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[link.direction]!!, false) + + if (newState !== blockState && SERVER_IS_LIVE) + level.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + } } } } @@ -56,19 +69,29 @@ class StorageCableBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Matt override fun onNeighbour(link: Link) { if (link is DirectionLink) { - val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[link.direction]!!, true) + if (!SERVER_IS_LIVE) return + val level = level - if (newState !== blockState && SERVER_IS_LIVE) - level!!.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + level?.once { + val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[link.direction]!!, true) + + if (newState !== blockState && SERVER_IS_LIVE) + level.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + } } } override fun onUnNeighbour(link: Link) { if (link is DirectionLink) { - val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[link.direction]!!, false) + if (!SERVER_IS_LIVE) return + val level = level - if (newState !== blockState && SERVER_IS_LIVE) - level!!.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + level?.once { + val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[link.direction]!!, false) + + if (newState !== blockState && SERVER_IS_LIVE) + level.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + } } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableBlockEntity.kt index e9ce7b301..26817675e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableBlockEntity.kt @@ -19,6 +19,7 @@ import ru.dbotthepony.mc.otm.core.math.BlockRotation import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.math.RelativeSide import ru.dbotthepony.mc.otm.graph.GraphNode +import ru.dbotthepony.mc.otm.once import ru.dbotthepony.mc.otm.onceServer import java.util.Collections import java.util.EnumMap @@ -110,10 +111,16 @@ abstract class EnergyCableBlockEntity(type: BlockEntityType<*>, blockPos: BlockP } private fun updateBlockState(side: Direction, status: Boolean) { - val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[side]!!, status) + if (!SERVER_IS_LIVE) return + val level = level - if (newState !== blockState && SERVER_IS_LIVE) - level?.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + level?.once { + if (!node.isValid) return@once + val newState = blockState.setValue(CableBlock.MAPPING_CONNECTION_PROP[side]!!, status) + + if (newState !== blockState && SERVER_IS_LIVE) + level.setBlock(blockPos, newState, Block.UPDATE_CLIENTS) + } } // whenever this changes, graph#invalidatePathCache() MUST be called diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/GraphNode.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/GraphNode.kt index e6b2b754e..86d5f126e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/GraphNode.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/GraphNode.kt @@ -127,8 +127,8 @@ open class GraphNode, G : GraphNodeList>(val graphFact protected open fun onNeighbour(link: Link) {} protected open fun onUnNeighbour(link: Link) {} - protected open fun invalidate() {} - protected open fun revive() {} + protected open fun onInvalidated() {} + protected open fun onRevived() {} var isValid: Boolean = true set(value) { @@ -144,9 +144,9 @@ open class GraphNode, G : GraphNodeList>(val graphFact graph.removeNode(this as N) rebuildGraphs(neighbours.map { it.second }) - invalidate() + onInvalidated() } else { - revive() + onRevived() } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/storage/StorageNode.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/storage/StorageNode.kt index a176a192c..441b851c3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/storage/StorageNode.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/storage/StorageNode.kt @@ -6,12 +6,6 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.capability.energy.IMatteryEnergyStorage import ru.dbotthepony.mc.otm.graph.GraphNode import ru.dbotthepony.mc.otm.storage.IStorage -import ru.dbotthepony.mc.otm.storage.IVirtualStorageComponent -import ru.dbotthepony.mc.otm.storage.StorageStack -import ru.dbotthepony.mc.otm.storage.VirtualComponent -import ru.dbotthepony.mc.otm.storage.powered.PoweredVirtualComponent -import java.util.* -import java.util.function.Supplier open class StorageNode(private val energyDemander: IMatteryEnergyStorage? = null) : GraphNode(::StorageGraph) { protected val components = ObjectArraySet>() @@ -19,9 +13,9 @@ open class StorageNode(private val energyDemander: IMatteryEnergyStorage? = null /** * Setting this to true will render non functional default [attachComponents], * make [addStorageComponent] and [removeStorageComponent] not add/remove components - * to attached graph, and [revive] not re-attach components. + * to attached graph, and [onRevived] not re-attach components. * - * [invalidate] and [removeComponents] still detach all components + * [onInvalidated] and [removeComponents] still detach all components */ protected var manualAttaching = false private var demandingEnergy = false @@ -103,13 +97,13 @@ open class StorageNode(private val energyDemander: IMatteryEnergyStorage? = null } } - override fun invalidate() { + override fun onInvalidated() { for (component in components) { graph.remove(component) } } - override fun revive() { + override fun onRevived() { if (!manualAttaching) { for (component in components) { graph.add(component)