From 9ea3727dfb863e2d27fac8611c232d6764fabaab Mon Sep 17 00:00:00 2001 From: ionut Date: Thu, 30 Apr 2026 17:47:30 +0300 Subject: [PATCH] cables are fully cabling now --- .../neoflock/neocomputers/block/CableBlock.kt | 4 ++++ .../neoflock/neocomputers/block/DeviceBlock.kt | 17 +++++++++++++++-- .../neoflock/neocomputers/network/DeviceNode.kt | 16 +++++++++------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/org/neoflock/neocomputers/block/CableBlock.kt b/src/main/kotlin/org/neoflock/neocomputers/block/CableBlock.kt index 83f0a5e..4fde7ad 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/block/CableBlock.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/block/CableBlock.kt @@ -165,6 +165,10 @@ class CableBlock() : DeviceBlock(Properties.of()), EntityBlock { if (stack.item is DyeItem) { val dyeitem = stack.item as DyeItem level.setBlockAndUpdate(pos, state.setValue(COLOR, dyeitem.dyeColor)) + val ent = level.getBlockEntity(pos) + if(ent is CableEntity) { + ent.connectionsAreDirty = true + } return ItemInteractionResult.SUCCESS } return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION diff --git a/src/main/kotlin/org/neoflock/neocomputers/block/DeviceBlock.kt b/src/main/kotlin/org/neoflock/neocomputers/block/DeviceBlock.kt index a651276..098dae9 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/block/DeviceBlock.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/block/DeviceBlock.kt @@ -22,6 +22,7 @@ import net.minecraft.world.level.block.entity.BlockEntityTicker import net.minecraft.world.level.block.entity.BlockEntityType import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.phys.BlockHitResult +import org.neoflock.neocomputers.NeoComputers import org.neoflock.neocomputers.network.DeviceNode import org.neoflock.neocomputers.network.Networking import org.neoflock.neocomputers.network.NodeSynchronizer @@ -35,6 +36,7 @@ abstract class SingleDeviceBlockEntity(type: BlockEntityType<*>, pos: BlockPos, abstract class DeviceBlockEntity(type: BlockEntityType<*>, pos: BlockPos, state: BlockState): BlockEntity(type, pos, state) { val connetionsInDir = MutableList(Direction.entries.size) { null } + val nodesInDir = MutableList(Direction.entries.size) { null } var alreadySetup = false var receivedServerState = false var connectionsAreDirty = true @@ -77,11 +79,22 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>, pos: BlockPos, state: open fun handleConnectionsFor(direction: Direction) { // refuse connections on no node to reduce CPU load - val node = getNodeFromSide(direction) ?: return + val node = getNodeFromSide(direction) + val oldNode = nodesInDir[direction.ordinal] + nodesInDir[direction.ordinal] = node val old = connetionsInDir[direction.ordinal] val now = getCurrentlyConnectedNodeIn(direction) + if(node == null) { + if(old != null) oldNode?.disconnectFrom(old) + if(now != null) oldNode?.disconnectFrom(now) + return + } + if(oldNode != null && oldNode.address != node.address) { + if(old != null) oldNode.disconnectFrom(old) + if(now != null) oldNode.disconnectFrom(now) + } - if(old?.address != now?.address) { + if(old?.address != now?.address || oldNode == null) { if(old != null) node.disconnectFrom(old) if(now != null) node.connectTo(now) } diff --git a/src/main/kotlin/org/neoflock/neocomputers/network/DeviceNode.kt b/src/main/kotlin/org/neoflock/neocomputers/network/DeviceNode.kt index a6a90f6..a37a6e1 100644 --- a/src/main/kotlin/org/neoflock/neocomputers/network/DeviceNode.kt +++ b/src/main/kotlin/org/neoflock/neocomputers/network/DeviceNode.kt @@ -209,16 +209,18 @@ open class DeviceNode(_address: UUID? = null) { } fun directConnectTo(other: DeviceNode) { - if(other == this) return; - if(other in connections) return; - connections.add(other); - this.onConnect(other); + if(other == this) return + if(other in connections) return + connections.add(other) + this.onConnect(other) + invalidateReachableCache() } fun directDisconnectFrom(other: DeviceNode) { - if(other !in connections) return; - connections.remove(other); - this.onDisconnect(other); + if(other !in connections) return + connections.remove(other) + this.onDisconnect(other) + invalidateReachableCache() } // Network synchronization with the NodeSynchronizer