Пример #1
0
    /// <summary>
    /// NOTE: Please use Inventory instead for moving inventory around.
    ///
    /// Server-side only. Adds the specified item to the slot, updating any observers.
    /// Note that this doesn't do anything other than saying the item is now in the slot.
    /// </summary>
    public void _ServerSetItem(Pickupable newItem)
    {
        var removedItem = item;

        item = newItem;
        OnSlotContentsChangeServer.Invoke();

        //server has to call their own client side hooks because by the time the message is received,
        //the server will not be able to determine what slot the item came from.
        OnSlotContentsChangeClient.Invoke();
        if (removedItem != null)
        {
            //we displaced an item
            var info = ClientInventoryMove.OfType(ClientInventoryMoveType.Removed);
            foreach (var hook in removedItem.GetComponents <IClientInventoryMove>())
            {
                hook.OnInventoryMoveClient(info);
            }
        }

        if (newItem != null)
        {
            //we are adding an item to this slot
            var info = ClientInventoryMove.OfType(ClientInventoryMoveType.Added);
            foreach (var hook in newItem.GetComponents <IClientInventoryMove>())
            {
                hook.OnInventoryMoveClient(info);
            }
        }

        UpdateItemSlotMessage.Send(serverObserverPlayers, this);
    }
Пример #2
0
        public override void Process(NetMessage msg)
        {
            //server calls their own client side hooks, so server doesn't do anything here.
            //It's necessary for it to be this way because by the time the server reaches this point,
            //the change to this slot has already occurred so it can't figure out what the previous
            //slot was for this item.
            if (!CustomNetworkManager.IsServer)
            {
                LoadMultipleObjects(new uint[] { msg.Storage, msg.Item });
                if (NetworkObjects[0] == null)
                {
                    return;
                }

                ItemSlot slot = null;
                if (msg.SlotIndex == -1)
                {
                    slot = ItemSlot.GetNamed(NetworkObjects[0].GetComponent <ItemStorage>(), msg.NamedSlot);
                }
                else
                {
                    slot = ItemSlot.GetIndexed(NetworkObjects[0].GetComponent <ItemStorage>(), msg.SlotIndex);
                }

                var previouslyInSlot = slot.ItemObject;
                var pickupable       = msg.Item == NetId.Invalid ? null : NetworkObjects[1].GetComponent <Pickupable>();
                slot.ClientUpdate(pickupable);


                if (previouslyInSlot != null)
                {
                    if (pickupable != null)
                    {
                        //was removed from slot
                        pickupable._SetItemSlot(null);
                    }

                    var moveInfo = ClientInventoryMove.OfType(ClientInventoryMoveType.Removed);
                    var hooks    = previouslyInSlot.GetComponents <IClientInventoryMove>();
                    foreach (var hook in hooks)
                    {
                        hook.OnInventoryMoveClient(moveInfo);
                    }
                }

                if (pickupable != null)
                {
                    //was added to slot
                    pickupable._SetItemSlot(slot);
                    var moveInfo = ClientInventoryMove.OfType(ClientInventoryMoveType.Added);
                    var hooks    = pickupable.GetComponents <IClientInventoryMove>();
                    foreach (var hook in hooks)
                    {
                        hook.OnInventoryMoveClient(moveInfo);
                    }
                }
            }
        }