Module:Sandbox/MonkeysHK

-- -- Taken from: https://minecraft.gamepedia.com/Module:UI local getArgs = require('Module:Arguments').getArgs local loader = require('Module:Loader')

local string, table, yesno, _, uiText, inventorySlot, crafting, Interface, random = loader.require('String', 'Table', 'Yesno', 'LibraryUtil', 'UIText', 'Inventory slot', 'Crafting', 'UI/Core', 'Random')

local slot = inventorySlot.slot local parseFrameText = inventorySlot.parseFrameText

local addSlot = crafting.addSlot

local p = {}

local function getrandomid return 'ui-' .. random.number{100000000, 999999999} end local function wrapdiv(s, wr) return yesno(wr, false) and (' %s '):format(s) or s end

function p.ui(frame) local args = getArgs(frame, { removeBlanks = false }) local ui = Interface(args[1], args.id, {		text = (args.return_text or args.goback_text or args['return'] or args.goback),		link = (args.return_link or args.goback_link),		id = (args.return_id or args.goback_id),	}, yesno(args.hide, false), args.fill, args.rows, args.cols, args.noarrow, args.noclose, (args.arrow or args.arrow_), (args.close or args.close_)) local x, y, rowCount = 1, 1, 1 local maxRows, maxCols = tonumber(args.rows) or 6, tonumber(args.cols) or 9 local fill = yesno(args.fill, true) local defaultnolink = yesno((args['defaultnolink'] or args['dnl']), false) local function decodeCommas(...) local t = { ... }		for i, v in ipairs(t) do			t[i] = t[i]:gsub('\255', ',') end return unpack(t) end local function setSlot(v, x, y)		local isCustom = v:match('(.-)') v = v:gsub('\\,', '\255') local val, id, title, text = decodeCommas(string.unpackedSplit(v, '%s*,%s*')) local _val = val local val, num = val:match('^%s*(.+)%s*[;]%s*(%d+)%s*$') val = val or _val local link_specify if not id or id == '' then id = nil else local _id = id			id, link_specify = id:match('^%s*(.+)%s*[;]%s*(.+)%s*$') id = id or _id end -- [IMPORTANT] Transform newlines into `/` if text and text:match('\n') then text = text:gsub('\n', '/') end ui:setSlot(x, y, isCustom and v or {			val .. (num and ', ' .. num or ),			class = (id and id ~= 'none') and 'goto-' .. id:gsub('^goto%-', ),			link = (id and id ~= 'none') and 'none' or (link_specify or (defaultnolink and 'none' or'')),			title = title,			text = text,		}, isCustom) end for i, v in ipairs(args) do		if i > 1 then if x > 6 then break; end if v:match('^%-$') then for y2 = y, maxCols do					if fill then ui:setBlankSlot(x, y2) else ui:setSlot(x, y2, '') end end x = x+1 y = 1 elseif y > maxCols then y = 1 x = x+1 else setSlot(v, x, y)				y = y+1 end end end for k, v in pairs(args) do		local k = tostring(k) if k:match('^%d+%s*,%s*%d+$') then local x, y = k:match('^(%d+)%s*,%s*(%d+)$') setSlot(v, tonumber(x), tonumber(y)) elseif k:match('^row%s*([1-6])%s*$') then local x = k:match('^row%s*([1-6])$') local endParams = v:match('%s*;%s*([%d%s,]-)%s*$') v = v:gsub('%s*;%s*([%d%s,]-)%s*$', '') endParams = endParams and string.split(endParams, '%s*,%s*') or {1, maxCols} if #endParams > 2 then for dummy, y in pairs(endParams) do					setSlot(v:gsub('%$n', y):gsub('\\%$n', '$n'), tonumber(x), tonumber(y)) end else for y = tonumber(endParams[1] or 1), tonumber(endParams[2] or maxCols), 1 do					setSlot(v:gsub('%$n', y):gsub('\\%$n', '$n'), tonumber(x), tonumber(y)) end end elseif k:match('^colu?m?n?%s*([1-9])%s*$') then local y = k:match('^colu?m?n?%s*([1-9])%s*$') local endParams = v:match('%s*;%s*([%d%s,]-)%s*$') v = v:gsub('%s*;%s*([%d%s,]-)%s*$', '') endParams = endParams and string.split(endParams, '%s*,%s*') or {1, maxRows} if #endParams > 2 then for dummy, x in pairs(endParams) do					setSlot(v:gsub('%$n', x):gsub('\\%$n', '$n'), tonumber(x), tonumber(y)) end else for x = tonumber(endParams[1] or 1), tonumber(endParams[2] or maxRows), 1 do					setSlot(v:gsub('%$n', x):gsub('\\%$n', '$n'), tonumber(x), tonumber(y)) end end end end return tostring(ui) end

function p.uiPager(frame) local function rc(r, c)		return ('%s, %s'):format(r, c)	end local function pagerid(base, i)		if i > 1 then base = ('%s-%s'):format(base, i)		end return base end local args = getArgs(frame, { removeBlanks = false }) local id = args.id or getrandomid if yesno(args.title_index, false) then args[1] = '({0}/{1}) ' .. args[1] end local ui = table.merge({		[1] = args[1],		fill = 'surround',	}, table.mapNamed(args, function (k, v)		if type(k) ~= 'number' then return v else return nil end end)) -- allocate items onto UI pages local pages = {} local rows, cols = args.rows or 6, args.cols or 9 local i = 2 while args[i] do		local ui2 = table.deepCopy(ui) for r = 2, rows - 1 do			for c = 2, cols - 1 do				if args[i] then ui2[rc(r, c)] = args[i] i = i + 1 else break end end end table.push(pages, ui2) end return wrapdiv(table.concat(table.map(pages, function (v, i, t)		if t[i - 1] and not v[rc(rows, 1)] then			v[rc(rows, 1)] = ('Arrow, %s, &aPrevious Page, &ePage %s'):format(pagerid(id, i - 1), i - 1)		end		if t[i + 1] and not v[rc(rows, cols)] then			v[rc(rows, cols)] = ('Arrow, %s, &aNext Page, &ePage %s'):format(pagerid(id, i + 1), i + 1)		end		v.id = pagerid(id, i)		if i > 1 then			v.hide = true		end		v[1] = v[1]:gsub('{0}', i):gsub('{1}', #t)		mw.logObject(v)		return tostring(p.ui(v))	end)), #pages > 1 and args.clickable) end

function p.shopUI(frame) local args = getArgs(frame, { removeBlanks = false }) return p.uiPager(table.merge({ ['6, 5'] = 'Hopper, none; none, &aSell Item, &7Click items in your inventory to/&7sell them to this Shop!', close = 'none', arrow = 'none', }, table.deepCopy(args, true))) end

function p.test local ui = Interface({"Foo", fill=false}) local ui2 = ui	mw.logObject(ui:isEmptySlot(2, 2)) mw.logObject(ui2:isEmptySlot(2, 2)) ui2:setSlot(2, 2, {"Water Bottle"}) mw.logObject(ui:isEmptySlot(2, 2)) mw.logObject(ui2:isEmptySlot(2, 2)) mw.logObject(ui2.slots) end

p.createBlankUI = Interface p.slot = slot return p