mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-17 10:25:21 +02:00
Add an item pick up callback (2) (#7712)
Co-authored-by: SmallJoker <mk939@ymail.com> Co-authored-by: Jude Melton-Houghton <jwmhjwmh@gmail.com>
This commit is contained in:
@@ -318,16 +318,29 @@ core.register_entity(":__builtin:item", {
|
||||
end
|
||||
end,
|
||||
|
||||
on_punch = function(self, hitter)
|
||||
local inv = hitter:get_inventory()
|
||||
if inv and self.itemstring ~= "" then
|
||||
local left = inv:add_item("main", self.itemstring)
|
||||
if left and not left:is_empty() then
|
||||
self:set_item(left)
|
||||
return
|
||||
end
|
||||
on_punch = function(self, hitter, ...)
|
||||
if self.itemstring == "" then
|
||||
self.object:remove()
|
||||
return
|
||||
end
|
||||
|
||||
-- Call on_pickup callback in item definition.
|
||||
local itemstack = ItemStack(self.itemstring)
|
||||
local callback = itemstack:get_definition().on_pickup
|
||||
|
||||
local ret = callback(itemstack, hitter, {type = "object", ref = self.object}, ...)
|
||||
if not ret then
|
||||
-- Don't modify (and don't reset rotation)
|
||||
return
|
||||
end
|
||||
itemstack = ItemStack(ret)
|
||||
|
||||
-- Handle the leftover itemstack
|
||||
if itemstack:is_empty() then
|
||||
self.itemstring = ""
|
||||
self.object:remove()
|
||||
else
|
||||
self:set_item(itemstack)
|
||||
end
|
||||
self.itemstring = ""
|
||||
self.object:remove()
|
||||
end,
|
||||
})
|
||||
|
Reference in New Issue
Block a user