item_entity.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. -- Minetest: builtin/item_entity.lua
  2. function minetest.spawn_item(pos, item)
  3. -- Take item in any format
  4. local stack = ItemStack(item)
  5. local obj = minetest.env:add_entity(pos, "__builtin:item")
  6. obj:get_luaentity():set_item(stack:to_string())
  7. return obj
  8. end
  9. minetest.register_entity("__builtin:item", {
  10. initial_properties = {
  11. hp_max = 1,
  12. physical = true,
  13. collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
  14. visual = "sprite",
  15. visual_size = {x=0.5, y=0.5},
  16. textures = {""},
  17. spritediv = {x=1, y=1},
  18. initial_sprite_basepos = {x=0, y=0},
  19. is_visible = false,
  20. },
  21. itemstring = '',
  22. physical_state = true,
  23. set_item = function(self, itemstring)
  24. self.itemstring = itemstring
  25. local stack = ItemStack(itemstring)
  26. local itemtable = stack:to_table()
  27. local itemname = nil
  28. if itemtable then
  29. itemname = stack:to_table().name
  30. end
  31. local item_texture = nil
  32. local item_type = ""
  33. if minetest.registered_items[itemname] then
  34. item_texture = minetest.registered_items[itemname].inventory_image
  35. item_type = minetest.registered_items[itemname].type
  36. end
  37. prop = {
  38. is_visible = true,
  39. visual = "sprite",
  40. textures = {"unknown_item.png"}
  41. }
  42. if item_texture and item_texture ~= "" then
  43. prop.visual = "sprite"
  44. prop.textures = {item_texture}
  45. prop.visual_size = {x=0.50, y=0.50}
  46. else
  47. prop.visual = "wielditem"
  48. prop.textures = {itemname}
  49. prop.visual_size = {x=0.20, y=0.20}
  50. prop.automatic_rotate = math.pi * 0.25
  51. end
  52. self.object:set_properties(prop)
  53. end,
  54. get_staticdata = function(self)
  55. return self.itemstring
  56. end,
  57. on_activate = function(self, staticdata)
  58. self.itemstring = staticdata
  59. self.object:set_armor_groups({immortal=1})
  60. self.object:setvelocity({x=0, y=2, z=0})
  61. self.object:setacceleration({x=0, y=-10, z=0})
  62. self:set_item(self.itemstring)
  63. end,
  64. on_step = function(self, dtime)
  65. local p = self.object:getpos()
  66. p.y = p.y - 0.3
  67. local nn = minetest.env:get_node(p).name
  68. if minetest.registered_nodes[nn].walkable then
  69. if self.physical_state then
  70. self.object:setvelocity({x=0,y=0,z=0})
  71. self.object:setacceleration({x=0, y=0, z=0})
  72. self.physical_state = false
  73. self.object:set_properties({
  74. physical = false
  75. })
  76. end
  77. else
  78. if not self.physical_state then
  79. self.object:setvelocity({x=0,y=0,z=0})
  80. self.object:setacceleration({x=0, y=-10, z=0})
  81. self.physical_state = true
  82. self.object:set_properties({
  83. physical = true
  84. })
  85. end
  86. end
  87. end,
  88. on_punch = function(self, hitter)
  89. if self.itemstring ~= '' then
  90. hitter:get_inventory():add_item("main", self.itemstring)
  91. end
  92. self.object:remove()
  93. end,
  94. })