init.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. minetest.register_entity("itemframes:item", {
  2. visual = "wielditem",
  3. visual_size = {x = 0.33, y = 0.33},
  4. collisionbox = {0},
  5. physical = false,
  6. on_activate = function(self, staticdata)
  7. local ent = self.object
  8. -- remove entity for missing frames
  9. local node = minetest.get_node_or_nil(ent:get_pos())
  10. if not node or node.name ~= "itemframes:frame" then
  11. ent:remove()
  12. return
  13. end
  14. local data = staticdata:split(";")
  15. ent:set_properties({textures = {data[2]}})
  16. self.texture = data[2]
  17. end,
  18. get_staticdata = function(self)
  19. local texture = self.texture or "air"
  20. return " ;" .. texture
  21. end
  22. })
  23. local pi = math.pi
  24. local postab = {
  25. [2] = {{x = 0.41, z = 0}, pi * 1.5},
  26. [3] = {{x = -0.41, z = 0}, pi * 0.5},
  27. [4] = {{x = 0, z = 0.41}, 0},
  28. [5] = {{x = 0, z = -0.41}, pi}
  29. }
  30. local update_item = function(pos, node)
  31. local meta = minetest.get_meta(pos)
  32. local item = meta:get_string("item")
  33. local item_name = ItemStack(item):get_name()
  34. if item_name == "" then return end
  35. local param2 = node.param2 or 0
  36. if param2 < 2 or param2 > 5 then return end
  37. local posad = postab[param2][1]
  38. pos.x = pos.x + posad.x
  39. pos.z = pos.z + posad.z
  40. -- Strange to stay compatible with the previous implementation
  41. local staticdata = " ;" .. item_name
  42. local entity = minetest.add_entity(pos, "itemframes:item", staticdata)
  43. if param2 ~= 4 then
  44. entity:set_yaw(postab[param2][2])
  45. end
  46. end
  47. local drop_item = function(pos)
  48. local meta = minetest.get_meta(pos)
  49. local item = meta:get_string("item")
  50. if item ~= "" then
  51. minetest.add_item(pos, item)
  52. meta:set_string("item", "")
  53. end
  54. for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do
  55. local ent = obj:get_luaentity()
  56. if ent and ent.name == "itemframes:item" then
  57. obj:remove()
  58. end
  59. end
  60. end
  61. local function check_item(pos, node)
  62. local meta = minetest.get_meta(pos)
  63. local item = meta:get_string("item")
  64. if item == "" then return end
  65. for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do
  66. local ent = obj:get_luaentity()
  67. if ent and ent.name == "itemframes:item" then
  68. return
  69. end
  70. end
  71. update_item(pos, node)
  72. end
  73. local function after_dig_node(pos)
  74. local meta = minetest.get_meta(pos)
  75. local item = meta:get_string("item")
  76. if item == "" then return end
  77. drop_item(pos)
  78. end
  79. minetest.register_node("itemframes:frame",{
  80. description = "Item frame",
  81. drawtype = "nodebox",
  82. node_box = {
  83. type = "wallmounted",
  84. wall_side = {-0.5, -15/32, -15/32, -7/16, 15/32, 15/32}
  85. },
  86. tiles = {"itemframe.png"},
  87. inventory_image = "itemframe.png",
  88. wield_image = "itemframe.png",
  89. paramtype = "light",
  90. paramtype2 = "wallmounted",
  91. node_placement_prediction = "",
  92. sunlight_propagates = true,
  93. groups = {choppy = 2, dig_immediate = 2, attached_node = 1},
  94. sounds = default.node_sound_wood_defaults(),
  95. on_place = function(itemstack, placer, pointed_thing)
  96. if pointed_thing.type == "node" then
  97. local undery = pointed_thing.under.y
  98. local posy = pointed_thing.above.y
  99. if undery == posy then -- allowed wall-mounted only
  100. itemstack = minetest.item_place(itemstack, placer, pointed_thing)
  101. minetest.sound_play({name = "default_place_node_hard", gain = 1},
  102. {pos = pointed_thing.above})
  103. end
  104. end
  105. return itemstack
  106. end,
  107. after_place_node = function(pos, placer)
  108. local meta = minetest.get_meta(pos)
  109. local pn = placer:get_player_name()
  110. meta:set_string("owner", pn)
  111. meta:set_string("infotext", "Item frame" .. "\n" .. "Owned by " .. pn)
  112. end,
  113. on_rightclick = function(pos, node, clicker, itemstack)
  114. if not clicker or
  115. minetest.is_protected(pos, clicker:get_player_name()) then return end
  116. drop_item(pos)
  117. if not itemstack or itemstack:get_name() == "" then return end
  118. local item = clicker:get_wielded_item():get_name() -- get real item name
  119. local meta = minetest.get_meta(pos)
  120. if minetest.registered_tools[item] then
  121. meta:set_string("item", itemstack:take_item():to_string())
  122. else
  123. meta:set_string("item", item)
  124. itemstack:take_item() -- take 1 item
  125. end
  126. update_item(pos, node)
  127. return itemstack
  128. end,
  129. can_dig = function(pos, player)
  130. if minetest.is_protected(pos, player and player:get_player_name()) then
  131. return false
  132. end
  133. return true
  134. end,
  135. on_punch = check_item,
  136. after_dig_node = after_dig_node,
  137. on_destruct = after_dig_node
  138. })
  139. minetest.register_lbm({
  140. label = "Check Itemframe item",
  141. name = "itemframes:item",
  142. nodenames = {"itemframes:frame"},
  143. run_at_every_load = true,
  144. action = check_item
  145. })
  146. -- Craft
  147. minetest.register_craft({
  148. output = "itemframes:frame",
  149. recipe = {
  150. {"default:stick", "default:stick", "default:stick"},
  151. {"default:stick", "default:paper", "default:stick"},
  152. {"default:stick", "default:stick", "default:stick"}
  153. }
  154. })
  155. minetest.register_craft({
  156. type = "fuel",
  157. recipe = "itemframes:frame",
  158. burntime = 10
  159. })