Ir para conteúdo

Ye Olde Blacksmith


Oneshot

Posts Recomendados

Nome: Ye Olde Blacksmith

Tipo: NPC

Autor: Oneshot

 


 

Este é um simples NPC pedido por muitos, ele troca certos itens por outros itens, similar ao sistema de forja que desenvolvi.

 


 

Crie um arquivo chamado blacksmith.lua em data/npc/scripts, com o nome que você desejar. Coloque o conteúdo abaixo:

 


local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

local forge = {}


local recipes = {
[2390] = {
	{2157, 1},
	{2400, 1},
	{2149, 100}
}
}

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 onGreetCallback(cid)
talkState[cid] = 0
forge[cid] = {match = nil, recipe = nil}
return true
end

function onCreatureSayCallback(cid, class, msg)
if(not npcHandler:isFocused(cid)) then
	return false
end

local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

if talkState[talkUser] == 0 then
	for itemid, items in pairs(recipes) do
		if msgcontains(msg, getItemNameById(itemid)) then
			forge[talkUser] = {match = itemid, recipe = items}
			break
		end
	end

	if forge[talkUser].match ~= nil then
		local str, sep = "Good choice, I will need"
		local n = 1
		for _, item in ipairs(forge[talkUser].recipe) do
			sep = (n == 1) and " " or (n == #forge[talkUser].recipe and " and " or ", ")
			str = str .. sep .. (item[2] or 1) .." ".. getItemNameById(item[1])
			n = n + 1
		end
		str = str ..". Agreed?"
		npcHandler:say(str, cid)
		talkState[talkUser] = 1
	else
		npcHandler:say("Sorry, I don't know how to forge it.", cid)
	end
elseif talkState[talkUser] == 1 then
	if msgcontains(msg, "yes") then
		local ret = {}
		for _, item in ipairs(forge[talkUser].recipe) do
			local g = getPlayerItemById(cid, true, item[1])
			if g.uid > 0 and math.max(1, g.type) >= item[2] then
				table.insert(ret, {g.uid, item[2]})
			else
				ret = {}
				break
			end
		end

		if #ret > 0 then
			for _, item in ipairs(ret) do
				doRemoveItem(item[1], item[2])
			end
			doPlayerAddItemEx(cid, doCreateItemEx(forge[talkUser].match, 1))
			npcHandler:say("Thank you, heh, here is your ".. getItemNameById(forge[talkUser].match) ..".", cid)
		else
			npcHandler:say("Sorry, but you don't have all the items I need.", cid)
			talkState[talkUser] = 0
			forge[talkUser] = {match = nil, recipe = nil}
		end
	elseif msgcontains(msg, "no") then
		npcHandler:say("Alright, do you need anything else?.", cid)
		talkState[talkUser] = 0
		forge[talkUser] = {match = nil, recipe = nil}
	end
end
return true
end

local greet, sep = "Hello, |PLAYERNAME|. Actually I can trade {rare} stuff for some items, pretty, heh? This is what I have:"
local n = 1
for item, recipe in pairs(recipes) do
sep = (n == 1) and " " or (n == #recipes and " and " or ", ")
greet = greet .. sep .. "{".. getItemNameById(item) .."}"
n = n + 1
end

npcHandler:setMessage(MESSAGE_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, onCreatureSayCallback)
npcHandler:setCallback(CALLBACK_GREET, onGreetCallback)
npcHandler:addModule(FocusModule:new())

 

Crie um arquivo XML para o NPC em data/npc/

 


<?xml version="1.0" encoding="UTF-8"?>
<npc name="Ye Olde Blacksmith" script="nome_do_arquivo.lua" walkinterval="0" floorchange="0">
<health now="100" max="100"/>
<look type="128" head="17" body="54" legs="114" feet="0" addons="2"/>
</npc>

 

Você pode mudar o visual do NPC alterando os números dessa linha:

 

<look type="128" head="17" body="54" legs="114" feet="0" addons="2"/>

 


 

Para configurar os itens que o NPC pode fazer e os itens que são necessários, altere a tabela abaixo:

 



local recipes = {
[2390] = {
	{2157, 1},
	{2400, 1},
	{2149, 100}
}
}

 

Sempre seguindo a estrutura da tabela, colocando vírgula no final da chave "}" com exceção da última.

 

13:45 Ye Olde Blacksmith: Hello, Oneshot. Actually I can trade rare stuff for some items, pretty, heh? This is what I have: magic longsword

13:45 Oneshot [200]: magic longsword

13:45 Ye Olde Blacksmith: Good choice, I will need 1 gold nugget, 1 magic sword and 100 small emerald. Agreed?

 


 

Abraços.

Link para o comentário
Compartilhar em outros sites

×
×
  • Criar Novo...