public override object ActivateSlot(int slotId, ItemSlot 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 if (cartSlot.TradeItem?.Stack != null) { 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)); }
private void RefreshBuyingSellingInventory() { if (TradeProps == null) { return; } TradeProps.Buying.List.Shuffle(World.Rand); int quantity = Math.Min(TradeProps.Buying.List.Length, TradeProps.Buying.MaxItems); for (int i = 0; i < quantity; i++) { ItemSlotTrade slot = Inventory.GetBuyingSlot(i); TradeItem tradeItem = TradeProps.Buying.List[i]; if (tradeItem.Name == null) { tradeItem.Name = i + ""; } slot.SetTradeItem(tradeItem.Resolve(World)); slot.MarkDirty(); } TradeProps.Selling.List.Shuffle(World.Rand); quantity = Math.Min(TradeProps.Selling.List.Length, TradeProps.Selling.MaxItems); //Console.WriteLine("=================="); //Console.WriteLine("resolving for " + EntityId + ", total items: " +TradeConf.Selling.List.Length + ", on side " + api.Side); for (int i = 0; i < quantity; i++) { ItemSlotTrade slot = Inventory.GetSellingSlot(i); TradeItem tradeItem = TradeProps.Selling.List[i]; if (tradeItem.Name == null) { tradeItem.Name = i + ""; } slot.SetTradeItem(tradeItem.Resolve(World)); slot.MarkDirty(); //Console.WriteLine("resolved to: " + slot.Itemstack); } ITreeAttribute tree = GetOrCreateTradeStore(); Inventory.ToTreeAttributes(tree); }
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); }