mirror of
git://repo.or.cz/minetest_inventory_icon.git
synced 2025-01-10 07:20:17 +01:00
Add unified_inventory support (part 1)
This commit is contained in:
parent
2f4c3c85fc
commit
9c63d43fef
1
depends.txt
Normal file
1
depends.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
unified_inventory?
|
56
init.lua
56
init.lua
@ -1,12 +1,11 @@
|
|||||||
inventory_icon = {}
|
inventory_icon = {}
|
||||||
inventory_icon.hudids = {}
|
inventory_icon.hudids = {}
|
||||||
|
|
||||||
function inventory_icon.get_inventory_state(player)
|
function inventory_icon.get_inventory_state(inv, listname)
|
||||||
local inv = player:get_inventory()
|
local size = inv:get_size(listname)
|
||||||
local size = inv:get_size("main")
|
|
||||||
local occupied = 0
|
local occupied = 0
|
||||||
for i=1,size do
|
for i=1,size do
|
||||||
local stack = inv:get_stack("main", i)
|
local stack = inv:get_stack(listname, i)
|
||||||
if not stack:is_empty() then
|
if not stack:is_empty() then
|
||||||
occupied = occupied + 1
|
occupied = occupied + 1
|
||||||
end
|
end
|
||||||
@ -17,21 +16,22 @@ end
|
|||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
inventory_icon.hudids[name] = {}
|
inventory_icon.hudids[name] = {}
|
||||||
local occupied, size = inventory_icon.get_inventory_state(player)
|
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main")
|
||||||
local icon
|
local icon
|
||||||
if occupied >= size then
|
if occupied >= size then
|
||||||
icon = "inventory_icon_backpack_full.png"
|
icon = "inventory_icon_backpack_full.png"
|
||||||
else
|
else
|
||||||
icon = "inventory_icon_backpack_free.png"
|
icon = "inventory_icon_backpack_free.png"
|
||||||
end
|
end
|
||||||
inventory_icon.hudids[name].icon = player:hud_add({
|
inventory_icon.hudids[name].main = {}
|
||||||
|
inventory_icon.hudids[name].main.icon = player:hud_add({
|
||||||
hud_elem_type = "image",
|
hud_elem_type = "image",
|
||||||
position = {x=1,y=1},
|
position = {x=1,y=1},
|
||||||
scale = {x=1,y=1},
|
scale = {x=1,y=1},
|
||||||
offset = {x=-32,y=-32},
|
offset = {x=-32,y=-32},
|
||||||
text = icon,
|
text = icon,
|
||||||
})
|
})
|
||||||
inventory_icon.hudids[name].text = player:hud_add({
|
inventory_icon.hudids[name].main.text = player:hud_add({
|
||||||
hud_elem_type = "text",
|
hud_elem_type = "text",
|
||||||
position = {x=1,y=1},
|
position = {x=1,y=1},
|
||||||
scale = {x=1,y=1},
|
scale = {x=1,y=1},
|
||||||
@ -40,6 +40,42 @@ minetest.register_on_joinplayer(function(player)
|
|||||||
number = 0xFFFFFF,
|
number = 0xFFFFFF,
|
||||||
text = string.format("%d/%d", occupied, size)
|
text = string.format("%d/%d", occupied, size)
|
||||||
})
|
})
|
||||||
|
if minetest.get_modpath("unified_inventory") ~= nil then
|
||||||
|
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 = ""
|
||||||
|
icon = "bags_small.png"
|
||||||
|
else
|
||||||
|
scale = { x = 2, y = 2 }
|
||||||
|
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
|
||||||
|
text = string.format("%d/%d", occupied, size)
|
||||||
|
icon = minetest.registered_items[bag:get_name()].inventory_image
|
||||||
|
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
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_leaveplayer(function(player)
|
minetest.register_on_leaveplayer(function(player)
|
||||||
@ -52,15 +88,15 @@ minetest.register_globalstep(function(dtime)
|
|||||||
if inventory_icon.timer > 1 then
|
if inventory_icon.timer > 1 then
|
||||||
for playername,hudids in pairs(inventory_icon.hudids) do
|
for playername,hudids in pairs(inventory_icon.hudids) do
|
||||||
local player = minetest.get_player_by_name(playername)
|
local player = minetest.get_player_by_name(playername)
|
||||||
local occupied, size = inventory_icon.get_inventory_state(player)
|
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main")
|
||||||
local icon, color
|
local icon, color
|
||||||
if occupied >= size then
|
if occupied >= size then
|
||||||
icon = "inventory_icon_backpack_full.png"
|
icon = "inventory_icon_backpack_full.png"
|
||||||
else
|
else
|
||||||
icon = "inventory_icon_backpack_free.png"
|
icon = "inventory_icon_backpack_free.png"
|
||||||
end
|
end
|
||||||
player:hud_change(hudids.icon, "text", icon)
|
player:hud_change(hudids.main.icon, "text", icon)
|
||||||
player:hud_change(hudids.text, "text", string.format("%d/%d", occupied, size))
|
player:hud_change(hudids.main.text, "text", string.format("%d/%d", occupied, size))
|
||||||
end
|
end
|
||||||
inventory_icon.timer = 0
|
inventory_icon.timer = 0
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user