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
View File

@ -57,38 +57,3 @@ function microexpansion.register_item(itemstring, def)
register_recipe(BASENAME..":"..itemstring, def.recipe)
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
View 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

View File

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

View File

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