Beispiel #1
0
        //Detects when the player interacts with left click, usually to remove a component
        public override float OnGettingBroken(IPlayer player, BlockSelection blockSel, ItemSlot itemslot, float remainingResistance, float dt, int counter)
        {
            BlockEntity entity = api.World.BlockAccessor.GetBlockEntity(blockSel.Position);

            entity?.GetBehavior <BEBehaviorCircuitHolder>()?.OnUseOver(player, blockSel, true);
            return(base.OnGettingBroken(player, blockSel, itemslot, remainingResistance, dt, counter));
        }
 public void SetLargeGear(BlockEntity be)
 {
     if (largeGear == null)
     {
         largeGear = be.GetBehavior <BEBehaviorMPLargeGear3m>();
     }
 }
Beispiel #3
0
        private void OnServerTick(float dt)
        {
            if (!lit)
            {
                return;
            }

            if (startingAfterTotalHours <= Api.World.Calendar.TotalHours && state == 0)
            {
                finishedAfterTotalHours = Api.World.Calendar.TotalHours + BurnHours;
                state = 1;
                MarkDirty(false);
            }

            if (state == 0)
            {
                return;
            }


            BlockPos holePos = FindHoleInPit();

            if (holePos != null)
            {
                finishedAfterTotalHours = Api.World.Calendar.TotalHours + BurnHours;

                BlockPos    tmpPos     = new BlockPos();
                BlockFacing firefacing = BlockFacing.UP;

                Block block = Api.World.BlockAccessor.GetBlock(holePos);
                if (block.BlockId != 0 && block.BlockId != Block.BlockId)
                {
                    foreach (BlockFacing facing in BlockFacing.ALLFACES)
                    {
                        tmpPos.Set(holePos).Add(facing);
                        if (Api.World.BlockAccessor.GetBlock(tmpPos).BlockId == 0)
                        {
                            holePos.Set(tmpPos);
                            firefacing = facing;
                            break;
                        }
                    }
                }

                Block fireblock = Api.World.GetBlock(new AssetLocation("fire"));
                Api.World.BlockAccessor.SetBlock(fireblock.BlockId, holePos);

                BlockEntity befire = Api.World.BlockAccessor.GetBlockEntity(holePos);
                befire?.GetBehavior <BEBehaviorBurning>()?.OnFirePlaced(firefacing, startedByPlayerUid);

                return;
            }

            if (finishedAfterTotalHours <= Api.World.Calendar.TotalHours)
            {
                ConvertPit();
            }
        }
        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);
            }
        }
        public bool TrySpreadTo(BlockPos pos)
        {
            // 1. Replaceable test
            var block = Api.World.BlockAccessor.GetBlock(pos);

            if (block.Replaceable < 6000)
            {
                return(false);
            }

            BlockEntity be = Api.World.BlockAccessor.GetBlockEntity(pos);

            if (be?.GetBehavior <BEBehaviorBurning>() != null)
            {
                return(false);
            }

            // 2. fuel test
            bool     hasFuel = false;
            BlockPos npos    = null;

            foreach (BlockFacing firefacing in BlockFacing.ALLFACES)
            {
                npos  = pos.AddCopy(firefacing);
                block = Api.World.BlockAccessor.GetBlock(npos);
                if (canBurn(npos) && Api.World.BlockAccessor.GetBlockEntity(npos)?.GetBehavior <BEBehaviorBurning>() == null)
                {
                    hasFuel = true;
                    break;
                }
            }
            if (!hasFuel)
            {
                return(false);
            }

            // 3. Land claim test
            IPlayer player = Api.World.PlayerByUid(startedByPlayerUid);

            if (player != null && Api.World.Claims.TestAccess(player, pos, EnumBlockAccessFlags.BuildOrBreak) != EnumWorldAccessResponse.Granted)
            {
                return(false);
            }


            Api.World.BlockAccessor.SetBlock(fireBlock.BlockId, pos);

            //Api.World.Logger.Debug(string.Format("Fire @{0}: Spread to {1}.", FirePos, pos));

            BlockEntity befire = Api.World.BlockAccessor.GetBlockEntity(pos);

            befire.GetBehavior <BEBehaviorBurning>()?.OnFirePlaced(pos, npos, startedByPlayerUid);

            return(true);
        }
Beispiel #6
0
        internal void ExchangeBlockAt(IWorldAccessor world, BlockPos pos)
        {
            world.BlockAccessor.ExchangeBlock(BlockId, pos);

            BlockEntity      be   = world.BlockAccessor.GetBlockEntity(pos);
            BEBehaviorMPBase bemp = be.GetBehavior <BEBehaviorMPBase>();

            bemp.Block = this;
            bemp.SetOrientations();
            bemp.Shape = Shape;
        }
        protected void CheckLargeGearJoin()
        {
            string orientations = (Block as BlockAngledGears).Orientation;

            if (orientations.Length == 2 && orientations[0] == orientations[1])
            {
                BlockPos    largeGearPos = this.Position.AddCopy(this.orientation.Opposite);
                BlockEntity be           = this.Api.World?.BlockAccessor.GetBlockEntity(largeGearPos);
                largeGear = be?.GetBehavior <BEBehaviorMPLargeGear3m>();
            }
        }
Beispiel #8
0
        public override Cuboidf[] GetCollisionBoxes(IBlockAccessor blockAccessor, BlockPos pos)
        {
            BlockEntity bec = blockAccessor.GetBlockEntity(pos);

            if (bec != null)
            {
                Cuboidf[] selectionBoxes = bec?.GetBehavior <BEBehaviorCircuitHolder>()?.GetCollisionBoxes(blockAccessor, pos);

                return(selectionBoxes);
            }
            return(base.GetSelectionBoxes(blockAccessor, pos));
        }
Beispiel #9
0
        public override Cuboidf[] GetSelectionBoxes(IBlockAccessor blockAccessor, BlockPos pos)
        {
            ItemStack   holdingItemStack = (api as ICoreClientAPI)?.World?.Player.Entity.RightHandItemSlot.Itemstack?.Clone();
            BlockEntity bec = blockAccessor.GetBlockEntity(pos);

            Cuboidf[] entitySB = bec?.GetBehavior <BEBehaviorCircuitHolder>()?.GetSelectionBoxes(blockAccessor, pos, holdingItemStack);
            if (entitySB == null || entitySB.Length == 0)
            {
                return(base.GetSelectionBoxes(blockAccessor, pos));
            }

            return(entitySB);
        }
Beispiel #10
0
        internal void ExchangeBlockAt(IWorldAccessor world, BlockPos pos)
        {
            world.BlockAccessor.ExchangeBlock(BlockId, pos);

            BlockEntity      be   = world.BlockAccessor.GetBlockEntity(pos);
            BEBehaviorMPBase bemp = be?.GetBehavior <BEBehaviorMPBase>();

            if (bemp != null)
            {
                bemp.SetOrientations();
                bemp.Shape = Shape;
                be.MarkDirty();
            }
        }
        public float LargeGearAngleRad(float unchanged)
        {
            if (largeGear == null)
            {
                BlockPos    largeGearPos = this.Position.AddCopy(this.orientation.Opposite);
                BlockEntity be           = this.Api.World?.BlockAccessor.GetBlockEntity(largeGearPos);
                largeGear = be?.GetBehavior <BEBehaviorMPLargeGear3m>();
                if (largeGear == null)
                {
                    return(unchanged);
                }
            }
            int dir = this.orientation == BlockFacing.SOUTH ? -1 : 1;

            return(dir * largeGear.GetSmallgearAngleRad() % GameMath.TWOPI);
        }
Beispiel #12
0
        public ISignalNodeProvider GetDeviceAt(BlockPos pos)
        {
            BlockEntity be = Api.World.BlockAccessor.GetBlockEntity(pos);

            if (be == null)
            {
                return(null);
            }
            ISignalNodeProvider device = be as ISignalNodeProvider;

            if (device != null)
            {
                return(device);
            }
            return(be.GetBehavior <BEBehaviorSignalNodeProvider>() as ISignalNodeProvider);
        }
        internal float RotationNeighbour(int side, bool allowIndirect)
        {
            BlockPos pos = Position.AddCopy(orients[side]);
            IMechanicalPowerBlock block = Api.World.BlockAccessor.GetBlock(pos) as IMechanicalPowerBlock;

            if (block?.HasMechPowerConnectorAt(Api.World, pos, orients[side].Opposite) != true)
            {
                block = null;
            }
            BlockEntity            be   = block == null ? null : Api.World.BlockAccessor.GetBlockEntity(pos);
            IMechanicalPowerDevice node = be?.GetBehavior <BEBehaviorMPBase>() as IMechanicalPowerDevice;

            if (node is BEBehaviorMPTransmission trans && !trans.engaged)
            {
                node = null;
            }
            float rot;

            if (node == null || node.Network == null)
            {
                if (this.engaged && allowIndirect)
                {
                    rot           = this.RotationNeighbour(1 - side, false);
                    rotPrev[side] = rot;
                }
                else
                {
                    rot = rotPrev[side];
                }
            }
            else
            {
                rot = node.Network.AngleRad * node.GearedRatio;
                bool invert = node.GetPropagationDirection() != orients[side];
                if (side == 1)
                {
                    invert = !invert;
                }
                if (invert)
                {
                    rot = GameMath.TWOPI - rot;
                }
                rotPrev[side] = rot;
            }
            return(rot);
        }
Beispiel #14
0
        public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, ref EnumHandling handling)
        {
            if (blockSel == null || secondsUsed < 3)
            {
                return;
            }

            IPlayer byPlayer = (byEntity as EntityPlayer)?.Player;

            if (!byEntity.World.Claims.TryAccess(byPlayer, blockSel.Position, EnumBlockAccessFlags.Use))
            {
                return;
            }


            EnumHandling handled = EnumHandling.PassThrough;
            Block        block   = byEntity.World.BlockAccessor.GetBlock(blockSel.Position);

            block.OnTryIgniteBlockOver(byEntity, blockSel.Position, secondsUsed, ref handled);

            if (handled != EnumHandling.PassThrough)
            {
                return;
            }

            handling = EnumHandling.PreventDefault;

            if (!byEntity.World.Claims.TryAccess(byPlayer, blockSel.Position, EnumBlockAccessFlags.BuildOrBreak))
            {
                return;
            }

            if (blockSel != null && byEntity.World.Side == EnumAppSide.Server)
            {
                BlockPos bpos = blockSel.Position.AddCopy(blockSel.Face);
                block = byEntity.World.BlockAccessor.GetBlock(bpos);

                if (block.BlockId == 0)
                {
                    byEntity.World.BlockAccessor.SetBlock(byEntity.World.GetBlock(new AssetLocation("fire")).BlockId, bpos);
                    BlockEntity befire = byEntity.World.BlockAccessor.GetBlockEntity(bpos) as BlockEntity;
                    befire?.GetBehavior <BEBehaviorBurning>()?.OnFirePlaced(blockSel.Face, (byEntity as EntityPlayer).PlayerUID);
                }
            }
        }
Beispiel #15
0
        //Detects when the player interacts with right click, usually to place a component
        public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel)
        {
            HangingWiresMod mod = api.ModLoader.GetModSystem <HangingWiresMod>();

            if (mod != null && api.Side == EnumAppSide.Client)
            {
                NodePos pos = GetNodePosForWire(world, blockSel);
                if (pos != null)
                {
                    mod.SetPendingNode(GetNodePosForWire(world, blockSel));
                }
            }

            base.OnBlockInteractStart(world, byPlayer, blockSel);
            if (api.Side == EnumAppSide.Client)
            {
                BlockEntity entity = world.BlockAccessor.GetBlockEntity(blockSel.Position);
                entity?.GetBehavior <BEBehaviorCircuitHolder>()?.OnUseOver(byPlayer, blockSel, false);
            }

            return(true);
        }
Beispiel #16
0
        public static bool Fix(BlockAngledGears bAg, IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref string failureCode)
        {
            Block blockExisting = world.BlockAccessor.GetBlock(blockSel.Position);

            if (!bAg.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 = bAg.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);
                if (beAngledGear != null)
                {
                    beAngledGear.newlyPlaced = true;
                    if (!beAngledGear.tryConnect(firstFace) && secondFace != null)
                    {
                        beAngledGear.tryConnect(secondFace);
                    }
                    beAngledGear.newlyPlaced = false;
                }

                return(true);
            }

            failureCode = "requiresaxle";

            return(false);
        }
        public override bool TryPlaceBlock(IWorldAccessor world, IPlayer byPlayer, ItemStack itemstack, BlockSelection blockSel, ref string failureCode)
        {
            List <BlockPos> smallGears = new List <BlockPos>();

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

            bool ok = base.DoPlaceBlock(world, byPlayer, blockSel, itemstack);

            if (ok)
            {
                int                dx, dz;
                BlockEntity        beOwn       = world.BlockAccessor.GetBlockEntity(blockSel.Position);
                List <BlockFacing> connections = new List <BlockFacing>();

                foreach (var smallGear in smallGears)
                {
                    dx = smallGear.X - blockSel.Position.X;
                    dz = smallGear.Z - blockSel.Position.Z;
                    char orient = 'n';
                    if (dx == 1)
                    {
                        orient = 'e';
                    }
                    else if (dx == -1)
                    {
                        orient = 'w';
                    }
                    else if (dz == 1)
                    {
                        orient = 's';
                    }
                    BlockMPBase toPlaceBlock = world.GetBlock(new AssetLocation("angledgears-" + orient + orient)) as BlockMPBase;
                    BlockFacing bf           = BlockFacing.FromFirstLetter(orient);
                    toPlaceBlock.ExchangeBlockAt(world, smallGear);
                    toPlaceBlock.DidConnectAt(world, smallGear, bf.Opposite);
                    connections.Add(bf);
                    //IGearAcceptor beg = beOwn as IGearAcceptor;
                    //if (beg == null) world.Logger.Error("large gear wrong block entity type - not a gear acceptor");
                    //beg?.AddGear(smallGear);
                }
                PlaceFakeBlocks(world, blockSel.Position, smallGears);

                BEBehaviorMPBase beMechBase = beOwn?.GetBehavior <BEBehaviorMPBase>();
                BlockPos         pos        = blockSel.Position.DownCopy();
                if (world.BlockAccessor.GetBlock(pos) is IMechanicalPowerBlock block && block.HasMechPowerConnectorAt(world, pos, BlockFacing.UP))
                {
                    block.DidConnectAt(world, pos, BlockFacing.UP);
                    connections.Add(BlockFacing.DOWN);
                }
                else
                {
                    pos   = blockSel.Position.UpCopy();
                    block = world.BlockAccessor.GetBlock(pos) as IMechanicalPowerBlock;
                    if (block != null && block.HasMechPowerConnectorAt(world, pos, BlockFacing.DOWN))
                    {
                        block.DidConnectAt(world, pos, BlockFacing.DOWN);
                        connections.Add(BlockFacing.UP);
                    }
                }

                foreach (BlockFacing face in connections)
                {
                    beMechBase?.WasPlaced(face);
                }
            }