From bba533ea74d3326d75ea60e17ddc12a8d75668d2 Mon Sep 17 00:00:00 2001 From: Wouters Dorian Date: Mon, 6 Jul 2015 15:28:45 +0200 Subject: [PATCH] Add [inventory_icon] mod --- mods/inventory_icon/README.txt | 23 +++ mods/inventory_icon/depends.txt | 1 + mods/inventory_icon/description.txt | 1 + mods/inventory_icon/init.lua | 138 ++++++++++++++++++ .../textures/inventory_icon_backpack_free.png | Bin 0 -> 386 bytes .../textures/inventory_icon_backpack_full.png | Bin 0 -> 383 bytes .../textures/inventory_icon_bags_large.png | Bin 0 -> 660 bytes .../textures/inventory_icon_bags_medium.png | Bin 0 -> 836 bytes .../textures/inventory_icon_bags_small.png | Bin 0 -> 625 bytes 9 files changed, 163 insertions(+) create mode 100644 mods/inventory_icon/README.txt create mode 100644 mods/inventory_icon/depends.txt create mode 100644 mods/inventory_icon/description.txt create mode 100644 mods/inventory_icon/init.lua create mode 100644 mods/inventory_icon/textures/inventory_icon_backpack_free.png create mode 100644 mods/inventory_icon/textures/inventory_icon_backpack_full.png create mode 100644 mods/inventory_icon/textures/inventory_icon_bags_large.png create mode 100644 mods/inventory_icon/textures/inventory_icon_bags_medium.png create mode 100644 mods/inventory_icon/textures/inventory_icon_bags_small.png diff --git a/mods/inventory_icon/README.txt b/mods/inventory_icon/README.txt new file mode 100644 index 00000000..35975a46 --- /dev/null +++ b/mods/inventory_icon/README.txt @@ -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) diff --git a/mods/inventory_icon/depends.txt b/mods/inventory_icon/depends.txt new file mode 100644 index 00000000..20b7c424 --- /dev/null +++ b/mods/inventory_icon/depends.txt @@ -0,0 +1 @@ +unified_inventory? diff --git a/mods/inventory_icon/description.txt b/mods/inventory_icon/description.txt new file mode 100644 index 00000000..792a3860 --- /dev/null +++ b/mods/inventory_icon/description.txt @@ -0,0 +1 @@ +Shows a little backpack icon in the HUD, which shows how many slots are available and free in the player inventory. diff --git a/mods/inventory_icon/init.lua b/mods/inventory_icon/init.lua new file mode 100644 index 00000000..e9e4e149 --- /dev/null +++ b/mods/inventory_icon/init.lua @@ -0,0 +1,138 @@ +inventory_icon = {} +inventory_icon.hudids = {} + +inventory_icon.COLORIZE_STRING = "[colorize:#A00000:192" + +function inventory_icon.get_inventory_state(inv, listname) + local size = inv:get_size(listname) + local occupied = 0 + for i=1,size do + local stack = inv:get_stack(listname, i) + if not stack:is_empty() then + occupied = occupied + 1 + end + end + return occupied, size +end + +function inventory_icon.replace_icon(name) + return "inventory_icon_"..name +end + +minetest.register_on_joinplayer(function(player) + local name = player:get_player_name() + inventory_icon.hudids[name] = {} + local occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main") + local icon + if occupied >= size then + icon = "inventory_icon_backpack_full.png" + else + icon = "inventory_icon_backpack_free.png" + end + inventory_icon.hudids[name].main = {} + inventory_icon.hudids[name].main.icon = player:hud_add({ + hud_elem_type = "image", + position = {x=1,y=1}, + scale = {x=1,y=1}, + offset = {x=-32,y=-32}, + text = icon, + }) + inventory_icon.hudids[name].main.text = player:hud_add({ + hud_elem_type = "text", + position = {x=1,y=1}, + scale = {x=1,y=1}, + offset = {x=-36,y=-20}, + alignment = {x=0,y=0}, + number = 0xFFFFFF, + 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 = "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(minetest.registered_items[bag:get_name()].inventory_image) + if occupied >= size then + icon = icon .. "^" .. inventory_icon.COLORIZE_STRING + end + 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) + +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 occupied, size = inventory_icon.get_inventory_state(player:get_inventory(), "main") + local icon, color + if occupied >= size then + icon = "inventory_icon_backpack_full.png" + else + icon = "inventory_icon_backpack_free.png" + end + player:hud_change(hudids.main.icon, "text", icon) + player:hud_change(hudids.main.text, "text", string.format("%d/%d", occupied, size)) + + if minetest.get_modpath("unified_inventory") ~= nil then + local bags_inv = minetest.get_inventory({type = "detached", name = playername.."_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 = "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(minetest.registered_items[bag:get_name()].inventory_image) + if occupied >= size then + icon = icon .. "^" .. inventory_icon.COLORIZE_STRING + end + 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) diff --git a/mods/inventory_icon/textures/inventory_icon_backpack_free.png b/mods/inventory_icon/textures/inventory_icon_backpack_free.png new file mode 100644 index 0000000000000000000000000000000000000000..799f712afc1809fef7dcee9e2ef8976cd3da44b3 GIT binary patch literal 386 zcmV-|0e$|7P)vTE87@_DPuJ~E=wjHfal%n z^^$Jx+p_hnVD&=7m0Y63lLAaw)?q;I$A6MRP6FkHM~7J+-eW?jv#V;mYFE;+LzH2! zj@6(-u!ThJuqF_gB~tsj8J1j`P}>4YKP|5PQH3Vlund@Hc%HNJqrtDi5{wtqbtuc# zF8f4Hze%JnjW94u)j3!0+o;L_xsyJ7z9Pw-7MS;zUvoAXZ>4%c#A~8(pNJh42zt6i g^kO~VC;vIxUur~N4ll>h($0d!JMQvg8b*k%9#0T@X{K~yNuWs=)<#2^er zvC{woC;!3?EDUE02)Kga{#%K7c+DpIyk|6wkd%^IsFj*)u~`auqgy^tp$U2t=A*$3 zKf}QSPw+TP>t_0|4CW0UrFGAcV(#5+?&oR5WMk~bR?UJ{<=31I##`xL5b>JG-zQ=R1-zav5xq#y d_ep<__7{C2Qy@Aj#IgVY002ovPDHLkV1m5wp4b2Y literal 0 HcmV?d00001 diff --git a/mods/inventory_icon/textures/inventory_icon_bags_large.png b/mods/inventory_icon/textures/inventory_icon_bags_large.png new file mode 100644 index 0000000000000000000000000000000000000000..ebe3d91f292515d0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