Show inventory icon on player joining
This commit is contained in:
parent
c4959b055b
commit
7f9af415bb
45
init.lua
Normal file
45
init.lua
Normal 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)
|
Loading…
Reference in New Issue
Block a user