Try to fix mahmutelmas06's reported bug
[minetest_inventory_icon.git] / init.lua
blob16828cffec6ff17ff60f1e20809c4d2f2bf41a83
1 local S
2 if (minetest.get_modpath("intllib")) then
3 S = intllib.Getter()
4 else
5 S = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
6 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)
18 local occupied = 0
19 for i=1,size do
20 local stack = inv:get_stack(listname, i)
21 if not stack:is_empty() then
22 occupied = occupied + 1
23 end
24 end
25 return occupied, size
26 end
28 function inventory_icon.replace_icon(name)
29 return name.."^[resize:32x32"
30 end
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")
36 local icon
37 if occupied >= size then
38 icon = "inventory_icon_backpack_full.png"
39 else
40 icon = "inventory_icon_backpack_free.png"
41 end
42 inventory_icon.hudids[name].main = {}
43 inventory_icon.hudids[name].main.icon = player:hud_add({
44 hud_elem_type = "image",
45 position = {x=1,y=1},
46 scale = {x=1,y=1},
47 offset = {x=-32,y=-32},
48 text = icon,
50 inventory_icon.hudids[name].main.text = player:hud_add({
51 hud_elem_type = "text",
52 position = {x=1,y=1},
53 scale = {x=1,y=1},
54 offset = {x=-36,y=-20},
55 alignment = {x=0,y=0},
56 number = 0xFFFFFF,
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"})
62 if bags_inv then
63 for i=1,4 do
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 }
68 text = ""
69 icon = "bags_small.png^[resize:32x32"
70 else
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
77 end
78 end
79 inventory_icon.hudids[name].bags[i] = {}
80 inventory_icon.hudids[name].bags[i].icon = player:hud_add({
81 hud_elem_type = "image",
82 position = {x=1,y=1},
83 scale = scale,
84 size = { x=32, y=32 },
85 offset = {x=-36,y=-32 -40*i},
86 text = icon,
88 inventory_icon.hudids[name].bags[i].text = player:hud_add({
89 hud_elem_type = "text",
90 position = {x=1,y=1},
91 scale = scale,
92 offset = {x=-36,y=-20 -40*i},
93 alignment = {x=0,y=0},
94 number = 0xFFFFFF,
95 text = text,
97 end
98 end
99 end
100 end)
102 minetest.register_on_leaveplayer(function(player)
103 inventory_icon.hudids[player:get_player_name()] = nil
104 end)
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")
113 local icon, color
114 if occupied >= size then
115 icon = "inventory_icon_backpack_full.png"
116 else
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"})
124 if bags_inv then
125 for i=1,4 do
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 }
130 text = ""
131 icon = "bags_small.png^[resize:32x32"
132 else
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
152 end)