Module:Crafting

-- -- Taken from: https://minecraft.gamepedia.com/Module:Crafting -- And modified for use in this wiki local p = {} local getArgs = require('Module:Arguments').getArgs local string = require('Module:String') local table = require('Module:Table') local yesno = require('Module:Yesno')

local i18n = { colored = 'Colored', coloredDyes = { 'Orange Dye', 'Magenta Dye', 'Light Blue Dye', 'Yellow Dye', 'Lime Dye', 'Pink Dye', 'Gray Dye', 'Light Gray Dye', 'Cyan Dye', 'Purple Dye', 'Lapis Lazuli', 'Cocoa Beans', 'Cactus Green', 'Red Dye', 'Ink Sack' },	categoryIngredientUsage = 'Category:Recipe using $1', categoryRecipeType = 'Category:$1 recipe', categoryUpcoming = 'Category:Upcoming', itemBlockOfQuartz = 'Block of Quartz', itemBoneMeal = 'Bone Meal', itemBrownMushroom = 'Brown Mushroom', itemCharcoal = 'Charcoal', itemCoal = 'Coal', itemColoredDye = 'Colored Dye', itemDye = 'Dye', itemMushroom = 'Mushroom', itemQuartzBlock = 'Quartz Block', itemRedMushroom = 'Red Mushroom', itemStone = 'Stone', moduleArgs = Module:ProcessArgs, moduleRecipe = Module:Recipe table, moduleSlot = Module:Inventory slot, stoneVariants = { 'Stone', 'Andesite', 'Granite', 'Diorite' }, type = 'Crafting', variantPages = { 'Andesite', 'Banner', 'Bed', 'Diorite', 'Firework Star', 'Granite', 'Pressure Plate', 'Sand', 'Sandstone', 'Shield', 'Slab', 'Stained Glass Pane', 'Stained Glass', 'Stairs', 'Stone Bricks', 'Wood Planks', 'Wood', 'Wool', }, } p.i18n = i18n

local slot = require( i18n.moduleSlot ) local recipeTable = require( i18n.moduleRecipe ).table local bazaar = require('Module:Bazaar') local element = require('Module:Element')

local cArgVals = { 'A1', 'B1', 'C1', 'A2', 'B2', 'C2', 'A3', 'B3', 'C3' } p.cArgVals = cArgVals

function p.table( f ) local args = f	if f == mw.getCurrentFrame then args = require( i18n.moduleArgs ).merge( true ) else f = mw.getCurrentFrame end -- Automatic shapeless positioning if args[1] then args.shapeless = 1 if args[7] then args.A1 = args[1] args.B1 = args[2] args.C1 = args[3] args.A2 = args[4] args.B2 = args[5] args.C2 = args[6] if args[8] then -- ◼◼◼     ◼◼◼				-- ◼◼◼  OR  ◼◼◼ -- ◼◼◼     ◼◼◻				args.A3 = args[7] args.B3 = args[8] args.C3 = args[9] if args[9] then local identical = true for i = 1, 8 do						if args[i] ~= args[i + 1] then identical = false break end end if identical then args.shapeless = nil end end else -- ◼◼◼				-- ◼◼◼				-- ◻◼◻				args.B3 = args[7] end elseif args[2] then args.A2 = args[1] args.B2 = args[2] if args[5] then -- ◻◻◻     ◻◻◻				-- ◼◼◼  OR  ◼◼◼ -- ◼◼◼     ◼◼◻				args.C2 = args[3] args.A3 = args[4] args.B3 = args[5] args.C3 = args[6] elseif args[4] then -- ◻◻◻				-- ◼◼◻				-- ◼◼◻				args.A3 = args[3] args.B3 = args[4] else -- ◻◻◻     ◻◻◻				-- ◼◼◻  OR  ◼◼◻ -- ◻◼◻     ◻◻◻				args.B3 = args[3] end else -- ◻◻◻			-- ◻◼◻			-- ◻◻◻			args.B2 = args[1] args.shapeless = nil end for i = 1, 9 do			args[i] = nil end end -- Create recipe table, and list of ingredients local out, ingredientSets = recipeTable( args, {		uiFunc = 'craftingTable',		type = i18n.type,		ingredientArgs = cArgVals,		outputArgs = { 'Output' },	} ) local title = mw.title.getCurrentTitle if args.nocat == '1' or title.namespace ~= 0 or title.isSubpage then return out end local categories = {} local cI = 1 if args.upcoming then categories[cI] =  .. i18n.categoryUpcoming ..  cI = cI + 1 end if args.type then categories[cI] =  .. i18n.categoryRecipeType:gsub( '%$1', args.type ) ..  cI = cI + 1 end if args.ignoreusage ~= '1' then -- Create ingredient categories for DPL local usedNames = {} for _, ingredientSet in pairs( ingredientSets ) do			for _, ingredient in pairs( ingredientSet ) do				local name = ingredient.name if not ingredient.mod and not usedNames[name] and name ~= title.text then -- List each dye individually as they have their own pages if name == slot.i18n.prefixes.any .. ' ' .. i18n.itemDye or name == slot.i18n.prefixes.matching .. ' ' .. i18n.itemDye or name == slot.i18n.prefixes.any .. ' ' .. i18n.itemColoredDye or name == slot.i18n.prefixes.matching .. ' ' .. i18n.itemColoredDye then if not name:find( i18n.colored ) then categories[cI] =  .. i18n.categoryIngredientUsage:gsub( '%$1', i18n.itemBoneMeal ) ..  cI = cI + 1 usedNames[i18n.itemBoneMeal] = true end for _, dye in pairs( i18n.coloredDyes ) do categories[cI] =  .. i18n.categoryIngredientUsage:gsub( '%$1', dye ) ..  cI = cI + 1 usedNames[dye] = true end -- List stone variants individually as they have their own pages elseif name == slot.i18n.prefixes.any .. ' ' .. i18n.itemStone or name == slot.i18n.prefixes.matching .. ' ' .. i18n.itemStone then for _, stone in pairs( i18n.stoneVariants ) do categories[cI] =  .. i18n.categoryIngredientUsage:gsub( '%$1', stone ) ..  cI = cI + 1 usedNames[stone] = true end else -- Merge item variants which use a single page if name == slot.i18n.prefixes.any .. ' ' .. i18n.itemMushroom or name == slot.i18n.prefixes.matching .. ' ' .. i18n.itemMushroom or							name == i18n.itemRedMushroom or							name == i18n.itemBrownMushroom then name = i18n.itemMushroom elseif name == i18n.itemCharcoal then name = i18n.itemCoal elseif name:find( ' ' .. i18n.itemQuartzBlock .. '$' ) then name = i18n.itemBlockOfQuartz else for _, variant in pairs( i18n.variantPages ) do if name:find( ' ' .. variant .. '$' ) then name = variant break end end -- Remove prefixes for _, prefix in pairs( slot.i18n.prefixes ) do if name:find( '^' .. prefix .. ' ' ) then name = name:gsub( '^' .. prefix .. ' ', '' ) break end end end if not usedNames[name] then categories[cI] =  .. i18n.categoryIngredientUsage:gsub( '%$1', name ) ..  cI = cI + 1 usedNames[name] = true end end end end end end return out, table.concat( categories, '' ) end

function p.craftingGrid ( frame ) local args = getArgs(frame) local replpttn = '[%s\'",;:\.]'	local collapse = yesno(args.collapse, false)	local out = args.Output or args['output']	local grid = string.dedent( class="mcui-input"		|- class="mcui-row"\n|%s\n|%s\n|%s		|- class="mcui-row"\n|%s\n|%s\n|%s		|- class="mcui-row"\n|%s\n|%s\n|%s		|}):format(		slot.slot({ args.A1 }), slot.slot({ args.B1 }), slot.slot({ args.C1 }),		slot.slot({ args.A2 }), slot.slot({ args.B2 }), slot.slot({ args.C2 }),		slot.slot({ args.A3 }), slot.slot({ args.B3 }), slot.slot({ args.C3 })		)	grid = (not collapse and out and string.wrapHtml(out, 'center') or '') ..		string.wrapHtml(grid, 'div', { class = "mcui mcui-Crafting_Table pixel-image" })	grid = string.wrapHtml(grid, 'div', { style = { display = 'inline-block' } })	local bzar	if yesno(args.bazaar, false) then		local t = {}		table.each(cArgVals, function(v)			if args[v] then table.push(t, args[v]) end		end)		bzar = ("Bazaar Material Cost: %s"):format(bazaar.calcMaterialBuyPrices(t)) bzar = string.wrapHtml(bzar, 'div') end grid = grid..(bzar or '') if collapse then local id = out and ('%s-table'):format(out:gsub(replpttn, '-')) grid = ('%s\n%s'):format(element.collapsibleButton( {'▦ Recipe', id=id} ),			element.collapsible( {grid, id=id} )		) grid = string.wrapHtml(grid, 'div') end return grid end

function p.parseRecipe(str) function validchar(c) return c == 'A' or c == 'B' or c == 'C'	end function validnum(n) return n == '1' or n == '2' or n == '3' end function err(reason) error(('Syntax error while trying to parse slot indicator "%s"%s'):format(str, reason and (' (%s)'):format(reason) or '')) end function parseSlot(str) -- returns unordered list of all slots indicated -- syntax: -- ex1: parsing 'A123B13C123' gives { 'A1', 'A2', 'A3', 'B1', 'B3', 'C1', 'C2', 'C3' } -- ex2: parsing 'A*B13C*' also gives { 'A1', 'A2', 'A3', 'B1', 'B3', 'C1', 'C2', 'C3' } -- ex3: parsing '*13A2C2' gives { 'A1', 'B1', 'C1', 'A3', 'B3', 'C3', 'A2', 'C2' } (result equivallent to ex1 and ex2) -- ex4: parsing '**' gives all nine slots { 'A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3' } local args = string.split(str, '') local tb = {} local row = '' local flag = false for _, c in ipairs(args) do			if c == '*' then if row == '' or not flag then -- treat '*' as row selector row = 'ABC' flag = true else for _, r in ipairs(string.split(row, '')) do table.push(tb, r .. '1') table.push(tb, r .. '2') table.push(tb, r .. '3') end flag = false end elseif validchar(c) then if flag then err('Unused row specifier') else row = c					flag = true end elseif validnum(c) then if row == '' then err('Column number used before row specifier') else for _, r in ipairs(string.split(row, '')) do table.push(tb, r .. c)					end flag = false end else err('Illegal character') end end if flag then err('Unused row specifier') end if table.length(tb) ~= table.Set(tb)['size'] then err('Duplicate items') end return tb	end local args = string.matchAll(str, '[ABC123*]+%s+".-"') local ret, slots = {}, {} for _, v in ipairs(args) do		v = v[1] local arg1, arg2 = string.match(v, '([ABC123*]+)%s+"(.-)"') for _, w in ipairs(parseSlot(arg1)) do			ret[w] = arg2 end end return ret end

return p