2 if (minetest
.get_modpath("intllib")) then
5 S
= function(s
,a
,...)a
={a
,...}return s
:gsub("@(%d+)",function(n
)return a
[tonumber(n
)]end)end
8 local inventory_icon
= {}
9 inventory_icon
.hudids
= {}
11 inventory_icon
.COLORIZE_STRING
= "[colorize:#A00000:192"
13 local mod_ui
= minetest
.get_modpath("unified_inventory") ~= nil
14 local mod_bags
= minetest
.get_modpath("bags") ~= nil
16 function inventory_icon
.get_inventory_state(inv
, listname
)
17 local size
= inv
:get_size(listname
)
20 local stack
= inv
:get_stack(listname
, i
)
21 if not stack
:is_empty() then
22 occupied
= occupied
+ 1
28 function inventory_icon
.replace_icon(name
)
29 return name
.."^[resize:32x32"
32 minetest
.register_on_joinplayer(function(player
)
33 local name
= player
:get_player_name()
34 inventory_icon
.hudids
[name
] = {}
35 local occupied
, size
= inventory_icon
.get_inventory_state(player
:get_inventory(), "main")
37 if occupied
>= size
then
38 icon
= "inventory_icon_backpack_full.png"
40 icon
= "inventory_icon_backpack_free.png"
42 inventory_icon
.hudids
[name
].main
= {}
43 inventory_icon
.hudids
[name
].main
.icon
= player
:hud_add({
44 hud_elem_type
= "image",
47 offset
= {x
=-32,y
=-32},
50 inventory_icon
.hudids
[name
].main
.text
= player
:hud_add({
51 hud_elem_type
= "text",
54 offset
= {x
=-36,y
=-20},
55 alignment
= {x
=0,y
=0},
57 text
= S("@1/@2", occupied
, size
)
59 if mod_ui
or mod_bags
then
60 inventory_icon
.hudids
[name
].bags
= {}
61 local bags_inv
= minetest
.get_inventory({type = "detached", name
= name
.."_bags"})
64 local bag
= bags_inv
:get_stack("bag"..i
, 1)
65 local scale
, text
, icon
66 if bag
:is_empty() then
67 scale
= { x
= 0, y
= 0 }
69 icon
= "bags_small.png^[resize:32x32"
71 scale
= { x
= 1, y
= 1 }
72 local occupied
, size
= inventory_icon
.get_inventory_state(player
:get_inventory(), "bag"..i
.."contents")
73 text
= S("@1/@2", occupied
, size
)
74 icon
= inventory_icon
.replace_icon(minetest
.registered_items
[bag
:get_name()].inventory_image
)
75 if occupied
>= size
then
76 icon
= icon
.. "^" .. inventory_icon
.COLORIZE_STRING
79 inventory_icon
.hudids
[name
].bags
[i
] = {}
80 inventory_icon
.hudids
[name
].bags
[i
].icon
= player
:hud_add({
81 hud_elem_type
= "image",
84 size
= { x
=32, y
=32 },
85 offset
= {x
=-36,y
=-32 -40*i
},
88 inventory_icon
.hudids
[name
].bags
[i
].text
= player
:hud_add({
89 hud_elem_type
= "text",
92 offset
= {x
=-36,y
=-20 -40*i
},
93 alignment
= {x
=0,y
=0},
102 minetest
.register_on_leaveplayer(function(player
)
103 inventory_icon
.hudids
[player
:get_player_name()] = nil
106 inventory_icon
.timer
= 0
107 minetest
.register_globalstep(function(dtime
)
108 inventory_icon
.timer
= inventory_icon
.timer
+ dtime
109 if inventory_icon
.timer
> 1 then
110 for playername
,hudids
in pairs(inventory_icon
.hudids
) do
111 local player
= minetest
.get_player_by_name(playername
)
112 local occupied
, size
= inventory_icon
.get_inventory_state(player
:get_inventory(), "main")
114 if occupied
>= size
then
115 icon
= "inventory_icon_backpack_full.png"
117 icon
= "inventory_icon_backpack_free.png"
119 player
:hud_change(hudids
.main
.icon
, "text", icon
)
120 player
:hud_change(hudids
.main
.text
, "text", S("@1/@2", occupied
, size
))
122 if mod_ui
or mod_bags
then
123 local bags_inv
= minetest
.get_inventory({type = "detached", name
= playername
.."_bags"})
126 local bag
= bags_inv
:get_stack("bag"..i
, 1)
127 local scale
, text
, icon
128 if bag
:is_empty() then
129 scale
= { x
= 0, y
= 0 }
131 icon
= "bags_small.png^[resize:32x32"
133 scale
= { x
= 1, y
= 1 }
134 local occupied
, size
= inventory_icon
.get_inventory_state(player
:get_inventory(), "bag"..i
.."contents")
135 text
= S("@1/@2", occupied
, size
)
136 icon
= inventory_icon
.replace_icon(minetest
.registered_items
[bag
:get_name()].inventory_image
)
137 if occupied
>= size
then
138 icon
= icon
.. "^" .. inventory_icon
.COLORIZE_STRING
141 player
:hud_change(inventory_icon
.hudids
[playername
].bags
[i
].icon
, "text", icon
)
142 player
:hud_change(inventory_icon
.hudids
[playername
].bags
[i
].icon
, "scale", scale
)
144 player
:hud_change(inventory_icon
.hudids
[playername
].bags
[i
].text
, "text", text
)
145 player
:hud_change(inventory_icon
.hudids
[playername
].bags
[i
].text
, "scale", scale
)
150 inventory_icon
.timer
= 0