Move on_place boilerplate to rp_util
parent
4109893801
commit
5bc7589042
|
@ -19,29 +19,14 @@ for b=1, #water_buckets do
|
|||
liquids_pointable = true,
|
||||
groups = { bucket = 2, bucket_water = 1 },
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
-- Boilerplace to handle pointed node's rightclick handler
|
||||
if not placer or not placer:is_player() then
|
||||
local handled, handled_itemstack = util.on_place_pointed_node_handler(itemstack, placer, pointed_thing)
|
||||
if handled then
|
||||
return handled_itemstack
|
||||
end
|
||||
if util.handle_node_protection(placer, pointed_thing) then
|
||||
return itemstack
|
||||
end
|
||||
if pointed_thing.type ~= "node" then
|
||||
return minetest.item_place_node(itemstack, placer, pointed_thing)
|
||||
end
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def.on_rightclick and
|
||||
((not placer) or (placer and not placer:get_player_control().sneak)) then
|
||||
return def.on_rightclick(pointed_thing.under, node, placer, itemstack,
|
||||
pointed_thing) or itemstack
|
||||
end
|
||||
|
||||
-- Check protection
|
||||
local pos_protected = minetest.get_pointed_thing_position(pointed_thing, true)
|
||||
if minetest.is_protected(pos_protected, placer:get_player_name()) and
|
||||
not minetest.check_player_privs(placer, "protection_bypass") then
|
||||
minetest.record_protection_violation(pos_protected, placer:get_player_name())
|
||||
return itemstack
|
||||
end
|
||||
|
||||
|
||||
local inv=placer:get_inventory()
|
||||
|
||||
local pos = pointed_thing.above
|
||||
|
|
|
@ -99,28 +99,14 @@ minetest.register_craftitem(
|
|||
inventory_image = "default_fertilizer_inventory.png",
|
||||
wield_scale = {x=1,y=1,z=2},
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
-- Boilerplace to handle pointed node's rightclick handler
|
||||
if not placer or not placer:is_player() then
|
||||
-- Boilerplate to handle pointed node and protection
|
||||
local handled, handled_itemstack = util.on_place_pointed_node_handler(itemstack, placer, pointed_thing)
|
||||
if handled then
|
||||
return handled_itemstack
|
||||
end
|
||||
if util.handle_node_protection(placer, pointed_thing) then
|
||||
return itemstack
|
||||
end
|
||||
if pointed_thing.type ~= "node" then
|
||||
return minetest.item_place_node(itemstack, placer, pointed_thing)
|
||||
end
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def.on_rightclick and
|
||||
((not placer) or (placer and not placer:get_player_control().sneak)) then
|
||||
return def.on_rightclick(pointed_thing.under, node, placer, itemstack,
|
||||
pointed_thing) or itemstack
|
||||
end
|
||||
|
||||
-- Check protection
|
||||
local pos_protected = minetest.get_pointed_thing_position(pointed_thing, true)
|
||||
if minetest.is_protected(pos_protected, placer:get_player_name()) and
|
||||
not minetest.check_player_privs(placer, "protection_bypass") then
|
||||
minetest.record_protection_violation(pos_protected, placer:get_player_name())
|
||||
return itemstack
|
||||
end
|
||||
|
||||
-- Fertilize node (depending on node type)
|
||||
local undernode = minetest.get_node(pointed_thing.under)
|
||||
|
|
|
@ -48,19 +48,10 @@ end
|
|||
-- Saplings growing and placing
|
||||
|
||||
function default.place_sapling(itemstack, placer, pointed_thing)
|
||||
-- Boilerplace to handle pointed node's rightclick handler
|
||||
if not placer or not placer:is_player() then
|
||||
return itemstack
|
||||
end
|
||||
if pointed_thing.type ~= "node" then
|
||||
return minetest.item_place_node(itemstack, placer, pointed_thing)
|
||||
end
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def.on_rightclick and
|
||||
((not placer) or (placer and not placer:get_player_control().sneak)) then
|
||||
return def.on_rightclick(pointed_thing.under, node, placer, itemstack,
|
||||
pointed_thing) or itemstack
|
||||
-- Boilerplate to handle pointed node handlers
|
||||
local handled, handled_itemstack = util.on_place_pointed_node_handler(itemstack, placer, pointed_thing)
|
||||
if handled then
|
||||
return handled_itemstack
|
||||
end
|
||||
|
||||
-- Find position to place sapling at
|
||||
|
|
|
@ -706,37 +706,24 @@ minetest.register_tool(
|
|||
|
||||
-- Trim node (as defined by node definition's _on_trim field)
|
||||
local trim = function(itemstack, placer, pointed_thing)
|
||||
-- Boilerplace to handle pointed node's rightclick handler
|
||||
if not placer or not placer:is_player() then
|
||||
-- Handle pointed node handlers and protection
|
||||
local handled, handled_itemstack = util.on_place_pointed_node_handler(itemstack, placer, pointed_thing)
|
||||
if handled then
|
||||
return handled_itemstack
|
||||
end
|
||||
if util.handle_node_protection(placer, pointed_thing) then
|
||||
return itemstack
|
||||
end
|
||||
if pointed_thing.type ~= "node" then
|
||||
return minetest.item_place_node(itemstack, placer, pointed_thing)
|
||||
end
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def.on_rightclick and
|
||||
((not placer) or (placer and not placer:get_player_control().sneak)) then
|
||||
return def.on_rightclick(pointed_thing.under, node, placer, itemstack,
|
||||
pointed_thing) or itemstack
|
||||
end
|
||||
|
||||
-- Trimming
|
||||
do
|
||||
local pos = pointed_thing.under
|
||||
local node = minetest.get_node(pos)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def._on_trim then
|
||||
-- Check protection
|
||||
if minetest.is_protected(pos, placer:get_player_name()) and
|
||||
not minetest.check_player_privs(placer, "protection_bypass") then
|
||||
minetest.record_protection_violation(pos, placer:get_player_name())
|
||||
return
|
||||
end
|
||||
-- Trim node
|
||||
return def._on_trim(pos, node, placer, itemstack, pointed_thing)
|
||||
end
|
||||
local pos = pointed_thing.under
|
||||
local node = minetest.get_node(pos)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def._on_trim then
|
||||
-- Trim node
|
||||
return def._on_trim(pos, node, placer, itemstack, pointed_thing)
|
||||
end
|
||||
return itemstack
|
||||
end
|
||||
|
||||
minetest.register_tool(
|
||||
|
|
|
@ -144,8 +144,9 @@ minetest.register_tool(
|
|||
-- Use lock on chest to lock it
|
||||
local put_lock = function(itemstack, putter, pointed_thing)
|
||||
if pointed_thing.type ~= "node" then
|
||||
return itemstack
|
||||
return itemstack
|
||||
end
|
||||
|
||||
local pos = pointed_thing.under
|
||||
local node = minetest.get_node(pos)
|
||||
if node.name == "rp_default:chest" then
|
||||
|
@ -183,6 +184,15 @@ local put_lock = function(itemstack, putter, pointed_thing)
|
|||
return itemstack
|
||||
end
|
||||
|
||||
local put_lock_place = function(itemstack, putter, pointed_thing)
|
||||
-- Handle pointed node handlers and protection
|
||||
local handled, handled_itemstack = util.on_place_pointed_node_handler(itemstack, putter, pointed_thing)
|
||||
if handled then
|
||||
return handled_itemstack
|
||||
end
|
||||
put_lock(itemstack, putter, pointed_thing)
|
||||
end
|
||||
|
||||
minetest.register_craftitem(
|
||||
"rp_locks:lock",
|
||||
{
|
||||
|
@ -194,7 +204,7 @@ minetest.register_craftitem(
|
|||
|
||||
-- Place or punch lock on chest to lock the chest
|
||||
on_use = put_lock,
|
||||
on_place = put_lock,
|
||||
on_place = put_lock_place,
|
||||
})
|
||||
|
||||
minetest.register_node(
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
name = rp_locks
|
||||
depends = rp_sounds, rp_default, rp_formspec, rp_crafting, rp_achievements
|
||||
depends = rp_sounds, rp_util, rp_default, rp_formspec, rp_crafting, rp_achievements
|
||||
|
|
|
@ -147,19 +147,10 @@ end
|
|||
|
||||
for c=0,7 do
|
||||
local magnetize_on_place = function(itemstack, placer, pointed_thing)
|
||||
-- Boilerplace to handle pointed node's rightclick handler
|
||||
if not placer or not placer:is_player() then
|
||||
return itemstack
|
||||
end
|
||||
if pointed_thing.type ~= "node" then
|
||||
return minetest.item_place_node(itemstack, placer, pointed_thing)
|
||||
end
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def.on_rightclick and
|
||||
((not placer) or (placer and not placer:get_player_control().sneak)) then
|
||||
return def.on_rightclick(pointed_thing.under, node, placer, itemstack,
|
||||
pointed_thing) or itemstack
|
||||
-- Handle pointed node handlers
|
||||
local handled, handled_itemstack = util.on_place_pointed_node_handler(itemstack, placer, pointed_thing)
|
||||
if handled then
|
||||
return handled_itemstack
|
||||
end
|
||||
|
||||
-- Magnetize compass when placing on a magnetic node
|
||||
|
|
|
@ -9,28 +9,14 @@ minetest.register_craftitem(
|
|||
wield_image = "rp_supertools_growth_tool.png",
|
||||
groups = { supertool = 1 },
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
-- Boilerplace to handle pointed node's rightclick handler
|
||||
if not placer or not placer:is_player() then
|
||||
-- Handle pointed node handlers and protection
|
||||
local handled, handled_itemstack = util.on_place_pointed_node_handler(itemstack, placer, pointed_thing)
|
||||
if handled then
|
||||
return handled_itemstack
|
||||
end
|
||||
if util.handle_node_protection(placer, pointed_thing) then
|
||||
return itemstack
|
||||
end
|
||||
if pointed_thing.type ~= "node" then
|
||||
return minetest.item_place_node(itemstack, placer, pointed_thing)
|
||||
end
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def.on_rightclick and
|
||||
((not placer) or (placer and not placer:get_player_control().sneak)) then
|
||||
return def.on_rightclick(pointed_thing.under, node, placer, itemstack,
|
||||
pointed_thing) or itemstack
|
||||
end
|
||||
|
||||
-- Check protection
|
||||
local pos_protected = minetest.get_pointed_thing_position(pointed_thing, true)
|
||||
if minetest.is_protected(pos_protected, placer:get_player_name()) and
|
||||
not minetest.check_player_privs(placer, "protection_bypass") then
|
||||
minetest.record_protection_violation(pos_protected, placer:get_player_name())
|
||||
return itemstack
|
||||
end
|
||||
|
||||
-- Handle growing things
|
||||
local apos = pointed_thing.above
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
name = rp_supertools
|
||||
description = Adds powerful tools for Creative Mode
|
||||
depends = rp_default, rp_farming
|
||||
depends = rp_default, rp_util, rp_farming
|
||||
|
|
|
@ -252,3 +252,52 @@ function util.pointed_thing_to_place_pos(pointed_thing)
|
|||
return place_in, place_on
|
||||
end
|
||||
|
||||
-- Use this function for the on_place handler of tools and similar items
|
||||
-- that are supposed to do something special when "placing" them on
|
||||
-- a node. This makes sure the on_rightclick handler of the node
|
||||
-- takes precedence, unless the player held down the sneak key.
|
||||
-- Parameters: Same as the on_place of nodes
|
||||
-- Returns <handled>, <handled_itemstack>
|
||||
-- * <handled>: true if the function handled the placement. Your on_place handler should return <handled_itemstack>.
|
||||
-- false if the function did not handle the placement. Your on_place handler can proceed normally.
|
||||
-- * <handled_itemstack>: Only set if <handled> is true. Contains the itemstack you should return in your
|
||||
-- on_place handler
|
||||
-- Recommended usage is by putting this boilerplate code at the beginning of your function:
|
||||
--[[
|
||||
local handled, handled_itemstack = on_place_pointed_node_handler(itemstack, placer, pointed_thing)
|
||||
if handled then
|
||||
return handled_itemstack
|
||||
end
|
||||
]]
|
||||
function util.on_place_pointed_node_handler(itemstack, placer, pointed_thing)
|
||||
if not placer or not placer:is_player() then
|
||||
return true, itemstack
|
||||
end
|
||||
if pointed_thing.type ~= "node" then
|
||||
return true, minetest.item_place_node(itemstack, placer, pointed_thing)
|
||||
end
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
if def and def.on_rightclick and
|
||||
((not placer) or (placer and not placer:get_player_control().sneak)) then
|
||||
return true, (def.on_rightclick(pointed_thing.under, node, placer, itemstack, pointed_thing) or itemstack)
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Check if pointed_thing is protected, if player is the "user" of that thing,
|
||||
-- and does the protection violation handling if needed.
|
||||
-- returns true if it was protected (and protection dealt with), false otherwise.
|
||||
-- Always returns false for non-nodes
|
||||
function util.handle_node_protection(player, pointed_thing)
|
||||
if pointed_thing.type ~= "node" then
|
||||
return false
|
||||
end
|
||||
local pos_protected = minetest.get_pointed_thing_position(pointed_thing, true)
|
||||
if minetest.is_protected(pos_protected, player:get_player_name()) and
|
||||
not minetest.check_player_privs(player, "protection_bypass") then
|
||||
minetest.record_protection_violation(pos_protected, player:get_player_name())
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue