コード例 #1
0
        public void AddItem(GameObject item, NitroxId containerId)
        {
            Optional <GameObject> owner = NitroxEntity.GetObjectFrom(containerId);

            if (!owner.HasValue)
            {
                Log.Error($"Unable to find inventory container with id {containerId} for {item.name}");
                return;
            }
            Optional <ItemsContainer> opContainer = InventoryContainerHelper.TryGetContainerByOwner(owner.Value);

            if (!opContainer.HasValue)
            {
                Log.Error($"Could not find container field on GameObject {owner.Value.GetHierarchyPath()}");
                return;
            }

            ItemsContainer container  = opContainer.Value;
            Pickupable     pickupable = item.RequireComponent <Pickupable>();

            using (packetSender.Suppress <ItemContainerAdd>())
            {
                container.UnsafeAdd(new InventoryItem(pickupable));
                Log.Debug($"Received: Added item {pickupable.GetTechType()} to container {owner.Value.GetHierarchyPath()}");
            }
        }
コード例 #2
0
        public void BroadcastItemAdd(Pickupable pickupable, Transform containerTransform)
        {
            NitroxId itemId = NitroxEntity.GetId(pickupable.gameObject);

            byte[]   bytes   = SerializationHelper.GetBytesWithoutParent(pickupable.gameObject);
            NitroxId ownerId = InventoryContainerHelper.GetOwnerId(containerTransform);

            ItemData  itemData;
            Plantable plant = pickupable.GetComponent <Plantable>();

            if (plant && plant.currentPlanter)
            {
                // special case: we want to remember the time when the plant was added, so we can simulate growth
                itemData = new PlantableItemData(ownerId, itemId, bytes, DayNightCycle.main.timePassedAsDouble);
            }
            else
            {
                itemData = new ItemData(ownerId, itemId, bytes);
            }

            if (packetSender.Send(new ItemContainerAdd(itemData)))
            {
                Log.Debug($"Sent: Added item {pickupable.GetTechType()} to container {containerTransform.gameObject.GetHierarchyPath()}");
            }
        }
コード例 #3
0
ファイル: ItemContainers.cs プロジェクト: veter12/Nitrox
        public void AddItem(ItemData itemData)
        {
            Optional <GameObject> owner = GuidHelper.GetObjectFrom(itemData.ContainerGuid);

            if (owner.IsEmpty())
            {
                Log.Info("Unable to find inventory container with id: " + itemData.ContainerGuid);
                return;
            }

            Optional <ItemsContainer> opContainer = InventoryContainerHelper.GetBasedOnOwnersType(owner.Get());

            if (opContainer.IsPresent())
            {
                ItemsContainer container  = opContainer.Get();
                GameObject     item       = SerializationHelper.GetGameObject(itemData.SerializedData);
                Pickupable     pickupable = item.RequireComponent <Pickupable>();

                using (packetSender.Suppress <ItemContainerAdd>())
                {
                    container.UnsafeAdd(new InventoryItem(pickupable));
                }
            }
            else
            {
                Log.Error("Could not find container field on object " + owner.Get().name);
            }
        }
コード例 #4
0
ファイル: ItemContainers.cs プロジェクト: nallston/Nitrox
        public void AddItem(GameObject item, NitroxId containerId)
        {
            Optional <GameObject> owner = NitroxEntity.GetObjectFrom(containerId);

            if (!owner.HasValue)
            {
                Log.Info("Unable to find inventory container with id: " + containerId);
                return;
            }
            Optional <ItemsContainer> opContainer = InventoryContainerHelper.GetBasedOnOwnersType(owner.Value);

            if (!opContainer.HasValue)
            {
                Log.Error("Could not find container field on object " + owner.Value.name);
                return;
            }

            ItemsContainer container  = opContainer.Value;
            Pickupable     pickupable = item.RequireComponent <Pickupable>();

            using (packetSender.Suppress <ItemContainerAdd>())
            {
                container.UnsafeAdd(new InventoryItem(pickupable));
            }
        }
コード例 #5
0
        public void BroadcastItemRemoval(Pickupable pickupable, Transform containerTransform)
        {
            NitroxId itemId = NitroxEntity.GetId(pickupable.gameObject);

            if (packetSender.Send(new ItemContainerRemove(InventoryContainerHelper.GetOwnerId(containerTransform), itemId)))
            {
                Log.Debug($"Sent: Removed item {pickupable.GetTechType()} from container {containerTransform.gameObject.GetHierarchyPath()}");
            }
        }
コード例 #6
0
ファイル: ItemContainers.cs プロジェクト: nallston/Nitrox
        public void RemoveItem(NitroxId ownerId, NitroxId itemId)
        {
            GameObject owner = NitroxEntity.RequireObjectFrom(ownerId);
            GameObject item  = NitroxEntity.RequireObjectFrom(itemId);
            Optional <ItemsContainer> opContainer = InventoryContainerHelper.GetBasedOnOwnersType(owner);

            if (!opContainer.HasValue)
            {
                Log.Error("Could not find container field on object " + owner.name);
                return;
            }

            ItemsContainer container  = opContainer.Value;
            Pickupable     pickupable = item.RequireComponent <Pickupable>();

            using (packetSender.Suppress <ItemContainerRemove>())
            {
                container.RemoveItem(pickupable, true);
            }
        }
コード例 #7
0
        public void RemoveItem(NitroxId ownerId, NitroxId itemId)
        {
            GameObject owner = NitroxEntity.RequireObjectFrom(ownerId);
            GameObject item  = NitroxEntity.RequireObjectFrom(itemId);
            Optional <ItemsContainer> opContainer = InventoryContainerHelper.TryGetContainerByOwner(owner);

            if (!opContainer.HasValue)
            {
                Log.Error($"Could not find item container behaviour on object {owner.GetHierarchyPath()} with id {ownerId}");
                return;
            }

            ItemsContainer container  = opContainer.Value;
            Pickupable     pickupable = item.RequireComponent <Pickupable>();

            using (packetSender.Suppress <ItemContainerRemove>())
            {
                container.RemoveItem(pickupable, true);
                Log.Debug($"Received: Removed item {pickupable.GetTechType()} to container {owner.GetHierarchyPath()}");
            }
        }
コード例 #8
0
ファイル: ItemContainers.cs プロジェクト: veter12/Nitrox
        public void RemoveItem(string ownerGuid, string itemGuid)
        {
            GameObject owner = GuidHelper.RequireObjectFrom(ownerGuid);
            GameObject item  = GuidHelper.RequireObjectFrom(itemGuid);
            Optional <ItemsContainer> opContainer = InventoryContainerHelper.GetBasedOnOwnersType(owner);

            if (opContainer.IsPresent())
            {
                ItemsContainer container  = opContainer.Get();
                Pickupable     pickupable = item.RequireComponent <Pickupable>();

                using (packetSender.Suppress <ItemContainerRemove>())
                {
                    container.RemoveItem(pickupable, true);
                }
            }
            else
            {
                Log.Error("Could not find container field on object " + owner.name);
            }
        }
コード例 #9
0
        public override void Process(ItemContainerAdd packet)
        {
            GameObject owner = GuidHelper.RequireObjectFrom(packet.OwnerGuid);
            Optional <ItemsContainer> opContainer = InventoryContainerHelper.GetBasedOnOwnersType(owner);

            if (opContainer.IsPresent())
            {
                ItemsContainer container  = opContainer.Get();
                GameObject     item       = SerializationHelper.GetGameObject(packet.ItemData);
                Pickupable     pickupable = item.RequireComponent <Pickupable>();

                using (packetSender.Suppress <ItemContainerAdd>())
                {
                    container.UnsafeAdd(new InventoryItem(pickupable));
                }
            }
            else
            {
                Log.Error("Could not find container field on object " + owner.name);
            }
        }
コード例 #10
0
        public override void Process(ItemContainerRemove packet)
        {
            Optional <GameObject> opOwner = GuidHelper.GetObjectFrom(packet.OwnerGuid);
            Optional <GameObject> opItem  = GuidHelper.GetObjectFrom(packet.ItemGuid);

            if (opOwner.IsPresent() && opItem.IsPresent())
            {
                GameObject owner = opOwner.Get();
                GameObject item  = opItem.Get();

                Optional <ItemsContainer> opContainer = InventoryContainerHelper.GetBasedOnOwnersType(owner);

                if (opContainer.IsPresent())
                {
                    ItemsContainer container  = opContainer.Get();
                    Pickupable     pickupable = item.GetComponent <Pickupable>();

                    if (pickupable != null)
                    {
                        using (packetSender.Suppress <ItemContainerRemove>())
                        {
                            container.RemoveItem(pickupable, true);
                        }
                    }
                    else
                    {
                        Console.WriteLine(item.name + " did not have a corresponding pickupable script!");
                    }
                }
                else
                {
                    Console.WriteLine("Could not find container field on object " + owner.name);
                }
            }
            else
            {
                Console.WriteLine("Owner or item was not found: " + opOwner + " " + opItem + " " + packet.OwnerGuid + " " + packet.ItemGuid);
            }
        }
        public override void Process(ItemContainerAdd packet)
        {
            Optional <GameObject> opOwner = GuidHelper.GetObjectFrom(packet.OwnerGuid);

            if (opOwner.IsPresent())
            {
                GameObject owner = opOwner.Get();

                Optional <ItemsContainer> opContainer = InventoryContainerHelper.GetBasedOnOwnersType(owner);

                if (opContainer.IsPresent())
                {
                    ItemsContainer container  = opContainer.Get();
                    GameObject     item       = SerializationHelper.GetGameObject(packet.ItemData);
                    Pickupable     pickupable = item.GetComponent <Pickupable>();

                    if (pickupable != null)
                    {
                        using (packetSender.Suppress <ItemContainerAdd>())
                        {
                            container.UnsafeAdd(new InventoryItem(pickupable));
                        }
                    }
                    else
                    {
                        Console.WriteLine(item.name + " did not have a corresponding pickupable script!");
                    }
                }
                else
                {
                    Console.WriteLine("Could not find container field on object " + owner.name);
                }
            }
            else
            {
                Console.WriteLine("Could not find owner with guid: " + packet.OwnerGuid);
            }
        }
コード例 #12
0
        public override IEnumerator Process(InitialPlayerSync packet, WaitScreen.ManualWaitItem waitScreenItem)
        {
            int totalItemDataSynced = 0;

            using (packetSender.Suppress <ItemContainerAdd>())
            {
                ItemGoalTracker itemGoalTracker = (ItemGoalTracker)itemGoalTrackerMainField.GetValue(null);
                Dictionary <TechType, List <ItemGoal> > goals = (Dictionary <TechType, List <ItemGoal> >)itemGoalTrackerGoalsField.GetValue(itemGoalTracker);

                foreach (ItemData itemData in packet.InventoryItems)
                {
                    waitScreenItem.SetProgress(totalItemDataSynced, packet.InventoryItems.Count);

                    GameObject item;

                    try
                    {
                        item = SerializationHelper.GetGameObject(itemData.SerializedData);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, $"Error deserializing item data. Id: {itemData.ItemId}");
                        continue;
                    }

                    Log.Debug($"Initial item data for {item.name} giving to container {itemData.ContainerId}");

                    Pickupable pickupable = item.GetComponent <Pickupable>();
                    Validate.NotNull(pickupable);

                    if (itemData.ContainerId == packet.PlayerGameObjectId)
                    {
                        goals.Remove(pickupable.GetTechType());  // Remove notification goal event from item player has in any container

                        ItemsContainer container     = Inventory.Get().container;
                        InventoryItem  inventoryItem = new InventoryItem(pickupable)
                        {
                            container = container
                        };
                        inventoryItem.item.Reparent(container.tr);

                        container.UnsafeAdd(inventoryItem);
                    }
                    else if (NitroxEntity.TryGetObjectFrom(itemData.ContainerId, out GameObject containerOwner))
                    {
                        Optional <ItemsContainer> opContainer = InventoryContainerHelper.TryGetContainerByOwner(containerOwner);
                        Validate.IsPresent(opContainer);
                        opContainer.Value.UnsafeAdd(new InventoryItem(pickupable));

                        ContainerAddItemPostProcessor postProcessor = ContainerAddItemPostProcessor.From(item);
                        postProcessor.process(item, itemData);
                    }

                    totalItemDataSynced++;
                    yield return(null);
                }

                foreach (NitroxTechType usedItem in packet.UsedItems)
                {
                    Player.main.AddUsedTool(usedItem.ToUnity());
                }

                string[] quickSlotsBinding = packet.QuickSlotsBinding.ToArray();
                Inventory.main.serializedQuickSlots = quickSlotsBinding;
                Inventory.main.quickSlots.RestoreBinding(quickSlotsBinding);
            }

            Log.Info($"Received initial sync with {totalItemDataSynced} inventory items");
        }