item_entity.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.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. collide_with_objects = false,
  14. collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
  15. visual = "sprite",
  16. visual_size = {x=0.5, y=0.5},
  17. textures = {""},
  18. spritediv = {x=1, y=1},
  19. initial_sprite_basepos = {x=0, y=0},
  20. is_visible = false,
  21. },
  22. itemstring = '',
  23. physical_state = true,
  24. set_item = function(self, itemstring)
  25. self.itemstring = itemstring
  26. local stack = ItemStack(itemstring)
  27. local itemtable = stack:to_table()
  28. local itemname = nil
  29. if itemtable then
  30. itemname = stack:to_table().name
  31. end
  32. local item_texture = nil
  33. local item_type = ""
  34. if minetest.registered_items[itemname] then
  35. item_texture = minetest.registered_items[itemname].inventory_image
  36. item_type = minetest.registered_items[itemname].type
  37. end
  38. prop = {
  39. is_visible = true,
  40. visual = "sprite",
  41. textures = {"unknown_item.png"}
  42. }
  43. if item_texture and item_texture ~= "" then
  44. prop.visual = "sprite"
  45. prop.textures = {item_texture}
  46. prop.visual_size = {x=0.50, y=0.50}
  47. else
  48. prop.visual = "wielditem"
  49. prop.textures = {itemname}
  50. prop.visual_size = {x=0.20, y=0.20}
  51. prop.automatic_rotate = math.pi * 0.25
  52. end
  53. self.object:set_properties(prop)
  54. end,
  55. get_staticdata = function(self)
  56. --return self.itemstring
  57. return minetest.serialize({
  58. itemstring = self.itemstring,
  59. always_collect = self.always_collect,
  60. })
  61. end,
  62. on_activate = function(self, staticdata)
  63. if string.sub(staticdata, 1, string.len("return")) == "return" then
  64. local data = minetest.deserialize(staticdata)
  65. if data and type(data) == "table" then
  66. self.itemstring = data.itemstring
  67. self.always_collect = data.always_collect
  68. end
  69. else
  70. self.itemstring = staticdata
  71. end
  72. self.object:set_armor_groups({immortal=1})
  73. self.object:setvelocity({x=0, y=2, z=0})
  74. self.object:setacceleration({x=0, y=-10, z=0})
  75. self:set_item(self.itemstring)
  76. end,
  77. on_step = function(self, dtime)
  78. local p = self.object:getpos()
  79. p.y = p.y - 0.3
  80. local nn = minetest.get_node(p).name
  81. -- If node is not registered or node is walkably solid and resting on nodebox
  82. local v = self.object:getvelocity()
  83. if not minetest.registered_nodes[nn] or minetest.registered_nodes[nn].walkable and v.y == 0 then
  84. if self.physical_state then
  85. self.object:setvelocity({x=0,y=0,z=0})
  86. self.object:setacceleration({x=0, y=0, z=0})
  87. self.physical_state = false
  88. self.object:set_properties({
  89. physical = false
  90. })
  91. end
  92. else
  93. if not self.physical_state then
  94. self.object:setvelocity({x=0,y=0,z=0})
  95. self.object:setacceleration({x=0, y=-10, z=0})
  96. self.physical_state = true
  97. self.object:set_properties({
  98. physical = true
  99. })
  100. end
  101. end
  102. end,
  103. on_punch = function(self, hitter)
  104. if self.itemstring ~= '' then
  105. local left = hitter:get_inventory():add_item("main", self.itemstring)
  106. if not left:is_empty() then
  107. self.itemstring = left:to_string()
  108. return
  109. end
  110. end
  111. self.object:remove()
  112. end,
  113. })