diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt index 206d40f..ede82fa 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/cable/CableBlock.kt @@ -23,6 +23,7 @@ import net.minecraft.world.World import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.api.NetworkCable import net.shadowfacts.phycon.api.NetworkComponent +import net.shadowfacts.phycon.init.PhyItems import net.shadowfacts.phycon.util.CableConnection import java.util.* @@ -111,6 +112,38 @@ class CableBlock: Block(Settings.of(CABLE_MATERIAL)), NetworkCable { } } + override fun activate(state: BlockState, world: World, pos: BlockPos, player: PlayerEntity, hand: Hand, hitResult: BlockHitResult): Boolean { + if (player.getStackInHand(hand).item == PhyItems.SCREWDRIVER) { + val hitPos = Vec3d(hitResult.pos.x - pos.x, hitResult.pos.y - pos.y, hitResult.pos.z - pos.z) + val hitConnection = SIDE_SHAPES.entries.firstOrNull { (_, shape) -> + val box = shape.boundingBox + hitPos.x >= box.minX && hitPos.x <= box.maxX && hitPos.y >= box.minY && hitPos.y <= box.maxY && hitPos.z >= box.minZ && hitPos.z <= box.maxZ + } + if (hitConnection != null) { + val side = hitConnection.key + val prop = CONNECTIONS[side] + val newState = when (state[prop]) { + CableConnection.DISABLED -> { + // if the block this cable is connecting to on the side that will be re-enabled is a cable that + // is disabled on the side that connects it to us, we also re-enable that cable to make sure both + // "halves" of the connection are in the same state + val connectedToPos = pos.offset(side) + val connectedTo = world.getBlockState(connectedToPos) + if (connectedTo.block == this && connectedTo[CONNECTIONS[side.opposite]] == CableConnection.DISABLED) { + world.setBlockState(connectedToPos, connectedTo.with(CONNECTIONS[side.opposite], CableConnection.ON)) + } + + state.with(prop, if (connectedTo.block is NetworkComponent) CableConnection.ON else CableConnection.OFF) + } + else -> state.with(prop, CableConnection.DISABLED) + } + world.setBlockState(pos, newState) + } + return true + } + return false + } + @Environment(EnvType.CLIENT) override fun getRenderLayer(): BlockRenderLayer { return BlockRenderLayer.TRANSLUCENT