Exemplo n.º 1
0
        public override int TryPutInto(ItemSlot sinkSlot, ref ItemStackMoveOperation op)
        {
            if (!sinkSlot.CanTakeFrom(this) || !CanTake() || Itemstack == null)
            {
                return(0);
            }

            // Fill up sink slot
            if (op.ShiftDown)
            {
                if (Empty)
                {
                    return(0);
                }

                int maxstacksize = Itemstack.Collectible.MaxStackSize;

                if (sinkSlot.Itemstack == null)
                {
                    op.RequestedQuantity = maxstacksize;
                }
                else
                {
                    op.RequestedQuantity = maxstacksize - sinkSlot.StackSize;
                }
            }

            // Fill the destination slot with as many items as we can
            if (sinkSlot.Itemstack == null)
            {
                sinkSlot.Itemstack = TakeOut(op.RequestedQuantity);
                sinkSlot.OnItemSlotModified(sinkSlot.Itemstack);
                op.MovedQuantity = sinkSlot.StackSize;
                return(op.MovedQuantity);
            }

            ItemStack ownStack = Itemstack.Clone();

            ItemStackMergeOperation mergeop = op.ToMergeOperation(sinkSlot, this);

            op = mergeop;

            sinkSlot.Itemstack.Collectible.TryMergeStacks(mergeop);

            // Ignore any changes made to our slot
            Itemstack = ownStack;

            sinkSlot.OnItemSlotModified(sinkSlot.Itemstack);

            return(op.MovedQuantity);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the quantity of items that were not merged (left over in the source slot)
        /// </summary>
        /// <param name="sinkSlot"></param>
        /// <param name="op"></param>
        /// <returns>Amount of moved items</returns>
        public virtual int TryPutInto(ItemSlot sinkSlot, ref ItemStackMoveOperation op)
        {
            if (!sinkSlot.CanTakeFrom(this) || !CanTake() || itemstack == null)
            {
                return(0);
            }

            if (sinkSlot.inventory?.CanContain(sinkSlot, this) == false)
            {
                return(0);
            }


            // Fill the destination slot with as many items as we can
            if (sinkSlot.Itemstack == null)
            {
                int q = Math.Min(sinkSlot.GetRemainingSlotSpace(itemstack), op.RequestedQuantity);

                if (q > 0)
                {
                    sinkSlot.Itemstack = TakeOut(q);

                    // Has to be above the modified calls because e.g. when moving stuff into the ground slot this will eject the item
                    // onto the ground and sinkSlot.StackSize is 0 right after
                    op.MovedQuantity = op.MovableQuantity = Math.Min(sinkSlot.StackSize, q);

                    sinkSlot.OnItemSlotModified(sinkSlot.Itemstack);
                    OnItemSlotModified(sinkSlot.Itemstack);
                }

                return(op.MovedQuantity);
            }

            ItemStackMergeOperation mergeop = op.ToMergeOperation(sinkSlot, this);

            op = mergeop;
            int origRequestedQuantity = op.RequestedQuantity;

            op.RequestedQuantity = Math.Min(sinkSlot.GetRemainingSlotSpace(itemstack), op.RequestedQuantity);

            sinkSlot.Itemstack.Collectible.TryMergeStacks(mergeop);

            if (mergeop.MovedQuantity > 0)
            {
                sinkSlot.OnItemSlotModified(sinkSlot.Itemstack);
                OnItemSlotModified(sinkSlot.Itemstack);
            }

            op.RequestedQuantity = origRequestedQuantity; //ensures op.NotMovedQuantity will be correct in calling code if used with slots with limited slot maxStackSize, e.g. InventorySmelting with a cooking container has slots with maxStackSize == 6
            return(mergeop.MovedQuantity);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Activates the left click functions of the given slot.
        /// </summary>
        /// <param name="sourceSlot"></param>
        /// <param name="op"></param>
        protected virtual void ActivateSlotLeftClick(ItemSlot sourceSlot, ref ItemStackMoveOperation op)
        {
            // 1. Current slot empty: Take items
            if (Empty)
            {
                if (!CanHold(sourceSlot))
                {
                    return;
                }

                int q = Math.Min(sourceSlot.StackSize, MaxSlotStackSize);
                q = Math.Min(q, GetRemainingSlotSpace(sourceSlot.itemstack));

                itemstack        = sourceSlot.TakeOut(q);
                op.MovedQuantity = itemstack.StackSize;
                OnItemSlotModified(itemstack);
                return;
            }

            // 2. Current slot non empty, source slot empty: Put items
            if (sourceSlot.Empty)
            {
                op.RequestedQuantity = StackSize;
                TryPutInto(sourceSlot, ref op);
                return;
            }

            // 3. Both slots not empty, and they are stackable: Fill slot
            int maxq = itemstack.Collectible.GetMergableQuantity(itemstack, sourceSlot.itemstack, op.CurrentPriority);

            if (maxq > 0)
            {
                int origRequestedQuantity = op.RequestedQuantity;
                op.RequestedQuantity = GameMath.Min(maxq, sourceSlot.itemstack.StackSize, GetRemainingSlotSpace(sourceSlot.itemstack));

                ItemStackMergeOperation mergeop = op.ToMergeOperation(this, sourceSlot);
                op = mergeop;

                itemstack.Collectible.TryMergeStacks(mergeop);

                sourceSlot.OnItemSlotModified(itemstack);
                OnItemSlotModified(itemstack);

                op.RequestedQuantity = origRequestedQuantity; //ensures op.NotMovedQuantity will be correct in calling code if used with slots with limited slot maxStackSize, e.g. InventorySmelting with a cooking container has slots with maxStackSize == 6
                return;
            }

            // 4. Both slots not empty and not stackable: Exchange items
            TryFlipWith(sourceSlot);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Activates the left click functions of the given slot.
        /// </summary>
        /// <param name="sourceSlot"></param>
        /// <param name="op"></param>
        protected virtual void ActivateSlotLeftClick(ItemSlot sourceSlot, ref ItemStackMoveOperation op)
        {
            // 1. Current slot empty: Take items
            if (Empty)
            {
                if (!CanHold(sourceSlot))
                {
                    return;
                }

                itemstack        = sourceSlot.TakeOut(Math.Min(sourceSlot.StackSize, MaxSlotStackSize));
                op.MovedQuantity = itemstack.StackSize;
                OnItemSlotModified(itemstack);
                return;
            }

            // 2. Current slot non empty, source slot empty: Put items
            if (sourceSlot.Empty)
            {
                op.RequestedQuantity = StackSize;
                TryPutInto(sourceSlot, ref op);
                return;
            }

            // 3. Both slots not empty, and they are stackable: Fill slot
            int maxq = itemstack.Collectible.GetMergableQuantity(itemstack, sourceSlot.itemstack, op.CurrentPriority);

            if (maxq > 0)
            {
                op.RequestedQuantity = GameMath.Min(maxq, sourceSlot.itemstack.StackSize, RemainingSlotSpace);

                ItemStackMergeOperation mergeop = op.ToMergeOperation(this, sourceSlot);
                op = mergeop;

                itemstack.Collectible.TryMergeStacks(mergeop);

                sourceSlot.OnItemSlotModified(itemstack);
                OnItemSlotModified(itemstack);

                return;
            }

            // 4. Both slots not empty and not stackable: Exchange items
            TryFlipWith(sourceSlot);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns the quantity of items that were not merged (left over in the source slot)
        /// </summary>
        /// <param name="sinkSlot"></param>
        /// <param name="op"></param>
        /// <returns>Amount of moved items</returns>
        public virtual int TryPutInto(ItemSlot sinkSlot, ref ItemStackMoveOperation op)
        {
            if (!sinkSlot.CanTakeFrom(this) || !CanTake() || itemstack == null)
            {
                return(0);
            }


            // Fill the destination slot with as many items as we can
            if (sinkSlot.Itemstack == null)
            {
                int q = Math.Min(sinkSlot.RemainingSlotSpace, op.RequestedQuantity);

                if (q > 0)
                {
                    sinkSlot.Itemstack = TakeOut(q);

                    // Has to be above the modified calls because e.g. when moving stuff into the ground slot this will eject the item
                    // onto the ground and sinkSlot.StackSize is 0 right after
                    op.MovedQuantity = op.MovableQuantity = Math.Min(sinkSlot.StackSize, q);

                    sinkSlot.OnItemSlotModified(sinkSlot.Itemstack);
                    OnItemSlotModified(sinkSlot.Itemstack);
                }

                return(op.MovedQuantity);
            }

            ItemStackMergeOperation mergeop = op.ToMergeOperation(sinkSlot, this);

            op = mergeop;
            op.RequestedQuantity = Math.Min(sinkSlot.RemainingSlotSpace, op.RequestedQuantity);

            sinkSlot.Itemstack.Collectible.TryMergeStacks(mergeop);

            if (mergeop.MovedQuantity > 0)
            {
                sinkSlot.OnItemSlotModified(sinkSlot.Itemstack);
                OnItemSlotModified(sinkSlot.Itemstack);
            }

            return(mergeop.MovedQuantity);
        }
Exemplo n.º 6
0
        protected override void ActivateSlotLeftClick(ItemSlot sinkSlot, ref ItemStackMoveOperation op)
        {
            // 1. Current slot empty: Remove items
            if (Empty)
            {
                sinkSlot.TakeOutWhole();
                return;
            }

            // 2. Current slot non empty, source slot empty: Put items
            if (sinkSlot.Empty)
            {
                op.RequestedQuantity = StackSize;
                TryPutInto(sinkSlot, ref op);
                return;
            }

            // 3. Both slots not empty, and they are stackable: Fill source slot
            int maxq = Itemstack.Collectible.GetMergableQuantity(sinkSlot.Itemstack, Itemstack, op.CurrentPriority);

            if (maxq > 0)
            {
                op.RequestedQuantity = 1;
                ItemStackMergeOperation mergeop = op.ToMergeOperation(sinkSlot, this);
                op = mergeop;

                ItemStack ownStack = Itemstack.Clone();

                sinkSlot.Itemstack.Collectible.TryMergeStacks(mergeop);

                // Ignore any changes made to our slot
                Itemstack = ownStack;

                return;
            }

            // 4. Both slots not empty and not stackable: Remove items
            sinkSlot.TakeOutWhole();
        }
Exemplo n.º 7
0
        protected override void ActivateSlotLeftClick(ItemSlot sinkSlot, ref ItemStackMoveOperation op)
        {
            // 1. Current slot empty: Remove items
            if (Empty)
            {
                sinkSlot.TakeOutWhole();
                return;
            }

            // 2. Current slot non empty, source slot empty: Put items
            if (sinkSlot.Empty)
            {
                op.RequestedQuantity = StackSize;
                TryPutInto(sinkSlot, ref op);
                return;
            }

            // 3. Both slots not empty, and they are the same: Fill source slot
            if (sinkSlot.Itemstack.Equals(op.World, Itemstack, GlobalConstants.IgnoredStackAttributes))
            {
                op.RequestedQuantity = 1;
                ItemStackMergeOperation mergeop = op.ToMergeOperation(sinkSlot, this);
                op = mergeop;

                ItemStack ownStack = Itemstack.Clone();

                sinkSlot.Itemstack.Collectible.TryMergeStacks(mergeop);

                // Ignore any changes made to our slot
                Itemstack = ownStack;

                return;
            }

            // 4. Both slots not empty and not stackable: Remove items
            sinkSlot.TakeOutWhole();
        }
Exemplo n.º 8
0
        public override int TryPutInto(ItemSlot sinkSlot, ref ItemStackMoveOperation op)
        {
            if (!sinkSlot.CanTakeFrom(this) || !CanTake() || Itemstack == null)
            {
                return(0);
            }

            if (sinkSlot.Inventory?.CanContain(sinkSlot, this) == false)
            {
                return(0);
            }

            // Fill up sink slot
            if (op.ShiftDown)
            {
                if (Empty)
                {
                    return(0);
                }

                int maxstacksize = Itemstack.Collectible.MaxStackSize;

                if (sinkSlot.Itemstack == null)
                {
                    op.RequestedQuantity = maxstacksize;
                }
                else
                {
                    op.RequestedQuantity = maxstacksize - sinkSlot.StackSize;
                }
            }

            // Fill the destination slot with as many items as we can
            if (sinkSlot.Itemstack == null)
            {
                int q = Math.Min(sinkSlot.GetRemainingSlotSpace(itemstack), op.RequestedQuantity);

                sinkSlot.Itemstack = TakeOut(q);
                sinkSlot.OnItemSlotModified(sinkSlot.Itemstack);
                op.MovedQuantity = sinkSlot.StackSize;
                return(op.MovedQuantity);
            }

            ItemStack ownStack = Itemstack.Clone();

            ItemStackMergeOperation mergeop = op.ToMergeOperation(sinkSlot, this);

            op = mergeop;
            int origRequestedQuantity = op.RequestedQuantity;

            op.RequestedQuantity = Math.Min(sinkSlot.GetRemainingSlotSpace(itemstack), op.RequestedQuantity);

            sinkSlot.Itemstack.Collectible.TryMergeStacks(mergeop);

            // Ignore any changes made to our slot
            Itemstack = ownStack;

            if (mergeop.MovedQuantity > 0)
            {
                sinkSlot.OnItemSlotModified(sinkSlot.Itemstack);
            }

            op.RequestedQuantity = origRequestedQuantity; //ensures op.NotMovedQuantity will be correct in calling code if used with slots with limited slot maxStackSize, e.g. InventorySmelting with a cooking container has slots with maxStackSize == 6
            return(op.MovedQuantity);
        }