ME Drives: Show capacity in description

Introduces microexpansion.cell_desc(inv, listname, stack_pos) to update the item description to show the capacity. Requires f2f9a92351 or later.
This commit is contained in:
octacian 2017-02-23 15:05:15 -08:00
parent 40ed277961
commit a3a150ece9
3 changed files with 35 additions and 1 deletions

@ -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

@ -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

@ -2,6 +2,6 @@
-- [drive] 8k
microexpansion.register_cell("cell_8k", {
description = "8k Storage Cell",
description = "8k ME Storage Cell",
capacity = 8000,
})