Show inventory icon on player joining

This commit is contained in:
Wuzzy 2015-05-27 17:51:35 +02:00
parent c4959b055b
commit 7f9af415bb
1 changed files with 45 additions and 0 deletions

45
init.lua Normal file
View File

@ -0,0 +1,45 @@
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)