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

Module:Crafting: Difference between revisions

From Mine in Abyss
minecraft>Magiczocker
mNo edit summary
 
minecraft>Mudscape
Add hash of json to subname to ensure uniqueness
Line 2: Line 2:


local i18n = {
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',
'Blue Dye', 'Brown Dye', 'Green Dye',
'Red Dye', 'Black Dye',
},
categoryIngredientUsage = 'Category:Recipe using $1',
categoryRecipeType = 'Category:$1 recipe',
itemBlockOfQuartz = 'Block of Quartz',
itemBlockOfQuartz = 'Block of Quartz',
itemBrownMushroom = 'Brown Mushroom',
itemCharcoal = 'Charcoal',
itemCoal = 'Coal',
itemColoredDye = 'Colored Dye',
itemDye = 'Dye',
itemMushroom = 'Mushroom',
itemQuartzBlock = 'Quartz Block',
itemSmoothQuartzBlock = 'Smooth Quartz Block',
itemRedMushroom = 'Red Mushroom',
itemStone = 'Stone',
itemWhiteDye = 'White Dye',
moduleArgs = [[Module:ProcessArgs]],
moduleArgs = [[Module:ProcessArgs]],
moduleRecipe = [[Module:Recipe table]],
moduleRecipe = [[Module:Recipe table]],
moduleSlot = [[Module:Inventory slot]],
moduleSlot = [[Module:Inventory slot]],
stoneVariants = { 'Stone', 'Andesite', 'Granite', 'Diorite' },
moduleAliases = [[Module:Inventory slot/Aliases]],
type = 'Crafting',
type = 'Crafting'
variantPages = {
'Andesite', 'Banner', 'Bed', 'Diorite', 'Firework Star', 'Granite',
'Pressure Plate', 'Sand', 'Sandstone', 'Shield', 'Slab', 'Stained Glass Pane',
'Stained Glass', 'Stairs', 'Wood Planks', 'Wood', 'Wool',
},
    CopperBlockLinks = {
'Block of Copper', 'Exposed Copper', 'Weathered Copper', 'Oxidized Copper', 'Cut Copper', 'Exposed Cut Copper', 'Weathered Cut Copper', 'Oxidized Cut Copper', 'Waxed Block of Copper', 'Waxed Copper', 'Waxed Exposed Copper', 'Waxed Weathered Copper', 'Waxed Cut Copper', 'Waxed Exposed Cut Copper', 'Waxed Weathered Cut Copper', 'Copper Block'
},
    CopperBlock = 'Block of Copper',
}
}
p.i18n = i18n
p.i18n = i18n
Line 43: Line 12:
local slot = require( i18n.moduleSlot )
local slot = require( i18n.moduleSlot )
local recipeTable = require( i18n.moduleRecipe ).table
local recipeTable = require( i18n.moduleRecipe ).table
local aliases = require( i18n.moduleAliases)
local cArgVals = { 'A1', 'B1', 'C1', 'A2', 'B2', 'C2', 'A3', 'B3', 'C3' }
local cArgVals = { 'A1', 'B1', 'C1', 'A2', 'B2', 'C2', 'A3', 'B3', 'C3' }
p.cArgVals = cArgVals
p.cArgVals = cArgVals
Line 126: Line 96:
-- Create recipe table, and list of ingredients
-- Create recipe table, and list of ingredients
local out, ingredientSets = recipeTable( args, {
local out, ingredientSets, outputSets = recipeTable( args, {
uiFunc = 'craftingTable',
uiFunc = 'craftingTable',
type = i18n.type,
type = i18n.type,
Line 134: Line 104:
local title = mw.title.getCurrentTitle()
local title = mw.title.getCurrentTitle()
if args.nocat == '1' or title.namespace ~= 0 or title.isSubpage then
if args.ignoreusage or args.nocat == '1' or title.namespace ~= 0 or title.isSubpage then
return out
return out
end
end
local categories = {}
-- SMW
local cI = 1
-- Json contains the full frames, the other SMW properties are just item names.
local smw_json = {
["type"] = args.type,
["shapeless"] = args.shapeless,
["fixed"] = args.fixed,
["notfixed"] = args.notfixed,
["description"] = mw.text.unstrip(args.description or ''), --Remove ref tags
["name"] = args.name,
["ingredients"] = args.ingredients,
["nocat"] = args.nocat,
["A1"] = args.A1,
["A2"] = args.A2,
["A3"] = args.A3,
["B1"] = args.B1,
["B2"] = args.B2,
["B3"] = args.B3,
["C1"] = args.C1,
["C2"] = args.C2,
["C3"] = args.C3,
["Output"] = args.Output,
["group"] = args.group
}
if args.type and args.ignoreusage ~= '1' then
-- Flatten the item sets into a simple list
categories[cI] = '[[' .. i18n.categoryRecipeType:gsub( '%$1', args.type ) .. ']]'
local function makeNameList(itemSets)
cI = cI + 1
local retTable = {}
end
local inputRetTable = {}
if args.ignoreusage ~= '1' then
-- Create ingredient categories for DPL
local usedNames = {}
local usedNames = {}
for _, itemSet in pairs(itemSets) do
local function addName(name)
for _, item in pairs(itemSet) do
if not usedNames[name] then -- redundant with most current code, but not with all, and might prevent other issues
--local name = item.mod .. ":" .. item.name     -- use instead of just item.name if mods are covered
categories[cI] = '[[' .. i18n.categoryIngredientUsage:gsub( '%$1', name ) .. ']]'
if not usedNames[item.name] then
cI = cI + 1
table.insert(retTable, item.name)
usedNames[name] = true
table.insert(inputRetTable, item.name)
end
usedNames[item.name] = true
end
 
if aliases[item.name] then
for _, ingredientSet in pairs( ingredientSets ) do
for _, aliasEntry in ipairs(aliases[item.name]) do
for _, ingredient in pairs( ingredientSet ) do
if not usedNames[aliasEntry] then
local name = ingredient.name
table.insert(retTable, (aliasEntry.name or aliasEntry)) -- Sometimes the alias entry is a table, so try and grab its name from within the table with a fallback to just the entry.
if not ingredient.mod and not usedNames[name] then
usedNames[aliasEntry] = true
-- 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
addName( i18n.itemWhiteDye )
end
for _, dye in pairs( i18n.coloredDyes ) do
addName( dye )
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
addName( stone )
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.itemSmoothQuartzBlock then name = i18n.itemSmoothQuartzBlock
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
for _, variant in pairs( i18n.CopperBlockLinks ) do
if name:find( variant ) then
name = i18n.CopperBlock
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
end
-- handle "A or B" names
local orA, orB = name:match("(.-) or (.+)")
if orA then
addName( orA )
addName( orB )
else
addName( name )
end
end
end
end
end
end
end
end
end
return retTable, inputRetTable
end
local output_names, original_output_names = makeNameList(outputSets)
local ingredient_names, original_ingredient_names = makeNameList(ingredientSets)
local group_names = {}
if args.group then
for name in string.gmatch(args.group, "[^,]+") do
table.insert(group_names, mw.text.trim(name))
end
end
end
end
return out, table.concat( categories, '' )
local json = mw.text.jsonEncode(smw_json)
local smw_sub = {}
-- We must have a unique subname since Crafting usage cannot handle when multiple json entries are within one sub object.
local subname = 'CRAFTING_OUT_'..table.concat(original_output_names,'_'):sub(1,100)..'_IN_'..table.concat(original_ingredient_names,'_'):sub(1,100)..'_'..mw.hash.hashValue("md4",json)
local smw_sub = { -- the actual SMW sub-object
['Crafting ingredient'] = ingredient_names,
['Crafting output'] = output_names,
['Crafting JSON'] = json,
['Crafting type'] = args.type
}
mw.smw.subobject(smw_sub, subname)
return out
end
end


return p
return p

Revision as of 02:42, 17 July 2024

This module implements {{crafting}}.

Parent arguments are automatically merged with directly passed arguments (the latter overwriting the former).

Dependencies

See also


cs:Modul:Crafting de:Modul:Herstellen es:Módulo:Fabricación fr:Module:Fabrication it:Modulo:Fabbricazione ja:モジュール:Crafting ko:모듈:Crafting lzh:模組:Crafting nl:Module:Vervaardiging pl:Moduł:Crafting pt:Módulo:Crafting ru:Модуль:Крафт th:มอดูล:Crafting uk:Модуль:Майстрування zh:Module:Crafting



local p = {}

local i18n = {
	moduleArgs = [[Module:ProcessArgs]],
	moduleRecipe = [[Module:Recipe table]],
	moduleSlot = [[Module:Inventory slot]],
	moduleAliases = [[Module:Inventory slot/Aliases]],
	type = 'Crafting'
}
p.i18n = i18n

local slot = require( i18n.moduleSlot )
local recipeTable = require( i18n.moduleRecipe ).table
local aliases = require( i18n.moduleAliases)
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, outputSets = recipeTable( args, {
		uiFunc = 'craftingTable',
		type = i18n.type,
		ingredientArgs = cArgVals,
		outputArgs = { 'Output' },
	} )
	
	local title = mw.title.getCurrentTitle()
	if args.ignoreusage or args.nocat == '1' or title.namespace ~= 0 or title.isSubpage then
		return out
	end
	
	-- SMW
	-- Json contains the full frames, the other SMW properties are just item names.
	local smw_json = {
		["type"] = args.type,
		["shapeless"] = args.shapeless,
		["fixed"] = args.fixed,
		["notfixed"] = args.notfixed,
		["description"] = mw.text.unstrip(args.description or ''), --Remove ref tags
		["name"] = args.name,
		["ingredients"] = args.ingredients,
		["nocat"] = args.nocat,
		["A1"] = args.A1,
		["A2"] = args.A2,
		["A3"] = args.A3,
		["B1"] = args.B1,
		["B2"] = args.B2,
		["B3"] = args.B3,
		["C1"] = args.C1,
		["C2"] = args.C2,
		["C3"] = args.C3,
		["Output"] = args.Output,
		["group"] = args.group
	}
	
	-- Flatten the item sets into a simple list
	local function makeNameList(itemSets)
		local retTable = {}
		local inputRetTable = {}
		local usedNames = {}
		for _, itemSet in pairs(itemSets) do
			for _, item in pairs(itemSet) do
				--local name = item.mod .. ":" .. item.name     -- use instead of just item.name if mods are covered
				if not usedNames[item.name] then
					table.insert(retTable, item.name)
					table.insert(inputRetTable, item.name)
					usedNames[item.name] = true
					
					if aliases[item.name] then
						for _, aliasEntry in ipairs(aliases[item.name])	do
							if not usedNames[aliasEntry] then
								table.insert(retTable, (aliasEntry.name or aliasEntry)) -- Sometimes the alias entry is a table, so try and grab its name from within the table with a fallback to just the entry.
								usedNames[aliasEntry] = true
							end
						end
					end
				end
			end
		end
		return retTable, inputRetTable
	end
	
	local output_names, original_output_names = makeNameList(outputSets)
	local ingredient_names, original_ingredient_names = makeNameList(ingredientSets)
	local group_names = {}
	if args.group then
		for name in string.gmatch(args.group, "[^,]+") do
			table.insert(group_names, mw.text.trim(name))
		end
	end
	
	local json = mw.text.jsonEncode(smw_json)
	local smw_sub = {}
	-- We must have a unique subname since Crafting usage cannot handle when multiple json entries are within one sub object.
	local subname = 'CRAFTING_OUT_'..table.concat(original_output_names,'_'):sub(1,100)..'_IN_'..table.concat(original_ingredient_names,'_'):sub(1,100)..'_'..mw.hash.hashValue("md4",json)
	local smw_sub = { -- the actual SMW sub-object
		['Crafting ingredient'] = ingredient_names,
		['Crafting output'] = output_names,
		['Crafting JSON'] = json,
		['Crafting type'] = args.type
	}
	mw.smw.subobject(smw_sub, subname)
	
	return out
end

return p