• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Problem with loot seller

xenoria

Member
Joined
May 11, 2022
Messages
42
Reaction score
6
Hello i have big problem, someone can help me rewrite this scripts on TFS 1.5?

Gesior: Action - item 'item seller'/'fast loot' (use npc items list config!) (https://otland.net/threads/item-item-seller-fast-loot-use-npc-items-list-config.51076/)

Lua:
-- rest of config (item prices) is under function, paste there your items list from npc
local config = {
    price_percent = 90, -- how many % of shop price player receive when sell by 'item seller'
    cash_to_bank = true -- try to send cash automaticly to bank account (try to put in bp and  then send to bank)
}

shopModule = {shopItems = {}}
function shopModule:new()
    local obj = {}
    setmetatable(obj, self)
    self.__index = self
    return obj
end

function shopModule:addSellableItem(names, itemid, cost, realName)
    if(self.shopItems[itemid] == nil) then
        self.shopItems[itemid] = {sellPrice = -1}
    end
    self.shopItems[itemid].sellPrice = cost
end

function shopModule:addBuyableItemContainer(names, container, itemid, cost, subType, realName) end
function shopModule:addBuyableItem(names, itemid, cost, subType, realName) end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    msg = ""
    if(shopModule.shopItems[itemEx.itemid] ~= nil) then
        count = 1
        if(itemEx.type > 1 and isItemStackable(itemEx.itemid)) then
            count = itemEx.type
        end
        cash = math.ceil(shopModule.shopItems[itemEx.itemid].sellPrice * count / 100 * config.price_percent)
        doRemoveItem(itemEx.uid)
        msg = 'You sold ' .. count .. ' ' .. getItemNameById(itemEx.itemid) .. ' for ' .. cash .. ' gold coins'
        if(cash > 0) then
            doPlayerAddMoney(cid, cash)
            if(config.cash_to_bank) then
                doPlayerDepositMoney(cid, cash)
                msg = msg .. ' (cash auto transfered to bank account or drop on floor)'
            end
        end
        doSendAnimatedText(fromPosition, "$$$", TEXTCOLOR_LIGHTBLUE)
    else
        msg = getItemNameById(itemEx.itemid) .. " is not sellable item"
    end
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg .. '.')
    return false
end

-- here paste your list of items from NPC lua file
shopModule:addSellableItem({'steel boots', 'steel boots'},                2645, 30000,    'steel boots')
shopModule:addSellableItem({'golden boots', 'golden boots'},              2646, 400000,   'golden boots')
shopModule:addSellableItem({'crocodile boots', 'crocodile boots'},        3982, 1000,     'crocodile boots')
shopModule:addSellableItem({'pirate boots', 'pirate boots'},              5462, 5000,     'pirate boots')
shopModule:addSellableItem({'fur boots', 'fur boots'},                    7457, 2000,     'fur boots')
shopModule:addSellableItem({'terra boots', 'terra boots'},                7886, 5000,     'terra boots')
shopModule:addSellableItem({'magma boots', 'magma boots'},                7891, 7000,     'magma boots')
shopModule:addSellableItem({'glacier shoes', 'glacier shoes'},            7892, 3000,     'glacier shoes')
shopModule:addSellableItem({'lighting boots', 'lighting boots'},          7893, 7000,     'lighting boots')
shopModule:addSellableItem({'lighting boots', 'lighting boots'},          2197, 500,     'stone skin amulet')

Thanks for help, ++reps.
 
Solution
@abdala ragab
Code that you posted is not even related to question. Wtf.

@xenoria
I rewrote it to 1.x Lua and added some new features:
  • support for items list from .xml file
  • limit range to 1 SQM
  • block possibility to sell item in house, otherwise someone could sell item that lays in house doors
  • block possibility to sell items with actionID or uniqueID

Lua:
-- rest of config (item prices) is under function, paste there your items list from npc
local config = {
    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 shopItems = {}

function onUse(player, item, fromPosition, target, toPosition...
@abdala ragab
Code that you posted is not even related to question. Wtf.

@xenoria
I rewrote it to 1.x Lua and added some new features:
  • support for items list from .xml file
  • limit range to 1 SQM
  • block possibility to sell item in house, otherwise someone could sell item that lays in house doors
  • block possibility to sell items with actionID or uniqueID

Lua:
-- rest of config (item prices) is under function, paste there your items list from npc
local config = {
    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 shopItems = {}

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 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')

-- here paste list from .xml file
shopModule:parseList('crossbow,2455,150;bow,2456,130')
shopModule:parseList('knight armor, 2476, 10000')
 
Solution
@abdala ragab
Code that you posted is not even related to question. Wtf.

@xenoria
I rewrote it to 1.x Lua and added some new features:
  • support for items list from .xml file
  • limit range to 1 SQM
  • block possibility to sell item in house, otherwise someone could sell item that lays in house doors
  • block possibility to sell items with actionID or uniqueID

Lua:
-- rest of config (item prices) is under function, paste there your items list from npc
local config = {
    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 shopItems = {}

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 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')

-- here paste list from .xml file
shopModule:parseList('crossbow,2455,150;bow,2456,130')
shopModule:parseList('knight armor, 2476, 10000')
Thank you Gesior, you are amazing! ++ /close
 
Back
Top