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

NPC [TFS 1.X] NPC Addons (Modal Window)

darkmu

Well-Known Member
Joined
Aug 26, 2007
Messages
274
Solutions
1
Reaction score
50
Location
Paraná,Brazil
For those who like ModalWindow, I made a system for addon.
Be at the discretion of you to help improve my system, sorry I am still not so good at making perfect systems.

Locate this function in your global.lua or if you don't have it, dependency on some file in your lib
Lua:
function capAll(str)
    local newStr = ""; wordSeparate = string.gmatch(str, "([^%s]+)")
    for v in wordSeparate do
        v = v:gsub("^%l", string.upper)
        if newStr ~= "" then
            newStr = newStr.." "..v
        else
            newStr = v
        end
    end
    return newStr
end

Create a file in your LIB root folder with name addonModal or any name.
Lua:
function Player:sendMainAddonWindow(config)
    local window = ModalWindow {
        title = "Select one of the Addons",
        message = "To proceed select the Addon",
    }
  
    local cid = self:getId()
  
    -- Add choices from the action script
    for i = 1, #config.system do
        if not self:hasOutfit(config.system[i].male, 3) and not self:hasOutfit(config.system[i].female, 3) then 
            local choice = window:addChoice(config.system[i].addonName)
            choice.addonID = i
        end
    end 
  
    buttonText = "Selecionar"
    window:addButton(buttonText,
        function(button, choice)
            local self = Player(cid)
            if self then
                self:sendAllAddonListWindow(config, choice.id)
            end
        end
    )
    window:setDefaultEnterButton(buttonText)
  
  
    window:addButton("Exit")

    window:setDefaultEnterButton("Selecionar")
    window:setDefaultEscapeButton("Exit")
    window:sendToPlayer(self)
end

function Player:checkSexPlayer(config)
    if self:getSex() == 0 then
        return config.outfitId.female
    else
        return config.outfitId.male
    end 
end

function Player:sendAllAddonListWindow(config, lastChoice)
    local window = ModalWindow {
        title = config.system[lastChoice].addonName,
        message = "Select your Addon (First ou Second Addon)",
    }   
    local idOutfit = self:checkSexPlayer(config.system[lastChoice]) 
    local addonTable = config.system[lastChoice].items[idOutfit]
    local onlyIdOutfits = config.system[lastChoice].outfitId

    local cid = self:getId()
  
    if not self:hasOutfit(onlyIdOutfits.male, 1) and not self:hasOutfit(onlyIdOutfits.female, 1) then 
        buttonText = "First"
        window:addButton(buttonText,
            function(button, choice)
                local self = Player(cid)
                if self then
                    self:acceptAddonWindow(addonTable, 1, onlyIdOutfits, lastChoice, config)
                end
            end
        )
    end
  
    if not self:hasOutfit(onlyIdOutfits.male, 2) and not self:hasOutfit(onlyIdOutfits.female, 2) then 
        buttonText = "Second"
        window:addButton(buttonText,
            function(button, choice)
                local self = Player(cid)
                if self then
                    self:acceptAddonWindow(addonTable, 2, onlyIdOutfits, lastChoice, config)
                end
            end
        )
    end 

    window:addButton("Exit")
    window:setDefaultEscapeButton("Exit")

    window:sendToPlayer(self)
end

function Player:acceptAddonWindow(config, addonId, onlyIdOutfits, lastChoice, configMainTable)
    local window = ModalWindow {
        title = "Select an option to proceed",
        message = "Click on Details to check the necessary items or click on Accept to get your addon",
    }   
    local cid = self:getId()     
    local checkItensAddon = self:checkItensAddon(config[addonId])
  
    if checkItensAddon then
        window:addButton("OK",
            function(button, choice)
                local self = Player(cid)
                if self then
                    self:setAddonId(onlyIdOutfits, addonId)
                end
                return true
            end
        )
    end 
  
    buttonText = "Back"
    window:addButton(buttonText,
        function(button, choice)
            local self = Player(cid)
            if self then
                self:sendAllAddonListWindow(configMainTable, lastChoice)
            end
        end
    )

    buttonText = "Details"
    window:addButton(buttonText,
        function(button, choice)
            local self = Player(cid)
            if self then
                self:detailsAddonWindowFirst(config[addonId], config , addonId, onlyIdOutfits, lastChoice, configMainTable)
            end
        end
    )
  
    window:addButton("Exit")
    window:setDefaultEscapeButton("Exit")
    window:sendToPlayer(self)
end

function Player:detailsAddonWindowFirst(config, tableConfig, addonId, onlyIdOutfits, lastChoice, configMainTable)
    local window = ModalWindow {
        title = "Itens Requiridos para seu Addon",
        message = self:detailsAddonWindow(config),
    }
  
    local cid = self:getId()
    local checkItensAddon = self:checkItensAddon(config)
  
    if checkItensAddon then
        window:addButton("OK",
            function(button, choice)
                local self = Player(cid)
                if self then
                    self:setAddonId(config, onlyIdOutfits, addonId, configMainTable, lastChoice)
                end
                return true
            end
        )
    end
  
    window:addButton("Back",
        function(button, choice)
            local self = Player(cid)
            if self then
                self:acceptAddonWindow(tableConfig, addonId, onlyIdOutfits , lastChoice,  configMainTable)
            end
            return true
        end
    )
  
    window:sendToPlayer(self)
end

function Player:checkItensAddon(config)
    for i = 1, #config.reqItems do
        if self:getItemCount(config.reqItems[i].item) < config.reqItems[i].count then         
            return false
        end
    end 
  
    return true
end

function Player:detailsAddonWindow(config)
    local itemTable = config.reqItems 
    local details = "Required Addon Items:\n "
    for i = 1, #itemTable do         
        local reqItems = itemTable[i].item             
      
        local reqItemsCount = itemTable[i].count
        local reqItemsOnPlayer = self:getItemCount(reqItems)
            details = details.."\n- ".. (capAll(ItemType(reqItems):getName()) .." ["..reqItemsOnPlayer.."/"..reqItemsCount.."]")
    end 
    return details
end

function Player:setAddonId(config, outfits, addonId, configMainTable, lastChoice)
    for i = 1, #config.reqItems do
        self:removeItem(config.reqItems[i].item, config.reqItems[i].count)
    end     
  
    self:addOutfitAddon(outfits.female, addonId)
    self:addOutfitAddon(outfits.male, addonId)
    self:getPosition():sendMagicEffect(CONST_ME_FIREATTACK)

end

Now create any NPC, and add this configuration to your NPC
 

Attachments

  • NPC Addon (Modal Window).zip
    5.9 KB · Views: 87 · VirusTotal
Last edited:
For those who like ModalWindow, I made a system for addon.
Be at the discretion of you to help improve my system, sorry I am still not so good at making perfect systems.

Locate this function in your global.lua or if you don't have it, dependency on some file in your lib
Lua:
function capAll(str)
    local newStr = ""; wordSeparate = string.gmatch(str, "([^%s]+)")
    for v in wordSeparate do
        v = v:gsub("^%l", string.upper)
        if newStr ~= "" then
            newStr = newStr.." "..v
        else
            newStr = v
        end
    end
    return newStr
end

Create a file in your LIB root folder with name addonModal or any name.
Lua:
function Player:sendMainAddonWindow(config)
    local window = ModalWindow {
        title = "Select one of the Addons",
        message = "To proceed select the Addon",
    }
 
    local cid = self:getId()
 
    -- Add choices from the action script
    for i = 1, #config.system do
        if not self:hasOutfit(config.system[i].male, 3) and not self:hasOutfit(config.system[i].female, 3) then
            local choice = window:addChoice(config.system[i].addonName)
            choice.addonID = i
        end
    end
 
    buttonText = "Selecionar"
    window:addButton(buttonText,
        function(button, choice)
            local self = Player(cid)
            if self then
                self:sendAllAddonListWindow(config, choice.id)
            end
        end
    )
    window:setDefaultEnterButton(buttonText)
 
 
    window:addButton("Exit")

    window:setDefaultEnterButton("Selecionar")
    window:setDefaultEscapeButton("Exit")
    window:sendToPlayer(self)
end

function Player:checkSexPlayer(config)
    if self:getSex() == 0 then
        return config.outfitId.female
    else
        return config.outfitId.male
    end
end

function Player:sendAllAddonListWindow(config, lastChoice)
    local window = ModalWindow {
        title = config.system[lastChoice].addonName,
        message = "Select your Addon (First ou Second Addon)",
    }  
    local idOutfit = self:checkSexPlayer(config.system[lastChoice])
    local addonTable = config.system[lastChoice].items[idOutfit]
    local onlyIdOutfits = config.system[lastChoice].outfitId

    local cid = self:getId()
 
    if not self:hasOutfit(onlyIdOutfits.male, 1) and not self:hasOutfit(onlyIdOutfits.female, 1) then
        buttonText = "First"
        window:addButton(buttonText,
            function(button, choice)
                local self = Player(cid)
                if self then
                    self:acceptAddonWindow(addonTable, 1, onlyIdOutfits, lastChoice, config)
                end
            end
        )
    end
 
    if not self:hasOutfit(onlyIdOutfits.male, 2) and not self:hasOutfit(onlyIdOutfits.female, 2) then
        buttonText = "Second"
        window:addButton(buttonText,
            function(button, choice)
                local self = Player(cid)
                if self then
                    self:acceptAddonWindow(addonTable, 2, onlyIdOutfits, lastChoice, config)
                end
            end
        )
    end

    window:addButton("Exit")
    window:setDefaultEscapeButton("Exit")

    window:sendToPlayer(self)
end

function Player:acceptAddonWindow(config, addonId, onlyIdOutfits, lastChoice, configMainTable)
    local window = ModalWindow {
        title = "Select an option to proceed",
        message = "Click on Details to check the necessary items or click on Accept to get your addon",
    }  
    local cid = self:getId()    
    local checkItensAddon = self:checkItensAddon(config[addonId])
 
    if checkItensAddon then
        window:addButton("OK",
            function(button, choice)
                local self = Player(cid)
                if self then
                    self:setAddonId(onlyIdOutfits, addonId)
                end
                return true
            end
        )
    end
 
    buttonText = "Back"
    window:addButton(buttonText,
        function(button, choice)
            local self = Player(cid)
            if self then
                self:sendAllAddonListWindow(configMainTable, lastChoice)
            end
        end
    )

    buttonText = "Details"
    window:addButton(buttonText,
        function(button, choice)
            local self = Player(cid)
            if self then
                self:detailsAddonWindowFirst(config[addonId], config , addonId, onlyIdOutfits, lastChoice, configMainTable)
            end
        end
    )
 
    window:addButton("Exit")
    window:setDefaultEscapeButton("Exit")
    window:sendToPlayer(self)
end

function Player:detailsAddonWindowFirst(config, tableConfig, addonId, onlyIdOutfits, lastChoice, configMainTable)
    local window = ModalWindow {
        title = "Itens Requiridos para seu Addon",
        message = self:detailsAddonWindow(config),
    }
 
    local cid = self:getId()
    local checkItensAddon = self:checkItensAddon(config)
 
    if checkItensAddon then
        window:addButton("OK",
            function(button, choice)
                local self = Player(cid)
                if self then
                    self:setAddonId(config, onlyIdOutfits, addonId, configMainTable, lastChoice)
                end
                return true
            end
        )
    end
 
    window:addButton("Back",
        function(button, choice)
            local self = Player(cid)
            if self then
                self:acceptAddonWindow(tableConfig, addonId, onlyIdOutfits , lastChoice,  configMainTable)
            end
            return true
        end
    )
 
    window:sendToPlayer(self)
end

function Player:checkItensAddon(config)
    for i = 1, #config.reqItems do
        if self:getItemCount(config.reqItems[i].item) < config.reqItems[i].count then        
            return false
        end
    end
 
    return true
end

function Player:detailsAddonWindow(config)
    local itemTable = config.reqItems
    local details = "Required Addon Items:\n "
    for i = 1, #itemTable do        
        local reqItems = itemTable[i].item            
     
        local reqItemsCount = itemTable[i].count
        local reqItemsOnPlayer = self:getItemCount(reqItems)
            details = details.."\n- ".. (capAll(ItemType(reqItems):getName()) .." ["..reqItemsOnPlayer.."/"..reqItemsCount.."]")
    end
    return details
end

function Player:setAddonId(config, outfits, addonId, configMainTable, lastChoice)
    for i = 1, #config.reqItems do
        self:removeItem(config.reqItems[i].item, config.reqItems[i].count)
    end    
 
    self:addOutfitAddon(outfits.female, addonId)
    self:addOutfitAddon(outfits.male, addonId)
    self:getPosition():sendMagicEffect(CONST_ME_FIREATTACK)

end

Now create any NPC, and add this configuration to your NPC
is it possible that this npc sell items for some token currency?
 
i get this error tfs 1.5. npc replies but modal windows never appears
Lua:
Lua Script Error: [Npc interface]
data/npc/scripts/addoner.lua:onCreatureSay
data/lib/addonModal.lua:13: attempt to index local 'choice' (a boolean value)
stack traceback:
        data/lib/addonModal.lua:13: in function 'sendMainAddonWindow'
        data/npc/scripts/addoner.lua:817: in function 'callback'
        data/npc/lib/npcsystem/npchandler.lua:430: in function 'onCreatureSay'
        data/npc/scripts/addoner.lua:805: in function <data/npc/scripts/addoner.lua:805>
 
Back
Top