Guest User

Untitled

a guest
Nov 29th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.69 KB | None | 0 0
  1. cabinets = {}
  2. cabinets_funcs = {}
  3.  
  4. --[[Arguments:
  5. *cabinet - table that contains name and position of the cabinet
  6. *cabinet_boxes_data - table with cabinet data in what are names of cabinets are set in-world, position of each cabinet and their boxes datas, their modes ("opened"/"closed") and inventory formspecs.
  7. ]]
  8. cabinets_funcs.put_data_into_cabinet = function (cabinet, cabinet_boxes_data)
  9. local name = cabinet[1]
  10. if not cabinets[name] then
  11. cabinets[name] = {}
  12. end
  13.  
  14. local pos = cabinet[2]
  15. cabinets[name][pos] = {}
  16.  
  17. for box_name, box in pairs(cabinet_boxes_data) do
  18. local box = cabinet_boxes_data[box_name]
  19. cabinets[name][pos][box_name] = box
  20. local meta = minetest.get_meta(cabinets[name][pos][box_name][2])
  21. meta:set_string("inventory", cabinets[name][pos][box_name][4])
  22. --minetest.debug(dump(cabinet_boxes_data[box_name][2]))
  23. --minetest.debug(dump(cabinet_boxes_data[box_name][1].name))
  24. minetest.set_node(cabinet_boxes_data[box_name][2], cabinet_boxes_data[box_name][1])
  25. --minetest.debug("DGSDGDSGFDSGSFDGSDGSD")
  26. local box_meta = minetest.get_meta(cabinet_boxes_data[box_name][2])
  27. box_meta:set_string(cabinet_boxes_data[box_name][1].name, minetest.serialize({cabinet[1], cabinet[2]}))
  28. --minetest.debug("DDDDDDDDDDDD")
  29. end
  30.  
  31. minetest.debug(dump(cabinets[name][pos]))
  32. end
  33.  
  34.  
  35. --[[Arguments:
  36. *opener - Player
  37. *cabinet - table that contains name and position of the cabinet
  38. *cabinets_data - table that contains "name" field of the cabinet inside "pos" field of each set the cabinet in-world and its data (list of boxes inside)
  39. *cabinet_box - table that contains data of clicked box (name, itemstring, pos, mode and inv)
  40. *offset - table with coordinates (x, y, z) that means how many coordinate points this is needed to offset
  41. *formspec - table that contains listname, itemstacks_list, formname and formspec itself]]
  42. cabinets_funcs.open = function (opener, cabinet, cabinets_data, cabinet_box, offset, formspec)
  43. local name = cabinet[1]
  44. local pos = cabinet[3]
  45. local cabinet_box_name = cabinet_box[1]
  46. if cabinets_data[name] then
  47. if cabinets_data[pos] then
  48. --for _, box in ipairs(cabinets_data[pos]) do
  49. local inv = minetest.get_inventory({type="node", pos = pos})
  50. local lists = inv:get_lists()
  51. local list = lists[1]
  52. cabinets[name][pos][cabinet_box_name][4] = {formspec[1], formspec[2], formspec[3], formspec[4]}
  53. cabinets[name][pos][cabinet_box_name][3] = "opened"
  54. --cabinets_data[cabinet_pos][cabinet_box[2] .. tostring(#cabinets_data[cabinet_pos])][3] = list
  55. --cabinets_data[cabinet_pos][cabinet_box[2] .. tostring(#cabinets_data[cabinet_pos])][4] = formspec_data_field
  56. minetest.remove_node(cabinet_box[2])
  57. local new_pos = {x=pos.x + offset.x, y=pos.y + offset.y, z=pos.z + offset.z}
  58. cabinets[name][pos][cabinet_box_name][2] = new_pos
  59. minetest.set_node(new_pos, cabinet_box[2])
  60.  
  61. local meta = minetest.get_meta(new_pos)
  62. meta:set_string("inventory", cabinets[name][pos][cabinet_box_name][4][4])
  63. local new_inv = minetest.get_inventory({type="node", pos=new_pos})
  64. local new_lists = new_inv:get_lists()
  65. local new_list = new_lists[1]
  66.  
  67. for _, itemstack in ipairs(new_list) do
  68. new_inv:add_item(cabinets[name][pos][cabinet_box_name][4][1], itemstack:get_count())
  69. end
  70.  
  71. minetest.show_formspec(opener, cabinets[name][pos][cabinet_box_name][4][3], cabinets[name][pos][cabinet_box_name][4][4])
  72. end
  73. end
  74. end
  75.  
  76. --[[Arguments:
  77. *Same ones as for cabinets_funcs.open(), expect "offset" where should be new position for new node placing]]
  78. cabinets_funcs.close = function (closer, cabinet, cabinets_data, cabinet_box, offset)
  79. local name = cabinet[1]
  80. local pos = cabinet[2]
  81. local cabinet_box_name = cabinet_box[1]
  82. local inv = minetest.get_inventory({type="node", pos=pos})
  83. local lists = inv:get_lists()
  84. local list = lists[1]
  85.  
  86. cabinets[name][pos][cabinet_box_name][4][2] = list
  87. cabinets[name][pos][cabinet_box_name][3] = "closed"
  88.  
  89. minetest.remove_node(cabinet_box[2])
  90. local new_pos = {x=pos.x + offset.x, y=pos.y + offset.y, z=pos.z + offset.z}
  91. cabinets[name][pos][cabinet_box_name][1] = new_pos
  92. minetest.add_node(new_pos, cabinet_box[2])
  93. end
  94.  
  95.  
  96.  
  97. minetest.register_node("luxury_decor:kitchen_wooden_cabinet", {
  98. description = "Kitchen Wooden Cabinet",
  99. visual_scale = 0.5,
  100. mesh = "kitchen_wooden_cabinet.obj",
  101. tiles = {"wood_material.png"},
  102. --inventory_image = "simple_wooden_table_inv.png",
  103. paramtype = "light",
  104. paramtype2 = "facedir",
  105. groups = {choppy = 3.5},
  106. drawtype = "mesh",
  107. collision_box = {
  108. type = "fixed",
  109. fixed = {
  110. --[[{-0.5, -0.5, -0.5, -0.4, 0.5, 0.5}, -- Left nodebox
  111. {0.4, -0.5, -0.5, 0.5, 0.5, 0.5},]] -- Right nodebox
  112. {-0.4, 0.4, -0.5, 0.5, 0.5, 0.5}, -- Upper nodebox
  113. --[[{-0.4, -0.5, -0.6, 0.4, -0.4, 0.5}, -- Downer nodebox
  114. {-0.4, -0.4, 0.5, 0.4, -0.4, 0.4}, -- Back nodebox
  115. {-0.4, -0.05, -0.5, 0.4, 0.05, 0.4},]] -- Shelf Nodebox
  116.  
  117.  
  118. }
  119. },
  120. selection_box = {
  121. type = "fixed",
  122. fixed = {
  123. {-0.415, -0.415, -0.45, -0.5, 0.42, 0.45}, -- Left nodebox
  124. {0.415, -0.415, -0.45, 0.5, 0.42, 0.45}, -- Right nodebox
  125. {-0.5, 0.42, -0.45, 0.5, 0.5, 0.45}, -- Upper nodebox
  126. {-0.5, -0.42, -0.45, 0.5, -0.5, 0.45}, -- Downer nodebox
  127. {-0.45, -0.42, 0.37, 0.45, 0.42, 0.45}, -- Back nodebox
  128. {-0.41, -0.037, -0.45, 0.41, 0.037, 0.45}, -- Shelf Nodebox
  129.  
  130. }
  131. },
  132. sounds = default.node_sound_wood_defaults(),
  133. on_construct = function (pos)
  134. local name = minetest.get_node(pos).name
  135. local cabinet_data = {
  136. {minetest.get_node(pos).name, pos},
  137. --{"box1", "box2"},
  138. {
  139. ["box1"] = {
  140. {
  141. name="luxury_decor:kitchen_wooden_cabinet_box",
  142. param1 = minetest.get_node(pos).param1,
  143. param2 = minetest.get_node(pos).param2
  144. },
  145. {x=pos.x, y=pos.y+0.25, z=pos.z},
  146. "closed",
  147. "box_inventory[{"..pos.x..","..pos.y..","..pos.z.."}]", "size[8, 4][nodemeta:"..pos.x..",".. pos.y+0.25 ..","..pos.z..";main;0, 1;8, 3]list[current_player;main;0, 5;8, 3]"
  148.  
  149. },
  150. ["box2"] = {
  151. {
  152. name="luxury_decor:kitchen_wooden_cabinet_box",
  153. param1 = minetest.get_node(pos).param1,
  154. param2 = minetest.get_node(pos).param2
  155. },
  156. {x=pos.x, y=pos.y-0.25, z=pos.z},
  157. "closed",
  158. "box_inventory[{"..pos.x..","..pos.y..","..pos.z.."}]", "size[8, 4][nodemeta:"..pos.x..",".. pos.y-0.25 ..","..pos.z..";main;0, 1;8, 3]list[current_player;main;0, 5;8, 3]"
  159. }
  160. }
  161. }
  162. cabinets_funcs.put_data_into_cabinet(cabinet_data[1], cabinet_data[2])
  163. --[[local name = minetest.get_node(pos).name
  164. if not cabinets[name] then
  165. cabinets[name] = {}
  166. end
  167.  
  168. cabinets[name][pos] = {}
  169. cabinets[name][pos][ = {{x=pos.x, y=pos.y+0.25, z=pos.z}, "closed"}
  170. kitchen_wooden_cabinets[pos]["box2"] = {{x=pos.x, y=pos.y-0.25, z=pos.z}, "closed"}
  171.  
  172. minetest.add_node({x=pos.x, y=pos.y+0.25, z=pos.z}, "luxury_decor:kitchen_wooden_cabinet_box")
  173. minetest.add_node({x=pos.x, y=pos.y-0.25, z=pos.z}, "luxury_decor:kitchen_wooden_cabinet_box")]]
  174. end
  175.  
  176. })
  177.  
  178. minetest.register_node("luxury_decor:kitchen_wooden_cabinet_box", {
  179. description = "Kitchen Wooden Cabinet Box",
  180. visual_scale = 0.5,
  181. mesh = "kitchen_wooden_cabinet_box.obj",
  182. tiles = {"wood_material.png"},
  183. --inventory_image = "simple_wooden_table_inv.png",
  184. paramtype = "light",
  185. paramtype2 = "facedir",
  186. groups = {choppy = 3.5, not_in_creative_inventory=1},
  187. drawtype = "mesh",
  188. collision_box = {
  189. type = "fixed",
  190. fixed = {
  191. {-0.45, 0.05, -0.475, 0.45, 0.43, 0.48},
  192. }
  193. },
  194. selection_box = {
  195. type = "fixed",
  196. fixed = {
  197. {-0.45, 0.05, -0.475, 0.45, 0.43, 0.48},
  198. }
  199. },
  200. sounds = default.node_sound_wood_defaults(),
  201. on_rightclick = function (pos, node, clicker, itemstack, pointed_thing)
  202. local meta = minetest.get_meta(pos)
  203. --minetest.debug(meta[1])
  204. --minetest.debug(meta[2])
  205. local cabinet_name, cabinet_pos = meta:get_string(meta[1]), meta:get_string(meta[2])
  206.  
  207. if cabinet_name ~= nil or cabinet_name ~= "" then
  208. local cabinet_data = cabinets[cabinet_name][cabinet_pos]
  209. for box_name, box in ipairs(cabinet_data) do
  210. if cabinet_data[box_name][1].name == node.name then
  211. local cabinet = {meta[1], meta[2]}
  212. local cabinet_box = {box_name, cabinet_data[box_name][1].name, cabinet_data[box_name][2], cabinet_data[box_name][3], cabinet_data[box_name][4]}
  213. local offset = {x=0, y=0, z=-0.7}
  214. cabinets_funcs.open(clicker:get_player_name(), cabinet, cabinets, cabinet_box, offset, {"main", {}, "box_formspec", cabinets_data[box_name][4][4]})
  215.  
  216. break
  217. end
  218. end
  219. end
  220. end,
  221.  
  222. on_receive_fields = function (pos, formname, fields, sender)
  223. if fields.quit then
  224. local meta = minetest.get_meta(pos)
  225. local cabinet_name, cabinet_pos, cabinet_data = meta:get_string(meta[1]), meta:get_string(meta[2]), cabinets[cabinet_name][cabinet_pos]
  226. for box_name, box in ipairs(cabinet_data) do
  227. if cabinet_data[box_name][1].name == node.name then
  228. local cabinet_box = {box_name, cabinet_data[box_name][1].name, cabinet_data[box_name][2], cabinet_data[box_name][3], cabinet_data[box_name][4]}
  229.  
  230. cabinets_funcs.close(sender:get_player_name(), {meta[1], meta[2]}, cabinets, cabinet_box, offset)
  231.  
  232. break
  233. end
  234. end
  235. end
  236. end
  237. })
Advertisement
Add Comment
Please, Sign In to add comment