public static WorldEditBlock CreateNew(Block pBlock, Vector3i pPosition, Vector3i?pSourcePosition)
        {
            WorldEditBlock web = new WorldEditBlock();

            web.Position = pPosition;
            web.Type     = pBlock.GetType();

            var constuctor = web.Type.GetConstructor(Type.EmptyTypes);

            if (constuctor == null)
            {
                if (pBlock is PlantBlock)
                {
                    web.Data = EcoSerializer.Serialize(PlantBlock.GetPlant(pPosition)).ToArray();
                }

                if (pBlock is WorldObjectBlock)
                {
                    WorldObjectBlock wob = (WorldObjectBlock)pBlock;

                    if (wob.WorldObjectHandle.Object.Position3i == pSourcePosition)
                    {
                        web.Data = EcoSerializer.Serialize(wob.WorldObjectHandle.Object).ToArray();
                    }
                }
            }

            return(web);
        }
        private void FindDestinationSignAndTeleport(Player pPlayer, Vector3i pos, Vector3i pDirection)
        {
            if (!hasNeightborIfNeeded(pos))
            {
                pPlayer.SendTemporaryError($"The sign lift only works if there is a { EcoSignLift.Config.GetString("NameOfRequiredItemNextToSign") } next to it.");
                return;
            }

            pos += pDirection * 2;

            while (pos.Y > 0 && pos.Y < EcoSignLift.Config.GetInt("MaximumLiftHeight"))
            {
                pos += pDirection;

                WorldObjectBlock worldObjectBlock = Eco.World.World.GetBlock(pos) as WorldObjectBlock;

                if (worldObjectBlock != null)
                {
                    WorldObject destination = worldObjectBlock.WorldObjectHandle.Object;

                    if (!destination.GetType().ToString().Contains(SIGN_IDENTIFIER))
                    {
                        continue;
                    }

                    if (destination != null)
                    {
                        string destext = destination.GetComponent <CustomTextComponent>().Text;
                        if (destext == null || !destext.ToLower().Contains("lift"))
                        {
                            continue;
                        }

                        Vector3 ppos = pPlayer.Position;

                        Vector3i destvec = new Vector3i((int)Math.Round(ppos.X), (int)Math.Round(destination.Position.Y - 1), (int)Math.Round(ppos.Z));

                        if (RouteManager.WorldBlockIsWalkable(destvec))
                        {
                            pPlayer.SetPosition(destvec);
                            return;
                        }

                        foreach (var neightbor in destvec.XZNeighbors)
                        {
                            if (RouteManager.WorldBlockIsWalkable(neightbor))
                            {
                                pPlayer.SetPosition(neightbor);
                                return;
                            }
                        }

                        pPlayer.SendTemporaryError($"Destination is occupied");
                        return;
                    }
                }
            }

            pPlayer.SendTemporaryError($"No destination found!");
        }
Ejemplo n.º 3
0
        private void FindDestinationSignAndTeleport(Player pPlayer, Vector3i pDirection)
        {
            Vector3i pos = Position3i;

            if (!hasNeightborIfNeeded(pos))
            {
                pPlayer.SendTemporaryError("The sign lift only works if there is a " + NAME_OF_BLOCK + " next to it.");
                return;
            }

            pos += pDirection * 2;

            while (pos.Y > 0 && pos.Y < MAX_HEIGHT)
            {
                pos += pDirection;

                WorldObjectBlock worldObjectBlock = Eco.World.World.GetBlock(pos) as WorldObjectBlock;

                if (worldObjectBlock != null)
                {
                    WoodSignObject destination = worldObjectBlock.WorldObjectHandle.Object as WoodSignObject;

                    if (destination != null)
                    {
                        string destext = destination.GetComponent <CustomTextComponent>().Text;
                        if (destext == null || !destext.ToLower().Contains("lift"))
                        {
                            continue;
                        }

                        Vector3 ppos = pPlayer.Position;

                        Vector3i destvec = new Vector3i((int)Math.Round(ppos.X), (int)Math.Round(destination.Position.Y - 1), (int)Math.Round(ppos.Z));

                        if (RouteManager.WorldBlockIsWalkable(destvec))
                        {
                            pPlayer.SetPosition(destvec);
                            return;
                        }

                        foreach (var neightbor in destvec.XZNeighbors)
                        {
                            if (RouteManager.WorldBlockIsWalkable(neightbor))
                            {
                                pPlayer.SetPosition(neightbor);
                                return;
                            }
                        }

                        pPlayer.SendTemporaryError("Destination is occupied");
                        return;
                    }
                }
            }

            pPlayer.SendTemporaryError("No destination found!");
        }
        public static void SetBlock(Type pType, Vector3i pPosition, UserSession pSession = null, Vector3i?pSourcePosition = null, Block pSourceBlock = null, byte[] pData = null)
        {
            if (pType == null || pType.DerivesFrom <PickupableBlock>())
            {
                pType = typeof(EmptyBlock);
            }

            WorldObjectBlock worldObjectBlock = Eco.World.World.GetBlock(pPosition) as WorldObjectBlock;

            if (worldObjectBlock != null)
            {
                if (worldObjectBlock.WorldObjectHandle.Object.Position3i == pPosition)
                {
                    worldObjectBlock.WorldObjectHandle.Object.Destroy();
                }
            }

            if (pType == typeof(EmptyBlock))
            {
                Eco.World.World.DeleteBlock(pPosition);
                return;
            }

            var constuctor = pType.GetConstructor(Type.EmptyTypes);

            if (constuctor != null)
            {
                Eco.World.World.SetBlock(pType, pPosition);
                return;
            }

            Type[] types = new Type[1];
            types[0] = typeof(WorldPosition3i);

            constuctor = pType.GetConstructor(types);

            if (constuctor != null)
            {
                object obj = null;

                if (pData != null)
                {
                    MemoryStream ms = new MemoryStream(pData);
                    obj = EcoSerializer.Deserialize(ms);
                }

                if (pType.DerivesFrom <PlantBlock>())
                {
                    PlantSpecies ps = null;
                    Plant        pb = null;

                    if (obj != null)
                    {
                        pb = (Plant)obj;
                        ps = pb.Species;
                    }
                    else
                    {
                        ps = WorldUtils.GetPlantSpecies(pType);

                        if (pSourceBlock != null)
                        {
                            pb = PlantBlock.GetPlant(pPosition);
                        }
                    }

                    Plant newplant = EcoSim.PlantSim.SpawnPlant(ps, pPosition);

                    if (pb != null)
                    {
                        newplant.YieldPercent  = pb.YieldPercent;
                        newplant.Dead          = pb.Dead;
                        newplant.DeadType      = pb.DeadType;
                        newplant.GrowthPercent = pb.GrowthPercent;
                        newplant.DeathTime     = pb.DeathTime;
                        newplant.Tended        = pb.Tended;
                    }

                    return;
                }

                AsphaltLog.WriteLine("Unknown Type: " + pType);
            }

            types[0]   = typeof(WorldObject);
            constuctor = pType.GetConstructor(types);

            if (constuctor != null)
            {
                WorldObject wObject = null;

                if (pSourceBlock != null)
                {
                    wObject = ((WorldObjectBlock)pSourceBlock).WorldObjectHandle.Object;

                    //if this is not the "main block" of an object do nothing
                    if (wObject.Position3i != pSourcePosition.Value)
                    {
                        return;
                    }
                }
                else if (pData != null)
                {
                    MemoryStream ms  = new MemoryStream(pData);
                    var          obj = EcoSerializer.Deserialize(ms);

                    if (obj is WorldObject)
                    {
                        wObject = obj as WorldObject;
                    }
                    else
                    {
                        throw new InvalidOperationException("obj is not WorldObjectBlock");
                    }
                }

                if (wObject == null)
                {
                    return;
                }


                //    WorldObject newObject = WorldObjectUtil.Spawn(wObject.GetType().Name, wObject.Creator.User, pPosition);
                //    newObject.Rotation = wObject.Rotation;


                WorldObject newObject = (WorldObject)Activator.CreateInstance(wObject.GetType(), true);
                newObject = WorldObjectManager.Add(newObject, wObject.Creator.User, pPosition, wObject.Rotation);


                {
                    StorageComponent newSC = newObject.GetComponent <StorageComponent>();

                    if (newSC != null)
                    {
                        StorageComponent oldPSC = wObject.GetComponent <StorageComponent>();
                        newSC.Inventory.AddItems(oldPSC.Inventory.Stacks);
                        //    newSC.Inventory.OnChanged.Invoke(null);
                    }
                }

                {
                    CustomTextComponent newTC = newObject.GetComponent <CustomTextComponent>();

                    if (newTC != null)
                    {
                        CustomTextComponent oldTC = wObject.GetComponent <CustomTextComponent>();

                        typeof(CustomTextComponent).GetProperty("Text", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(newTC, oldTC.Text);
                    }
                }

                return;
            }

            AsphaltLog.WriteLine("Unknown Type: " + pType);
        }