Module:Item/Variants

local table = require("Module:Table") local string = require("Module:String") local MCColors = require("Module:Color/Data").MCColors

-- -- TABLE OF CONTENTS -- 01: More aliases support: Conversion tables -- 02: Main function for the aliases processing -- 03: Main table for lists of item variants -- 04: Individual procedures to generate lists of item variants -- -- Notes: -- 1. More aliases -- Add the *singular forms* of additional aliases to the singularFormConversions table -- 2. Plurals -- Use $s to set plural positions, for example: 'Crab Hat$s of Celebration' (default: the end of string) -- Add $u *at the end of the string* to disable plural, for example: 'Clay$u', 'Stairs$u' -- Add irregular plurals to the pluralOverrides table --

local p = {}

local function _getAliases -- 01: More aliases support: Conversion tables -- -- Other names to copy from the original name -- -- WITH $? FORMAT REMOVED (e.g. 'Crab Hat$s of' Cel -> 'Crab Hat of Cel') -- local singularFormConversions = { -- [' '] = ' '		-- [' '] = {' ', ...}		['Crab Hat of Celebration'] = 'Crab Hat', }	-- Irregular plurals (i.e. not 's', 'es', 'ies', 'ves') must be substituted here, -- -- including new aliases from singularFormConversions (if their plurals are irregular) -- -- Only the single word on plural positions (default is the end of string) needs to be added, -- -- unless you want to be specific so as to not affect other items -- local pluralOverrides = { -- irregular: [' '] = ' ' }	local aliases = {} local function gsubFn(...) -- Function for one or more gsub + trim -- function: gsubFn(<...>) | parameters must be in pairs of, , and in order local args = { ... }		if #args % 2 == 1 then error("gsubFn: Patterns and replacements not in pairs") end return function(n) for i = 1,#args,2 do				n = n:gsub(args[i], args[i+1]) end return string.trim(n) end end -- 02: Main function for the aliases processing -- -- function: createMultiAliases(name: str, items: table, customPrefixSingular: str/table, customPrefixPlural?: str/table) -- -- Additional prefixes for singular form (other than 'Any'/'Match') (if any) can be passed into 3rd param or items.CPS -- -- Additional prefixes for plural form (other than 'All') (if any) can be passed into 4th param or items.CPP -- local function createMultiAliases(name, items, customPrefixSingular, customPrefixPlural) local normalize = gsubFn('%$.', '') customPrefixSingular, customPrefixPlural = customPrefixSingular or items.customPrefixSingular or items.CPS, customPrefixPlural or items.customPrefixPlural or items.CPP mw.log(items.CPS, items.CPP) local allSingularAliases = table.map(table.merge({}, name, singularFormConversions[normalize(name)]), function(sing)			return gsubFn('[]', '')(sing)		end) local allPluralAliases = table.map(allSingularAliases, function(sing)			-- get plural			if sing:match('%$u$') then return gsubFn('%$u$','')(sing) end			local a,b = sing:find('%$s')			for k,v in pairs(pluralOverrides) do				if (a and sing:match(k..'%$s') or sing:match(k..'$')) then					return (a and gsubFn(k..'%$s', v)(sing) or gsubFn(k..'$', v)(sing))				end			end			if a then				if a < 2 then error('Wrong plural placement of item '..sing) end				return gsubFn('(sh?)%$s','%1es','(x)%$s','%1es','(ch)%$s','%1es','y%$s','ies','fe?%$s','ves','%$s','s')(sing)			else				return gsubFn('(sh?)$','%1es','(x)$','%1es','(ch)$','%1es','y$','ies','([^s])$','%1s')(sing)			end		end) items, allSingularAliases, allPluralAliases = table.map(items, normalize), table.map(allSingularAliases, normalize), table.map(allPluralAliases, normalize) table.each(allSingularAliases, function(n) -- Don't use .eachNamed: we don't want named indexes			mw.logObject({'Any','Matching'}, customPrefixSingular or {})			table.each(table.merge({'Any','Matching'}, customPrefixSingular or {}), function(p) aliases[p .. ' ' .. n] = items end)		end) table.each(allPluralAliases, function(n) -- Don't use .eachNamed: we don't want named indexes			table.each(table.merge({'All'}, customPrefixPlural or {}), function(p) aliases[p .. ' ' .. n] = items end)		end) end -- 03: Main table for lists of item variants -- -- Mainly for adding items with lesser iteration -- -- Use keys CPS (for singular) and CPP (for plural) to add additional custom prefixes -- local itemVariants = { -- ['Item group'] = { 'Item1', <'Item2'>, <...>, ,  } -- SACKS -- ['Agronomy Sack'] = { 'Small Agronomy Sack', 'Medium Agronomy Sack', 'Large Agronomy Sack' }, ['Combat Sack'] = { 'Small Combat Sack', 'Medium Combat Sack', 'Large Combat Sack' }, ['Husbandry Sack'] = { 'Small Husbandry Sack', 'Medium Husbandry Sack', 'Large Husbandry Sack' }, ['Foraging Sack'] = { 'Small Foraging Sack', 'Medium Foraging Sack', 'Large Foraging Sack' }, ['Fishing Sack'] = { 'Small Fishing Sack', 'Medium Fishing Sack', 'Large Fishing Sack' }, ['Mining Sack'] = { 'Small Mining Sack', 'Medium Mining Sack', 'Large Mining Sack' }, ['Slayer Sack'] = { 'Small Slayer Sack', 'Medium Slayer Sack', 'Large Slayer Sack' }, -- MISC -- ['Crab Hat$s of Celebration'] = { 'Red Crab Hat of Celebration', 'Orange Crab Hat of Celebration', 'Yellow Crab Hat of Celebration', 'Lime Crab Hat of Celebration', 'Green Crab Hat of Celebration', 'Aqua Crab Hat of Celebration', 'Purple Crab Hat of Celebration', 'Pink Crab Hat of Celebration', 'Black Crab Hat of Celebration' }, ['Century Cake'] = { 'Crab-Colored Century Cake', 'Pet Rock Century Cake', 'aPunch Century Cake', 'Potato-Style Century Cake', 'Barry Century Cake', 'Sea Emperor Century Cake', 'Century Cake of the Next Dungeon Floor', 'Latest Update Century Cake', 'Streamer\'s Century Cake' }, ['Flower'] = { 'Oxeye Daisy', 'Red Tulip', 'Orange Tulip', 'White Tulip', 'Pink Tulip', 'Azure Bluet', 'Allium', 'Blue Orchid', 'Poppy', 'Dandelion' }, ['Horse Armor$u'] = { 'Iron Horse Armor', 'Gold Horse Armor', 'Diamond Horse Armor' }, ['Mushroom'] = { 'Red Mushroom', 'Brown Mushroom' }, ['Quartz Block'] = { 'Block of Quartz', 'Chiseled Quartz Block', 'Quartz Pillar' }, ['Red Sandstone'] = { 'Red Sandstone', 'Chiseled Red Sandstone', 'Smooth Red Sandstone' }, ['Sandstone'] = { 'Sandstone', 'Chiseled Sandstone', 'Smooth Sandstone' }, ['Stone Bricks$u'] = { 'Stone Bricks', 'Mossy Stone Bricks', 'Cracked Stone Bricks', 'Chiseled Stone Bricks' }, ['Stone Slab'] = { 'Sandstone Slab', 'Cobblestone Slab', 'Brick Slab', 'Stone Brick Slab', 'Nether Brick Slab', 'Quartz Slab' }, ['Stone'] = { 'Stone', 'Andesite', 'Granite', 'Diorite', 'Polished Andesite', 'Polished Granite', 'Polished Diorite' }, ['Tulip'] = { 'Red Tulip', 'Orange Tulip', 'White Tulip', 'Pink Tulip' }, }	for n,v in pairs(itemVariants) do		createMultiAliases(n, v)	end -- Below this line: 04: Individual procedures to generate lists of item variants -- -- Colored dye -- local coloredDyes = { 'Orange Dye', 'Magenta Dye', 'Light Blue Dye', 'Dandelion Yellow', 'Lime Dye', 'Pink Dye', 'Gray Dye', 'Light Gray Dye', 'Cyan Dye', 'Purple Dye', 'Lapis Lazuli', 'Cocoa Beans', 'Cactus Green', 'Rose Red', 'Ink Sack', }	createMultiAliases('Colored Dye', coloredDyes) createMultiAliases('Dye', table.merge(mw.clone(coloredDyes), 'Bone Meal')) -- Colored items -- -- Items that use the sixteen colors -- local coloredItems = { {'Carpet', 'Dyed Carpet'}, {'Wool', 'Dyed Wool'}, {'Glass', 'Stained Glass'}, {'Glass Pane', 'Stained Glass Pane'}, {'Clay$u', 'Stained Clay$u'}, {'Jumbo Backpack$s ($1)', CPS="Dyed"}, {'Greater Backpack$s ($1)', CPS="Dyed"}, {'Large Backpack$s ($1)', CPS="Dyed"}, {'Medium Backpack$s ($1)', CPS="Dyed"}, {'Small Backpack$s ($1)', CPS="Dyed"}, }	for _,item in ipairs(coloredItems) do		local itemAny, listAny, itemColored, listColored local cps, cpp = item.CPS, item.CPP if type(coloredItems) == "table" then itemAny, listAny = item[1], {} itemColored, listColored = item[2], {} else itemAny, listAny = item, {} end table.eachNamed(MCColors, function(color)			table.push(listAny, itemAny:match('%$1') and itemAny:gsub('%$1', color) or color .. ' ' .. itemAny)			if color:lower ~= "white" and not not not not itemColored then				table.push(listColored, itemColored:match('%$1') and itemColored:gsub('%$1', color) or color .. ' ' .. itemColored)			end		end) createMultiAliases(gsubFn('%$1%s*', '')(itemAny), listAny, cps, cpp) if itemColored then createMultiAliases(gsubFn('%$1%s*', '')(itemColored), listColored, cps, cpp) end end -- Item groups -- -- Dragon armors -- local dragonArmors = { 'Protector', 'Old', 'Unstable', 'Holy', 'Wise', 'Young', 'Strong', 'Superior' } for _, piece in ipairs{ 'Helmet', 'Chestplate', 'Leggings$u', 'Boots$u', 'Fragment' } do		local dragonRelatedAliases = table.map(table.deepCopy(dragonArmors), function(v) 			return v .. ' Dragon ' .. piece		end) createMultiAliases(piece, dragonRelatedAliases) end -- Wood -- local woods = { 'Oak', 'Spruce', 'Birch', 'Dark Oak', 'Acacia', 'Jungle' }	local woodItems = { 'Wood', 'Wood Plank', -- Skyblock specific 'Wood Slab', 'Wood Stairs$u', 'Fence', 'Fence Gate', 'Sapling', 'Leaves$u', }	for _, item in ipairs(woodItems) do		local itemName = '' local woodAliases = {} for _, wood in ipairs(woods) do			if item:find('wood') then itemName = string.gsub(item, 'wood', wood) elseif item:find('%$1') then itemName = string.gsub(item, '%$1', wood) else itemName = wood .. ' ' .. item end table.insert(woodAliases, itemName) end item = string.gsub(item, '%$1 ', '') createMultiAliases(item, woodAliases) end return aliases end

function p.getAliases return _getAliases end

p.getVariants = p.getAliases

return p