protected override void UpdateLightAndTransformMatrix(int index, Vec3f distToCamera, float rotationRad, IMechanicalPowerRenderable dev)
        {
            BEBehaviorMPAngledGears gear = dev as BEBehaviorMPAngledGears;

            if (gear != null)
            {
                BlockFacing inTurn = gear.GetPropagationDirection();
                if (inTurn == gear.axis1 || inTurn == gear.axis2)
                {
                    rotationRad = -rotationRad;
                }
            }
            float rotX = rotationRad * dev.AxisSign[0];
            float rotY = rotationRad * dev.AxisSign[1];
            float rotZ = rotationRad * dev.AxisSign[2];

            UpdateLightAndTransformMatrix(floatsPeg.Values, index, distToCamera, dev.LightRgba, rotX, rotY, rotZ);// - 0.08f);

            if (dev.AxisSign.Length < 4)
            {
                //System.Diagnostics.Debug.WriteLine("3 length AxisSign");
                return;
            }
            rotX = rotationRad * dev.AxisSign[3];
            rotY = rotationRad * dev.AxisSign[4];
            rotZ = rotationRad * dev.AxisSign[5];
            UpdateLightAndTransformMatrix(floatsCage.Values, index, distToCamera, dev.LightRgba, rotX, rotY, rotZ);
        }
        public override void WasPlaced(IWorldAccessor world, BlockPos ownPos, BlockFacing connectedOnFacing)
        {
            if (connectedOnFacing == null)
            {
                return;
            }
            BEBehaviorMPAngledGears beMechBase = world.BlockAccessor.GetBlockEntity(ownPos)?.GetBehavior <BEBehaviorMPAngledGears>();

            beMechBase?.tryConnect(connectedOnFacing);
        }
        protected override void UpdateLightAndTransformMatrix(int index, Vec3f distToCamera, float rotation, IMechanicalPowerRenderable dev)
        {
            BEBehaviorMPAngledGears gear = dev as BEBehaviorMPAngledGears;

            rotation = gear.LargeGearAngleRad(rotation);

            float rotX = rotation * dev.AxisSign[0];
            float rotY = rotation * dev.AxisSign[1];
            float rotZ = rotation * dev.AxisSign[2];

            UpdateLightAndTransformMatrix(matrixAndLightFloats.Values, index, distToCamera, dev.LightRgba, rotX, rotY, rotZ);
        }
        internal void ToPegGear(IWorldAccessor world, BlockPos pos)
        {
            string orient = Variant["orientation"];

            if (orient.Length == 2 && orient[1] == orient[0])
            {
                Block toPlaceBlock = world.GetBlock(new AssetLocation(FirstCodePart() + "-" + orient[0]));
                ((BlockMPBase)toPlaceBlock).ExchangeBlockAt(world, pos);
                BEBehaviorMPAngledGears beg = world.BlockAccessor.GetBlockEntity(pos).GetBehavior <BEBehaviorMPAngledGears>();
                beg?.ClearLargeGear();

                //#TODO: do a firstface/second face axle check as in TryPlaceBlock()
            }
        }
        public override bool TryPlaceBlock(IWorldAccessor world, IPlayer byPlayer, ItemStack itemstack, BlockSelection blockSel, ref string failureCode)
        {
            Block blockExisting = world.BlockAccessor.GetBlock(blockSel.Position);

            if (!CanPlaceBlock(world, byPlayer, blockSel, ref failureCode, blockExisting))
            {
                return(false);
            }

            BlockFacing           firstFace     = null;
            BlockFacing           secondFace    = null;
            BlockMPMultiblockGear largeGearEdge = blockExisting as BlockMPMultiblockGear;
            bool validLargeGear = false;

            if (largeGearEdge != null)
            {
                BEMPMultiblock be = world.BlockAccessor.GetBlockEntity(blockSel.Position) as BEMPMultiblock;
                if (be != null)
                {
                    validLargeGear = be.Centre != null;
                }
            }

            foreach (BlockFacing face in BlockFacing.ALLFACES)
            {
                if (validLargeGear && (face == BlockFacing.UP || face == BlockFacing.DOWN))
                {
                    continue;
                }
                BlockPos pos = blockSel.Position.AddCopy(face);
                IMechanicalPowerBlock block = world.BlockAccessor.GetBlock(pos) as IMechanicalPowerBlock;
                if (block != null && block.HasMechPowerConnectorAt(world, pos, face.Opposite))
                {
                    if (firstFace == null)
                    {
                        firstFace = face;
                    }
                    else
                    {
                        if (face.IsAdjacent(firstFace))
                        {
                            secondFace = face;
                            break;
                        }
                    }
                }
            }

            if (firstFace != null)
            {
                BlockPos              firstPos  = blockSel.Position.AddCopy(firstFace);
                BlockEntity           be        = world.BlockAccessor.GetBlockEntity(firstPos);
                IMechanicalPowerBlock neighbour = be?.Block as IMechanicalPowerBlock;

                BEBehaviorMPAxle bempaxle = be?.GetBehavior <BEBehaviorMPAxle>();
                if (bempaxle != null && !BEBehaviorMPAxle.IsAttachedToBlock(world.BlockAccessor, neighbour as Block, firstPos))
                {
                    failureCode = "axlemusthavesupport";
                    return(false);
                }

                BlockEntity largeGearBE = validLargeGear ? largeGearEdge.GearPlaced(world, blockSel.Position) : null;

                Block toPlaceBlock = getGearBlock(world, validLargeGear, firstFace, secondFace);
                //world.BlockAccessor.RemoveBlockEntity(blockSel.Position);  //## needed in 1.12, but not with new chunk BlockEntity Dictionary in 1.13
                world.BlockAccessor.SetBlock(toPlaceBlock.BlockId, blockSel.Position);

                if (secondFace != null)
                {
                    BlockPos secondPos = blockSel.Position.AddCopy(secondFace);
                    IMechanicalPowerBlock neighbour2 = world.BlockAccessor.GetBlock(secondPos) as IMechanicalPowerBlock;
                    neighbour2?.DidConnectAt(world, secondPos, secondFace.Opposite);
                }

                BEBehaviorMPAngledGears beAngledGear = world.BlockAccessor.GetBlockEntity(blockSel.Position)?.GetBehavior <BEBehaviorMPAngledGears>();
                if (largeGearBE?.GetBehavior <BEBehaviorMPBase>() is BEBehaviorMPLargeGear3m largeGear)
                {
                    beAngledGear.AddToLargeGearNetwork(largeGear, firstFace);
                }

                //do this last even for the first face so that both neighbours are correctly set
                neighbour?.DidConnectAt(world, firstPos, firstFace.Opposite);
                beAngledGear.newlyPlaced = true;
                if (!beAngledGear.tryConnect(firstFace) && secondFace != null)
                {
                    beAngledGear.tryConnect(secondFace);
                }
                beAngledGear.newlyPlaced = false;

                return(true);
            }

            failureCode = "requiresaxle";

            return(false);
        }