Skip to content

Commit

Permalink
Allow to manually specify param2 in minetest.item_place() and return …
Browse files Browse the repository at this point in the history
…success
  • Loading branch information
PilzAdam committed Sep 28, 2013
1 parent 5dce44e commit 7313928
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
24 changes: 12 additions & 12 deletions builtin/item.lua
Expand Up @@ -179,11 +179,11 @@ function minetest.get_node_drops(nodename, toolname)
return got_items
end

function minetest.item_place_node(itemstack, placer, pointed_thing)
function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
local item = itemstack:peek_item()
local def = itemstack:get_definition()
if def.type ~= "node" or pointed_thing.type ~= "node" then
return itemstack
return itemstack, false
end

local under = pointed_thing.under
Expand All @@ -194,7 +194,7 @@ function minetest.item_place_node(itemstack, placer, pointed_thing)
if not oldnode_under or not oldnode_above then
minetest.log("info", placer:get_player_name() .. " tried to place"
.. " node in unloaded position " .. minetest.pos_to_string(above))
return itemstack
return itemstack, false
end

local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
Expand All @@ -206,7 +206,7 @@ function minetest.item_place_node(itemstack, placer, pointed_thing)
minetest.log("info", placer:get_player_name() .. " tried to place"
.. " node in invalid position " .. minetest.pos_to_string(above)
.. ", replacing " .. oldnode_above.name)
return itemstack
return itemstack, false
end

-- Place above pointed node
Expand All @@ -222,18 +222,18 @@ function minetest.item_place_node(itemstack, placer, pointed_thing)
.. def.name .. " at " .. minetest.pos_to_string(place_to))

local oldnode = minetest.get_node(place_to)
local newnode = {name = def.name, param1 = 0, param2 = 0}
local newnode = {name = def.name, param1 = 0, param2 = param2}

-- Calculate direction for wall mounted stuff like torches and signs
if def.paramtype2 == 'wallmounted' then
if def.paramtype2 == 'wallmounted' and not param2 then
local dir = {
x = under.x - above.x,
y = under.y - above.y,
z = under.z - above.z
}
newnode.param2 = minetest.dir_to_wallmounted(dir)
-- Calculate the direction for furnaces and chests and stuff
elseif def.paramtype2 == 'facedir' then
elseif def.paramtype2 == 'facedir' and not param2 then
local placer_pos = placer:getpos()
if placer_pos then
local dir = {
Expand All @@ -251,7 +251,7 @@ function minetest.item_place_node(itemstack, placer, pointed_thing)
not check_attached_node(place_to, newnode) then
minetest.log("action", "attached node " .. def.name ..
" can not be placed at " .. minetest.pos_to_string(place_to))
return itemstack
return itemstack, false
end

-- Add node and update
Expand Down Expand Up @@ -283,7 +283,7 @@ function minetest.item_place_node(itemstack, placer, pointed_thing)
if take_item then
itemstack:take_item()
end
return itemstack
return itemstack, true
end

function minetest.item_place_object(itemstack, placer, pointed_thing)
Expand All @@ -295,19 +295,19 @@ function minetest.item_place_object(itemstack, placer, pointed_thing)
return itemstack
end

function minetest.item_place(itemstack, placer, pointed_thing)
function minetest.item_place(itemstack, placer, pointed_thing, param2)
-- Call on_rightclick if the pointed node defines it
if pointed_thing.type == "node" and placer and
not placer:get_player_control().sneak then
local n = minetest.get_node(pointed_thing.under)
local nn = n.name
if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].on_rightclick then
return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n, placer, itemstack) or itemstack
return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n, placer, itemstack) or itemstack, false
end
end

if itemstack:get_definition().type == "node" then
return minetest.item_place_node(itemstack, placer, pointed_thing)
return minetest.item_place_node(itemstack, placer, pointed_thing, param2)
end
return itemstack
end
Expand Down
8 changes: 6 additions & 2 deletions doc/lua_api.txt
Expand Up @@ -1361,14 +1361,18 @@ minetest.rollback_revert_actions_by(actor, seconds) -> bool, log messages

Defaults for the on_* item definition functions:
(These return the leftover itemstack)
minetest.item_place_node(itemstack, placer, pointed_thing)
minetest.item_place_node(itemstack, placer, pointed_thing, param2)
^ Place item as a node
^ param2 overrides facedir and wallmounted param2
^ returns itemstack, success
minetest.item_place_object(itemstack, placer, pointed_thing)
^ Place item as-is
minetest.item_place(itemstack, placer, pointed_thing)
minetest.item_place(itemstack, placer, pointed_thing, param2)
^ Use one of the above based on what the item is.
^ Calls on_rightclick of pointed_thing.under if defined instead
^ Note: is not called when wielded item overrides on_place
^ param2 overrides facedir and wallmounted param2
^ returns itemstack, success
minetest.item_drop(itemstack, dropper, pos)
^ Drop the item
minetest.item_eat(hp_change, replace_with_item)
Expand Down

0 comments on commit 7313928

Please sign in to comment.