-- rest of config (item prices) is under function, paste there your items list from npc
local config = {
containerAllowedIds = {2000, 2001, 2002}, -- if item is used on given container ID, script sells all items inside
containerMaxWeight = 321000, -- (321k) ignore all items in containers with higher weight
containerMaxItemsToProcess = 10000, -- process only first X items in container
containerMaxItemsToSell = 500, -- sell only first X sellable items from container
price_percent = 90, -- how many % of shop price player receive when sell by 'item seller'
cash_to_bank = true -- send money to bank, not add to player BP
}
local allowedContainers = {}
for _, itemId in pairs(config.containerAllowedIds) do
allowedContainers[itemId] = true
end
local shopItems = {}
local function sellAllItemsFromContainer(player, container)
if not container.getItems then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'Invalid configuration of Loot Seller items. Contact with GM.')
return
end
if container:getWeight() > config.containerMaxWeight * 100 then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'Container weight is above ' .. config.containerMaxWeight .. '!')
return
end
local sellableItemsInContainer = {}
local totalSellableItems = 0
for i, item in pairs(container:getItems(true)) do
if shopItems[item.itemid] and not (item:getUniqueId() < 65535 or item:getActionId() > 0) then
if not sellableItemsInContainer[item.itemid] then
sellableItemsInContainer[item.itemid] = {}
end
table.insert(sellableItemsInContainer[item.itemid], item)
totalSellableItems = totalSellableItems + 1
if totalSellableItems == config.containerMaxItemsToSell then
break
end
end
if i == config.containerMaxItemsToProcess then
break
end
end
local totalItemCount = 0
local totalItemValue = 0
for itemId, items in pairs(sellableItemsInContainer) do
local itemCount = 0
local itemType = ItemType(itemId)
for _, item in pairs(items) do
if itemType:isStackable() then
itemCount = itemCount + item.type
else
itemCount = itemCount + 1
end
item:remove()
end
totalItemCount = totalItemCount + itemCount
local itemValue = math.ceil(shopItems[items[1].itemid] * itemCount / 100 * config.price_percent)
totalItemValue = totalItemValue + itemValue
local message = 'You sold ' .. itemCount .. ' ' .. itemType:getName() .. ' for ' .. itemValue .. ' gold coins.'
if config.cash_to_bank then
player:setBankBalance(player:getBankBalance() + itemValue)
message = message .. ' Money was added to your bank account.'
else
player:addMoney(itemValue)
end
player:sendTextMessage(MESSAGE_INFO_DESCR, message)
end
local itemName = container:getName()
if totalItemCount > 0 then
player:sendTextMessage(MESSAGE_INFO_DESCR, totalItemCount .. ' items from ' .. itemName .. ' sold for ' .. totalItemValue .. ' gold coins.')
else
player:sendTextMessage(MESSAGE_INFO_DESCR, 'Items inside ' .. itemName .. ' are worthless.')
end
end
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
if toPosition.x ~= 65535 and getDistanceBetween(player:getPosition(), toPosition) > 1 then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'This item is too far away.')
return true
end
local targetTile = Tile(toPosition)
if targetTile then
-- this is to prevent selling item that lays in house doors
local targetHouse = targetTile:getHouse()
if targetHouse then
-- this blocks only selling items laying on house floor
-- if player open BP that lays on house floor, he can sell items inside it
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You cannot sell items in house.')
return true
end
end
local itemEx = Item(target.uid)
if not itemEx then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'This is not an item.')
return true
end
if allowedContainers[itemEx.itemid] then
sellAllItemsFromContainer(player, itemEx)
return true
end
if itemEx:getUniqueId() < 65535 or itemEx:getActionId() > 0 then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You cannot sell quest item.')
return true
end
if not shopItems[itemEx.itemid] then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'This is not sellable item.')
return true
end
local itemCount = 1
local itemType = ItemType(itemEx.itemid)
if itemType:isStackable() then
itemCount = itemEx.type
end
local itemName = itemEx:getName()
local itemValue = math.ceil(shopItems[itemEx.itemid] * itemCount / 100 * config.price_percent)
if itemValue > 0 then
itemEx:getPosition():sendMagicEffect(CONST_ME_GIFT_WRAPS)
itemEx:remove()
local message = 'You sold ' .. itemCount .. ' ' .. itemName .. ' for ' .. itemValue .. ' gold coins.'
if config.cash_to_bank then
player:setBankBalance(player:getBankBalance() + itemValue)
message = message .. ' Money was added to your bank account.'
else
player:addMoney(itemValue)
end
player:sendTextMessage(MESSAGE_INFO_DESCR, message)
else
player:sendTextMessage(MESSAGE_INFO_DESCR, itemName .. ' is worthless.')
end
return true
end
local shopModule = {}
function shopModule:addBuyableItemContainer()
end
function shopModule:addBuyableItem()
end
function shopModule:addSellableItem(names, itemid, cost, realName)
shopItems[itemid] = cost
end
function shopModule:parseList(data)
for item in string.gmatch(data, "[^;]+") do
local i = 1
local itemid = -1
local cost = 0
for temp in string.gmatch(item, "[^,]+") do
if i == 2 then
itemid = tonumber(temp)
elseif i == 3 then
cost = tonumber(temp)
end
i = i + 1
end
shopItems[itemid] = cost
end
end
-- here paste list of items from NPC lua file
shopModule:addSellableItem({ 'poison arrow' }, 2545, 5, 'poison arrow')
shopModule:addSellableItem({ 'hota' }, 2342, 500, 'helmet of the ancients')
shopModule:addSellableItem({ 'magic sword' }, 2400, 1300)
-- here paste list from .xml file
shopModule:parseList('crossbow,2455,150;bow,2456,130')
shopModule:parseList('knight armor, 2476, 10000')