Exemplo n.º 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()}");
            }
        }
Exemplo n.º 2
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()}");
            }
        }
Exemplo n.º 3
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");
        }