Beispiel #1
0
        public override void OnNeighbourBlockChange(IWorldAccessor world, BlockPos pos, BlockPos neibpos)
        {
            BEBehaviorMPAxle bempaxle = world.BlockAccessor.GetBlockEntity(pos)?.GetBehavior <BEBehaviorMPAxle>();

            if (bempaxle != null && !BEBehaviorMPAxle.IsAttachedToBlock(world.BlockAccessor, bempaxle.Block, pos))
            {
                bool connected = false;
                foreach (BlockFacing face in BlockFacing.ALLFACES)
                {
                    BlockPos npos = pos.AddCopy(face);
                    IMechanicalPowerBlock block = world.BlockAccessor.GetBlock(npos) as IMechanicalPowerBlock;
                    bool prevConnected          = connected;
                    if (block != null && block.HasMechPowerConnectorAt(world, pos, face.Opposite) && world.BlockAccessor.GetBlockEntity(pos)?.GetBehavior <BEBehaviorMPBase>()?.disconnected == false)
                    {
                        connected = true;
                    }
                    BlockAngledGears blockagears = block as BlockAngledGears;
                    if (blockagears == null)
                    {
                        continue;
                    }
                    if (blockagears.Facings.Contains(face.Opposite) && blockagears.Facings.Length == 1)
                    {
                        world.BlockAccessor.BreakBlock(npos, null);
                        connected = prevConnected;  //undo connected = true in this situation
                    }
                }
                if (!connected)
                {
                    world.BlockAccessor.BreakBlock(pos, null);
                }
            }

            base.OnNeighbourBlockChange(world, pos, neibpos);
        }
Beispiel #2
0
        private bool RequiresStand(BlockPos pos, Vec3i vector)
        {
            BlockMPBase block = Api.World.BlockAccessor.GetBlock(pos.X + vector.X, pos.Y + vector.Y, pos.Z + vector.Z) as BlockMPBase;

            if (block == null)
            {
                return(true);
            }
            BlockPos         sidePos = new BlockPos(pos.X + vector.X, pos.Y + vector.Y, pos.Z + vector.Z);
            BEBehaviorMPBase bemp    = Api.World.BlockAccessor.GetBlockEntity(sidePos)?.GetBehavior <BEBehaviorMPBase>();

            if (bemp == null)
            {
                return(true);
            }
            BEBehaviorMPAxle bempaxle = bemp as BEBehaviorMPAxle;

            if (bempaxle == null)
            {
                if (bemp is BEBehaviorMPBrake || bemp is BEBehaviorMPCreativeRotor)
                {
                    BlockFacing side = BlockFacing.FromNormal(vector);
                    if (side != null && block.HasMechPowerConnectorAt(Api.World, sidePos, side.Opposite))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            if (IsAttachedToBlock(Api.World.BlockAccessor, block, sidePos))
            {
                return(false);
            }
            return(bempaxle.RequiresStand(sidePos, vector));
        }
        public override void OnNeighbourBlockChange(IWorldAccessor world, BlockPos pos, BlockPos neibpos)
        {
            string orients = Orientation;

            if (orients.Length == 2 && orients[0] == orients[1])
            {
                orients = "" + orients[0];
            }

            BlockFacing[] facings;
            facings = orients.Length == 1 ? new BlockFacing[] { BlockFacing.FromFirstLetter(orients[0]) } : new BlockFacing[] { BlockFacing.FromFirstLetter(orients[0]), BlockFacing.FromFirstLetter(orients[1]) };

            List <BlockFacing> lostFacings = new List <BlockFacing>();

            foreach (BlockFacing facing in facings)
            {
                BlockPos npos = pos.AddCopy(facing);
                IMechanicalPowerBlock nblock = world.BlockAccessor.GetBlock(npos) as IMechanicalPowerBlock;

                if (nblock == null || !nblock.HasMechPowerConnectorAt(world, npos, facing.Opposite) || world.BlockAccessor.GetBlockEntity(pos)?.GetBehavior <BEBehaviorMPBase>()?.disconnected == true)
                {
                    lostFacings.Add(facing);
                }
            }

            if (lostFacings.Count == orients.Length)
            {
                world.BlockAccessor.BreakBlock(pos, null);
                return;
            }

            if (lostFacings.Count > 0)
            {
                MechanicalNetwork nw = GetNetwork(world, pos);

                orients = orients.Replace("" + lostFacings[0].Code[0], "");
                Block toPlaceBlock = world.GetBlock(new AssetLocation(FirstCodePart() + "-" + orients));
                (toPlaceBlock as BlockMPBase).ExchangeBlockAt(world, pos);
                BlockEntity      be   = world.BlockAccessor.GetBlockEntity(pos);
                BEBehaviorMPBase bemp = be.GetBehavior <BEBehaviorMPBase>();
                bemp.LeaveNetwork();

                //check for connect to adjacent valid facings, similar to TryPlaceBlock
                BlockFacing           firstFace = BlockFacing.FromFirstLetter(orients[0]);
                BlockPos              firstPos  = pos.AddCopy(firstFace);
                BlockEntity           beNeib    = world.BlockAccessor.GetBlockEntity(firstPos);
                IMechanicalPowerBlock neighbour = beNeib?.Block as IMechanicalPowerBlock;

                BEBehaviorMPAxle bempaxle = beNeib?.GetBehavior <BEBehaviorMPAxle>();
                if (bempaxle != null && !BEBehaviorMPAxle.IsAttachedToBlock(world.BlockAccessor, neighbour as Block, firstPos))
                {
                    return;
                }
                neighbour?.DidConnectAt(world, firstPos, firstFace.Opposite);
                WasPlaced(world, pos, firstFace);
            }
        }
Beispiel #4
0
        private bool RequiresStand(BlockPos pos, Vec3i vector)
        {
            try
            {
                BlockMPBase block = Api.World.BlockAccessor.GetBlock(pos.X + vector.X, pos.Y + vector.Y, pos.Z + vector.Z) as BlockMPBase;
                if (block == null)
                {
                    return(true);
                }

                BlockPos         sidePos = new BlockPos(pos.X + vector.X, pos.Y + vector.Y, pos.Z + vector.Z);
                BEBehaviorMPBase bemp    = Api.World.BlockAccessor.GetBlockEntity(sidePos)?.GetBehavior <BEBehaviorMPBase>();
                if (bemp == null)
                {
                    return(true);
                }

                BEBehaviorMPAxle bempaxle = bemp as BEBehaviorMPAxle;
                if (bempaxle == null)
                {
                    if (bemp is BEBehaviorMPBrake || bemp is BEBehaviorMPCreativeRotor)
                    {
                        BlockFacing side = BlockFacing.FromNormal(vector);
                        if (side != null && block.HasMechPowerConnectorAt(Api.World, sidePos, side.Opposite))
                        {
                            return(false);
                        }
                    }
                    return(true);
                }

                if (IsAttachedToBlock(Api.World.BlockAccessor, block, sidePos))
                {
                    return(false);
                }
                return(bempaxle.RequiresStand(sidePos, vector));
            }
            catch (Exception e)
            {
#if DEBUG
                throw (e);
#else
                Api.Logger.Error("Exception thrown in RequiresStand, will log exception but silently ignore it");
                Api.Logger.Error("{0}", e);
                return(false);
#endif
            }
        }
Beispiel #5
0
        public override void OnNeighbourBlockChange(IWorldAccessor world, BlockPos pos, BlockPos neibpos)
        {
            BEBehaviorMPAxle bempaxle = world.BlockAccessor.GetBlockEntity(pos)?.GetBehavior <BEBehaviorMPAxle>();

            if (bempaxle != null && !BEBehaviorMPAxle.IsAttachedToBlock(world.BlockAccessor, bempaxle.Block, pos))
            {
                foreach (BlockFacing face in BlockFacing.HORIZONTALS)
                {
                    BlockPos         npos        = pos.AddCopy(face);
                    BlockAngledGears blockagears = world.BlockAccessor.GetBlock(npos) as BlockAngledGears;
                    if (blockagears == null)
                    {
                        continue;
                    }
                    if (blockagears.Facings.Contains(face.Opposite) && blockagears.Facings.Length == 1)
                    {
                        world.BlockAccessor.BreakBlock(npos, null);
                    }
                }
            }

            base.OnNeighbourBlockChange(world, pos, neibpos);
        }
        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);
        }