Module
BannerFromCommand
From Dogcraft Wiki
No edit summary |
(documentation) |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 22: | Line 22: | ||
-- get the arguments | -- get the arguments | ||
if frame == mw.getCurrentFrame() then | |||
args = frame:getParent().args | args = frame:getParent().args | ||
else | else | ||
args = frame.args | args = frame.args | ||
end | end | ||
args = | --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) | local command = tostring(args.command) | ||
if command == nil or command == "nil" then | if command == nil or command == "nil" then | ||
| Line 34: | Line 47: | ||
end | end | ||
--get background color and beggining of nbt | |||
local bannerstart, bannerstop, bannercolor = string.find(command, "%s*(%S*)_banner%s*%[") | local bannerstart, bannerstop, bannercolor = string.find(command, "%s*(%S*)_banner%s*%[") | ||
--get nbt data part of the string | |||
local nbt = string.sub(command, bannerstop) | local nbt = string.sub(command, bannerstop) | ||
local iterator = string.gmatch(nbt, "{( | --get an iterator over each pattern | ||
local iterator = string.gmatch(nbt, "{([^}]*)}") | |||
--this stores the arguments for the banner template | |||
local opts = {} | 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 | for i = 1, 16 do | ||
| Line 50: | Line 67: | ||
end | 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 patternstart, patternstop, pattern = string.find(part, "pattern:%s*([^%s,}]*)") | ||
local guipattern = frame:expandTemplate{title="BannerPatternTranslate", args={"fromNBT", pattern}} | 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,}]*)") | local colorstart, colorstop, color = string.find(part, "color:%s*([^%s,}]*)") | ||
--add the arguments | |||
opts["pattern" .. i] = guipattern | opts["pattern" .. i] = guipattern | ||
opts["patternColor" .. i] = color | opts["patternColor" .. i] = color | ||
end | end | ||
res = | --finally expand the template with all the arguments collected and return the result | ||
res = frame:expandTemplate{title="Banner", args=opts} | |||
return res | return res | ||
Latest revision as of 18:04, 10 February 2026
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