local sellables = {
--[itemid] = price,
[2643] = 50,
}
local SOME_ACTIONID = 6543
local function sellItems(player, item)
local itemCount, totalValue = 0, 0
if item:isContainer() then
for _, it in ipairs(item:getItems()) do
local count, value = sellItems(player, it)
itemCount = itemCount + count
totalValue = totalValue + value
end
return itemCount, totalValue
else
local value = sellables[item:getId()]
if value then
local count = item:getCount()
item:remove()
return count, count * value
end
return 0, 0
end
end
local ev = EventCallback
function ev.onItemMoved(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
local ground = toCylinder:isTile() and toCylinder:getGround()
if ground and ground:getActionId() == SOME_ACTIONID then
local count, value = sellItems(player, item)
if item:isContainer() then
item:moveTo(player)
end
player:addMoney(value)
player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Sold %s item(s) for %s gold.", count, value))
end
return true
end
ev:register()