registration.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. homedecor = homedecor or {}
  2. local placeholder_node = "homedecor:expansion_placeholder"
  3. --wrapper around minetest.register_node that sets sane defaults and interprets some specialized settings
  4. function homedecor.register(name, original_def)
  5. local def = table.copy(original_def)
  6. def.drawtype = def.drawtype
  7. or (def.mesh and "mesh")
  8. or (def.node_box and "nodebox")
  9. def.paramtype = def.paramtype or "light"
  10. -- avoid facedir for some drawtypes as they might be used internally for something else
  11. -- even if undocumented
  12. if not (def.drawtype == "glasslike_framed"
  13. or def.drawtype == "raillike"
  14. or def.drawtype == "plantlike"
  15. or def.drawtype == "firelike") then
  16. def.paramtype2 = def.paramtype2 or "facedir"
  17. end
  18. homedecor.handle_inventory(name, def, original_def)
  19. local infotext = def.infotext
  20. --def.infotext = nil -- currently used to set locked refrigerator infotexts
  21. if infotext then
  22. local on_construct = def.on_construct
  23. def.on_construct = function(pos)
  24. local meta = minetest.get_meta(pos)
  25. meta:set_string("infotext", infotext)
  26. if on_construct then on_construct(pos) end
  27. end
  28. end
  29. local expand = def.expand
  30. def.expand = nil
  31. local after_unexpand = def.after_unexpand
  32. def.after_unexpand = nil
  33. if expand then
  34. -- dissallow rotating only half the expanded node by default
  35. -- unless we know better
  36. def.on_rotate = def.on_rotate
  37. or (minetest.get_modpath("screwdriver") and (def.mesh and expand.top and screwdriver.rotate_simple)
  38. or screwdriver.disallow) or nil
  39. def.on_place = def.on_place or function(itemstack, placer, pointed_thing)
  40. if expand.top then
  41. return homedecor.stack_vertically(itemstack, placer, pointed_thing, itemstack:get_name(), expand.top)
  42. elseif expand.right then
  43. return homedecor.stack_sideways(itemstack, placer, pointed_thing, itemstack:get_name(), expand.right, true)
  44. elseif expand.forward then
  45. return homedecor.stack_sideways(itemstack, placer, pointed_thing, itemstack:get_name(), expand.forward, false)
  46. end
  47. end
  48. def.after_dig_node = def.after_dig_node or function(pos, oldnode, oldmetadata, digger)
  49. if expand.top and expand.forward ~= "air" then
  50. local top_pos = { x=pos.x, y=pos.y+1, z=pos.z }
  51. local node = minetest.get_node(top_pos).name
  52. if node == expand.top or node == placeholder_node then
  53. minetest.remove_node(top_pos)
  54. end
  55. end
  56. local fdir = oldnode.param2
  57. if not fdir or fdir > 3 then return end
  58. if expand.right and expand.forward ~= "air" then
  59. local right_pos = {
  60. x = pos.x + homedecor.fdir_to_right[fdir+1][1],
  61. y = pos.y,
  62. z = pos.z + homedecor.fdir_to_right[fdir+1][2]
  63. }
  64. local node = minetest.get_node(right_pos).name
  65. if node == expand.right or node == placeholder_node then
  66. minetest.remove_node(right_pos)
  67. end
  68. end
  69. if expand.forward and expand.forward ~= "air" then
  70. local forward_pos = { x=pos.x+homedecor.fdir_to_fwd[fdir+1][1], y=pos.y, z=pos.z+homedecor.fdir_to_fwd[fdir+1][2] }
  71. local node = minetest.get_node(forward_pos).name
  72. if node == expand.forward or node == placeholder_node then
  73. minetest.remove_node(forward_pos)
  74. end
  75. end
  76. if after_unexpand then
  77. after_unexpand(pos)
  78. end
  79. end
  80. end
  81. -- register the actual minetest node
  82. minetest.register_node(":homedecor:" .. name, def)
  83. end