Fixed shelves lists saving

This commit is contained in:
Andrey2470T 2023-03-03 00:48:29 +03:00
parent d64e8e812e
commit aa0800a51f
3 changed files with 100 additions and 95 deletions

View File

@ -34,11 +34,13 @@ function multidecor.helpers.upper_first_letters(s)
return new_s
end
function multidecor.helpers.build_name_from_tmp(name, type, i)
local res = name .. "_" .. i .. "_".. type
function multidecor.helpers.build_name_from_tmp(name, type, i, pos)
local strpos = pos.x .. "_" .. pos.y .. "_" .. pos.z
local res = name .. "_" .. i .. "_".. type .. "_" .. strpos
if not name:match("multidecor:") then
res = "multidecor:" .. res
end
return res
end

View File

@ -129,13 +129,9 @@ function multidecor.shelves.set_shelves(pos)
return
end
local dir = def.paramtype2 == "facedir" and minetest.facedir_to_dir(node.param2) or
def.paramtype2 == "wallmounted" and minetest.wallmounted_to_dir(node.param2)
local rot_y = vector.dir_to_rotation(dir)
for i, shelf_data in ipairs(def.add_properties.shelves_data) do
local inv_name = multidecor.helpers.build_name_from_tmp(def.add_properties.shelves_data.common_name, "inv", i)
local list_name = multidecor.helpers.build_name_from_tmp(def.add_properties.shelves_data.common_name, "list", i)
local inv_name = multidecor.helpers.build_name_from_tmp(def.add_properties.shelves_data.common_name, "inv", i, pos)
local list_name = multidecor.helpers.build_name_from_tmp(def.add_properties.shelves_data.common_name, "list", i, pos)
local list_type = shelf_data.invlist_type or "storage"
local padding = 0.25
@ -209,14 +205,16 @@ multidecor.shelves.default_on_activate = function(self, staticdata)
self.cook_info = data[10]
end
minetest.debug("self.inv_list: " .. dump(self.inv_list))
local node = minetest.get_node(self.connected_to.pos)
if node.name ~= self.connected_to.name then
local shelves_data = minetest.registered_nodes[self.connected_to.name].add_properties.shelves_data
if not node.name:match(shelves_data.common_name) then
self.object:remove()
return
end
local shelf_data = minetest.registered_nodes[self.connected_to.name].add_properties.shelves_data[self.shelf_data_i]
local shelf_data = shelves_data[self.shelf_data_i]
local obj_props = {}
obj_props.visual_size = self.object:get_properties().visual_size
@ -238,80 +236,82 @@ multidecor.shelves.default_on_activate = function(self, staticdata)
multidecor.shelves.rotate_shelf_bbox(self.object)
local inv_name = multidecor.helpers.build_name_from_tmp(minetest.registered_nodes[self.connected_to.name].add_properties.shelves_data.common_name, "inv", self.shelf_data_i)
minetest.create_detached_inventory(inv_name, {
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
return count
end,
allow_put = function(inv, listname, index, stack, player)
local c = stack:get_count()
local inv_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "inv", self.shelf_data_i, self.connected_to.pos)
local inv = minetest.get_inventory({type="detached", name=inv_name})
if shelf_data.invlist_type == "cooker" then
local output = minetest.get_craft_result({method="cooking", width=1, items={stack}})
if not inv then
inv = minetest.create_detached_inventory(inv_name, {
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
return count
end,
allow_put = function(inv, listname, index, stack, player)
local c = stack:get_count()
if not output or output.time == 0 then
c = 0
if shelf_data.invlist_type == "cooker" then
local output = minetest.get_craft_result({method="cooking", width=1, items={stack}})
if not output or output.time == 0 then
c = 0
end
end
return c
end,
allow_take = function(inv, listname, index, stack, player)
return stack:get_count()
end,
on_put = function(inv, listname, index, stack, player)
if shelf_data.invlist_type == "trash" then
stack:clear()
inv:remove_item(listname, inv:get_stack(listname, 1))
minetest.sound_play("multidecor_trash", {to_player=player:get_player_name()})
elseif shelf_data.invlist_type == "cooker" then
local name = stack:get_name()
local output = minetest.get_craft_result({method="cooking", width=1, items=inv:get_list(listname)})
local total_time = output.time*stack:get_count()
local obj = open_shelves[player:get_player_name()]
local self = obj:get_luaentity()
local padding = 0.25
local list_w = 9+7*padding
local list_x = list_w/2-0.5
local fire_fs = "image[0.5,1;1,1;multidecor_cooker_fire_on.png]style_type[label;font=bold;font_size=*2]label[7,1.5;%s]"
local cook_active_fs = "image[" .. list_x .. ",1;1,1;multidecor_cooker_active_bg.png^[lowpart:%f:multidecor_cooker_active_fs.png]"
minetest.swap_node(self.connected_to.pos, {
name="multidecor:" ..shelves_data.common_name .. "_activated",
param2=minetest.get_node(self.connected_to.pos).param2
})
local sound_handle = minetest.sound_play("multidecor_hum", {object=obj, fade=1.0, max_hear_distance=10, loop=true})
minetest.get_meta(self.connected_to.pos):set_string("sound_handle", minetest.serialize(sound_handle))
self.cook_info = {output, 0, total_time, self.inv, fire_fs, cook_active_fs, stack:get_count()}
end
end
return c
end,
allow_take = function(inv, listname, index, stack, player)
return stack:get_count()
end,
on_put = function(inv, listname, index, stack, player)
if shelf_data.invlist_type == "trash" then
stack:clear()
inv:remove_item(listname, inv:get_stack(listname, 1))
minetest.sound_play("multidecor_trash", {to_player=player:get_player_name()})
elseif shelf_data.invlist_type == "cooker" then
local name = stack:get_name()
})
local output = minetest.get_craft_result({method="cooking", width=1, items=inv:get_list(listname)})
local total_time = output.time*stack:get_count()
local list_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "list", self.shelf_data_i, self.connected_to.pos)
local inv_list = {}
local obj = open_shelves[player:get_player_name()]
local self = obj:get_luaentity()
for _, stack_t in ipairs(self.inv_list) do
local stack = ItemStack(stack_t.name)
stack:set_count(stack_t.count)
stack:set_wear(stack_t.wear)
local padding = 0.25
local list_w = 9+7*padding
local list_x = list_w/2-0.5
local fire_fs = "image[0.5,1;1,1;multidecor_cooker_fire_on.png]style_type[label;font=bold;font_size=*2]label[7,1.5;%s]"
local cook_active_fs = "image[" .. list_x .. ",1;1,1;multidecor_cooker_active_bg.png^[lowpart:%f:multidecor_cooker_active_fs.png]"
minetest.swap_node(self.connected_to.pos, {
name="multidecor:" .. minetest.registered_nodes[self.connected_to.name].add_properties.shelves_data.common_name .. "_activated",
param2=minetest.get_node(self.connected_to.pos).param2
})
local sound_handle = minetest.sound_play("multidecor_hum", {object=obj, fade=1.0, max_hear_distance=10, loop=true})
minetest.get_meta(self.connected_to.pos):set_string("sound_handle", minetest.serialize(sound_handle))
self.cook_info = {output, 0, total_time, self.inv, fire_fs, cook_active_fs, stack:get_count()}
end
table.insert(inv_list, stack)
end
})
local inv = minetest.get_inventory({type="detached", name=inv_name})
local inv_list = {}
local list_type = shelf_data.invlist_type or "storage"
local invsize = list_type == "storage" and shelf_data.inv_size or {w=1, h=1}
inv:set_list(list_name, inv_list)
inv:set_size(list_name, invsize.w*invsize.h)
inv:set_width(list_name, invsize.w)
for _, stack_t in ipairs(self.inv_list) do
local stack = ItemStack(stack_t.name)
stack:set_count(stack_t.count)
stack:set_wear(stack_t.wear)
table.insert(inv_list, stack)
inv:set_size("main", 32)
inv:set_width("main", 8)
end
local shelves_data = minetest.registered_nodes[self.connected_to.name].add_properties.shelves_data
local list_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "list", self.shelf_data_i)
local list_type = shelf_data.invlist_type or "storage"
local invsize = list_type == "storage" and shelf_data.inv_size or {w=1, h=1}
inv:set_list(list_name, inv_list)
inv:set_size(list_name, invsize.w*invsize.h)
inv:set_width(list_name, invsize.w)
inv:set_size("main", 32)
inv:set_width("main", 8)
end
multidecor.shelves.default_get_staticdata = function(self)
@ -321,7 +321,7 @@ end
multidecor.shelves.default_on_rightclick = function(self, clicker)
open_shelves[clicker:get_player_name()] = self.object
local shelves_data = minetest.registered_nodes[self.connected_to.name].add_properties.shelves_data
minetest.show_formspec(clicker:get_player_name(), multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "fs", self.shelf_data_i), self.inv)
minetest.show_formspec(clicker:get_player_name(), multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "fs", self.shelf_data_i, self.connected_to.pos), self.inv)
if self.dir == 0 then
multidecor.shelves.open_shelf(self.object, 1)
@ -333,6 +333,7 @@ local function cook_step(self, dtime)
if not self.cook_info then
return
end
--minetest.debug("1")
self.cook_info[2] = self.cook_info[2] + dtime
local elapsed_time = self.cook_info[2]
@ -341,20 +342,23 @@ local function cook_step(self, dtime)
local str_perc = tostring(math.round(percents)) .. " %"
self.inv = self.cook_info[4]:sub(1, i-1) .. self.cook_info[6]:format(percents) .. self.cook_info[4]:sub(i) .. self.cook_info[5]:format(str_perc)
--minetest.debug("2")
local meta = minetest.get_meta(self.connected_to.pos)
meta:set_string("infotext", "Cooked to: " .. str_perc)
local shelves_data = minetest.registered_nodes[self.connected_to.name].add_properties.shelves_data
local inv_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "inv", self.shelf_data_i)
local inv_list = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "list", self.shelf_data_i)
local inv_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "inv", self.shelf_data_i, self.connected_to.pos)
local inv_list = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "list", self.shelf_data_i, self.connected_to.pos)
local inv = minetest.get_inventory({type="detached", name=inv_name})
--minetest.debug("3")
local time_elapsed = self.cook_info and self.cook_info[2] >= self.cook_info[3]
if inv:is_empty(inv_list) or time_elapsed then
if time_elapsed then
self.cook_info[1].item:set_count(self.cook_info[7])
inv:set_stack(inv_list, 1, self.cook_info[1].item)
end
--minetest.debug("4")
self.inv = self.cook_info[4]
self.cook_info = nil
meta:set_string("infotext", "")
@ -364,6 +368,7 @@ local function cook_step(self, dtime)
name="multidecor:" .. shelves_data.common_name,
param2=minetest.get_node(self.connected_to.pos).param2
})
--minetest.debug("5")
end
local show_to
@ -375,11 +380,13 @@ local function cook_step(self, dtime)
end
end
--minetest.debug("6")
local i, f = math.modf(elapsed_time)
if show_to and (f > 0 and f < 0.05) then
minetest.show_formspec(show_to, multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "fs", self.shelf_data_i), self.inv)
minetest.show_formspec(show_to, multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "fs", self.shelf_data_i, self.connected_to.pos), self.inv)
end
--minetest.debug("7")
end
multidecor.shelves.default_drawer_on_step = function(self, dtime)
@ -422,25 +429,20 @@ multidecor.shelves.default_door_on_step = function(self, dtime)
end
multidecor.shelves.default_on_receive_fields = function(player, formname, fields)
local is_table_inv = formname:find("%d+", -10)
local shelf_i = formname:find("%d+")
if not is_table_inv then
minetest.debug("1")
if not shelf_i then
return
end
local name = formname:sub(1, is_table_inv-2)
minetest.debug("formname: " .. formname)
minetest.debug("name: " .. name)
local name = formname:sub(1, shelf_i-2)
local def = minetest.registered_nodes[name]
if not def then
minetest.debug("2")
return
end
if not name:sub(1, name:find(":")-1) == "multidecor" then
minetest.debug("3")
return
end
@ -450,11 +452,12 @@ multidecor.shelves.default_on_receive_fields = function(player, formname, fields
local self = shelf:get_luaentity()
local shelves_data = minetest.registered_nodes[self.connected_to.name].add_properties.shelves_data
local inv_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "inv", self.shelf_data_i)
local inv_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "inv", self.shelf_data_i, self.connected_to.pos)
local inv = minetest.get_inventory({type="detached", name=inv_name})
local shelf_data = def.add_properties.shelves_data[self.shelf_data_i]
local list = inv:get_list(multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "list", self.shelf_data_i))
local list = inv:get_list(multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "list", self.shelf_data_i, self.connected_to.pos))
minetest.debug("list: " .. dump(list))
self.inv_list = {}
for _, stack in ipairs(list) do
table.insert(self.inv_list, {name=stack:get_name(), count=stack:get_count(), wear=stack:get_wear()})
end
@ -465,12 +468,12 @@ end
multidecor.shelves.default_can_dig = function(pos)
local name = minetest.get_node(pos).name
local add_props = minetest.registered_nodes[name].add_properties
local shelves_data = minetest.registered_nodes[name].add_properties.shelves_data
local is_all_empty = true
for i, shelf in ipairs(add_props.shelves_data) do
local inv_name = multidecor.helpers.build_name_from_tmp(add_props.shelves_data.common_name, "inv", i)
local list_name = multidecor.helpers.build_name_from_tmp(add_props.shelves_data.common_name, "list", i)
for i, shelf in ipairs(shelves_data) do
local inv_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "inv", i, pos)
local list_name = multidecor.helpers.build_name_from_tmp(shelves_data.common_name, "list", i, pos)
local inv = minetest.get_inventory({type="detached", name=inv_name})
is_all_empty = is_all_empty and inv:is_empty(list_name)

View File

@ -1,5 +1,5 @@
local function get_shelf_formspec(name, pos, w, h)
local listname = multidecor.helpers.build_name_from_tmp(name, "list", 1)
local listname = multidecor.helpers.build_name_from_tmp(name, "list", 1, pos)
return table.concat({
"formspec_version[5]",
"size[11,9]",
@ -15,7 +15,7 @@ local shelf_on_construct = function(pos)
meta:set_string("formspec", get_shelf_formspec(name, pos, add_props.inv_size.w, add_props.inv_size.h))
local inv = minetest.get_inventory({type="node", pos=pos})
local list_name = multidecor.helpers.build_name_from_tmp(name, "list", 1)
local list_name = multidecor.helpers.build_name_from_tmp(name, "list", 1, pos)
inv:set_size(list_name, add_props.inv_size.w*add_props.inv_size.h)
inv:set_width(list_name, add_props.inv_size.w)
@ -26,7 +26,7 @@ end
local shelf_can_dig = function(pos)
local inv = minetest.get_inventory({type="node", pos=pos})
return inv:is_empty(multidecor.helpers.build_name_from_tmp(minetest.get_node(pos).name, "list", 1))
return inv:is_empty(multidecor.helpers.build_name_from_tmp(minetest.get_node(pos).name, "list", 1, pos))
end