1 -- Minetest: builtin/item_entity.lua
3 function core.spawn_item(pos, item)
4 -- Take item in any format
5 local stack = ItemStack(item)
6 local obj = core.add_entity(pos, "__builtin:item")
7 obj:get_luaentity():set_item(stack:to_string())
11 -- If item_entity_ttl is not set, enity will have default life time
12 -- Setting it to -1 disables the feature
14 local time_to_live = tonumber(core.setting_get("item_entity_ttl"))
15 if not time_to_live then
19 core.register_entity(":__builtin:item", {
20 initial_properties = {
23 collide_with_objects = false,
24 collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
26 visual_size = {x = 0.4, y = 0.4},
28 spritediv = {x = 1, y = 1},
29 initial_sprite_basepos = {x = 0, y = 0},
34 physical_state = true,
37 set_item = function(self, itemstring)
38 self.itemstring = itemstring
39 local stack = ItemStack(itemstring)
40 local count = stack:get_count()
41 local max_count = stack:get_stack_max()
42 if count > max_count then
44 self.itemstring = stack:get_name().." "..max_count
46 local s = 0.2 + 0.1 * (count / max_count)
48 local itemtable = stack:to_table()
51 itemname = stack:to_table().name
53 local item_texture = nil
55 if core.registered_items[itemname] then
56 item_texture = core.registered_items[itemname].inventory_image
57 item_type = core.registered_items[itemname].type
62 textures = {itemname},
63 visual_size = {x = s, y = s},
64 collisionbox = {-c, -c, -c, c, c, c},
65 automatic_rotate = math.pi * 0.5,
67 self.object:set_properties(prop)
70 get_staticdata = function(self)
71 return core.serialize({
72 itemstring = self.itemstring,
73 always_collect = self.always_collect,
78 on_activate = function(self, staticdata, dtime_s)
79 if string.sub(staticdata, 1, string.len("return")) == "return" then
80 local data = core.deserialize(staticdata)
81 if data and type(data) == "table" then
82 self.itemstring = data.itemstring
83 self.always_collect = data.always_collect
85 self.age = data.age + dtime_s
91 self.itemstring = staticdata
93 self.object:set_armor_groups({immortal = 1})
94 self.object:setvelocity({x = 0, y = 2, z = 0})
95 self.object:setacceleration({x = 0, y = -10, z = 0})
96 self:set_item(self.itemstring)
99 on_step = function(self, dtime)
100 self.age = self.age + dtime
101 if time_to_live > 0 and self.age > time_to_live then
106 local p = self.object:getpos()
108 local nn = core.get_node(p).name
109 -- If node is not registered or node is walkably solid and resting on nodebox
110 local v = self.object:getvelocity()
111 if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
112 if self.physical_state then
113 local own_stack = ItemStack(self.object:get_luaentity().itemstring)
114 for _,object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
115 local obj = object:get_luaentity()
116 if obj and obj.name == "__builtin:item" and obj.physical_state == false then
117 local stack = ItemStack(obj.itemstring)
118 if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
119 local overflow = false
120 local count = stack:get_count() + own_stack:get_count()
121 local max_count = stack:get_stack_max()
122 if count>max_count then
124 count = count - max_count
128 local pos=object:getpos()
129 pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
130 object:moveto(pos, false)
132 local max_count = stack:get_stack_max()
133 local name = stack:get_name()
135 obj.itemstring = name.." "..count
136 s = 0.2 + 0.1 * (count / max_count)
138 object:set_properties({
139 visual_size = {x = s, y = s},
140 collisionbox = {-c, -c, -c, c, c, c}
147 object:set_properties({
148 visual_size = {x = s, y = s},
149 collisionbox = {-c, -c, -c, c, c, c}
151 obj.itemstring = name.." "..max_count
152 s = 0.2 + 0.1 * (count / max_count)
154 self.object:set_properties({
155 visual_size = {x = s, y = s},
156 collisionbox = {-c, -c, -c, c, c, c}
158 self.itemstring = name.." "..count
163 self.object:setvelocity({x = 0, y = 0, z = 0})
164 self.object:setacceleration({x = 0, y = 0, z = 0})
165 self.physical_state = false
166 self.object:set_properties({physical = false})
169 if not self.physical_state then
170 self.object:setvelocity({x = 0, y = 0, z = 0})
171 self.object:setacceleration({x = 0, y = -10, z = 0})
172 self.physical_state = true
173 self.object:set_properties({physical = true})
178 on_punch = function(self, hitter)
179 if self.itemstring ~= '' then
180 local left = hitter:get_inventory():add_item("main", self.itemstring)
181 if not left:is_empty() then
182 self.itemstring = left:to_string()