• 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!

tfs 1.2 sell and buy the same item.

OTAmator

Member
Joined
May 4, 2016
Messages
60
Reaction score
8
If npc sells and buys same item for example war hammer then i can only buy it. If i type war hammer he offers me to buy it and if i type sell war hmmer he offers me to buy it. Anyone have any idea about the solution?
Maybe the problem is in lib/npc.lua?


19:46 GOD [15]: war hammer
19:46 Nah'Bob: Do you want to buy 1 war hammer for 10000 gold coins?
19:46 GOD [15]: sell war hammer
19:46 Nah'Bob: Do you want to buy 1 war hammer for 10000 gold coins?
19:46 GOD [15]: sell blue robe
19:46 Nah'Bob: Do you want to sell 1 blue robe for 10000 gold coins?

Nah'bob.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>

<npc name="Nah'Bob" script="default.lua" walkinterval="2000" floorchange="0">

    <health now="185" max="185"/>

  <look type="80" head="0" body="0" legs="0" feet="0" corpse="6020"/>

    <parameters>

        <parameter key="module_shop" value="1"/>

        <parameter key="message_greet" value="Welcome!"/>

        <parameter key="message_farewall" value="Bye..."/>

        <parameter key="message_walkaway" value="How rude!"/>

        <parameter key="shop_sellable" value="beholder shield,2518,1200;crown shield,2519,8000;dragon shield,2516,4000;guardian shield,2515,2000;phoenix shield,2539,16000;blue robe,2656,10000;crown armor,2487,12000;noble armor,2486,900;boots of haste,2195,30000;broadsword,2413,500;dragon lance,2414,9000;fire axe,2432,8000;fire sword,2392,4000;ice rapier,2396,1000;obsidian lance,2425,500;spike sword,2383,1000;war hammer,2391,1200;glorious axe,7454,3000;thaian sword,7391,16000;shadow sceptre,7451,10000;butchers axe,7412,18000;angelic axe,7436,5000;crown helmet,2491,2500;crusader helmet,2497,6000;royal helmet,2498,30000;crown legs,2488,12000;"/>

        <parameter key="shop_buyable" value="beholder shield,2518,7000;noble armor,2486,8000;spike sword,2383,8000;war hammer,2391,10000;"/>

    </parameters>

</npc>

Default.lua
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

npcHandler:addModule(FocusModule:new())

lib/npc.lua
Code:
-- Including the Advanced NPC System
dofile('data/npc/lib/npcsystem/npcsystem.lua')

function msgcontains(message, keyword)
    local message, keyword = message:lower(), keyword:lower()
    if message == keyword then
        return true
    end

    return message:find(keyword) and not message:find('(%w+)' .. keyword)
end

function doNpcSellItem(cid, itemid, amount, subType, ignoreCap, inBackpacks, backpack)
    local amount = amount or 1
    local subType = subType or 0
    local item = 0
    if isItemStackable(itemid) then
        if inBackpacks then
            stuff = doCreateItemEx(backpack, 1)
            item = doAddContainerItem(stuff, itemid, math.min(100, amount))
        else
            stuff = doCreateItemEx(itemid, math.min(100, amount))
        end
        return doPlayerAddItemEx(cid, stuff, ignoreCap) ~= RETURNVALUE_NOERROR and 0 or amount, 0
    end

    local a = 0
    if inBackpacks then
        local container, b = doCreateItemEx(backpack, 1), 1
        for i = 1, amount do
            local item = doAddContainerItem(container, itemid, subType)
            if isInArray({(getContainerCapById(backpack) * b), amount}, i) then
                if doPlayerAddItemEx(cid, container, ignoreCap) ~= RETURNVALUE_NOERROR then
                    b = b - 1
                    break
                end

                a = i
                if amount > i then
                    container = doCreateItemEx(backpack, 1)
                    b = b + 1
                end
            end
        end
        return a, b
    end

    for i = 1, amount do -- normal method for non-stackable items
        local item = doCreateItemEx(itemid, subType)
        if doPlayerAddItemEx(cid, item, ignoreCap) ~= RETURNVALUE_NOERROR then
            break
        end
        a = i
    end
    return a, 0
end

local func = function(cid, text, type, e, pcid)
    if isPlayer(pcid) then
        doCreatureSay(cid, text, type, false, pcid, getCreaturePosition(cid))
        e.done = TRUE
    end
end

function doCreatureSayWithDelay(cid, text, type, delay, e, pcid)
    if isPlayer(pcid) then
        e.done = FALSE
        e.event = addEvent(func, delay < 1 and 1000 or delay, cid, text, type, e, pcid)
    end
end

function doPlayerTakeItem(cid, itemid, count)
    if getPlayerItemCount(cid,itemid) < count then
        return false
    end

    while count > 0 do
        local tempcount = 0
        if isItemStackable(itemid) then
            tempcount = math.min (100, count)
        else
            tempcount = 1
        end

        local ret = doPlayerRemoveItem(cid, itemid, tempcount)
        if ret ~= false then
            count = count - tempcount
        else
            return false
        end
    end

    if count ~= 0 then
        return false
    end
    return true
end

function doPlayerSellItem(cid, itemid, count, cost)
    if doPlayerTakeItem(cid, itemid, count) == true then
        if not doPlayerAddMoney(cid, cost) then
            error('Could not add money to ' .. getPlayerName(cid) .. '(' .. cost .. 'gp)')
        end
        return true
    end
    return false
end

function doPlayerBuyItemContainer(cid, containerid, itemid, count, cost, charges)
    if not doPlayerRemoveMoney(cid, cost) then
        return false
    end

    for i = 1, count do
        local container = doCreateItemEx(containerid, 1)
        for x = 1, getContainerCapById(containerid) do
            doAddContainerItem(container, itemid, charges)
        end

        if doPlayerAddItemEx(cid, container, true) ~= RETURNVALUE_NOERROR then
            return false
        end
    end
    return true
end

function getCount(string)
    local b, e = string:find("%d+")
    return b and e and tonumber(string:sub(b, e)) or -1
end
 
I reckon you need to add keywords[#keywords + 1] = "buy" to the keywords here:
Lua:
        if names ~= nil and SHOPMODULE_MODE ~= SHOPMODULE_MODE_TRADE then
            for i, name in pairs(names) do
                local parameters = {
                        itemid = itemid,
                        cost = cost,
                        eventType = SHOPMODULE_BUY_ITEM,
                        module = self,
                        realName = realName or ItemType(itemid):getName(),
                        subType = itemSubType or 1
                    }

                keywords = {}
                keywords[#keywords + 1] = name
                local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
                node:addChildKeywordNode(self.yesNode)
                node:addChildKeywordNode(self.noNode)
            end
        end
In other words just add it i.e above keywords[#keywords + 1] = name like this:
Lua:
        if names ~= nil and SHOPMODULE_MODE ~= SHOPMODULE_MODE_TRADE then
            for i, name in pairs(names) do
                local parameters = {
                        itemid = itemid,
                        cost = cost,
                        eventType = SHOPMODULE_BUY_ITEM,
                        module = self,
                        realName = realName or ItemType(itemid):getName(),
                        subType = itemSubType or 1
                    }

                keywords = {}
                keywords[#keywords + 1] = "buy"
                keywords[#keywords + 1] = name
                local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
                node:addChildKeywordNode(self.yesNode)
                node:addChildKeywordNode(self.noNode)
            end
        end
Let me know if it was of any help!
 
@Kaspar Ye but it still dosent work. I think u are miss understandind me. The problem is with selling items not with buying. Buy items works but i cant sell.

21:26 GOD [15]: buy war hammer
21:26 Nah'Bob: Do you want to buy 1 war hammer for 10000 gold coins?
21:26 GOD [15]: sell war hammer
21:26 Nah'Bob: Do you want to buy 1 war hammer for 10000 gold coins?
 
Strange. In my world you should now be required to write "buy" before purchasing an item.
I can't be of more help, sorry.
Someone else might be able to help you though! :)
 
Back
Top