Module
BannerFromCommand
From Dogcraft Wiki
Documentation for this module may be created at Module:BannerFromCommand/doc
-- Copyright (c) 2024, XPModder, Dogcraft.net and contributors
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
-- End license text.
-- A module to parse minecraft give commands for banners
local p = {}
-- The main function called on #invoke via the template
function p.Parse(frame)
local args = {}
-- get the arguments
if frame == mw.getCurrentFrame() then
args = frame:getParent().args
else
args = frame.args
end
--args = frame.args
--get alignment to pass on to banner template
local alignment = tostring(args.align)
if alignment == nil or alignment == "nil" then
alignment = "bottom"
end
--get scaling to pass on to banner template
local scaling = tonumber(args.scale)
if scaling == nil or scaling == NaN then
scaling = 1
end
--get command - this is the mc give command
local command = tostring(args.command)
if command == nil or command == "nil" then
return "Error! No command given!"
end
--get background color and beggining of nbt
local bannerstart, bannerstop, bannercolor = string.find(command, "%s*(%S*)_banner%s*%[")
--get nbt data part of the string
local nbt = string.sub(command, bannerstop)
--get an iterator over each pattern
local iterator = string.gmatch(nbt, "{([^}]*)}")
--this stores the arguments for the banner template
local opts = {align = alignment, scale = scaling}
--iterate over up to 16 patterns or until reaching the end of the nbt data string
for i = 1, 16 do
local part = iterator()
if part == nil then
break
end
--get the pattern name and use the BannerPatternTranslate template to translate the nbt name to the gui name used by Banner
local patternstart, patternstop, pattern = string.find(part, "pattern:%s*([^%s,}]*)")
local guipattern = frame:expandTemplate{title="BannerPatternTranslate", args={"fromNBT", pattern}}
--get the color for the pattern
local colorstart, colorstop, color = string.find(part, "color:%s*([^%s,}]*)")
--add the arguments
opts["pattern" .. i] = guipattern
opts["patternColor" .. i] = color
end
--finally expand the template with all the arguments collected and return the result
res = frame:expandTemplate{title="Banner", args=opts}
return res
end
return p