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

Module:VanillaImage

From Vault Hunters Official Wiki
Revision as of 05:32, 16 December 2025 by Mnooseman (talk | contribs) (Initial creation)
(diff) ← Older revision | Latest revision (diff) | Newer revision β†’ (diff)

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

-- Module:MinecraftImage
-- Links to images from minecraft.wiki

local p = {}

function p.item(frame)
    -- Get the image name from the template parameter
    local imageName = frame.args[1] or frame.args.name or ""
    local size = frame.args.size or frame.args[2] or "32px"
    local alt = frame.args.alt or imageName
    local link = frame.args.link or ""
    
    -- Remove any file extension if provided
    imageName = imageName:gsub("%.png$", ""):gsub("%.gif$", "")
    
    -- Clean up the image name (replace spaces with underscores)
    imageName = imageName:gsub(" ", "_")
    
    -- Construct the URL to minecraft.wiki
    local baseUrl = "https://minecraft.wiki/images/"
    
    -- For the actual file path, we need the MD5 hash structure that MediaWiki uses
    -- Since we can't calculate MD5 in Lua easily, we'll use the direct Special:Redirect approach
    local imageUrl = "https://minecraft.wiki/w/Special:Redirect/file/" .. mw.uri.encode(imageName .. ".png", "PATH")
    
    -- Build the image HTML
    local linkStart = ""
    local linkEnd = ""
    
    if link ~= "" then
        linkStart = "[[" .. link .. "|"
        linkEnd = "]]"
    end
    
    -- Return the formatted image
    return linkStart .. "[[File:" .. imageUrl .. "|" .. size .. "|alt=" .. alt .. "]]" .. linkEnd
end

function p.direct(frame)
    -- Direct URL approach - displays external image
    local imageName = frame.args[1] or frame.args.name or ""
    local size = frame.args.size or frame.args[2] or "32"
    local alt = frame.args.alt or imageName
    local link = frame.args.link or ""
    local prefix = frame.args.prefix or "auto"
    
    -- Remove any file extension if provided
    imageName = imageName:gsub("%.png$", ""):gsub("%.gif$", "")
    
    -- Clean up the image name
    imageName = imageName:gsub(" ", "_")
    
    -- Add Invicon_ prefix automatically if not already present
    if prefix == "auto" and not imageName:match("^Invicon_") then
        imageName = "Invicon_" .. imageName
    elseif prefix ~= "auto" and prefix ~= "" then
        imageName = prefix .. imageName
    end
    
    -- Construct the direct image URL
    local imageUrl = "https://minecraft.wiki/w/Special:Redirect/file/" .. mw.uri.encode(imageName .. ".png", "PATH")
    
    -- Parse size (remove 'px' if present)
    local sizeNum = size:gsub("px$", "")
    
    -- Build HTML
    local html = mw.html.create('img')
        :attr('src', imageUrl)
        :attr('alt', alt)
        :attr('width', sizeNum)
        :addClass('minecraft-item')
    
    if link ~= "" then
        local linkHtml = mw.html.create('a')
            :attr('href', mw.title.new(link):fullUrl())
            :node(html)
        return tostring(linkHtml)
    end
    
    return tostring(html)
end

return p