Esempio n. 1
0
        public override IEnumerator Process(InitialPlayerSync packet, WaitScreen.ManualWaitItem waitScreenItem)
        {
            int totalItemDataSynced = 0;

            using (packetSender.Suppress <ItemContainerAdd>())
            {
                ItemGoalTracker itemGoalTracker = (ItemGoalTracker)typeof(ItemGoalTracker).GetField("main", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
                Dictionary <TechType, List <ItemGoal> > goals = (Dictionary <TechType, List <ItemGoal> >)(typeof(ItemGoalTracker).GetField("goals", BindingFlags.Instance | BindingFlags.NonPublic).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>();

                    if (pickupable != null && itemdata.ContainerId == packet.PlayerGameObjectId)
                    {
                        goals.Remove(pickupable.GetTechType());  // Remove Notification Goal Event On Item Player Already have On Any Container

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

                        container.UnsafeAdd(inventoryItem);
                    }
                    else
                    {
                        itemContainers.AddItem(item, itemdata.ContainerId);

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

                    totalItemDataSynced++;
                    yield return(null);
                }
            }

            Log.Info($"Received initial sync with {totalItemDataSynced} inventory items");
        }
        public override void Process(ItemContainerAdd packet)
        {
            ItemData   itemData = packet.ItemData;
            GameObject item     = SerializationHelper.GetGameObject(itemData.SerializedData);

            NitroxEntity.SetNewId(item, itemData.ItemId);

            itemContainer.AddItem(item, itemData.ContainerId);

            ContainerAddItemPostProcessor postProcessor = ContainerAddItemPostProcessor.From(item);

            postProcessor.process(item, itemData);
        }
        public override IEnumerator Process(InitialPlayerSync packet, WaitScreen.ManualWaitItem waitScreenItem)
        {
            int totalItemDataSynced = 0;

            HashSet <NitroxId> onlinePlayers = new HashSet <NitroxId> {
                packet.PlayerGameObjectId
            };

            onlinePlayers.AddRange(packet.RemotePlayerData.Select(playerData => playerData.PlayerContext.PlayerNitroxId));

            using (packetSender.Suppress <ItemContainerAdd>())
            {
                ItemGoalTracker itemGoalTracker = (ItemGoalTracker)typeof(ItemGoalTracker).GetField("main", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
                Dictionary <TechType, List <ItemGoal> > goals = (Dictionary <TechType, List <ItemGoal> >)(typeof(ItemGoalTracker).GetField("goals", BindingFlags.Instance | BindingFlags.NonPublic).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>();

                    if (pickupable && 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 (onlinePlayers.Any(playerId => playerId.Equals(itemData.ContainerId)))
                    {
                        itemContainers.AddItem(item, itemData.ContainerId);

                        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");
        }
Esempio n. 4
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");
        }