Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:PlayerHead: Difference between revisions

From Vault Hunters Official Wiki
Created page with "local p = {} -- escape HTML attribute values becuase I have idea if this exists or not local function escape(s) return (s:gsub('[\"&\'<>]', { ['"'] = """, ["&"] = "&", ["'"] = "'", ["<"] = "<", [">"] = ">" })) end function p.render(frame) local args = frame:getParent().args local name = mw.text.trim(args[1] or "") local size = tonumber(args[2]) or 64 local title = args[3] or ""..."
 
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
--  escape HTML attribute values becuase I have idea if this exists or not
local function escape(s)
    return (s:gsub('[\"&\'<>]', {
        ['"'] = "&quot;",
        ["&"] = "&amp;",
        ["'"] = "&#39;",
        ["<"] = "&lt;",
        [">"] = "&gt;"
    }))
end


function p.render(frame)
function p.render(frame)
     local args   = frame:getParent().args
     local args = frame:getParent().args
     local name   = mw.text.trim(args[1] or "")
     local name = mw.text.trim(args[1] or "")
     local size   = tonumber(args[2]) or 64
     local size = tonumber(args[2]) or 64
     local title = args[3] or ""
     local title = mw.text.trim(args[3] or "")
     local overlay = (args.overlay or "yes"):lower()  -- "yes" (default) or "no"
     local overlay = mw.text.trim(args["overlay"] or "yes")


     if name == "" then
     if name == "" then
         return "<strong class=\"error\">PlayerHead: no player specified</strong>"
         return "Error: No player name provided."
    end
 
    -- Construct the image URL
    local base = "https://mc-heads.net/avatar/"
    local url = string.format("%s%s/%d", base, name, size)
    if overlay == "no" then
        url = url .. ".png?overlay=false"
     end
     end


     -- Build URL – MC-Heads supports sizes 8 - 600 px and an optional /nohelm suffix :contentReference[oaicite:0]{index=0}
     -- Build the <img> tag
     local url = string.format(
     local img = mw.html.create("img")
         "https://mc-heads.net/avatar/%s/%d%s",
         :addClass("mc-player-head")
         escape(name),
         :attr("src", url)
         size,
         :attr("width", size)
         overlay == "no" and "/nohelm" or ""
         :attr("height", size)
    )


     -- Make da tag
     if title ~= "" then
    local img = '<img class="mc-player-head"'
         img:attr("title", title)
         .. string.format(' src="%s"', url)
         img:attr("alt", title)
         .. string.format(' width="%d" height="%d"', size, size)
    else
         .. (title ~= "" and string.format(' title="%s" alt="%s"', escape(title), escape(title)) or ' alt=""')
         img:attr("alt", "")
        .. ' />'
    end


     return img
     return tostring(img)
end
end


return p
return p

Revision as of 17:46, 6 July 2025

Documentation for this module may be created at Module:PlayerHead/doc

local p = {}

function p.render(frame)
    local args = frame:getParent().args
    local name = mw.text.trim(args[1] or "")
    local size = tonumber(args[2]) or 64
    local title = mw.text.trim(args[3] or "")
    local overlay = mw.text.trim(args["overlay"] or "yes")

    if name == "" then
        return "Error: No player name provided."
    end

    -- Construct the image URL
    local base = "https://mc-heads.net/avatar/"
    local url = string.format("%s%s/%d", base, name, size)
    if overlay == "no" then
        url = url .. ".png?overlay=false"
    end

    -- Build the <img> tag
    local img = mw.html.create("img")
        :addClass("mc-player-head")
        :attr("src", url)
        :attr("width", size)
        :attr("height", size)

    if title ~= "" then
        img:attr("title", title)
        img:attr("alt", title)
    else
        img:attr("alt", "")
    end

    return tostring(img)
end

return p