minetest_inventory_icon/init.lua
2015-05-27 17:51:35 +02:00

46 lines
1.1 KiB
Lua

inventory_icon = {}
inventory_icon.hudids = {}
function inventory_icon.get_inventory_state(player)
local inv = player:get_inventory()
local size = inv:get_size("main")
local occupied = 0
for i=1,size do
local stack = inv:get_stack("main", i)
if not stack:is_empty() then
occupied = occupied + 1
end
end
return occupied, size
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
inventory_icon.hudids[name] = {}
local occupied, size = inventory_icon.get_inventory_state(player)
local icon, color
if occupied >= size then
icon = "inventory_icon_backpack_full.png"
color = "0xFF0000"
else
icon = "inventory_icon_backpack_free.png"
color = "0xFFFFFF"
end
inventory_icon.hudids[name].icon = player:hud_add({
hud_elem_type = "image",
position = {x=1,y=1},
scale = {x=1,y=1},
offset = {x=-32,y=-32},
text = icon,
})
inventory_icon.hudids[name].icon = player:hud_add({
hud_elem_type = "text",
position = {x=1,y=1},
scale = {x=1,y=1},
offset = {x=-36,y=-20},
alignment = {x=0,y=0},
number = color,
text = string.format("%d/%d", occupied, size)
})
end)