API CHANGE: after_dig_node instead of on_destruct

This also fixes the case where digging a block just drops
the dye on the ground instead of putting it in the player's inv.
This commit is contained in:
Vanessa Ezekowitz 2017-01-28 08:05:44 -05:00
parent 0fbf6baa72
commit aeb179a129
1 changed files with 25 additions and 11 deletions

View File

@ -31,6 +31,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
unifieddyes = {} unifieddyes = {}
local creative_mode = minetest.setting_getbool("creative_mode")
-- Boilerplate to support localized strings if intllib mod is installed. -- Boilerplate to support localized strings if intllib mod is installed.
local S local S
if minetest.get_modpath("intllib") then if minetest.get_modpath("intllib") then
@ -214,11 +216,18 @@ function unifieddyes.getpaletteidx(color, colorfdir)
end end
end end
function unifieddyes.on_destruct(pos) function unifieddyes.after_dig_node(pos, oldnode, oldmetadata, digger)
local meta = minetest.get_meta(pos) local prevdye
local prevdye = meta:get_string("dye") if oldmetadata and oldmetadata.fields then
if minetest.registered_items[prevdye] then prevdye = oldmetadata.fields.dye
minetest.add_item(pos,prevdye) end
if not creative_mode and prevdye and minetest.registered_items[prevdye] then
local inv = digger:get_inventory()
if inv:room_for_item("main", prevdye) then
inv:add_item("main", prevdye)
else
minetest.add_item(pos, prevdye)
end
end end
end end
@ -236,16 +245,19 @@ function unifieddyes.on_rightclick(pos, node, player, stack, pointed_thing, newn
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local prevdye = meta:get_string("dye") local prevdye = meta:get_string("dye")
if minetest.registered_items[prevdye] then if not creative_mode and minetest.registered_items[prevdye] then
local inv = player:get_inventory() local inv = player:get_inventory()
if inv:room_for_item("main",prevdye) then if inv:room_for_item("main", prevdye) then
inv:add_item("main",prevdye) inv:add_item("main", prevdye)
else else
minetest.add_item(pos,prevdye) minetest.add_item(pos, prevdye)
end end
end end
meta:set_string("dye",name) meta:set_string("dye",name)
stack:take_item() if not creative_mode and prevdye ~= name then
stack:take_item()
end
node.param2 = paletteidx node.param2 = paletteidx
local oldpaletteidx, oldhuenum = unifieddyes.getpaletteidx(prevdye, colorfdir) local oldpaletteidx, oldhuenum = unifieddyes.getpaletteidx(prevdye, colorfdir)
@ -310,7 +322,9 @@ function unifieddyes.on_rightclick(pos, node, player, stack, pointed_thing, newn
local fdir = minetest.dir_to_facedir(dir) local fdir = minetest.dir_to_facedir(dir)
minetest.set_node(pos2, { name = placeable_node.name, param2 = fdir }) minetest.set_node(pos2, { name = placeable_node.name, param2 = fdir })
stack:take_item() if not creative_mode then
stack:take_item()
end
return stack return stack
end end
end end