1
0
mirror of git://repo.or.cz/minetest_inventory_icon.git synced 2025-06-28 22:36:30 +02:00

Compare commits

1 Commits

Author SHA1 Message Date
57b65bd350 Version MFF. 2018-09-07 23:06:49 +02:00
14 changed files with 98 additions and 109 deletions

View File

@ -1,21 +0,0 @@
# Inventory Icon
Version: 1.1.0
## Description
This simple mod adds an icon to the player's HUD at the bottom right corner.
It shows how many inventory slots are available and how many of them are
occupied.
This mod also supports the Bags and Unified Inventory mods. If one of these
mods are used, icons for each bag are displayed, too.
## Licenses
### Code
The code is licensed under the MIT License.
### Media files
Licensed under [CC-BY-SA 3.0 Unported](https://creativecommons.org/licenses/by-sa/3.0/).
#### Authors
* `inventory_icon_backpack_free.png`: DitsyDM and Traipse OpenRPG (CC-BY-SA 3.0 Unported)
* `inventory_icon_backpack_full.png`: DitsyDM, Traipse OpenRPG and Wuzzy (CC-BY-SA 3.0 Unported)

23
README.txt Executable file
View File

@ -0,0 +1,23 @@
Inventory Icon
==============
Version: 1.0.1
This simple mod adds an icon to the player's HUD which shows how many inventory slots are available and how many of them are occupied.
This mod also supports the Unified Inventory mod. If this mod is used, icons for each bag are displayed, too.
----------------
License of code:
WTFPL
License of media files:
CC-BY-SA 3.0 Unported
Authors:
- inventory_icon_backpack_free.png: DitsyDM and Traipse OpenRPG (CC-BY-SA 3.0 Unported)
- inventory_icon_backpack_full.png: DitsyDM, Traipse OpenRPG and Wuzzy (CC-BY-SA 3.0 Unported)
- inventory_icon_bags_small.png: Tonyka (New BSD License)
- inventory_icon_bags_medium.png: Tonyka (New BSD License)
- inventory_icon_bags_large.png: Tonyka (New BSD License)

4
depends.txt Normal file → Executable file
View File

@ -1,3 +1 @@
bags?
unified_inventory?
intllib?
unified_inventory

2
description.txt Normal file → Executable file
View File

@ -1 +1 @@
Shows a backpack icon in the HUD, which shows how many slots are available and free in the player inventory and equipped bags (if available).
Shows a little backpack icon in the HUD, which shows how many slots are available and free in the player inventory.

152
init.lua Normal file → Executable file
View File

@ -1,18 +1,8 @@
local S
if (minetest.get_modpath("intllib")) then
S = intllib.Getter()
else
S = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
end
local inventory_icon = {}
inventory_icon = {}
inventory_icon.hudids = {}
inventory_icon.COLORIZE_STRING = "[colorize:#A00000:192"
local mod_ui = minetest.get_modpath("unified_inventory") ~= nil
local mod_bags = minetest.get_modpath("bags") ~= nil
function inventory_icon.get_inventory_state(inv, listname)
local size = inv:get_size(listname)
local occupied = 0
@ -26,7 +16,15 @@ function inventory_icon.get_inventory_state(inv, listname)
end
function inventory_icon.replace_icon(name)
return name.."^[resize:32x32"
local icon = ""
if name:find("small") then
icon = "inventory_icon_bags_small.png"
elseif name:find("medium") then
icon = "inventory_icon_bags_medium.png"
elseif name:find("large") then
icon = "inventory_icon_bags_large.png"
end
return icon
end
minetest.register_on_joinplayer(function(player)
@ -54,47 +52,45 @@ minetest.register_on_joinplayer(function(player)
offset = {x=-36,y=-20},
alignment = {x=0,y=0},
number = 0xFFFFFF,
text = S("@1/@2", occupied, size)
text = string.format("%d/%d", occupied, size)
})
if mod_ui or mod_bags then
if minetest.get_modpath("unified_inventory") ~= nil then
inventory_icon.hudids[name].bags = {}
local bags_inv = minetest.get_inventory({type = "detached", name = name.."_bags"})
if bags_inv then
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^[resize:32x32"
else
scale = { x = 1, y = 1 }
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
text = S("@1/@2", occupied, size)
icon = inventory_icon.replace_icon(minetest.registered_items[bag:get_name()].inventory_image)
if occupied >= size then
icon = icon .. "^" .. inventory_icon.COLORIZE_STRING
end
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 = "inventory_icon_bags_small.png"
else
scale = { x = 1, y = 1 }
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
text = string.format("%d/%d", occupied, size)
icon = inventory_icon.replace_icon(bag:get_name())
if occupied >= size then
icon = icon .. "^" .. inventory_icon.COLORIZE_STRING
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
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)
@ -103,12 +99,11 @@ 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)
local function tick()
minetest.after(1, tick)
for playername,hudids in pairs(inventory_icon.hudids) do
local player = minetest.get_player_by_name(playername)
if player then
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main")
local icon, color
if occupied >= size then
@ -117,36 +112,35 @@ minetest.register_globalstep(function(dtime)
icon = "inventory_icon_backpack_free.png"
end
player:hud_change(hudids.main.icon, "text", icon)
player:hud_change(hudids.main.text, "text", S("@1/@2", occupied, size))
player:hud_change(hudids.main.text, "text", string.format("%d/%d", occupied, size))
if mod_ui or mod_bags then
if minetest.get_modpath("unified_inventory") ~= nil then
local bags_inv = minetest.get_inventory({type = "detached", name = playername.."_bags"})
if bags_inv then
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^[resize:32x32"
else
scale = { x = 1, y = 1 }
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
text = S("@1/@2", occupied, size)
icon = inventory_icon.replace_icon(minetest.registered_items[bag:get_name()].inventory_image)
if occupied >= size then
icon = icon .. "^" .. inventory_icon.COLORIZE_STRING
end
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 = "inventory_icon_bags_small.png"
else
scale = { x = 1, y = 1 }
local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "bag"..i.."contents")
text = string.format("%d/%d", occupied, size)
icon = inventory_icon.replace_icon(bag:get_name())
if occupied >= size then
icon = icon .. "^" .. inventory_icon.COLORIZE_STRING
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
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
end
inventory_icon.timer = 0
end
end)
end
tick()

View File

@ -1 +0,0 @@
@1/@2 = @1/@2

View File

@ -1,3 +0,0 @@
# Shows number of occupied (@1) vs number of total inventory slots (@2).
# Try to keep the translation short.
@1/@2 =

View File

@ -1 +0,0 @@
name = inventory_icon

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.1 KiB

BIN
textures/inventory_icon_backpack_free.png Normal file → Executable file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 818 B

After

Width:  |  Height:  |  Size: 348 B

BIN
textures/inventory_icon_backpack_full.png Normal file → Executable file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 720 B

After

Width:  |  Height:  |  Size: 345 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 836 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 B