Модуль:Жидкостный слот

Материал из Playzone Minecraft Wiki
Версия от 01:27, 18 января 2017; Softer (обсуждение | вклад) (1 версия импортирована)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Для документации этого модуля может быть создана страница Модуль:Жидкостный слот/doc

-------------------------------------------------------------------
--- Модуль для отображения жидкостей в ёмкостях для механизмов из модов в Minecraft Wiki.
-------------------------------------------------------------------

local p = {}

--- Создание слота
function p.slot(f)
	--- Получение аргументов
	local args = f.args or f
	if f == mw.getCurrentFrame() and args[1] == nil then
		args = f:getParent().args
	end
	
	-- Первый аргумент
	args[1] = mw.text.trim(args[1] or '')
	
	--- Построение спрайта
	
	-- Параметры
	local sprite
	local ids = mw.loadData([[Модуль:ИнвСпрайт/ID]])["IDы"]
	local modIds = {}
	local pageName = mw.title.getCurrentTitle().text
	local imgClass = args["классизобр"]
	local numStyle = args["стильцифр"]
	
	local body = mw.html.create('span'):addClass('invslot-plain'):css{['vertical-align'] = args["выравн"]}
	
	if args["класс"] then
		body:addClass(args["класс"])
	end
	if args["стиль"] then
		body:cssText(args["стиль"])
	end
    body:cssText('display:inline-block; position:relative; z-index:20; width:36px; height:114px;')

    if args["фон"] then
        body:cssText(args["фон"])
    else
        body:cssText('background-color:#8b8b8b;')
    end

    --- Добавление картинки ёмкости с делениями
    local y
    y = math.floor(11*args["объём"])
    if y<0 then 
        y=0
    end
    if y>110 then
        y=110
    end

    body:wikitext('[[Файл:Container_transparent.png|link=]]')
    --- <div style="z-index:-10; position:absolute; top:{{#expr: 112-{{#var:z}}}}px; left:0px; width:36px; height:114px; overflow:hidden">
    local im
    im = body:tag('span'):cssText('display: block;z-index:10; position:absolute; top:' .. 112-y .. 'px; left:0px; width:36px; height:' .. y+2 .. 'px; overflow:hidden')
	--- Обработка фреймов
    frame = mw.text.split( args[1], ';' )[1]
    if y>0 then
    for x = 1, 4 do
		local item
		item = im:tag('span'):addClass('invslot-item'):cssText('display: block;position:absolute;top:'.. y-160+32*x .. 'px; left:2px; z-index:10;')
        if imgClass then
            item:addClass(imgClass)
        end
		if frame == '' then
			(item or im):tag('br')
		else
			local category
			local parts = p.getParts(frame, args["мод"])
			local title = parts.title or mw.text.trim(args["назв"] or '')
			local mod = parts.mod
			local name = parts.name
			local num = parts.num
			local description = parts.text
			
			local img, idData
			if mod then
				local modData = modIds[mod]
				if not modData and mw.title.new('Модуль:ИнвСпрайт/' .. mod .. '/ID').exists then
					modData = mw.loadData('Модуль:ИнвСпрайт/' .. mod .. '/ID')["IDы"]
					modIds[mod] = modData
				end
				if modData and modData[name] then
					idData = modData[name]
				else
					img = name .. ' (' .. mod .. ')'
				end
			elseif ids[name] then
				idData = ids[name]
			else
				img = name
			end
			
			local link = args["ссылка"] or ''
			if link == '' then
				if mod then
					link = mod .. '/' .. name
				else
					link = name
				end
			elseif mw.ustring.lower(link) == 'нет' then
				link = nil
			end
			if link == pageName then
				link = nil
			end
			
			local formattedTitle
			local plainTitle
			if title == '' then
				plainTitle = name
			elseif mw.ustring.lower(title) ~= 'нет' then
				plainTitle = mw.ustring.gsub(mw.ustring.gsub(title, '\\\\', '&#92;'), '\\&', '&#38;')
				
				local formatPattern = '&[0-9a-fk-or]'
				if mw.ustring.match(plainTitle, formatPattern) then
					formattedTitle = title
					plainTitle = mw.ustring.gsub(plainTitle, formatPattern, '')
				end
				
				if plainTitle == '' then
					plainTitle = name
				else
					plainTitle =  mw.ustring.gsub(mw.ustring.gsub(plainTitle, '&#92;', '\\'), '&#38;', '&')
				end
			elseif link then
				if img then
					formattedTitle = ''
				else
					plainTitle = ''
				end
			end
			
			item:attr{
				['data-minetip-title'] = formattedTitle,
				['data-minetip-text'] = description
			}
			
			if img then
				-- & is re-escaped because mw.html treats attributes
				-- as plain text, but MediaWiki doesn't
				local escapedTitle = ( plainTitle or '' ):gsub( '&', '&#38;' )
				item:addClass('invslot-item-image')
					:wikitext('[[Файл:Grid ', img, '.png|32x32px|link=', link or '', '|', escapedTitle, ']]')
			else
				if not sprite then
					sprite = require([[Модуль:Спрайт]]).sprite
				end
				local image
				if mod then
					image = (args["таблспрайтов"] or mod or "Inv") .. 'CSS.png'
				end
				if link then
					item:wikitext('[[', link, '|')
				end

				local image, spriteCat;
				if not mod then
				    image, spriteCat = sprite{
					    ["данныеID"] = idData, ["назв"] = plainTitle,
					    ["изобр"] = image, ["настройки"] = 'ИнвСпрайт'
					    }
				else 
				    image, spriteCat = sprite{
					    ["данныеID"] = idData, ["назв"] = plainTitle,
					    ["изобр"] = image, ["настройки"] = 'ИнвСпрайт/' .. mod
					    }
				end
				item:node(image)
				category = spriteCat
			end
			
			if num and num > 1 and num < 1000 then
				if img and link then
					item:wikitext('[[', link, '|')
				end
				local number = item
					:tag('span')
						:addClass('invslot-stacksize')
						:attr{title = plainTitle}
						:wikitext(num)
				if numStyle then
					number:cssText(numStyle)
				end
				if img and link then
					item:wikitext(']]')
				end
			end
			
			if idData and link then
				item:wikitext(']]')
			end
			
			item:wikitext(category)
		end
	end	
    end
	return tostring( body )
end


function p.getParts(frame, mod)
	local parts = {}
	parts.title = mw.ustring.match(frame, '^%[%s*([^%]]+)%s*%]')
	
    local modPattern
    if mw.ustring.match(frame, '^%[.*%]([a-zA-Zа-яА-Я0-9 _-]+):') then
    modPattern = '^%[.*%]([a-zA-Zа-яА-Я0-9 _-]+):'
    else
    modPattern = '^([a-zA-Zа-яА-Я0-9 _-]+):'
    end

	parts.mod = mw.text.trim(mw.ustring.match(frame, modPattern) or mod or '')
	
	local vanilla = {v = 1, vanilla = 1, mc = 1, minecraft = 1}
	if parts.mod == '' or vanilla[mw.ustring.lower(parts.mod)] then
		parts.mod = nil
	end

	local _, nameStartV = mw.ustring.find( frame, '^%[[^%]]*%]' )
	local nameStart = ( ({mw.ustring.find( frame, modPattern )})[2] or nameStartV or 0 ) + 1
	if nameStart - 1 == #frame then
		nameStart = 1
	end
	parts.name = mw.text.trim( mw.ustring.sub( frame, nameStart, ( mw.ustring.find( frame, '[,%[]', nameStart ) or 0 ) - 1 ) )
	
	parts.num = math.floor(mw.ustring.match(frame, ',%s*(%d+)') or 0)
	if parts.num == 0 then
		parts.num = nil
	end
	
	parts.text = mw.ustring.match(frame, '%[%s*([^%]]+)%s*%]$')
	
	return parts
end
 
return p