Skip to content

Commit

Permalink
add can_pickup for custom protection checks
Browse files Browse the repository at this point in the history
  • Loading branch information
OgelGames committed Apr 18, 2024
1 parent cc93ad0 commit 4aade58
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
7 changes: 6 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ Used by `wrench.register_node`.
-- If true, only the owner of the node can pickup the node.

timer = true,
-- If true, the node timer is saved, and restarted upon placement
-- If true, the node timer is saved, and restarted upon placement.

can_pickup = function(pos, player)
-- Function called before a node is picked up.
-- Return true to skip owner and protection checks.
-- Return false to disallow pickup. Can also return an optional error message.

before_pickup = function(pos, meta, node, player)
-- Function called before a node is picked up, but after metadata has been stored.
Expand Down
25 changes: 20 additions & 5 deletions functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,29 @@ function wrench.pickup_node(pos, player)
if not def then
return
end
local pickup_override = false
if def.can_pickup then
local can_pickup, err_msg = def.can_pickup(pos, player)
if can_pickup == false then
return false, err_msg
elseif can_pickup == true then
pickup_override = true
end
end
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if def.owned and not minetest.check_player_privs(player, "protection_bypass") then
local owner = meta:get_string("owner")
if owner ~= "" and owner ~= player:get_player_name() then
return false, errors.owned(owner)
if not pickup_override then
local name = player:get_player_name()
if minetest.is_protected(pos, name) then
return
end
if def.owned and not minetest.check_player_privs(player, "protection_bypass") then
local owner = meta:get_string("owner")
if owner ~= "" and owner ~= player:get_player_name() then
return false, errors.owned(owner)
end
end
end
local inv = meta:get_inventory()
if not safe_to_pickup(def, meta, inv) then
return false, errors.unknown
end
Expand Down
19 changes: 18 additions & 1 deletion nodes/bones.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@

-- Register wrench support for bones

local S = wrench.translator

if bones.redo then
local function can_pickup_bones(pos, player)
local meta = minetest.get_meta(pos)
if meta:get_string("infotext") == "" then
return false
end
if meta:get_int("time") >= bones.share_time then
return true
end
local name = player:get_player_name()
local owner = meta:get_string("owner")
if owner == "" or owner == name or minetest.check_player_privs(name, "protection_bypass") then
return true
end
return false, S("Cannot pickup node. Owned by @1.", owner)
end
wrench.register_node("bones:bones", {
lists = {"main"},
metas = {
owner = wrench.META_TYPE_STRING,
infotext = wrench.META_TYPE_STRING,
time = wrench.META_TYPE_INT,
},
owned = true,
timer = true,
can_pickup = can_pickup_bones,
})
else
wrench.register_node("bones:bones", {
Expand Down
10 changes: 3 additions & 7 deletions tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ minetest.register_tool("wrench:wrench", {
description = S("Wrench"),
inventory_image = "wrench_tool.png",
on_use = function(stack, player, pointed)
if not player or not pointed then
if not player or not pointed or not pointed.under then
return
end
local name = player:get_player_name()
local pos = pointed.under
if not pos or minetest.is_protected(pos, name) then
return
end
local picked_up, err_msg = wrench.pickup_node(pos, player)
local picked_up, err_msg = wrench.pickup_node(pointed.under, player)
if not picked_up then
if err_msg then
local name = player:get_player_name()
minetest.chat_send_player(name, err_msg)
end
return
Expand Down

0 comments on commit 4aade58

Please sign in to comment.