public static WorldEditWorldObjectBlockData From(WorldObject worldObject)
        {
            WorldEditWorldObjectBlockData worldObjectData = new WorldEditWorldObjectBlockData();

            worldObjectData.WorldObjectType = worldObject.GetType();
            worldObjectData.Rotation        = worldObject.Rotation;

            worldObjectData.Components = new Dictionary <Type, Object>();
            if (worldObject.HasComponent <StorageComponent>())
            {
                StorageComponent      storageComponent = worldObject.GetComponent <StorageComponent>();
                List <InventoryStack> inventoryStacks  = new List <InventoryStack>();

                foreach (ItemStack stack in storageComponent.Inventory.Stacks)
                {
                    if (stack.Empty())
                    {
                        continue;
                    }
                    inventoryStacks.Add(InventoryStack.Create(stack));
                }

                worldObjectData.Components.Add(typeof(StorageComponent), inventoryStacks);
            }

            if (worldObject.HasComponent <CustomTextComponent>())
            {
                CustomTextComponent textComponent = worldObject.GetComponent <CustomTextComponent>();
                worldObjectData.Components.Add(typeof(CustomTextComponent), textComponent.TextData.Text);
            }

            return(worldObjectData);
        }
Пример #2
0
 protected override void Initialize()
 {
     timeStampMillis = (DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
     doInit          = true;
     GetComponent <PropertyAuthComponent>().Initialize();
     textComp = GetComponent <CustomTextComponent>();
     GetComponent <PowerConsumptionComponent>().Initialize(0);
     GetComponent <PowerGridComponent>().Initialize(10, new ElectricPower());
 }
Пример #3
0
        public static bool Prefix(ref Player player, string text, ref CustomTextComponent __instance)
        {
            WorldObjectChangeTextEvent imie = new WorldObjectChangeTextEvent(player, __instance.Parent, text);
            IEvent imieEvent = imie;

            EventManager.CallEvent(ref imieEvent);

            return(true);
        }
Пример #4
0
        public static void RestoreWorldObjectBlock(Type type, Vector3i position, IWorldEditBlockData blockData, UserSession session)
        {
            if (blockData == null)
            {
                return;
            }
            WorldEditWorldObjectBlockData worldObjectBlockData = (WorldEditWorldObjectBlockData)blockData;

            ClearWorldObjectPlace(worldObjectBlockData.WorldObjectType, position, worldObjectBlockData.Rotation, session);

            WorldObject worldObject = null;

            try { worldObject = WorldObjectManager.ForceAdd(worldObjectBlockData.WorldObjectType, session.User, position, worldObjectBlockData.Rotation, true); }
            catch (Exception e)
            {
                Log.WriteException(e);
            }
            if (worldObject == null)
            {
                Log.WriteErrorLineLoc($"Unable spawn WorldObject {worldObjectBlockData.WorldObjectType} at {position}");
                return;
            }
            if (worldObject.HasComponent <StorageComponent>() && worldObjectBlockData.Components.ContainsKey(typeof(StorageComponent)))
            {
                StorageComponent      storageComponent = worldObject.GetComponent <StorageComponent>();
                List <InventoryStack> inventoryStacks;
                object component = worldObjectBlockData.Components[typeof(StorageComponent)];
                if (component is JArray)
                {
                    JArray jArray = (JArray)component;
                    inventoryStacks = jArray.ToObject <List <InventoryStack> >();
                }
                else
                {
                    inventoryStacks = (List <InventoryStack>)component;
                }

                foreach (InventoryStack stack in inventoryStacks)
                {
                    if (stack.ItemType == null)
                    {
                        continue;
                    }
                    Result result = storageComponent.Inventory.TryAddItems(stack.ItemType, stack.Quantity);
                    if (result.Failed)
                    {
                        session.Player.ErrorLocStr(result.Message.Trim());
                        try { storageComponent.Inventory.AddItems(stack.GetItemStack()); } catch (InvalidOperationException) { /*Already show error to user*/ }
                    }
                }
            }
            if (worldObject.HasComponent <CustomTextComponent>() && worldObjectBlockData.Components.ContainsKey(typeof(CustomTextComponent)))
            {
                CustomTextComponent textComponent = worldObject.GetComponent <CustomTextComponent>();
                textComponent.TextData.Text = (string)worldObjectBlockData.Components[typeof(CustomTextComponent)];
            }
        }
Пример #5
0
 public static void UpdateDynamicText(this CustomTextComponent comp)
 {
     if (comp.Text.Contains("[TIME]"))
     {
         comp.SetText(comp.Parent.OwnerUser.Player, "<size=0>[TIME]</size>" + DateTime.Now.ToString("hh:mm:ss"));
     }
     else if (comp.Text.Contains("[HOUSERANK]"))
     {
         comp.SetText(comp.Parent.OwnerUser.Player, "<size=0>[HOUSERANK]</size>" + MiscUtils.GetHouseRanking(10));
     }
 }
        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);
        }