ME Chest: Fix drive item limit bug

Previously the math was incorrect, allowing a single 8k drive to store over 300,000 items. This also cleans up the storage area a bit, moving the API related to storage drives from the main API.lua to storage/api.lua.
This commit is contained in:
octacian 2017-02-23 07:35:42 -08:00
parent 0d917954eb
commit 0a9d8880a9
4 changed files with 78 additions and 56 deletions

35
api.lua

@ -57,38 +57,3 @@ function microexpansion.register_item(itemstring, def)
register_recipe(BASENAME..":"..itemstring, def.recipe) register_recipe(BASENAME..":"..itemstring, def.recipe)
end end
end end
-- [function] register cell
function microexpansion.register_cell(itemstring, def)
if not def.inventory_image then
def.inventory_image = itemstring
end
-- register craftitem
minetest.register_craftitem(BASENAME..":"..itemstring, {
description = def.description,
inventory_image = BASENAME.."_"..def.inventory_image..".png",
groups = {microexpansion_cell = 1},
microexpansion = {
drive = {
capacity = def.capacity or 5000,
},
},
})
-- if recipe, register recipe
if def.recipe then
-- if recipe, register recipe
if def.recipe then
register_recipe(BASENAME..":"..itemstring, def.recipe)
end
end
end
-- [function] Get cell size
function microexpansion.get_cell_size(name)
local item = minetest.registered_craftitems[name]
if item then
return item.microexpansion.drive.capacity
end
end

48
modules/storage/api.lua Normal file

@ -0,0 +1,48 @@
-- storage/api.lua
local BASENAME = "microexpansion"
-- [function] register cell
function microexpansion.register_cell(itemstring, def)
if not def.inventory_image then
def.inventory_image = itemstring
end
-- register craftitem
minetest.register_craftitem(BASENAME..":"..itemstring, {
description = def.description,
inventory_image = BASENAME.."_"..def.inventory_image..".png",
groups = {microexpansion_cell = 1},
microexpansion = {
drive = {
capacity = def.capacity or 5000,
},
},
})
-- if recipe, register recipe
if def.recipe then
-- if recipe, register recipe
if def.recipe then
register_recipe(BASENAME..":"..itemstring, def.recipe)
end
end
end
-- [function] Get cell size
function microexpansion.get_cell_size(name)
local item = minetest.registered_craftitems[name]
if item then
return item.microexpansion.drive.capacity
end
end
-- [function] Calculate max stacks
function microexpansion.int_to_stacks(int)
return math.floor(int / 99)
end
-- [function] Calculate number of pages
function microexpansion.int_to_pagenum(int)
return math.floor(microexpansion.int_to_stacks(int) / 32)
end

@ -2,6 +2,9 @@
local module_path = microexpansion.get_module_path("storage") local module_path = microexpansion.get_module_path("storage")
-- Load API
dofile(module_path.."/api.lua")
-- Load storage devices -- Load storage devices
dofile(module_path.."/storage.lua") dofile(module_path.."/storage.lua")

@ -1,5 +1,7 @@
-- microexpansion/machines.lua -- microexpansion/machines.lua
local me = microexpansion
-- [me chest] Get formspec -- [me chest] Get formspec
local function chest_formspec(start_id, listname, page_max, query) local function chest_formspec(start_id, listname, page_max, query)
local list local list
@ -14,24 +16,28 @@ local function chest_formspec(start_id, listname, page_max, query)
page_number = "label[6.05,4.5;" .. math.floor((start_id / 32)) + 1 .. page_number = "label[6.05,4.5;" .. math.floor((start_id / 32)) + 1 ..
"/" .. page_max .."]" "/" .. page_max .."]"
end end
return "size[9,10]" .. return [[
microexpansion.gui_bg .. size[9,10]
microexpansion.gui_slots .. ]]..
list .. microexpansion.gui_bg ..
"list[current_name;cells;8,1.8;1,1;]" .. microexpansion.gui_slots ..
"list[current_player;main;0,5.5;8,1;]" .. list ..
"list[current_player;main;0,6.73;8,3;8]" .. [[
"button[5.4,4.35;0.8,0.9;prev;<]" .. list[current_name;cells;8,1.8;1,1;]
"button[7.25,4.35;0.8,0.9;next;>]" .. list[current_player;main;0,5.5;8,1;]
"field[0.3,4.6;2.2,1;filter;;" .. query .. "]" .. list[current_player;main;0,6.73;8,3;8]
"button[2.1,4.5;0.8,0.5;search;?]" .. button[5.4,4.35;0.8,0.9;prev;<]
"button[2.75,4.5;0.8,0.5;clear;X]" .. button[7.25,4.35;0.8,0.9;next;>]
"tooltip[search;Search]" .. field[0.3,4.6;2.2,1;filter;;]]..query..[[]
"tooltip[clear;Reset]" .. button[2.1,4.5;0.8,0.5;search;?]
"listring[current_name;main]" .. button[2.75,4.5;0.8,0.5;clear;X]
"listring[current_player;main]" .. tooltip[search;Search]
"field_close_on_enter[filter;false]" .. tooltip[clear;Reset]
page_number listring[current_name;main]
listring[current_player;main]
field_close_on_enter[filter;false]
]]..
page_number
end end
-- [me chest] Register node -- [me chest] Register node
@ -85,9 +91,9 @@ minetest.register_node("microexpansion:chest", {
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local items = minetest.deserialize(stack:get_meta():get_string("items")) local items = minetest.deserialize(stack:get_meta():get_string("items"))
local size = microexpansion.get_cell_size(stack:get_name()) local size = me.get_cell_size(stack:get_name())
local page_max = math.floor(size / 32) + 1 local page_max = me.int_to_pagenum(size) + 1
inv:set_size("main", size) inv:set_size("main", me.int_to_stacks(size))
if items then if items then
inv:set_list("main", items) inv:set_list("main", items)
end end