handle [inventorycube textures

This commit is contained in:
FaceDeer 2020-01-10 09:40:14 -07:00
parent 94e9446ae9
commit af67b830a4

View File

@ -2,32 +2,53 @@
local MP = minetest.get_modpath(minetest.get_current_modname()) local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua") local S, NS = dofile(MP.."/intllib.lua")
--[inventorycube{<top>{<left>{<right>
--Escaping does not apply here and `^` is replaced by `&` in texture names instead.
--Example:
-- [inventorycube{grass.png{dirt.png&grass_side.png{dirt.png&grass_side.png
--Creates an inventorycube with `grass.png`, `dirt.png^grass_side.png` and `dirt.png^grass_side.png` textures
local process_inventory_cube = function(texture_string)
if not texture_string:sub(1,14) == "[inventorycube" then
return texture_string
end
local split = texture_string:split("{")
local left = split[3] -- the "front" of the cube we're seeing in the inventory list
if left == nil then -- in case something weird happens, don't crash.
return texture_string
end
left = left:gsub("&", "^")
return left
end
local get_icon = function(item) local get_icon = function(item)
local def = minetest.registered_items[item] local def = minetest.registered_items[item]
local returnstring = "unknown_item.png" local returnstring = "unknown_item.png"
if def == nil then
return returnstring
end
if def then local inventory_image = def.inventory_image
local inventory_image = def.inventory_image if inventory_image and inventory_image ~= "" then
if inventory_image and inventory_image ~= "" then returnstring = inventory_image
returnstring = inventory_image else
else local tiles = def.tiles
local tiles = def.tiles if tiles then
if tiles then local tilecount = #tiles
local tilecount = #tiles -- Textures of node; +Y, -Y, +X, -X, +Z, -Z
-- Textures of node; +Y, -Y, +X, -X, +Z, -Z local selected_tile = tiles[math.min(5,tilecount)]
local selected_tile = tiles[math.min(5,tilecount)] if type(selected_tile) == "string" then
if type(selected_tile) == "string" then returnstring = selected_tile
returnstring = selected_tile else
else local tile_name = selected_tile.name
local tile_name = selected_tile.name if tile_name then
if tile_name then returnstring = tile_name
returnstring = tile_name
end
end end
end end
end end
end end
returnstring = process_inventory_cube(returnstring)
-- Formspec tables can't handle image compositing and modifiers -- Formspec tables can't handle image compositing and modifiers
local found_caret = returnstring:find("%^") local found_caret = returnstring:find("%^")
if found_caret then if found_caret then