store all the item data

metadata is now also stored when cells are removed
This commit is contained in:
theFox6 2020-10-04 11:48:05 +02:00
parent 96a66c3336
commit abe36940a7
No known key found for this signature in database
GPG Key ID: C884FE8D3BCE128A
2 changed files with 36 additions and 27 deletions

@ -3,42 +3,45 @@ me.networks = {}
local networks = me.networks local networks = me.networks
local path = microexpansion.get_module_path("network") local path = microexpansion.get_module_path("network")
--deprecated: use ItemStack(x) instead
--[[
local function split_stack_values(stack) local function split_stack_values(stack)
local stack_name, stack_count, stack_wear, stack_meta
if type(stack) == "string" then if type(stack) == "string" then
local split_string = stack:split(" ") local split_string = stack:split(" ")
stack_name = split_string[1] if (#split_string < 1) then
if (#split_string > 1) then return "",0,0,nil
stack_count = tonumber(split_string[2])
if (#split_string > 2) then
stack_wear = tonumber(split_string[3])
else
stack_wear = 0
end end
else local stack_name = split_string[1]
stack_count = 1 if (#split_string < 2) then
return stack_name,1,0,nil
end end
else local stack_count = tonumber(split_string[2])
stack_name = stack:get_name() if (#split_string < 3) then
stack_count = stack:get_count() return stack_name,stack_count,0,nil
stack_wear = stack:get_wear() end
stack_meta = stack:get_meta() local stack_wear = tonumber(split_string[3])
if (#split_string < 4) then
return stack_name,stack_count,stack_wear,nil
end
return stack_name,stack_count,stack_wear,true
else
return stack:get_name(), stack:get_count(), stack:get_wear(), stack:get_meta()
end end
return stack_name, stack_count, stack_wear, stack_meta
end end
--]]
function me.insert_item(stack, inv, listname) function me.insert_item(stack, inv, listname)
if me.settings.huge_stacks == false then if me.settings.huge_stacks == false then
return inv:add_item(listname, stack) return inv:add_item(listname, stack)
end end
local stack_name,stack_count,stack_wear,stack_meta = split_stack_values(stack) local to_insert = type(stack) == "userdata" and stack or ItemStack(stack)
local found = false local found = false
for i = 0, inv:get_size(listname) do for i = 0, inv:get_size(listname) do
local inside = inv:get_stack(listname, i) local inside = inv:get_stack(listname, i)
if inside:get_name() == stack_name and inside:get_wear() == stack_wear then if inside:get_name() == to_insert:get_name() and inside:get_wear() == to_insert:get_wear() then
if inside:get_meta():equals(stack_meta) then if inside:get_meta():equals(to_insert:get_meta()) then
local total_count = inside:get_count() + stack_count local total_count = inside:get_count() + to_insert:get_count()
-- bigger item count is not possible we only have unsigned 16 bit -- bigger item count is not possible, we only have unsigned 16 bit
if total_count <= math.pow(2,16) then if total_count <= math.pow(2,16) then
if not inside:set_count(total_count) then if not inside:set_count(total_count) then
minetest.log("error"," adding items to stack in microexpansion network failed") minetest.log("error"," adding items to stack in microexpansion network failed")

@ -88,7 +88,7 @@ local function write_to_cell(cell, items, item_count)
return cell return cell
end end
local function write_drive_cells(pos,network) --args: pos, listname, index, stack, player local function write_drive_cells(pos,network)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local own_inv = meta:get_inventory() local own_inv = meta:get_inventory()
if network == nil then if network == nil then
@ -113,14 +113,19 @@ local function write_drive_cells(pos,network) --args: pos, listname, index, stac
for i = 1, ctrl_inv:get_size("main") do for i = 1, ctrl_inv:get_size("main") do
local stack_inside = ctrl_inv:get_stack("main", i) local stack_inside = ctrl_inv:get_stack("main", i)
local stack_name = stack_inside:get_name() local item_string = stack_inside:to_string()
if stack_name ~= "" then if item_string ~= "" then
item_string = item_string:split(" ")
local item_count = stack_inside:get_count() local item_count = stack_inside:get_count()
if item_count > 1 and item_string[2] ~= tostring(item_count) then
minetest.log("warning","[microexpansion] stack count differs from second field of the item string")
end
while item_count ~= 0 and cell_idx ~= nil do while item_count ~= 0 and cell_idx ~= nil do
--print(("stack to store: %s %i"):format(stack_name,item_count)) --print(("stack to store: %q"):format(table.concat(item_string," ")))
if size < items_in_cell_count + item_count then if size < items_in_cell_count + item_count then
local space = size - items_in_cell_count local space = size - items_in_cell_count
table.insert(cell_items,("%s %i"):format(stack_name,space)) item_string[2] = tostring(space)
table.insert(cell_items,table.concat(item_string," "))
items_in_cell_count = items_in_cell_count + space items_in_cell_count = items_in_cell_count + space
own_inv:set_stack("main", cell_idx, write_to_cell(cells[cell_idx],cell_items,items_in_cell_count)) own_inv:set_stack("main", cell_idx, write_to_cell(cells[cell_idx],cell_items,items_in_cell_count))
@ -136,7 +141,8 @@ local function write_drive_cells(pos,network) --args: pos, listname, index, stac
item_count = item_count - space item_count = item_count - space
else else
items_in_cell_count = items_in_cell_count + item_count items_in_cell_count = items_in_cell_count + item_count
table.insert(cell_items, ("%s %i"):format(stack_name,item_count)) item_string[2] = tostring(item_count)
table.insert(cell_items,table.concat(item_string," "))
item_count = 0 item_count = 0
end end
end end