From a3a150ece9286e57b4193f49187260f3273d5989 Mon Sep 17 00:00:00 2001 From: octacian Date: Thu, 23 Feb 2017 15:05:15 -0800 Subject: [PATCH] ME Drives: Show capacity in description Introduces microexpansion.cell_desc(inv, listname, stack_pos) to update the item description to show the capacity. Requires https://github.com/minetest/minetest/commit/f2f9a923515386d787a245fac52f78e815b3a839 or later. --- modules/storage/api.lua | 32 ++++++++++++++++++++++++++++++++ modules/storage/machines.lua | 2 ++ modules/storage/storage.lua | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/modules/storage/api.lua b/modules/storage/api.lua index b8ee791..92b877f 100644 --- a/modules/storage/api.lua +++ b/modules/storage/api.lua @@ -14,6 +14,7 @@ function microexpansion.register_cell(itemstring, def) inventory_image = BASENAME.."_"..def.inventory_image..".png", groups = {microexpansion_cell = 1}, microexpansion = { + base_desc = def.description, drive = { capacity = def.capacity or 5000, }, @@ -62,3 +63,34 @@ function microexpansion.move_inv(inv1, inv2) end end end + +-- [function] Update cell description +function microexpansion.cell_desc(inv, listname, spos) + local stack = inv:get_stack(listname, spos) + + if stack:get_name() ~= "" then + local meta = stack:get_meta() + local base_desc = minetest.registered_craftitems[stack:get_name()].microexpansion.base_desc + local max_slots = inv:get_size("main") + local max_items = math.floor(max_slots * 99) + + local slots, items = 0, 0 + -- Get amount of items in drive + for i = 1, max_items do + local stack = inv:get_stack("main", i) + local item = stack:get_name() + if item ~= "" then + slots = slots + 1 + local num = stack:get_count() + if num == 0 then num = 1 end + items = items + num + end + end + + -- Update description + meta:set_string("description", base_desc.."\n".. + minetest.colorize("grey", tostring(items).."/"..tostring(max_items).." Items")) + -- Update stack + inv:set_stack(listname, spos, stack) + end +end diff --git a/modules/storage/machines.lua b/modules/storage/machines.lua index 75f4611..87a099a 100644 --- a/modules/storage/machines.lua +++ b/modules/storage/machines.lua @@ -96,6 +96,7 @@ minetest.register_node("microexpansion:chest", { local inv = minetest.get_meta(pos):get_inventory() inv:remove_item(listname, stack) inv:add_item(listname, stack) + microexpansion.cell_desc(inv, "cells", 1) elseif listname == "cells" then local meta = minetest.get_meta(pos) local inv = meta:get_inventory() @@ -115,6 +116,7 @@ minetest.register_node("microexpansion:chest", { if listname == "search" then inv:remove_item("main", stack) end + microexpansion.cell_desc(inv, "cells", 1) end, allow_metadata_inventory_take = function(pos, listname, index, stack, player) if listname == "cells" then diff --git a/modules/storage/storage.lua b/modules/storage/storage.lua index 5edb83b..7a59454 100644 --- a/modules/storage/storage.lua +++ b/modules/storage/storage.lua @@ -2,6 +2,6 @@ -- [drive] 8k microexpansion.register_cell("cell_8k", { - description = "8k Storage Cell", + description = "8k ME Storage Cell", capacity = 8000, })