From 5bc7589042318e875b77c8aae49938334997b9d4 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Fri, 6 May 2022 12:25:08 +0200 Subject: [PATCH] Move on_place boilerplate to rp_util --- mods/rp_default/bucket.lua | 27 +++++-------------- mods/rp_default/fertilizer.lua | 26 +++++------------- mods/rp_default/functions.lua | 17 +++--------- mods/rp_default/tools.lua | 39 +++++++++------------------ mods/rp_locks/init.lua | 14 ++++++++-- mods/rp_locks/mod.conf | 2 +- mods/rp_nav/compass.lua | 17 +++--------- mods/rp_supertools/init.lua | 26 +++++------------- mods/rp_supertools/mod.conf | 2 +- mods/rp_util/init.lua | 49 ++++++++++++++++++++++++++++++++++ 10 files changed, 102 insertions(+), 117 deletions(-) diff --git a/mods/rp_default/bucket.lua b/mods/rp_default/bucket.lua index 912550f..d8274ac 100644 --- a/mods/rp_default/bucket.lua +++ b/mods/rp_default/bucket.lua @@ -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 diff --git a/mods/rp_default/fertilizer.lua b/mods/rp_default/fertilizer.lua index 958ef68..7579e31 100644 --- a/mods/rp_default/fertilizer.lua +++ b/mods/rp_default/fertilizer.lua @@ -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) diff --git a/mods/rp_default/functions.lua b/mods/rp_default/functions.lua index 3f89333..35ead8d 100644 --- a/mods/rp_default/functions.lua +++ b/mods/rp_default/functions.lua @@ -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 diff --git a/mods/rp_default/tools.lua b/mods/rp_default/tools.lua index 19be819..b10639a 100644 --- a/mods/rp_default/tools.lua +++ b/mods/rp_default/tools.lua @@ -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( diff --git a/mods/rp_locks/init.lua b/mods/rp_locks/init.lua index 6143c93..d1e4af5 100644 --- a/mods/rp_locks/init.lua +++ b/mods/rp_locks/init.lua @@ -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( diff --git a/mods/rp_locks/mod.conf b/mods/rp_locks/mod.conf index e193433..5b10961 100644 --- a/mods/rp_locks/mod.conf +++ b/mods/rp_locks/mod.conf @@ -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 diff --git a/mods/rp_nav/compass.lua b/mods/rp_nav/compass.lua index 5123938..7a32ae5 100644 --- a/mods/rp_nav/compass.lua +++ b/mods/rp_nav/compass.lua @@ -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 diff --git a/mods/rp_supertools/init.lua b/mods/rp_supertools/init.lua index 2745894..500d257 100644 --- a/mods/rp_supertools/init.lua +++ b/mods/rp_supertools/init.lua @@ -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 diff --git a/mods/rp_supertools/mod.conf b/mods/rp_supertools/mod.conf index 99abda5..6a8551a 100644 --- a/mods/rp_supertools/mod.conf +++ b/mods/rp_supertools/mod.conf @@ -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 diff --git a/mods/rp_util/init.lua b/mods/rp_util/init.lua index 97366de..c5eb704 100644 --- a/mods/rp_util/init.lua +++ b/mods/rp_util/init.lua @@ -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 , +-- * : true if the function handled the placement. Your on_place handler should return . +-- false if the function did not handle the placement. Your on_place handler can proceed normally. +-- * : Only set if 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