minetest_inventory_icon/init.lua

72 lines
1.9 KiB
Lua
Raw Normal View History

2015-05-27 17:51:35 +02:00
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,
})
2015-05-27 18:07:28 +02:00
inventory_icon.hudids[name].text = player:hud_add({
2015-05-27 17:51:35 +02:00
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)
2015-05-27 18:07:28 +02:00
minetest.register_on_leaveplayer(function(player)
inventory_icon.hudids[player:get_player_name()] = nil
end)
inventory_icon.timer = 0
minetest.register_globalstep(function(dtime)
inventory_icon.timer = inventory_icon.timer + dtime
if inventory_icon.timer > 1 then
for playername,hudids in pairs(inventory_icon.hudids) do
local player = minetest.get_player_by_name(playername)
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
player:hud_change(hudids.icon, "text", icon)
player:hud_change(hudids.text, "text", string.format("%d/%d", occupied, size))
player:hud_change(hudids.text, "number", color)
end
end
end)