mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-15 18:57:08 +00: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:
parent
977f656e09
commit
22cbc05808
8 changed files with 189 additions and 13 deletions
|
@ -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,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue