Advanced group support in tooltips (modified by kilbith)

NOTE: Minetest 0.4.14-dev or 0.4.15 required.
This commit is contained in:
Wuzzy2 2016-12-03 15:35:04 +01:00 committed by JP Guerrero
parent 93c2d3dc81
commit ebcedb44b9
2 changed files with 45 additions and 8 deletions

View File

@ -1,10 +1,21 @@
local craftguide, datas, npp = {}, {}, 8*3
local min, ceil, floor = math.min, math.ceil, math.floor
local group_stereotypes = {
wool = "wool:white",
dye = "dye:white",
water_bucket = "bucket:bucket_water",
vessel = "vessels:glass_bottle",
coal = "default:coal_lump",
flower = "flowers:dandelion_yellow",
mesecon_conductor_craftable = "mesecons:wire_00000000_off",
}
function craftguide:get_recipe(item)
if item:sub(1,6) == "group:" then
if item:sub(-4) == "wool" or item:sub(-3) == "dye" then
item = item:sub(7)..":white"
local short_itemstr = item:sub(7)
if group_stereotypes[short_itemstr] then
item = group_stereotypes[short_itemstr]
elseif minetest.registered_items["default:"..item:sub(7)] then
item = item:gsub("group:", "default:")
else for node, def in pairs(minetest.registered_items) do
@ -15,6 +26,30 @@ function craftguide:get_recipe(item)
return item
end
function craftguide:extract_groups(itemstr)
if itemstr:sub(1,6) ~= "group:" then return end
return itemstr:sub(7):split(",")
end
function craftguide:get_tooltip(item, recipe_type, cooktime, groups)
local item_desc = minetest.registered_items[item].description
local tooltip = "tooltip["..item..";"..((groups and "") or item_desc)
if groups then
local groupstr = "Any item belonging to the "
for i=1, #groups do
groupstr = groupstr..minetest.colorize("#FFFF00", groups[i])..
((groups[i+1] and " and ") or "")
end
tooltip = tooltip..groupstr.." group(s)"
end
if recipe_type == "cooking" then
tooltip = tooltip.."\nCooking time: "..minetest.colorize("#FFFF00", cooktime)
end
return tooltip.."]"
end
function craftguide:get_formspec(player_name)
local data = datas[player_name]
data.pagenum = data.pagenum or 1
@ -53,9 +88,9 @@ function craftguide:get_formspec(player_name)
label[0,5.5;Recipe ]]..data.recipe_num.." of "..#recipes.."]"
end
local type = recipes[data.recipe_num].type
if type == "cooking" then formspec = formspec..
"image[3.75,4.6;0.5,0.5;default_furnace_front.png]"
local recipe_type = recipes[data.recipe_num].type
if recipe_type == "cooking" then
formspec = formspec.."image[3.75,4.6;0.5,0.5;default_furnace_front.png]"
end
local items = recipes[data.recipe_num].items
@ -68,11 +103,13 @@ function craftguide:get_formspec(player_name)
for i, v in pairs(items) do
local X = (i-1) % width + 4.5
local Y = floor((i-1) / width + (6 - min(2, rows)))
local label = ""
if v:sub(1,6) == "group:" then label = "\nG" end
local groups = self:extract_groups(v)
local label = (groups and "\nG") or ""
local item = self:get_recipe(v)
local tooltip = self:get_tooltip(item, recipe_type, width, groups)
formspec = formspec.."item_image_button["..X..","..Y..";1,1;"..
self:get_recipe(v)..";"..self:get_recipe(v)..";"..label.."]"
item..";"..item..";"..label.."]"..tooltip
end
local output = recipes[data.recipe_num].output

Binary file not shown.

Before

Width:  |  Height:  |  Size: 201 B

After

Width:  |  Height:  |  Size: 3.1 KiB