local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
local items = {
["two handed sword"] = {itemid = 2377, price = 450},
["battle axe"] = {itemid = 2378, price = 80},
-- Add more items as needed
}
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
function creatureSayCallback(cid, type, msg)
if (not npcHandler:isFocused(cid)) then
return false
end
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
local player = Player(cid)
if msgcontains(msg, 'sell') or msgcontains(msg, 'sells') or msgcontains(msg, 'vende') then
local itemList = "{"
local isFirstItem = true
for itemName, _ in pairs(items) do
if isFirstItem then
itemList = itemList .. itemName
isFirstItem = false
else
itemList = itemList .. ", " .. itemName
end
end
itemList = itemList .. "}"
npcHandler:say("I'm interested in the items here: " .. itemList, cid)
talkState[talkUser] = 1
elseif talkState[talkUser] == 1 then
local chosenItem = string.lower(msg)
local itemData = items[chosenItem]
local slotItem = player:getSlotItem(SLOT_ARMOR)
if itemData and player:removeItem(itemData.itemid, 1, -1, true) then
player:addMoney(itemData.price)
npcHandler:say("So will you sell me " .. chosenItem .. " for " .. itemData.price .. " gold coins.", cid)
else
npcHandler:say("You do not have the item in your backpack or it cannot be in the sell slots.", cid)
end
talkState[talkUser] = 0
elseif msgcontains(msg, 'offer') or msgcontains(msg, 'trade') then
local offerList = "two handed sword - 450 gold coins\nbattle - 80 gold coins"
npcHandler:say("Here are the available offers:\n" .. offerList, cid)
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())