Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cabinets = {}
- cabs_table = {}
- --[[Arguments:
- *pos - position of clicked node;
- *cabinet_name - same value as well as one of cabinets.open();
- *cabinet_num - number of cabinet, in other words this is component of the cabinet. Number is string;
- *data - table that contains list of all its components with each component name key and its data (formspec tables).]]
- cabinets.put_data_into_cabinet = function (pos, cabinet_name, cabinet_num, data, formspec)
- local meta = minetest.get_meta(pos)
- if type(data) == "table" and #data ~= 0 then
- meta:set_string(cabinet_name .. "_" .. cabinet_num, minetest.serialize(data))
- else
- return
- end
- meta:set_string(formspec["name"], formspec["data"])
- return true
- end
- --[[Arguments:
- *opener - player data;
- *pos - position of clicked node;
- *node_replace - itemstring, node that needed to be replaced to former.
- *cabinet_name - "generalized" name of all its components, that is kitchen_wooden_cabinet_1, kitchen_wooden_cabinet_2, kitchen_wooden_cabinet_3 are kitchen_wooden_cabinet indeed, they are just modified models;
- *clicked_button_name - name of button that was clicked;
- *formspec - table with next keys: name, data (formspec string itself);
- *sound_play - table that can keep ONLY two sound names that needed to be played during opening and closing. Keys are: first is "open", second is "close".]]
- cabinets.open = function (opener, pos, node_replace, cabinet_name, clicked_button_name, formspec, sound_play)
- local name = minetest.get_node(pos).name
- local meta = minetest.deserialize(minetest.get_meta(pos):get_string(name))
- -- The lower loop is running departments of the node as kitchen_wooden_cabinet_1... then it compares clicked_button_name is equal to the button name in the department.
- for depart_num, depart_data in pairs(meta) do
- if meta[depart_num]["button"] == clicked_button_name then
- changed_part_data = meta
- -- Onwards it determines the department is "opened" or "closed", if "closed" sets opposite to this mode in changed_depart_data, else returns nil.
- if meta[depart_num]["mode"] == "closed" then
- changed_part_data[depart_num]["mode"] = "opened"
- elseif meta[depart_num]["mode"] == "opened" then
- return
- end
- -- Here the loop is running cabs_table[name] table where "name" is "general" node name of all its "departments", then it`s running each "department" and comparing modes with mode of "changed_depart_data".
- -- (To the comment above) comparing modes is needed to determine which registered part of general node contains departments with same modes.
- for node_name, node_data in pairs(cabs_table[cabinet_name][name]) do
- for depart_num2, depart_data2 in pairs(node_data) do
- if node_data[depart_num2]["mode"] == changed_part_data[depart_num2]["mode"] then
- -- For success it copies inventory list of the department and put to corresponding one of changed_
- changed_part_data[depart_num2]["inv_list"] = node_data[depart_num2]["inv_list"]
- else
- return
- end
- end
- end
- end
- end
- minetest.remove_node(pos)
- minetest.set_node(pos, node_replace)
- if sound_play and sound_play["open"] then
- minetest.sound_play(sound_play["open"])
- end
- local new_meta = minetest.get_meta(pos)
- new_meta:set_string(formspec["name"], formspec["data"])
- local inv = minetest.get_inventory({type = "node", name = pos})
- local lists = inv:get_lists()
- for _, depart_data in ipairs(changed_part_data) do
- inv:set_list(depart_data["listname"], depart_data["inv_list"])
- if depart_data["mode"] == "opened" then
- inv:set_size(depart_data["listname"], depart_data["inv_size"])
- end
- end
- minetest.show_formspec(opener:get_player_name(), node_replace, formspec[name])
- end
- cabinets.close = function (closer, pos, node_replace, cabinet_name, clicked_button_name, formspec, sound_play)
- local name = minetest.get_node(pos).name
- local meta = minetest.deserialize(minetest.get_meta(pos):get_string(name))
- -- The lower loop is running departments of the node as kitchen_wooden_cabinet_1... then it compares clicked_button_name is equal to the button name in the department.
- for depart_num, depart_data in pairs(meta) do
- if meta[depart_num]["button"] == clicked_button_name then
- changed_part_data = meta
- -- Onwards it determines the department is "opened" or "closed", if "closed" sets opposite to this mode in changed_depart_data, else returns nil.
- if meta[depart_num]["mode"] == "opened" then
- changed_part_data[depart_num]["mode"] = "closed"
- elseif meta[depart_num]["mode"] == "closed" then
- return
- end
- -- Here the loop is running cabs_table[name] table where "name" is "general" node name of all its "departments", then it`s running each "department" and comparing modes with mode of "changed_depart_data".
- -- (To the comment above) comparing modes is needed to determine which registered part of general node contains departments with same modes.
- for node_name, node_data in pairs(cabs_table[cabinet_name][name]) do
- for depart_num2, depart_data2 in pairs(node_data) do
- if node_data[depart_num2]["mode"] == changed_part_data[depart_num2]["mode"] then
- -- For success it copies inventory list of the department and put to corresponding one of changed_
- changed_part_data[depart_num2]["inv_list"] = node_data[depart_num2]["inv_list"]
- else
- return
- end
- end
- end
- end
- end
- minetest.remove_node(pos)
- minetest.set_node(pos, node_replace)
- if sound_play and sound_play["close"] then
- minetest.sound_play(sound_play["close"])
- end
- local new_meta = minetest.get_meta(pos)
- new_meta:set_string(formspec["name"], formspec["data"])
- local inv = minetest.get_inventory({type = "node", name = pos})
- local lists = inv:get_lists()
- for _, depart_data in ipairs(changed_part_data) do
- inv:set_list(depart_data["listname"], depart_data["inv_list"])
- if depart_data["mode"] == "opened" then
- inv:set_size(depart_data["listname"], depart_data["inv_size"])
- end
- end
- minetest.show_formspec(closer:get_player_name(), node_replace, formspec[name])
- end
- cabinets.define_needed_cabinet = function (formname, nodename)
- for _, depart in ipairs(kit_wood_cabs[nodename]) do
- if depart["button"] == formname then
- modes = {}
- for _, depart2 in ipairs(kit_wood_cabs[name]) do
- modes[#modes+1] = depart2["mode"]
- end
- break
- end
- end
- for name, cab in pairs(kit_wood_cabs) do
- local mode_num = 0
- for name2, depart in ipairs(cab) do
- mode_num = mode_num + 1
- if modes[mode_num] == depart["mode"] then
- if modes[mode_num] == #modes then
- return name2
- end
- end
- end
- end
- end
- cabinets.define_mode = function (formname, nodename)
- for _, depart in ipairs(kit_wood_cabs[nodename]) do
- if depart["button"] == formname then
- return depart["mode"]
- end
- end
- end
- minetest.register_on_joinplayer(function (player)
- minetest.debug(dump(cabinets))
- end)
- -- Create a table with external table for each cabinet sort (depends to boxes). Inside each second field a list of boxes and their datas.
- kit_wood_cabs = {
- ["kitchen_wooden_cabinet_1"] = {
- --form_size="",
- {mode="closed", button = "kwc1_1", img_button = "close_button.png", listname = "kwc1_1", inv_list={}},
- {mode="closed", button = "kwc1_2", img_button = "close_button.png", listname = "kwc1_2", inv_list={}}
- },
- ["kitchen_wooden_cabinet_2"] = {
- --form_size="",
- {mode="opened", button = "kwc2_1", img_button = "open_button.png", listname = "kwc2_1", inv_list={}, inv_size=4*2},
- {mode="closed", button = "kwc2_2", img_button = "close_button.png", listname = "kwc2_2", inv_list={}},
- not_in_creative_inventory=1
- },
- ["kitchen_wooden_cabinet_3"] = {
- --form_size="",
- {mode="closed", button = "kwc3_1", img_button = "close_button.png", listname = "kwc3_1", inv_list={}},
- {mode="opened", button = "kwc3_2", img_button = "open_button.png", listname = "kwc3_2", inv_list={}, inv_size=4*2},
- not_in_creative_inventory=1
- },
- ["kitchen_wooden_cabinet_4"] = {
- --form_size="",
- {mode="opened", button = "kwc4_1", img_button = "open_button.png", listname = "kwc4_1", inv_list={}, inv_size=4*2},
- {mode="opened", button = "kwc4_2", img_button = "open_button.png", listname = "kwc4_2", inv_list={}, inv_size=4*2},
- not_in_creative_inventory=1
- }
- }
- if not cabs_table["kitchen_wooden_cabinet"] then
- cabs_table["kitchen_wooden_cabinet"] = kit_wood_cabs
- end
- -- The loop is running the table above and register each cabinet sort.
- local cabinet_num = 0
- local form
- for cab, cab_boxes in pairs(kit_wood_cabs) do
- cabinet_num = cabinet_num + 1
- minetest.register_node("luxury_decor:"..cab, {
- description = "Kitchen Wooden Cabinet",
- visual_scale = 0.5,
- mesh = cab..".obj",
- tiles = {"wood_material.png"},
- paramtype = "light",
- paramtype2 = "facedir",
- groups = {choppy=3, not_in_creative_inventory = cab_boxes["not_in_creative_inventory"]},
- drawtype = "mesh",
- collision_box = {
- type = "fixed",
- fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}
- },
- selection_box = {
- type = "fixed",
- fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}
- },
- sounds = default.node_sound_wood_defaults(),
- on_construct = function (pos)
- local name = minetest.get_node(pos).name
- local img_button1 = "image_button[0.5, 0;1, 2;" .. cab_boxes[1].img_button ..";" .. cab_boxes[1].button .. "]"
- local img_button2 = "image_button[0.5, 0.3;1, 2;" .. cab_boxes[2].img_button .. ";" .. cab_boxes[2].button .. "]"
- local list1 = "list[nodemeta:".. pos.x .. "," .. pos.y .. "," .. pos.z .. ";".. cab_boxes[1].listname .. ";1.5, 0;4, 2]"
- local list2 = "list[nodemeta:".. pos.x .. "," .. pos.y .. "," .. pos.z .. ";".. cab_boxes[2].listname .. ";1.5, 0.3;4, 2]"
- form = "size[6,5]" .. img_button1 .. img_button2 .. list1 .. list2 .. "]"
- --minetest.debug(dump(cabinets))
- cabinets.put_data_into_cabinet(pos, "kitchen_wooden_cabinet", tostring(cabinet_num), cab_boxes, {name=name, data=form})
- local inv = minetest.get_inventory({type="node", pos=pos})
- inv:set_list(cab_boxes[1].listname, cab_boxes[1].inv_list)
- inv:set_list(cab_boxes[2].listname, cab_boxes[2].inv_list)
- inv:set_size(cab_boxes[1].listname, 4*2)
- inv:set_size(cab_boxes[2].listname, 4*2)
- -- Fills "form_size" of each cabinet with needed size and "form_data" of each box with datas to build formspec with lists.
- --[[for cab2, cab_boxes2 in pairs(kit_wood_cabs) do
- cab_boxes2[cab2][form_size] = "size[8,4]"
- for box_num, cab_data in ipairs(cab_boxes2[2]) do
- cab_data[box_num][form_data] = inv_elems[1] .. "0.2, 0.1;0.5, 2" .. inv_elems[2]
- if cab_data[
- end
- end]]
- end,
- on_rightclick = function (pos, node, clicker, itemstack, pointed_thing)
- minetest.show_formspec(clicker:get_player_name(), cab, form)
- end,
- on_receive_fields = function (pos, formname, fields, sender)
- local name = minetest.get_node(pos).name
- local generalized_name = string.match(name, '^._-')
- --[[for _, depart in ipairs(kit_wood_cabs[name]) do
- if depart[button] == formname then
- if depart[mode] == "closed" then
- for _, depart2 in ipairs(kit_wood_cabs) do
- if ]]
- local defined_mode = cabinets.define_mode(formname, name)
- if defined_mode == "closed" then
- cabinets.open(sender, pos, cabinets.define_needed_cabinet(formname, name), generalized_name, formname, form)
- elseif defined_mode == "opened" then
- cabinets.close(sender, pos, cabinets.define_needed_cabinet(formname, name), generalized_name, formname, form)
- end
- end
- })
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement