Exemple #1
0
        public ItemSlotTrade GetSellingConditionsSlot(ItemStack forStack)
        {
            for (int i = 0; i < 4 * 4; i++)
            {
                ItemSlotTrade slot = GetSellingSlot(i);
                if (slot.Itemstack == null)
                {
                    continue;
                }

                if (slot.Itemstack.Equals(Api.World, forStack, GlobalConstants.IgnoredStackAttributes))
                {
                    return(slot);
                }
            }

            return(null);
        }
Exemple #2
0
        public int GetTotalCost()
        {
            int totalCost = 0;

            for (int i = 0; i < 4; i++)
            {
                ItemSlotTrade     buySlot   = GetBuyingCartSlot(i);
                ResolvedTradeItem tradeitem = buySlot.TradeItem;

                if (tradeitem != null)
                {
                    int cnt = buySlot.StackSize / tradeitem.Stack.StackSize;
                    totalCost += tradeitem.Price * cnt;
                }
            }

            return(totalCost);
        }
Exemple #3
0
        public override object ActivateSlot(int slotId, IItemSlot mouseSlot, ref ItemStackMoveOperation op)
        {
            // Player clicked an item from the selling list, move to buying cart
            if (slotId <= 15)
            {
                AddToBuyingCart(slots[slotId] as ItemSlotTrade);
                return(InvNetworkUtil.GetActivateSlotPacket(slotId, op));;
            }

            // Player clicked an item in the buying cart, remove it
            if (slotId <= 19)
            {
                ItemSlotTrade cartSlot = slots[slotId] as ItemSlotTrade;

                if (op.MouseButton == EnumMouseButton.Right)
                {
                    // Just remove one batch on right mouse
                    cartSlot.TakeOut(cartSlot.TradeItem.Stack.StackSize);
                    cartSlot.MarkDirty();
                }
                else
                {
                    cartSlot.Itemstack = null;
                    cartSlot.MarkDirty();
                }

                return(InvNetworkUtil.GetActivateSlotPacket(slotId, op));;
            }

            // Player clicked an item on the buy slot, ignore it
            if (slotId <= 34)
            {
                return(InvNetworkUtil.GetActivateSlotPacket(slotId, op));
            }

            // Player clicked an item in the selling cart, act like a normal slot
            if (slotId <= 39)
            {
                return(base.ActivateSlot(slotId, mouseSlot, ref op));
            }

            return(InvNetworkUtil.GetActivateSlotPacket(slotId, op));
        }
        public ItemSlotTrade GetBuyingConditionsSlot(ItemStack forStack)
        {
            for (int i = 0; i < 4 * 4; i++)
            {
                ItemSlotTrade slot = GetBuyingSlot(i);
                if (slot.Itemstack == null)
                {
                    continue;
                }

                if (slot.Itemstack.Equals(Api.World, forStack, GlobalConstants.IgnoredStackAttributes.Append("backpack")) || slot.Itemstack.Satisfies(forStack))
                {
                    if (forStack.Collectible.IsReasonablyFresh(traderEntity.World, forStack))
                    {
                        return(slot);
                    }
                }
            }

            return(null);
        }
        // wtf is this for?

        /*private ResolvedTradeItem GetTradeItemByName(string name, TradeList tradeList)
         * {
         *  if (name == null) return null;
         *
         *  for (int i = 0; i < tradeList.List.Length; i++)
         *  {
         *      TradeItem item = tradeList.List[i];
         *      string itemname = item.Name == null ? i + "" : item.Name;
         *      if (itemname.Equals(name)) return item.Resolve(Api.World);
         *  }
         *
         *  return null;
         * }*/



        public override void FromTreeAttributes(ITreeAttribute tree)
        {
            slots = SlotsFromTreeAttributes(tree, slots);
            ITreeAttribute tradeItems = tree.GetTreeAttribute("tradeItems");

            if (tradeItems == null)
            {
                return;
            }

            for (int slotId = 0; slotId < slots.Length; slotId++)
            {
                if (!(slots[slotId] is ItemSlotTrade) || slots[slotId].Empty)
                {
                    continue;
                }
                ItemSlotTrade tradeSlot = (slots[slotId] as ItemSlotTrade);

                tradeSlot.TradeItem = new ResolvedTradeItem(tradeItems.GetTreeAttribute(slotId + ""));
            }
        }
        public bool HasTraderEnoughDemand(IPlayer player)
        {
            Dictionary <int, int> Stocks = new Dictionary <int, int>();

            for (int i = 0; i < 4; i++)
            {
                ItemSlot slot = GetSellingCartSlot(i);
                if (slot.Itemstack == null)
                {
                    continue;
                }

                ItemSlotTrade     tradeSlot = GetBuyingConditionsSlot(slot.Itemstack);
                ResolvedTradeItem tradeItem = tradeSlot?.TradeItem;

                if (tradeItem == null)
                {
                    player.InventoryManager.NotifySlot(player, slot);
                    return(false);
                }

                int tradeslotid = GetSlotId(tradeSlot);
                int stock;
                if (!Stocks.TryGetValue(tradeslotid, out stock))
                {
                    stock = tradeItem.Stock;
                }

                Stocks[tradeslotid] = stock - slot.Itemstack.StackSize / tradeItem.Stack.StackSize;

                if (Stocks[tradeslotid] < 0)
                {
                    player.InventoryManager.NotifySlot(player, tradeSlot);
                    player.InventoryManager.NotifySlot(player, slot);
                    return(false);
                }
            }

            return(true);
        }
        private void AddToBuyingCart(ItemSlotTrade sellingSlot)
        {
            if (sellingSlot.Empty)
            {
                return;
            }

            // Try merge existing first
            for (int i = 0; i < 4; i++)
            {
                ItemSlotTrade slot = slots[16 + i] as ItemSlotTrade;
                if (slot.Empty)
                {
                    continue;
                }

                if (slot.Itemstack.Equals(sellingSlot.Itemstack) && slot.Itemstack.StackSize + sellingSlot.TradeItem.Stack.StackSize <= slot.Itemstack.Collectible.MaxStackSize)
                {
                    slot.Itemstack.StackSize += (sellingSlot as ItemSlotTrade).TradeItem.Stack.StackSize;
                    slot.MarkDirty();
                    return;
                }
            }

            // Otherwise find an empty slot
            for (int i = 0; i < 4; i++)
            {
                ItemSlotTrade slot = slots[16 + i] as ItemSlotTrade;
                if (!slot.Empty)
                {
                    continue;
                }

                slot.Itemstack = (sellingSlot as ItemSlotTrade).TradeItem.Stack.Clone();
                slot.Itemstack.ResolveBlockOrItem(Api.World);
                slot.TradeItem = (sellingSlot as ItemSlotTrade).TradeItem;
                slot.MarkDirty();
                return;
            }
        }
        internal EnumTransactionResult TryBuySell(IPlayer buyingPlayer)
        {
            if (!HasPlayerEnoughAssets(buyingPlayer))
            {
                return(EnumTransactionResult.PlayerNotEnoughAssets);
            }
            if (!HasTraderEnoughAssets())
            {
                return(EnumTransactionResult.TraderNotEnoughAssets);
            }

            if (!HasTraderEnoughStock(buyingPlayer))
            {
                return(EnumTransactionResult.TraderNotEnoughSupplyOrDemand);
            }
            if (!HasTraderEnoughDemand(buyingPlayer))
            {
                return(EnumTransactionResult.TraderNotEnoughSupplyOrDemand);
            }

            if (Api.Side == EnumAppSide.Client)
            {
                for (int i = 0; i < 4; i++)
                {
                    GetBuyingCartSlot(i).Itemstack = null;
                }
                return(EnumTransactionResult.Success);
            }

            // Take care of the money first
            if (!HandleMoneyTransaction(buyingPlayer))
            {
                return(EnumTransactionResult.Failure);
            }

            // Now hand over buying cart contents
            for (int i = 0; i < 4; i++)
            {
                ItemSlotTrade slot = GetBuyingCartSlot(i);
                if (slot.Itemstack == null)
                {
                    continue;
                }

                GiveOrDrop(buyingPlayer, slot.Itemstack);

                slot.TradeItem.Stock -= slot.Itemstack.StackSize / slot.TradeItem.Stack.StackSize;
                slot.Itemstack        = null;
                slot.MarkDirty();
            }

            // And delete selling cart contents
            for (int i = 0; i < 4; i++)
            {
                ItemSlot slot = GetSellingCartSlot(i);
                if (slot.Itemstack == null)
                {
                    continue;
                }

                ResolvedTradeItem tradeItem = GetBuyingConditionsSlot(slot.Itemstack).TradeItem;
                if (tradeItem == null)
                {
                    continue;
                }

                int q = slot.Itemstack.StackSize / tradeItem.Stack.StackSize;

                tradeItem.Stock -= q;
                slot.TakeOut(q * tradeItem.Stack.StackSize);
                slot.MarkDirty();
            }

            return(EnumTransactionResult.Success);
        }