2016-08-11 18:30:00 +02:00
|
|
|
local inventory_icon = {}
|
2015-05-27 17:51:35 +02:00
|
|
|
inventory_icon.hudids = {}
|
|
|
|
|
2015-06-23 21:40:03 +02:00
|
|
|
inventory_icon.COLORIZE_STRING = "[colorize:#A00000:192"
|
|
|
|
|
2016-09-01 20:16:24 +02:00
|
|
|
local mod_ui = minetest.get_modpath("unified_inventory") ~= nil
|
|
|
|
local mod_bags = minetest.get_modpath("bags") ~= nil
|
|
|
|
|
2015-06-23 18:13:19 +02:00
|
|
|
function inventory_icon.get_inventory_state(inv, listname)
|
|
|
|
local size = inv:get_size(listname)
|
2015-05-27 17:51:35 +02:00
|
|
|
local occupied = 0
|
|
|
|
for i=1,size do
|
2015-06-23 18:13:19 +02:00
|
|
|
local stack = inv:get_stack(listname, i)
|
2015-05-27 17:51:35 +02:00
|
|
|
if not stack:is_empty() then
|
|
|
|
occupied = occupied + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return occupied, size
|
|
|
|
end
|
|
|
|
|
2015-07-01 21:43:38 +02:00
|
|
|
function inventory_icon.replace_icon(name)
|
2016-09-01 20:05:40 +02:00
|
|
|
return name.."^[resize:32x32"
|
2015-07-01 21:43:38 +02:00
|
|
|
end
|
|
|
|
|
2015-05-27 17:51:35 +02:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
inventory_icon.hudids[name] = {}
|
2015-06-23 18:13:19 +02:00
|
|
|
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main")
|
2015-05-27 18:25:09 +02:00
|
|
|
local icon
|
2015-05-27 17:51:35 +02:00
|
|
|
if occupied >= size then
|
|
|
|
icon = "inventory_icon_backpack_full.png"
|
|
|
|
else
|
|
|
|
icon = "inventory_icon_backpack_free.png"
|
|
|
|
end
|
2015-06-23 18:13:19 +02:00
|
|
|
inventory_icon.hudids[name].main = {}
|
|
|
|
inventory_icon.hudids[name].main.icon = player:hud_add({
|
2015-05-27 17:51:35 +02:00
|
|
|
hud_elem_type = "image",
|
|
|
|
position = {x=1,y=1},
|
|
|
|
scale = {x=1,y=1},
|
|
|
|
offset = {x=-32,y=-32},
|
|
|
|
text = icon,
|
|
|
|
})
|
2015-06-23 18:13:19 +02:00
|
|
|
inventory_icon.hudids[name].main.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},
|
2015-05-27 18:25:09 +02:00
|
|
|
number = 0xFFFFFF,
|
2015-05-27 17:51:35 +02:00
|
|
|
text = string.format("%d/%d", occupied, size)
|
|
|
|
})
|
2016-09-01 20:16:24 +02:00
|
|
|
if mod_ui or mod_bags then
|
2015-06-23 18:13:19 +02:00
|
|
|
inventory_icon.hudids[name].bags = {}
|
|
|
|
local bags_inv = minetest.get_inventory({type = "detached", name = name.."_bags"})
|
|
|
|
for i=1,4 do
|
|
|
|
local bag = bags_inv:get_stack("bag"..i, 1)
|
|
|
|
local scale, text, icon
|
|
|
|
if bag:is_empty() then
|
|
|
|
scale = { x = 0, y = 0 }
|
|
|
|
text = ""
|
2016-09-01 20:05:40 +02:00
|
|
|
icon = "bags_small.png^[resize:32x32"
|
2015-06-23 18:13:19 +02:00
|
|
|
else
|
2015-07-01 21:43:38 +02:00
|
|
|
scale = { x = 1, y = 1 }
|
2015-06-23 18:13:19 +02:00
|
|
|
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
|
|
|
|
text = string.format("%d/%d", occupied, size)
|
2015-07-01 21:43:38 +02:00
|
|
|
icon = inventory_icon.replace_icon(minetest.registered_items[bag:get_name()].inventory_image)
|
2015-06-23 21:40:03 +02:00
|
|
|
if occupied >= size then
|
|
|
|
icon = icon .. "^" .. inventory_icon.COLORIZE_STRING
|
|
|
|
end
|
2015-06-23 18:13:19 +02:00
|
|
|
end
|
|
|
|
inventory_icon.hudids[name].bags[i] = {}
|
|
|
|
inventory_icon.hudids[name].bags[i].icon = player:hud_add({
|
|
|
|
hud_elem_type = "image",
|
|
|
|
position = {x=1,y=1},
|
|
|
|
scale = scale,
|
|
|
|
size = { x=32, y=32 },
|
|
|
|
offset = {x=-36,y=-32 -40*i},
|
|
|
|
text = icon,
|
|
|
|
})
|
|
|
|
inventory_icon.hudids[name].bags[i].text = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
|
|
|
position = {x=1,y=1},
|
|
|
|
scale = scale,
|
|
|
|
offset = {x=-36,y=-20 -40*i},
|
|
|
|
alignment = {x=0,y=0},
|
|
|
|
number = 0xFFFFFF,
|
|
|
|
text = text,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2015-05-27 17:51:35 +02:00
|
|
|
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)
|
2015-06-23 18:13:19 +02:00
|
|
|
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main")
|
2015-05-27 18:07:28 +02:00
|
|
|
local icon, color
|
|
|
|
if occupied >= size then
|
|
|
|
icon = "inventory_icon_backpack_full.png"
|
|
|
|
else
|
|
|
|
icon = "inventory_icon_backpack_free.png"
|
|
|
|
end
|
2015-06-23 18:13:19 +02:00
|
|
|
player:hud_change(hudids.main.icon, "text", icon)
|
|
|
|
player:hud_change(hudids.main.text, "text", string.format("%d/%d", occupied, size))
|
2015-06-23 21:35:26 +02:00
|
|
|
|
2016-09-01 20:16:24 +02:00
|
|
|
if mod_ui or mod_bags then
|
2015-06-23 21:35:26 +02:00
|
|
|
local bags_inv = minetest.get_inventory({type = "detached", name = playername.."_bags"})
|
|
|
|
for i=1,4 do
|
|
|
|
local bag = bags_inv:get_stack("bag"..i, 1)
|
|
|
|
local scale, text, icon
|
|
|
|
if bag:is_empty() then
|
|
|
|
scale = { x = 0, y = 0 }
|
|
|
|
text = ""
|
2016-09-01 20:05:40 +02:00
|
|
|
icon = "bags_small.png^[resize:32x32"
|
2015-06-23 21:35:26 +02:00
|
|
|
else
|
2015-07-01 21:43:38 +02:00
|
|
|
scale = { x = 1, y = 1 }
|
2015-06-23 21:35:26 +02:00
|
|
|
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
|
|
|
|
text = string.format("%d/%d", occupied, size)
|
2015-07-01 21:43:38 +02:00
|
|
|
icon = inventory_icon.replace_icon(minetest.registered_items[bag:get_name()].inventory_image)
|
2015-06-23 21:35:26 +02:00
|
|
|
if occupied >= size then
|
2015-06-23 21:40:03 +02:00
|
|
|
icon = icon .. "^" .. inventory_icon.COLORIZE_STRING
|
2015-06-23 21:35:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
player:hud_change(inventory_icon.hudids[playername].bags[i].icon, "text", icon)
|
|
|
|
player:hud_change(inventory_icon.hudids[playername].bags[i].icon, "scale", scale)
|
|
|
|
|
|
|
|
player:hud_change(inventory_icon.hudids[playername].bags[i].text, "text", text)
|
|
|
|
player:hud_change(inventory_icon.hudids[playername].bags[i].text, "scale", scale)
|
|
|
|
end
|
|
|
|
end
|
2015-05-27 18:07:28 +02:00
|
|
|
end
|
2015-05-27 18:25:09 +02:00
|
|
|
inventory_icon.timer = 0
|
2015-05-27 18:07:28 +02:00
|
|
|
end
|
|
|
|
end)
|