init.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. --------------------------------------------------------------------------------
  2. -- Mod: Itemframes
  3. -- Author: Zeg9
  4. -- License: WTFPL
  5. --------------------------------------------------------------------------------
  6. local tmp = {}
  7. screwdriver = screwdriver or {}
  8. local function on_rename_check(pos, nodename)
  9. local meta = minetest.get_meta(pos)
  10. local owner = meta:get_string("owner")
  11. -- Nobody placed this block.
  12. if owner == "" then
  13. return
  14. end
  15. local dname = rename.gpn(owner)
  16. meta:set_string("rename", dname)
  17. meta:set_string("infotext", nodename .. " (Owned by <" .. dname .. ">!)")
  18. end
  19. minetest.register_entity("itemframes:item",{
  20. hp_max = 1,
  21. visual="wielditem",
  22. visual_size={x = 0.33, y = 0.33},
  23. collisionbox = {0, 0, 0, 0, 0, 0},
  24. physical = false,
  25. textures = {"air"},
  26. on_activate = function(self, staticdata)
  27. if tmp.nodename ~= nil and tmp.texture ~= nil then
  28. self.nodename = tmp.nodename
  29. tmp.nodename = nil
  30. self.texture = tmp.texture
  31. tmp.texture = nil
  32. else
  33. if staticdata ~= nil and staticdata ~= "" then
  34. local data = staticdata:split(';')
  35. if data and data[1] and data[2] then
  36. self.nodename = data[1]
  37. self.texture = data[2]
  38. end
  39. end
  40. end
  41. if self.texture ~= nil then
  42. self.object:set_properties({textures = {self.texture}})
  43. end
  44. if self.nodename == "itemframes:pedestal" then
  45. self.object:set_properties({automatic_rotate = 1})
  46. end
  47. end,
  48. get_staticdata = function(self)
  49. if self.nodename ~= nil and self.texture ~= nil then
  50. return self.nodename .. ';' .. self.texture
  51. end
  52. return ""
  53. end,
  54. })
  55. local facedir = {}
  56. facedir[0] = {x = 0, y = 0, z = 1}
  57. facedir[1] = {x = 1, y = 0, z = 0}
  58. facedir[2] = {x = 0, y = 0, z = -1}
  59. facedir[3] = {x = -1, y = 0, z = 0}
  60. local remove_item = function(pos, node)
  61. local objs = nil
  62. if node.name == "itemframes:frame" then
  63. objs = minetest.get_objects_inside_radius(pos, .5)
  64. elseif node.name == "itemframes:pedestal" then
  65. objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y+1,z=pos.z}, .5)
  66. end
  67. if objs then
  68. for _, obj in ipairs(objs) do
  69. if obj and obj:get_luaentity() and obj:get_luaentity().name == "itemframes:item" then
  70. obj:remove()
  71. end
  72. end
  73. end
  74. end
  75. local update_item = function(pos, node)
  76. remove_item(pos, node)
  77. local meta = minetest.get_meta(pos)
  78. if meta:get_string("item") ~= "" then
  79. if node.name == "itemframes:frame" then
  80. local posad = facedir[node.param2]
  81. if not posad then return end
  82. pos.x = pos.x + posad.x * 6.5 / 16
  83. pos.y = pos.y + posad.y * 6.5 / 16
  84. pos.z = pos.z + posad.z * 6.5 / 16
  85. elseif node.name == "itemframes:pedestal" then
  86. pos.y = pos.y + 12 / 16 + 0.33
  87. end
  88. tmp.nodename = node.name
  89. tmp.texture = ItemStack(meta:get_string("item")):get_name()
  90. local e = minetest.add_entity(pos,"itemframes:item")
  91. if e and node.name == "itemframes:frame" then
  92. local yaw = math.pi * 2 - node.param2 * math.pi / 2
  93. e:setyaw(yaw)
  94. end
  95. end
  96. end
  97. local drop_item = function(pos, node)
  98. local meta = minetest.get_meta(pos)
  99. if meta:get_string("item") ~= "" then
  100. if node.name == "itemframes:frame" then
  101. minetest.add_item(pos, meta:get_string("item"))
  102. elseif node.name == "itemframes:pedestal" then
  103. minetest.add_item({x=pos.x,y=pos.y+1,z=pos.z}, meta:get_string("item"))
  104. end
  105. meta:set_string("item","")
  106. end
  107. remove_item(pos, node)
  108. end
  109. minetest.register_node("itemframes:frame",{
  110. description = "Item Frame\n\nSometimes stored items become invisible.\nPunch with a stick to restore them.",
  111. drawtype = "nodebox",
  112. node_box = {
  113. type = "fixed",
  114. fixed = {-0.5, -0.5, 7/16, 0.5, 0.5, 0.5}
  115. },
  116. selection_box = {
  117. type = "fixed",
  118. fixed = {-0.5, -0.5, 7/16, 0.5, 0.5, 0.5}
  119. },
  120. tiles = {"itemframes_frame.png"},
  121. inventory_image = "itemframes_frame.png",
  122. wield_image = "itemframes_frame.png",
  123. paramtype = "light",
  124. paramtype2 = "facedir",
  125. sunlight_propagates = true,
  126. groups = utility.dig_groups("bigitem"),
  127. legacy_wallmounted = true,
  128. sounds = default.node_sound_defaults(),
  129. on_rotate = screwdriver.disallow,
  130. after_place_node = function(pos, placer, itemstack)
  131. local meta = minetest.get_meta(pos)
  132. meta:set_string("owner",placer:get_player_name())
  133. meta:set_string("infotext", string.format("Item Frame (Owned by <%s>!)", rename.gpn(placer:get_player_name())))
  134. end,
  135. _on_rename_check = function(pos)
  136. on_rename_check(pos, "Item Frame")
  137. update_item(pos, minetest.get_node(pos))
  138. end,
  139. on_rightclick = function(pos, node, clicker, itemstack)
  140. if not itemstack then return end
  141. local meta = minetest.get_meta(pos)
  142. local name = clicker and clicker:get_player_name()
  143. if map.is_mapping_kit(itemstack:get_name()) then
  144. --minetest.chat_send_player("MustTest", "Updating inventory: " .. itemstack:get_name() .. ", " .. name)
  145. minetest.after(0, function() map.update_inventory_info(name) end)
  146. end
  147. if name == meta:get_string("owner") or minetest.check_player_privs(name, "protection_bypass") then
  148. drop_item(pos,node)
  149. local s = itemstack:take_item()
  150. meta:set_string("item",s:to_string())
  151. update_item(pos,node)
  152. end
  153. return itemstack
  154. end,
  155. on_punch = function(pos,node,puncher)
  156. local meta = minetest.get_meta(pos)
  157. local name = puncher and puncher:get_player_name()
  158. if name == meta:get_string("owner") or
  159. minetest.check_player_privs(name, "protection_bypass") then
  160. drop_item(pos, node)
  161. end
  162. end,
  163. can_dig = function(pos,player)
  164. if not player then return end
  165. local name = player and player:get_player_name()
  166. local meta = minetest.get_meta(pos)
  167. return name == meta:get_string("owner") or
  168. minetest.check_player_privs(name, "protection_bypass")
  169. end,
  170. on_destruct = function(pos)
  171. local meta = minetest.get_meta(pos)
  172. local node = minetest.get_node(pos)
  173. if meta:get_string("item") ~= "" then
  174. drop_item(pos, node)
  175. end
  176. end,
  177. on_finish_collapse = function(pos, node)
  178. -- Prevent item-duplication.
  179. local meta = minetest.get_meta(pos)
  180. meta:set_string("item", nil)
  181. end,
  182. })
  183. minetest.register_node("itemframes:pedestal",{
  184. description = "Pedestal\n\nSometimes stored items become invisible.\nPunch with a stick to restore them.",
  185. drawtype = "nodebox",
  186. node_box = {
  187. type = "fixed", fixed = {
  188. {-7/16, -8/16, -7/16, 7/16, -7/16, 7/16}, -- bottom plate
  189. {-6/16, -7/16, -6/16, 6/16, -6/16, 6/16}, -- bottom plate (upper)
  190. {-0.25, -6/16, -0.25, 0.25, 11/16, 0.25}, -- pillar
  191. {-7/16, 11/16, -7/16, 7/16, 12/16, 7/16}, -- top plate
  192. }
  193. },
  194. --selection_box = {
  195. -- type = "fixed",
  196. -- fixed = {-7/16, -0.5, -7/16, 7/16, 12/16, 7/16}
  197. --},
  198. tiles = {"itemframes_pedestal.png"},
  199. paramtype = "light",
  200. groups = utility.dig_groups("brick"),
  201. sounds = default.node_sound_defaults(),
  202. on_rotate = screwdriver.disallow,
  203. after_place_node = function(pos, placer, itemstack)
  204. local meta = minetest.get_meta(pos)
  205. meta:set_string("owner",placer:get_player_name())
  206. meta:set_string("infotext", string.format("Pedestal (Owned by <%s>!)", rename.gpn(placer:get_player_name())))
  207. end,
  208. _on_rename_check = function(pos)
  209. on_rename_check(pos, "Pedestal")
  210. update_item(pos, minetest.get_node(pos))
  211. end,
  212. on_rightclick = function(pos, node, clicker, itemstack)
  213. if not itemstack then return end
  214. local meta = minetest.get_meta(pos)
  215. local name = clicker and clicker:get_player_name()
  216. if map.is_mapping_kit(itemstack:get_name()) then
  217. minetest.after(0, function() map.update_inventory_info(name) end)
  218. end
  219. if name == meta:get_string("owner") or minetest.check_player_privs(name, "protection_bypass") then
  220. drop_item(pos,node)
  221. local s = itemstack:take_item()
  222. meta:set_string("item",s:to_string())
  223. update_item(pos,node)
  224. end
  225. return itemstack
  226. end,
  227. on_punch = function(pos,node,puncher)
  228. local meta = minetest.get_meta(pos)
  229. local name = puncher and puncher:get_player_name()
  230. if name == meta:get_string("owner") or
  231. minetest.check_player_privs(name, "protection_bypass") then
  232. drop_item(pos,node)
  233. end
  234. end,
  235. can_dig = function(pos,player)
  236. if not player then return end
  237. local name = player and player:get_player_name()
  238. local meta = minetest.get_meta(pos)
  239. return name == meta:get_string("owner") or
  240. minetest.check_player_privs(name, "protection_bypass")
  241. end,
  242. on_destruct = function(pos)
  243. local meta = minetest.get_meta(pos)
  244. local node = minetest.get_node(pos)
  245. if meta:get_string("item") ~= "" then
  246. drop_item(pos, node)
  247. end
  248. end,
  249. on_finish_collapse = function(pos, node)
  250. -- Prevent item-duplication.
  251. local meta = minetest.get_meta(pos)
  252. meta:set_string("item", nil)
  253. end,
  254. })
  255. -- crafts
  256. minetest.register_craft({
  257. output = 'itemframes:frame',
  258. recipe = {
  259. {'group:stick', 'group:stick', 'group:stick'},
  260. {'group:stick', 'default:paper', 'default:stick'},
  261. {'group:stick', 'group:stick', 'group:stick'},
  262. }
  263. })
  264. minetest.register_craft({
  265. output = 'itemframes:pedestal',
  266. recipe = {
  267. {'default:stone', 'default:stone', 'default:stone'},
  268. {'', 'default:stone', ''},
  269. {'default:stone', 'default:stone', 'default:stone'},
  270. }
  271. })
  272. -- stop mesecon pistons from pushing itemframes and pedestals
  273. if minetest.get_modpath("mesecons_mvps") then
  274. mesecon.register_mvps_stopper("itemframes:frame")
  275. mesecon.register_mvps_stopper("itemframes:pedestal")
  276. end