NPC Item Customizer
Informações
Nome: Item Customizer
Categoria: NPCs
Código e créditos gerais: Omega
Descrição
Esse NPC pode mudar o nome e a descrição de um item escolhido (armor, legs, boots e helmet) por um preço configurável. Somente letras, espaço, apóstrofo e hífen podem ser usados nos nomes. Para o nome do item, o número máximo de caracteres são 20, para descrições, 30. Para ambos, o mínimo são 5 caracteres.
Tutorial
data/npc/Hancock.xml
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Hancock" script="data/npc/scripts/custom_items.lua" walkinterval="3000" floorchange="0">
<health now="100" max="100"/>
<look type="132" head="76" body="114" legs="96" feet="115" addons="3"/>
<parameters>
<parameter key="message_greet" value="Hello, |PLAYERNAME|. Would you like to {customize} your items?"/>
<parameter key="message_farewell" value="Go away!"/>
<parameter key="message_walkaway" value="See ya!"/>
</parameters>
</npc>
data/npc/scripts/custom_items.lua
-- Configurações
local price = 50000 -- Preço em gps
local needStorage = false -- Precisa de storage para customizar? [true/false]
local storage = nil -- Caso a opção acima seja verdadeira, qual storage irá precisar
local storageValue = nil -- Valor da storage necessária (>=)
-- -- -- --
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
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
_ctrl_var = {}
local function hasIllegalChar(str)
local i = 1
local size = str:len()
while i <= size do
local char = str:byte(i)
if not ((char >= 65 and char <= 90) or (char >= 97 and char <= 122) or char == 32 or char == 45 or char == 39) then
return true
end
i = i + 1
end
return false
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 slots = {armor = 4, legs = 7, boots = 8, helmet = 1}
if msgcontains(msg, "customize") then
if (needStorage and getPlayerStorageValue(cid, storage) >= storageValue) or not needStorage then
selfSay("I can customize your items' {names} and {descriptions}. It will cost you {" .. price .. "}gps.", cid)
talkState[talkUser] = 1
_ctrl_var[cid] = nil
else
selfSay("I'm sorry, I cannot help you yet.", cid)
end
elseif talkState[talkUser] == 1 and msgcontains(msg, "name") then
selfSay("I can customize your {armor}, {legs}, {boots} or {helmet}.", cid)
talkState[talkUser] = 2
elseif talkState[talkUser] == 2 and slots[msg:lower()] then
local item = getPlayerSlotItem(cid, slots[msg:lower()])
if item and item.uid and item.uid > 0 then
selfSay("What name do you want your item to have?", cid)
_ctrl_var[cid] = slots[msg:lower()]
talkState[talkUser] = 3
else
selfSay("You must have an equipped " .. msg:lower() .. " to customize.", cid)
end
elseif talkState[talkUser] == 3 then
local msgSize = msg:len()
local item = getPlayerSlotItem(cid, _ctrl_var[cid])
if item and item.uid and item.uid > 0 then
if msgSize >= 5 and msgSize <= 20 then
if not hasIllegalChar(msg) then
if doPlayerRemoveMoney(cid, price) then
doItemSetAttribute(item.uid, "name", msg)
selfSay("Your item's name has been changed.", cid)
_ctrl_var[cid] = nil
talkState[talkUser] = 0
else
selfSay("You do not have enough money.", cid)
end
else
selfSay("Your selected name has illegal characters.", cid)
end
else
selfSay("The item's name must not be larger than 20 characters nor smaller than 5.", cid)
end
else
selfSay("Your item has been moved. I can customize your {armor}, {legs}, {boots} or {helmet}", cid)
talkState[talkUser] = 2
end
elseif talkState[talkUser] == 1 and msgcontains(msg, "description") then
selfSay("I can customize your {armor}, {legs}, {boots} or {helmet}.", cid)
talkState[talkUser] = 5
elseif talkState[talkUser] == 5 and slots[msg:lower()] then
local item = getPlayerSlotItem(cid, slots[msg:lower()])
if item and item.uid and item.uid > 0 then
selfSay("What description do you want your item to have?", cid)
_ctrl_var[cid] = slots[msg:lower()]
talkState[talkUser] = 6
else
selfSay("You must have an equipped " .. msg:lower() .. " to customize.", cid)
end
elseif talkState[talkUser] == 6 then
local msgSize = msg:len()
local item = getPlayerSlotItem(cid, _ctrl_var[cid])
if item and item.uid and item.uid > 0 then
if msgSize >= 5 and msgSize <= 30 then
if not hasIllegalChar(msg) then
if doPlayerRemoveMoney(cid, price) then
doItemSetAttribute(item.uid, "description", msg)
selfSay("Your item's description has been changed.", cid)
_ctrl_var[cid] = nil
talkState[talkUser] = 0
else
selfSay("You do not have enough money.", cid)
end
else
selfSay("Your selected description has illegal characters.", cid)
end
else
selfSay("The item's description must not be larger than 30 characters nor smaller than 5.", cid)
end
else
selfSay("Your item has been moved. I can customize your {armor}, {legs}, {boots} or {helmet}.", cid)
talkState[talkUser] = 5
end
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Configuração
O preço e a possibilidade de requerer um certo valor de storage para usar o NPC são configuráveis nas primeiras linhas do código custom_items.lua.