Skip to content

Commit abd8a30

Browse files
ashtrayozparamat
authored andcommittedDec 14, 2017
Add callback to preserve node metadata as item metadata
1 parent 6e5109f commit abd8a30

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed
 

Diff for: ‎builtin/game/falling.lua

+15-1
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,22 @@ end
150150

151151
local function drop_attached_node(p)
152152
local n = core.get_node(p)
153+
local drops = core.get_node_drops(n, "")
154+
local def = core.registered_items[n.name]
155+
if def and def.preserve_metadata then
156+
local oldmeta = core.get_meta(p):to_table().fields
157+
-- Copy pos and node because the callback can modify them.
158+
local pos_copy = {x=p.x, y=p.y, z=p.z}
159+
local node_copy = {name=n.name, param1=n.param1, param2=n.param2}
160+
local drop_stacks = {}
161+
for k, v in pairs(drops) do
162+
drop_stacks[k] = ItemStack(v)
163+
end
164+
drops = drop_stacks
165+
def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
166+
end
153167
core.remove_node(p)
154-
for _, item in pairs(core.get_node_drops(n, "")) do
168+
for _, item in pairs(drops) do
155169
local pos = {
156170
x = p.x + math.random()/2 - 0.25,
157171
y = p.y + math.random()/2 - 0.25,

Diff for: ‎builtin/game/item.lua

+14
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,20 @@ function core.node_dig(pos, node, digger)
579579
digger:set_wielded_item(wielded)
580580
end
581581

582+
-- Check to see if metadata should be preserved.
583+
if def and def.preserve_metadata then
584+
local oldmeta = core.get_meta(pos):to_table().fields
585+
-- Copy pos and node because the callback can modify them.
586+
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
587+
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
588+
local drop_stacks = {}
589+
for k, v in pairs(drops) do
590+
drop_stacks[k] = ItemStack(v)
591+
end
592+
drops = drop_stacks
593+
def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
594+
end
595+
582596
-- Handle drops
583597
core.handle_node_drops(pos, drops, digger)
584598

Diff for: ‎doc/lua_api.txt

+7
Original file line numberDiff line numberDiff line change
@@ -4710,6 +4710,13 @@ Definition tables
47104710
^ interval. Default: nil.
47114711
^ Warning: making a liquid node 'floodable' does not work and may cause problems. ]]
47124712

4713+
preserve_metadata = func(pos, oldnode, oldmeta, drops) --[[
4714+
^ Called when oldnode is about be converted to an item, but before the
4715+
node is deleted from the world or the drops are added. This is generally
4716+
the result of either the node being dug or an attached node becoming detached.
4717+
^ drops is a table of ItemStacks, so any metadata to be preserved can be
4718+
added directly to one or more of the dropped items. See "ItemStackMetaRef".
4719+
^ default: nil ]]
47134720
after_place_node = func(pos, placer, itemstack, pointed_thing) --[[
47144721
^ Called after constructing node when node was placed using
47154722
minetest.item_place_node / minetest.place_node

0 commit comments

Comments
 (0)
Please sign in to comment.