Advertisement
Guest User

Vodkart

a guest
Nov 22nd, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. -- Callback onSell() function. If you wish, you can change certain Npc to use your onSell().
  2.     function ShopModule:callbackOnSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)
  3.         local shopItem = nil
  4.         for _, item in ipairs(self.npcHandler.shopItems) do
  5.             if(item.id == itemid and item.subType == subType) then
  6.                 shopItem = item
  7.                 break
  8.             end
  9.         end
  10.  
  11.         if(shopItem == nil) then
  12.             print('[Warning - ' .. getCreatureName(getNpcId()) .. '] NpcSystem:', 'ShopModule.onSell - Item not found on shopItems list')
  13.             return false
  14.         end
  15.  
  16.         if(shopItem.sell == -1) then
  17.             print('[Warning - ' .. getCreatureName(getNpcId()) .. '] NpcSystem:', 'ShopModule.onSell - Attempt to sell an item which is only buyable')
  18.             return false
  19.         end
  20.  
  21.         local parseInfo = {
  22.             [TAG_PLAYERNAME] = getPlayerName(cid),
  23.             [TAG_ITEMCOUNT] = amount,
  24.             [TAG_TOTALCOST] = amount * shopItem.sell,
  25.             [TAG_ITEMNAME] = shopItem.name
  26.         }
  27.  
  28.         if(subType < 1 or getItemInfo(itemid).stackable) then
  29.             subType = -1
  30.         end
  31.  
  32.         if getPlayerSlotItem(cid, CONST_SLOT_BACKPACK).itemid ~= 0 then
  33.         local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
  34.         local z = getContainerItemsById(bp, itemid)
  35.         if #z >= amount then
  36.             for i = 1, amount do
  37.                 doRemoveItem(z[i].uid)
  38.             end
  39.             local msg = self.npcHandler:getMessage(MESSAGE_SOLD)
  40.             msg = self.npcHandler:parseMessage(msg, parseInfo)
  41.             doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg)
  42.             doPlayerAddMoney(cid, amount * shopItem.sell)
  43.             if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  44.                 self.npcHandler.talkStart[cid] = os.time()
  45.             else
  46.                 self.npcHandler.talkStart = os.time()
  47.             end
  48.             return true
  49.         else
  50.             local msg = self.npcHandler:getMessage(MESSAGE_NEEDITEM)
  51.             msg = self.npcHandler:parseMessage(msg, parseInfo)
  52.             doPlayerSendCancel(cid, msg)
  53.             if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  54.                 self.npcHandler.talkStart[cid] = os.time()
  55.             else
  56.                 self.npcHandler.talkStart = os.time()
  57.             end
  58.             return false
  59.         end
  60.     else
  61.         doPlayerSendCancel(cid, "I only buy items that are inside a BackPack")
  62.         if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  63.             self.npcHandler.talkStart[cid] = os.time()
  64.         else
  65.             self.npcHandler.talkStart = os.time()
  66.         end            
  67.         return false
  68.     end
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement