Example #1
0
        public static void QuickRestack(IContainerItem container)
        {
            if (Main.LocalPlayer.IsStackingItems())
            {
                return;
            }
            IList <Item> Items = container.GetItems();

            bool stacked = false;

            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].type > 0 && Items[i].stack > 0)
                {
                    int type  = Items[i].type;
                    int stack = Items[i].stack;
                    Items[i] = TakeItemFromNearbyChest(Items[i], Main.LocalPlayer.Center);
                    if (Items[i].type != type || Items[i].stack != stack)
                    {
                        stacked = true;
                    }
                }
            }

            if (stacked)
            {
                Main.PlaySound(7);
            }

            NetUtility.SyncItem(container.GetItem().item);
        }
Example #2
0
        private void MapToBody(IOperationContext context, ITile sourceTile, IContainerItem destinationContainer)
        {
            var requestor  = this.GetRequestor(context.CreatureManager);
            var itemMoving = sourceTile.TopItem;

            // Declare some pre-conditions.
            var sourceTileIsNull = sourceTile == null;
            var thingCanBeMoved  = itemMoving != null && (itemMoving == requestor || itemMoving.CanBeMoved);
            var locationsMatch   = itemMoving?.Location == this.FromLocation;
            var requestorInRange = requestor == null || (requestor.Location - this.FromLocation).MaxValueIn2D <= 1;
            var sourceTileHasEnoughItemAmount = this.ThingMovingId == itemMoving.TypeId && itemMoving.Amount >= this.Amount;

            if (sourceTileIsNull || !thingCanBeMoved)
            {
                this.DispatchTextNotification(context, OperationMessage.MayNotMoveThis);
            }
            else if (!locationsMatch)
            {
                // Silent fail.
                return;
            }
            else if (!sourceTileHasEnoughItemAmount)
            {
                this.DispatchTextNotification(context, OperationMessage.NotEnoughQuantity);
            }
            else if (!requestorInRange)
            {
                this.DispatchTextNotification(context, OperationMessage.TooFarAway);
            }
            else if (!this.PerformItemMovement(context, itemMoving, sourceTile, destinationContainer, toIndex: 0, amountToMove: this.Amount, requestorCreature: requestor))
            {
                // Something else went wrong.
                this.DispatchTextNotification(context);
            }
        }
Example #3
0
        /// <summary>
        /// Performs a container open action for a player.
        /// </summary>
        /// <param name="forCreatureId">The id of the creature for which the container is being opened.</param>
        /// <param name="container">The container to open.</param>
        /// <param name="atPosition">The position in which to open the container, for the player.</param>
        public void OpenContainer(uint forCreatureId, IContainerItem container, byte atPosition)
        {
            container.ThrowIfNull(nameof(container));

            // Check if this creature already has this container open at the specified position.
            // If so, we got nothing more to do.
            if (this.IsContainerOpen(container.UniqueId, forCreatureId, out IEnumerable <byte> openPositions) && openPositions.Contains(atPosition))
            {
                return;
            }

            // Otherwise, let's check if the player has something else open at the desired container position.
            var currentContainer = this.GetContainerAt(forCreatureId, atPosition);

            if (currentContainer != null)
            {
                // In this case, we need to close this container first.
                this.CloseContainerInternal(forCreatureId, currentContainer, atPosition);
            }

            // Now, actually open the container for this creature.
            byte containerId = this.OpenContainerInternal(forCreatureId, container, atPosition);

            if (this.creatureFinder.FindPlayerById(forCreatureId) is IPlayer player)
            {
                this.ContainerOpened?.Invoke(player, containerId, container);
            }
        }
Example #4
0
        public static void QuickStack(IContainerItem container, Func <Item, bool> selector = null)
        {
            if (Main.LocalPlayer.IsStackingItems())
            {
                return;
            }

            IList <Item> Items = container.GetItems();

            bool stacked = false;

            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].type > 0 && Items[i].stack > 0 && !Items[i].favorited && (selector?.Invoke(Items[i]) ?? true))
                {
                    int type  = Items[i].type;
                    int stack = Items[i].stack;
                    Items[i] = Chest.PutItemInNearbyChest(Items[i], Main.LocalPlayer.Center);
                    if (Items[i].type != type || Items[i].stack != stack)
                    {
                        stacked = true;
                    }
                }
            }

            if (stacked)
            {
                Main.PlaySound(7);
            }

            NetUtility.SyncItem(container.GetItem().item);
        }
Example #5
0
        /// <summary>
        /// Handles an event from a container content updated.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="indexOfUpdated">The index that was updated.</param>
        /// <param name="updatedItem">The updated item.</param>
        private void OnContainerContentUpdated(IContainerItem container, byte indexOfUpdated, IItem updatedItem)
        {
            if (updatedItem == null)
            {
                return;
            }

            lock (this.internalDictionariesLock)
            {
                if (!this.containersToCreatureIds.ContainsKey(container.UniqueId))
                {
                    return;
                }

                // The request has to be sent this way since the container id may be different for each player.
                foreach (var(containerId, creatureId) in this.containersToCreatureIds[container.UniqueId].ToList())
                {
                    if (!(this.creatureFinder.FindPlayerById(creatureId) is IPlayer player))
                    {
                        continue;
                    }

                    var notification = new GenericNotification(() => player.YieldSingleItem(), new ContainerUpdateItemPacket(indexOfUpdated, containerId, updatedItem));

                    this.scheduler.ScheduleEvent(notification);
                }
            }
        }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OpenContainerOperation"/> class.
 /// </summary>
 /// <param name="player">The player who has the container open.</param>
 /// <param name="containerItem">The container being opened.</param>
 /// <param name="containerPosition">Optional. The position at which the container is being opened, as seen by the player.</param>
 public OpenContainerOperation(IPlayer player, IContainerItem containerItem, byte containerPosition = 0xFF)
     : base(player.Id)
 {
     this.Player            = player;
     this.ContainerItem     = containerItem;
     this.ContainerPosition = containerPosition;
 }
Example #7
0
        private void ContainerToContainer(IOperationContext context, IContainerItem sourceContainer, IContainerItem destinationContainer)
        {
            // Declare some pre-conditions.
            var creatureHasSourceContainerOpen      = sourceContainer != null;
            var creatureHasDestinationContainerOpen = destinationContainer != null;

            var item = sourceContainer?[this.FromLocation.ContainerIndex];

            if (item == null)
            {
                this.DispatchTextNotification(context, OperationMessage.MayNotMoveThis);
            }
            else if (!creatureHasSourceContainerOpen)
            {
                this.DispatchTextNotification(context, OperationMessage.MustFirstOpenThatContainer);
            }
            else if (!creatureHasDestinationContainerOpen)
            {
                this.DispatchTextNotification(context, OperationMessage.MustFirstOpenThatContainer);
            }
            else if (!this.PerformItemMovement(context, item, sourceContainer, destinationContainer, this.FromLocation.ContainerIndex, this.ToLocation.ContainerIndex, this.Amount, this.GetRequestor(context.CreatureManager)))
            {
                // Something else went wrong.
                this.DispatchTextNotification(context);
            }
        }
Example #8
0
        public static void Sort(IContainerItem <T>[] array)
        {
            Guard.ArgumentNotNull(array, "array");

            int length = array.Length;

            if (length <= 1)
            {
                return;
            }

            if (length <= _maxCapacityForShell)
            {
                ShellSort(ref array, 0, length);
            }
            else
            {
                const int chunkSize = 512; //число должно быть меньше чем _maxCapacityForShell

                for (int i = 0; i < length; i += chunkSize)
                {
                    ShellSort(ref array, i, Math.Min(chunkSize, length - i));
                }

                var buffer = new IContainerItem <T> [length];
                for (int size = 1; size < length; size = size + size)
                {
                    for (int index = 0; index < length - size; index += size + size)
                    {
                        Merge(ref buffer, ref array, index, index + size - 1, Math.Min(index + size + size - 1, length - 1));
                    }
                }
                buffer = null;
            }
        }
        private void BodyToBody(IOperationContext context, IContainerItem sourceContainer, IContainerItem destinationContainer)
        {
            var thingMoving = sourceContainer?.FindThingAtIndex(this.FromLocation.ContainerIndex);

            if (!(thingMoving is IItem item))
            {
                this.DispatchTextNotification(context, OperationMessage.MayNotMoveThis);
            }
Example #10
0
        public static void DepositAll(IContainerItem container, Func <Item, bool> selector = null)
        {
            Player       player = Main.LocalPlayer;
            IList <Item> Items  = container.GetItems();

            MoveCoins(player.inventory, container);

            for (int pIndex = 49; pIndex >= 10; pIndex--)
            {
                if (player.inventory[pIndex].stack > 0 && player.inventory[pIndex].type > 0 && !player.inventory[pIndex].favorited && (selector?.Invoke(player.inventory[pIndex]) ?? true))
                {
                    if (player.inventory[pIndex].maxStack > 1)
                    {
                        for (int bIndex = 0; bIndex < Items.Count; bIndex++)
                        {
                            if (Items[bIndex].stack < Items[bIndex].maxStack && player.inventory[pIndex].IsTheSameAs(Items[bIndex]))
                            {
                                int stack = player.inventory[pIndex].stack;
                                if (player.inventory[pIndex].stack + Items[bIndex].stack > Items[bIndex].maxStack)
                                {
                                    stack = Items[bIndex].maxStack - Items[bIndex].stack;
                                }

                                player.inventory[pIndex].stack -= stack;
                                Items[bIndex].stack            += stack;
                                Main.PlaySound(7);

                                if (player.inventory[pIndex].stack <= 0)
                                {
                                    player.inventory[pIndex].SetDefaults();
                                    break;
                                }
                                if (Items[bIndex].type == 0)
                                {
                                    Items[bIndex] = player.inventory[pIndex].Clone();
                                    player.inventory[pIndex].SetDefaults();
                                }
                            }
                        }
                    }
                    if (player.inventory[pIndex].stack > 0)
                    {
                        for (int bIndex = 0; bIndex < Items.Count; bIndex++)
                        {
                            if (Items[bIndex].stack == 0)
                            {
                                Main.PlaySound(7);
                                Items[bIndex] = player.inventory[pIndex].Clone();
                                player.inventory[pIndex].SetDefaults();
                                break;
                            }
                        }
                    }
                }
            }

            NetUtility.SyncItem(container.GetItem().item);
        }
Example #11
0
        private static void Swap(ref IContainerItem <T>[] array, int i, int j)
        {
            IContainerItem <T> tmp = array[i];

            array[i] = array[j];
            array[j] = tmp;

            tmp = null;
        }
Example #12
0
        static void AddItem(Type intfType, IContainerItem item)
        {
            var added = container.TryAdd(intfType, item);

            if (!added)
            {
                throw new Exception($"Abstraction type {intfType.FullName} is already registered" +
                                    $" in {nameof(IocServices)}");
            }
        }
Example #13
0
        /// <summary>
        /// Gets the id of the given container as known by this player, if it is.
        /// </summary>
        /// <param name="container">The container to check.</param>
        /// <returns>The id of the container if known by this player.</returns>
        public sbyte GetContainerId(IContainerItem container)
        {
            for (sbyte i = 0; i < this.openContainers.Length; i++)
            {
                if (this.openContainers[i] == container)
                {
                    return(i);
                }
            }

            return(-1);
        }
Example #14
0
        /// <summary>
        /// Opens a container for a given creature at the specified position.
        /// </summary>
        /// <param name="forCreatureId">The id of the creature for which to open the container.</param>
        /// <param name="container">The container to open.</param>
        /// <param name="atPosition">The position at which to open the container.</param>
        /// <returns>The position at which the container was actually opened, which may not be the given <paramref name="atPosition"/>.</returns>
        private byte OpenContainerInternal(uint forCreatureId, IContainerItem container, byte atPosition)
        {
            lock (this.internalDictionariesLock)
            {
                byte openedAt = ItemConstants.UnsetContainerPosition;

                if (!this.creaturesToContainers.ContainsKey(forCreatureId))
                {
                    this.creaturesToContainers.Add(forCreatureId, new IContainerItem[ItemConstants.MaxContainersPerCreature]);
                }

                if (atPosition >= ItemConstants.MaxContainersPerCreature)
                {
                    // Find any available position.
                    for (byte i = 0; i < ItemConstants.MaxContainersPerCreature; i++)
                    {
                        if (this.creaturesToContainers[forCreatureId][i] != null)
                        {
                            continue;
                        }

                        openedAt = i;
                        this.creaturesToContainers[forCreatureId][i] = container;

                        break;
                    }
                }
                else
                {
                    openedAt = atPosition;
                    this.creaturesToContainers[forCreatureId][atPosition] = container;
                }

                // Now add to the other index per container.
                if (!this.containersToCreatureIds.ContainsKey(container.UniqueId))
                {
                    this.containersToCreatureIds.Add(container.UniqueId, new Dictionary <byte, uint>());

                    // This container is not being tracked at all, let's start tracking it.
                    container.ContentAdded    += this.OnContainerContentAdded;
                    container.ContentRemoved  += this.OnContainerContentRemoved;
                    container.ContentUpdated  += this.OnContainerContentUpdated;
                    container.LocationChanged += this.OnContainerLocationChanged;
                }

                this.containersToCreatureIds[container.UniqueId][openedAt] = forCreatureId;

                this.logger.Verbose($"Creature with id {forCreatureId} opened a {container.Type.Name} at position {openedAt}.");

                return(openedAt);
            }
        }
Example #15
0
        /// <summary>
        /// Opens a container by placing it at the given index id.
        /// If there is a container already open at this index, it is first closed.
        /// </summary>
        /// <param name="container">The container to open.</param>
        /// <param name="containerId">Optional. The index at which to open the container. Defaults to 0xFF which means open at any free index.</param>
        public void OpenContainerAt(IContainerItem container, byte containerId = 0xFF)
        {
            if (containerId == 0xFF)
            {
                this.OpenContainer(container);

                return;
            }

            this.openContainers[containerId]?.EndTracking(this.Id);
            this.openContainers[containerId] = container;
            this.openContainers[containerId].BeginTracking(this.Id, containerId);
        }
Example #16
0
        private void BodyToBody(IOperationContext context, IContainerItem sourceContainer, IContainerItem destinationContainer)
        {
            var item = sourceContainer?[this.FromLocation.ContainerIndex];

            if (item == null)
            {
                this.DispatchTextNotification(context, OperationMessage.MayNotMoveThis);
            }
            else if (!this.PerformItemMovement(context, item, sourceContainer, destinationContainer, 0, 0, this.Amount, this.GetRequestor(context.CreatureManager)))
            {
                // Something else went wrong.
                this.DispatchTextNotification(context);
            }
        }
Example #17
0
        public static void LootAll(IContainerItem container, Func <Item, bool> selector = null)
        {
            Player       player = Main.LocalPlayer;
            IList <Item> Items  = container.GetItems();

            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].type > 0 && (selector?.Invoke(Items[i]) ?? true))
                {
                    Items[i].position = player.Center;
                    Items[i]          = player.GetItem(Main.myPlayer, Items[i]);
                }
            }

            NetUtility.SyncItem(container.GetItem().item);
        }
        private void AddInventoryItem(ref INetworkMessage message, Slot slot, IContainerItem slotContainer)
        {
            var itemInContainer = slotContainer?.Content.FirstOrDefault();

            if (itemInContainer == null)
            {
                message.AddByte((byte)OutgoingGamePacketType.InventoryEmpty);
                message.AddByte((byte)slot);
            }
            else
            {
                message.AddByte((byte)OutgoingGamePacketType.InventoryItem);
                message.AddByte((byte)slot);
                message.AddItem(itemInContainer);
            }
        }
Example #19
0
        /// <summary>
        /// Performs a container close action for a player.
        /// </summary>
        /// <param name="forCreatureId">The id of the creature for which the container is being closed.</param>
        /// <param name="container">The container being closed.</param>
        /// <param name="atPosition">The position of the container being closed, as seen by the player.</param>
        public void CloseContainer(uint forCreatureId, IContainerItem container, byte atPosition)
        {
            container.ThrowIfNull(nameof(container));

            // Check if this creature doesn't have this container open, or it's no longer at the position specified.
            if (!this.IsContainerOpen(container.UniqueId, forCreatureId, out IEnumerable <byte> openPositions) || !openPositions.Contains(atPosition))
            {
                return;
            }

            this.CloseContainerInternal(forCreatureId, container, atPosition);

            if (this.creatureFinder.FindPlayerById(forCreatureId) is IPlayer player)
            {
                this.scheduler.ScheduleEvent(new GenericNotification(() => player.YieldSingleItem(), new ContainerClosePacket(atPosition)));
            }
        }
Example #20
0
        private void ContainerToMap(IOperationContext context, IContainerItem sourceContainer, ITile destinationTile)
        {
            var item = sourceContainer?[this.FromLocation.ContainerIndex];

            if (item == null)
            {
                this.DispatchTextNotification(context, OperationMessage.MayNotMoveThis);

                return;
            }

            // Declare some pre-conditions.
            var destinationHasGround           = destinationTile?.Ground != null;
            var destinationIsObstructed        = destinationTile.BlocksLay || (item.BlocksPass && destinationTile.BlocksPass);
            var creatureHasSourceContainerOpen = sourceContainer != null;
            var canThrowBetweenLocations       = context.Map.CanThrowBetweenLocations(sourceContainer.Location, this.ToLocation, checkLineOfSight: true);

            if (!destinationHasGround || !canThrowBetweenLocations)
            {
                this.DispatchTextNotification(context, OperationMessage.MayNotThrowThere);
            }
            else if (destinationIsObstructed)
            {
                this.DispatchTextNotification(context, OperationMessage.NotEnoughRoom);
            }
            else if (!creatureHasSourceContainerOpen)
            {
                this.DispatchTextNotification(context, OperationMessage.MustFirstOpenThatContainer);
            }
            else if (!this.PerformItemMovement(context, item, sourceContainer, destinationTile, fromIndex: this.FromLocation.ContainerIndex, amountToMove: this.Amount, requestorCreature: this.GetRequestor(context.CreatureManager)))
            {
                // Something else went wrong.
                this.DispatchTextNotification(context);
            }
            else if (this.GetRequestor(context.CreatureManager) is IPlayer player && this.ToLocation != player.Location && player != item)
            {
                var directionToDestination = player.Location.DirectionTo(this.ToLocation);

                context.Scheduler.ScheduleEvent(new TurnToDirectionOperation(player, directionToDestination));
            }
        }
        /// <summary>
        /// Performs a container open action for a player.
        /// </summary>
        /// <param name="forCreatureId">The id of the creature for which the container is being opened.</param>
        /// <param name="container">The container to open.</param>
        /// <param name="atPosition">The position in which to open the container, for the player.</param>
        public void OpenContainer(uint forCreatureId, IContainerItem container, byte atPosition)
        {
            container.ThrowIfNull(nameof(container));

            // Check if this creature already has this container open at the specified position.
            // If so, we got nothing more to do.
            if (this.IsContainerOpen(container.UniqueId, forCreatureId, out IEnumerable <byte> openPositions) && openPositions.Contains(atPosition))
            {
                return;
            }

            // Otherwise, let's check if the player has something else open at the desired container position.
            var currentContainer = this.GetContainerAt(forCreatureId, atPosition);

            if (currentContainer != null)
            {
                // In this case, we need to close this container first.
                this.CloseContainerInternal(forCreatureId, currentContainer, atPosition);
            }

            // Now, actually open the container for this creature.
            byte containerId = this.OpenContainerInternal(forCreatureId, container, atPosition);

            if (this.creatureFinder.FindPlayerById(forCreatureId) is IPlayer player)
            {
                var notification = new GenericNotification(
                    () => player.YieldSingleItem(),
                    new ContainerOpenPacket(
                        containerId,
                        container.TypeId,
                        container.Type.Name,
                        container.Capacity,
                        container.ParentContainer is IContainerItem parentContainer && parentContainer.Type.TypeId != 0,
                        container.Content));

                this.scheduler.ScheduleEvent(notification);
            }
        }
Example #22
0
        /// <summary>
        /// Closes a container for a creature at a given position.
        /// </summary>
        /// <param name="forCreatureId">The creature to check for.</param>
        /// <param name="container">The container to close.</param>
        /// <param name="atPosition">The position at which to close the container.</param>
        private void CloseContainerInternal(uint forCreatureId, IContainerItem container, byte atPosition)
        {
            lock (this.internalDictionariesLock)
            {
                if (!this.creaturesToContainers.ContainsKey(forCreatureId) ||
                    atPosition >= this.creaturesToContainers[forCreatureId].Length ||
                    container.UniqueId != this.creaturesToContainers[forCreatureId][atPosition].UniqueId)
                {
                    return;
                }

                // For the per-creature map, we need only close the one at the specific position.
                this.creaturesToContainers[forCreatureId][atPosition] = null;

                // For the containers map, we need to get a bit fancy.
                if (this.containersToCreatureIds.ContainsKey(container.UniqueId))
                {
                    if (this.containersToCreatureIds[container.UniqueId][atPosition] == forCreatureId)
                    {
                        this.containersToCreatureIds[container.UniqueId].Remove(atPosition);
                    }

                    // Clean up if this list is now empty.
                    if (this.containersToCreatureIds[container.UniqueId].Count == 0)
                    {
                        this.containersToCreatureIds.Remove(container.UniqueId);

                        // Clean up events because no one else cares about this container.
                        container.ContentAdded    -= this.OnContainerContentAdded;
                        container.ContentRemoved  -= this.OnContainerContentRemoved;
                        container.ContentUpdated  -= this.OnContainerContentUpdated;
                        container.LocationChanged -= this.OnContainerLocationChanged;
                    }
                }

                this.logger.Verbose($"Creature with id {forCreatureId} closed a {container.Type.Name} at position {atPosition}.");
            }
        }
Example #23
0
        /// <summary>
        /// Opens a container for this player, which tracks it.
        /// </summary>
        /// <param name="container">The container being opened.</param>
        /// <returns>The id of the container as seen by this player.</returns>
        public byte OpenContainer(IContainerItem container)
        {
            container.ThrowIfNull(nameof(container));

            for (byte i = 0; i < this.openContainers.Length; i++)
            {
                if (this.openContainers[i] != null)
                {
                    continue;
                }

                this.openContainers[i] = container;
                this.openContainers[i].BeginTracking(this.Id, i);

                return(i);
            }

            var lastIdx = (byte)(this.openContainers.Length - 1);

            this.openContainers[lastIdx] = container;

            return(lastIdx);
        }
Example #24
0
        /// <summary>
        /// Finds the position of a specified container as seen by a specific creature.
        /// </summary>
        /// <param name="creatureId">The id of the creature for which to find the container.</param>
        /// <param name="container">The container to look for.</param>
        /// <returns>The position of container found, or <see cref="ItemConstants.UnsetContainerPosition"/>> if not found.</returns>
        public byte FindForCreature(uint creatureId, IContainerItem container)
        {
            if (container == null)
            {
                return(ItemConstants.UnsetContainerPosition);
            }

            lock (this.internalDictionariesLock)
            {
                if (this.creaturesToContainers.ContainsKey(creatureId))
                {
                    for (byte i = 0; i < ItemConstants.MaxContainersPerCreature; i++)
                    {
                        if (this.creaturesToContainers[creatureId][i] != null && this.creaturesToContainers[creatureId][i].UniqueId == container.UniqueId)
                        {
                            return(i);
                        }
                    }
                }

                return(ItemConstants.UnsetContainerPosition);
            }
        }
Example #25
0
        public static void Restock(IContainerItem container)
        {
            Player player = Main.LocalPlayer;

            Item[]       inventory = player.inventory;
            IList <Item> item      = container.GetItems();

            HashSet <int> restackableItems = new HashSet <int>();
            List <int>    canRestackIndex  = new List <int>();
            List <int>    list2            = new List <int>();

            for (int i = 57; i >= 0; i--)
            {
                if ((i < 50 || i >= 54) && (inventory[i].type < 71 || inventory[i].type > 74))
                {
                    if (inventory[i].stack > 0 && inventory[i].maxStack > 1 && inventory[i].prefix == 0)
                    {
                        restackableItems.Add(inventory[i].netID);
                        if (inventory[i].stack < inventory[i].maxStack)
                        {
                            canRestackIndex.Add(i);
                        }
                    }
                    else if (inventory[i].stack == 0 || inventory[i].netID == 0 || inventory[i].type == 0)
                    {
                        list2.Add(i);
                    }
                }
            }
            bool restocked = false;

            for (int j = 0; j < item.Count; j++)
            {
                if (item[j].stack >= 1 && item[j].prefix == 0 && restackableItems.Contains(item[j].netID))
                {
                    bool flag2 = false;
                    for (int k = 0; k < canRestackIndex.Count; k++)
                    {
                        int num     = canRestackIndex[k];
                        int context = 0;
                        if (num >= 50)
                        {
                            context = 2;
                        }
                        if (inventory[num].netID == item[j].netID && ItemSlot.PickItemMovementAction(inventory, context, num, item[j]) != -1)
                        {
                            int num2 = item[j].stack;
                            if (inventory[num].maxStack - inventory[num].stack < num2)
                            {
                                num2 = inventory[num].maxStack - inventory[num].stack;
                            }
                            inventory[num].stack += num2;
                            item[j].stack        -= num2;
                            restocked             = true;
                            if (inventory[num].stack == inventory[num].maxStack)
                            {
                                canRestackIndex.RemoveAt(k);
                                k--;
                            }
                            if (item[j].stack == 0)
                            {
                                item[j] = new Item();
                                flag2   = true;
                                break;
                            }
                        }
                    }
                    if (!flag2 && list2.Count > 0 && item[j].ammo != 0)
                    {
                        for (int l = 0; l < list2.Count; l++)
                        {
                            int context2 = 0;
                            if (list2[l] >= 50)
                            {
                                context2 = 2;
                            }
                            if (ItemSlot.PickItemMovementAction(inventory, context2, list2[l], item[j]) != -1)
                            {
                                Item temp = inventory[list2[l]];
                                inventory[list2[l]] = item[j];
                                item[j]             = temp;

                                canRestackIndex.Add(list2[l]);
                                list2.RemoveAt(l);
                                restocked = true;
                                break;
                            }
                        }
                    }
                }
            }
            if (restocked)
            {
                Main.PlaySound(7);
            }

            NetUtility.SyncItem(container.GetItem().item);
        }
Example #26
0
 protected override IContainerShape GetShapeContainerForItemOverride(IContainerItem item)
 {
     return(new TableShape());
 }
Example #27
0
        public static void MoveCoins(Item[] playerInv, IContainerItem container)
        {
            IList <Item> containerInv = container.GetItems();

            int[] coins = new int[4];

            List <int> coinSlotsPlayer    = new List <int>();
            List <int> coinSlotsContainer = new List <int>();

            bool anyCoins = false;

            int[] coinValueArr = new int[containerInv.Count];

            for (int i = 0; i < containerInv.Count; i++)
            {
                coinValueArr[i] = -1;
                if (containerInv[i].stack < 1 || containerInv[i].type < 1)
                {
                    coinSlotsContainer.Add(i);
                    containerInv[i] = new Item();
                }
                if (containerInv[i] != null && containerInv[i].stack > 0)
                {
                    int num = 0;
                    switch (containerInv[i].type)
                    {
                    case 71:
                        num = 1;
                        break;

                    case 72:
                        num = 2;
                        break;

                    case 73:
                        num = 3;
                        break;

                    case 74:
                        num = 4;
                        break;
                    }
                    coinValueArr[i] = num - 1;
                    if (num > 0)
                    {
                        coins[num - 1] += containerInv[i].stack;
                        coinSlotsContainer.Add(i);
                        containerInv[i] = new Item();
                        anyCoins        = true;
                    }
                }
            }

            if (!anyCoins)
            {
                return;
            }
            Main.PlaySound(7);

            for (int j = 0; j < playerInv.Length; j++)
            {
                if (j != 58 && playerInv[j] != null && playerInv[j].stack > 0)
                {
                    int num2 = 0;
                    switch (playerInv[j].type)
                    {
                    case 71:
                        num2 = 1;
                        break;

                    case 72:
                        num2 = 2;
                        break;

                    case 73:
                        num2 = 3;
                        break;

                    case 74:
                        num2 = 4;
                        break;
                    }
                    if (num2 > 0)
                    {
                        coins[num2 - 1] += playerInv[j].stack;
                        coinSlotsPlayer.Add(j);
                        playerInv[j] = new Item();
                    }
                }
            }
            for (int k = 0; k < 3; k++)
            {
                while (coins[k] >= 100)
                {
                    coins[k] -= 100;
                    coins[k + 1]++;
                }
            }
            for (int l = 0; l < 40; l++)
            {
                if (coinValueArr[l] >= 0 && containerInv[l].type == 0)
                {
                    int num3 = l;
                    int num4 = coinValueArr[l];
                    if (coins[num4] > 0)
                    {
                        containerInv[num3].SetDefaults(71 + num4);
                        containerInv[num3].stack = coins[num4];
                        if (containerInv[num3].stack > containerInv[num3].maxStack)
                        {
                            containerInv[num3].stack = containerInv[num3].maxStack;
                        }
                        coins[num4]    -= containerInv[num3].stack;
                        coinValueArr[l] = -1;
                    }

                    coinSlotsContainer.Remove(num3);
                }
            }
            for (int m = 0; m < 40; m++)
            {
                if (coinValueArr[m] >= 0 && containerInv[m].type == 0)
                {
                    int num5 = m;
                    int n    = 3;
                    while (n >= 0)
                    {
                        if (coins[n] > 0)
                        {
                            containerInv[num5].SetDefaults(71 + n);
                            containerInv[num5].stack = coins[n];
                            if (containerInv[num5].stack > containerInv[num5].maxStack)
                            {
                                containerInv[num5].stack = containerInv[num5].maxStack;
                            }
                            coins[n]       -= containerInv[num5].stack;
                            coinValueArr[m] = -1;
                            break;
                        }
                        if (coins[n] == 0)
                        {
                            n--;
                        }
                    }

                    coinSlotsContainer.Remove(num5);
                }
            }
            while (coinSlotsContainer.Count > 0)
            {
                int num6 = coinSlotsContainer[0];
                int num7 = 3;
                while (num7 >= 0)
                {
                    if (coins[num7] > 0)
                    {
                        containerInv[num6].SetDefaults(71 + num7);
                        containerInv[num6].stack = coins[num7];
                        if (containerInv[num6].stack > containerInv[num6].maxStack)
                        {
                            containerInv[num6].stack = containerInv[num6].maxStack;
                        }
                        coins[num7] -= containerInv[num6].stack;
                        break;
                    }
                    if (coins[num7] == 0)
                    {
                        num7--;
                    }
                }

                coinSlotsContainer.RemoveAt(0);
            }
            int num8 = 3;

            while (num8 >= 0 && coinSlotsPlayer.Count > 0)
            {
                int num9 = coinSlotsPlayer[0];
                if (coins[num8] > 0)
                {
                    playerInv[num9].SetDefaults(71 + num8);
                    playerInv[num9].stack = coins[num8];
                    if (playerInv[num9].stack > playerInv[num9].maxStack)
                    {
                        playerInv[num9].stack = playerInv[num9].maxStack;
                    }
                    coins[num8] -= playerInv[num9].stack;
                }
                if (coins[num8] == 0)
                {
                    num8--;
                }
                coinSlotsPlayer.RemoveAt(0);
            }
        }
 /// <summary>
 /// Handles an item being added to the given container.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="addedItem">The item added.</param>
 private void HandleContentAdded(IContainerItem container, IItem addedItem)
 {
     this.InvokeSlotChanged(container as BodyContainerItem, addedItem);
 }
 /// <summary>
 /// Handles an item being removed from the given container.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="indexRemoved">The index of the removed item.</param>
 private void HandleContentRemoved(IContainerItem container, byte indexRemoved)
 {
     this.InvokeSlotChanged(container as BodyContainerItem, null);
 }
 /// <summary>
 /// Handles an item being updated in the given container.
 /// </summary>
 /// <param name="container">The container.</param>
 /// <param name="indexOfUpdated">The index of the updated item.</param>
 /// <param name="updatedItem">The item that was updated.</param>
 private void HandleContentUpdated(IContainerItem container, byte indexOfUpdated, IItem updatedItem)
 {
     this.InvokeSlotChanged(container as BodyContainerItem, container.Content.FirstOrDefault());
 }