A Minetest mod which shows a simple backpack icon at the bottom left corner of the screen, showing how much inventory space is free https://content.minetest.net/packages/Wuzzy/inventory_icon/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.7KB

  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
  7. local inventory_icon = {}
  8. inventory_icon.hudids = {}
  9. inventory_icon.COLORIZE_STRING = "[colorize:#A00000:192"
  10. local mod_ui = minetest.get_modpath("unified_inventory") ~= nil
  11. local mod_bags = minetest.get_modpath("bags") ~= nil
  12. function inventory_icon.get_inventory_state(inv, listname)
  13. local size = inv:get_size(listname)
  14. local occupied = 0
  15. for i=1,size do
  16. local stack = inv:get_stack(listname, i)
  17. if not stack:is_empty() then
  18. occupied = occupied + 1
  19. end
  20. end
  21. return occupied, size
  22. end
  23. function inventory_icon.replace_icon(name)
  24. return name.."^[resize:32x32"
  25. end
  26. minetest.register_on_joinplayer(function(player)
  27. local name = player:get_player_name()
  28. inventory_icon.hudids[name] = {}
  29. local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main")
  30. local icon
  31. if occupied >= size then
  32. icon = "inventory_icon_backpack_full.png"
  33. else
  34. icon = "inventory_icon_backpack_free.png"
  35. end
  36. inventory_icon.hudids[name].main = {}
  37. inventory_icon.hudids[name].main.icon = player:hud_add({
  38. hud_elem_type = "image",
  39. position = {x=1,y=1},
  40. scale = {x=1,y=1},
  41. offset = {x=-32,y=-32},
  42. text = icon,
  43. })
  44. inventory_icon.hudids[name].main.text = player:hud_add({
  45. hud_elem_type = "text",
  46. position = {x=1,y=1},
  47. scale = {x=1,y=1},
  48. offset = {x=-36,y=-20},
  49. alignment = {x=0,y=0},
  50. number = 0xFFFFFF,
  51. text = S("@1/@2", occupied, size)
  52. })
  53. if mod_ui or mod_bags then
  54. inventory_icon.hudids[name].bags = {}
  55. local bags_inv = minetest.get_inventory({type = "detached", name = name.."_bags"})
  56. if bags_inv then
  57. for i=1,4 do
  58. local bag = bags_inv:get_stack("bag"..i, 1)
  59. local scale, text, icon
  60. if bag:is_empty() then
  61. scale = { x = 0, y = 0 }
  62. text = ""
  63. icon = "bags_small.png^[resize:32x32"
  64. else
  65. scale = { x = 1, y = 1 }
  66. local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
  67. text = S("@1/@2", occupied, size)
  68. icon = inventory_icon.replace_icon(minetest.registered_items[bag:get_name()].inventory_image)
  69. if occupied >= size then
  70. icon = icon .. "^" .. inventory_icon.COLORIZE_STRING
  71. end
  72. end
  73. inventory_icon.hudids[name].bags[i] = {}
  74. inventory_icon.hudids[name].bags[i].icon = player:hud_add({
  75. hud_elem_type = "image",
  76. position = {x=1,y=1},
  77. scale = scale,
  78. size = { x=32, y=32 },
  79. offset = {x=-36,y=-32 -40*i},
  80. text = icon,
  81. })
  82. inventory_icon.hudids[name].bags[i].text = player:hud_add({
  83. hud_elem_type = "text",
  84. position = {x=1,y=1},
  85. scale = scale,
  86. offset = {x=-36,y=-20 -40*i},
  87. alignment = {x=0,y=0},
  88. number = 0xFFFFFF,
  89. text = text,
  90. })
  91. end
  92. end
  93. end
  94. end)
  95. minetest.register_on_leaveplayer(function(player)
  96. inventory_icon.hudids[player:get_player_name()] = nil
  97. end)
  98. inventory_icon.timer = 0
  99. minetest.register_globalstep(function(dtime)
  100. inventory_icon.timer = inventory_icon.timer + dtime
  101. if inventory_icon.timer > 1 then
  102. for playername,hudids in pairs(inventory_icon.hudids) do
  103. local player = minetest.get_player_by_name(playername)
  104. local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main")
  105. local icon, color
  106. if occupied >= size then
  107. icon = "inventory_icon_backpack_full.png"
  108. else
  109. icon = "inventory_icon_backpack_free.png"
  110. end
  111. player:hud_change(hudids.main.icon, "text", icon)
  112. player:hud_change(hudids.main.text, "text", S("@1/@2", occupied, size))
  113. if mod_ui or mod_bags then
  114. local bags_inv = minetest.get_inventory({type = "detached", name = playername.."_bags"})
  115. if bags_inv then
  116. for i=1,4 do
  117. local bag = bags_inv:get_stack("bag"..i, 1)
  118. local scale, text, icon
  119. if bag:is_empty() then
  120. scale = { x = 0, y = 0 }
  121. text = ""
  122. icon = "bags_small.png^[resize:32x32"
  123. else
  124. scale = { x = 1, y = 1 }
  125. local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
  126. text = S("@1/@2", occupied, size)
  127. icon = inventory_icon.replace_icon(minetest.registered_items[bag:get_name()].inventory_image)
  128. if occupied >= size then
  129. icon = icon .. "^" .. inventory_icon.COLORIZE_STRING
  130. end
  131. end
  132. player:hud_change(inventory_icon.hudids[playername].bags[i].icon, "text", icon)
  133. player:hud_change(inventory_icon.hudids[playername].bags[i].icon, "scale", scale)
  134. player:hud_change(inventory_icon.hudids[playername].bags[i].text, "text", text)
  135. player:hud_change(inventory_icon.hudids[playername].bags[i].text, "scale", scale)
  136. end
  137. end
  138. end
  139. end
  140. inventory_icon.timer = 0
  141. end
  142. end)