1
0
mirror of https://github.com/mt-mods/homedecor_modpack.git synced 2024-12-23 00:20:17 +01:00

Add on_key_use and on_skeleton_key_use api for locked nodes

This commit is contained in:
Louis Royer 2020-08-04 16:00:23 +02:00
parent 8417cddf71
commit 7f67f4a035

View File

@ -84,7 +84,8 @@ function homedecor.handle_inventory(name, def, original_def)
end end
def.can_dig = def.can_dig or default_can_dig def.can_dig = def.can_dig or default_can_dig
def.on_metadata_inventory_move = def.on_metadata_inventory_move or function(pos, from_list, from_index, to_list, to_index, count, player) def.on_metadata_inventory_move = def.on_metadata_inventory_move or
function(pos, from_list, from_index, to_list, to_index, count, player)
minetest.log("action", S("@1 moves stuff in @2 at @3", minetest.log("action", S("@1 moves stuff in @2 at @3",
player:get_player_name(), name, minetest.pos_to_string(pos) player:get_player_name(), name, minetest.pos_to_string(pos)
)) ))
@ -114,57 +115,82 @@ function homedecor.handle_inventory(name, def, original_def)
local allow_move = def.allow_metadata_inventory_move local allow_move = def.allow_metadata_inventory_move
def.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) def.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos) if not default.can_interact_with_node(player, pos) then
local owner = meta:get_string("owner") minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4",
local playername = player:get_player_name() player:get_player_name(), name,
minetest.get_meta(pos):get_string("owner"), minetest.pos_to_string(pos)
if playername == owner or ))
minetest.check_player_privs(playername, "protection_bypass") then return 0
return allow_move and
allow_move(pos, from_list, from_index, to_list, to_index, count, player) or
count
end end
return allow_move and allow_move(pos, from_list, from_index, to_list, to_index, count, player) or
minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4", count
playername, name, owner, minetest.pos_to_string(pos)
))
return 0
end end
local allow_put = def.allow_metadata_inventory_put local allow_put = def.allow_metadata_inventory_put
def.allow_metadata_inventory_put = function(pos, listname, index, stack, player) def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos) if not default.can_interact_with_node(player, pos) then
local owner = meta:get_string("owner") minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4",
local playername = player:get_player_name() player:get_player_name(), name,
minetest.get_meta(pos):get_string("owner"), minetest.pos_to_string(pos)
if playername == owner or ))
minetest.check_player_privs(playername, "protection_bypass") then return 0
return allow_put and allow_put(pos, listname, index, stack, player) or
stack:get_count()
end end
return allow_put and allow_put(pos, listname, index, stack, player) or
minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4", stack:get_count()
playername, name, owner, minetest.pos_to_string(pos)
))
return 0
end end
local allow_take = def.allow_metadata_inventory_take local allow_take = def.allow_metadata_inventory_take
def.allow_metadata_inventory_take = function(pos, listname, index, stack, player) def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if not default.can_interact_with_node(player, pos) then
minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4",
player:get_player_name(), name,
minetest.get_meta(pos):get_string("owner"), minetest.pos_to_string(pos)
))
return 0
end
return allow_take and allow_take(pos, listname, index, stack, player) or
stack:get_count()
end
local can_dig = def.can_dig
def.can_dig = function(pos, player)
return default.can_interact_with_node(player, pos) and (can_dig and (can_dig(pos, player) or true))
end
def.on_key_use = function(pos, player)
local secret = minetest.get_meta(pos):get_string("key_lock_secret")
local itemstack = player:get_wielded_item()
local key_meta = itemstack:get_meta()
if secret ~= key_meta:get_string("secret") then
return
end
minetest.show_formspec(
player:get_player_name(),
name.."_locked",
minetest.get_meta(pos):get_string("formspec")
)
end
def.on_skeleton_key_use = function(pos, player, newsecret)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner") local owner = meta:get_string("owner")
local playername = player:get_player_name() local playername = player:get_player_name()
if playername == owner or -- verify placer is owner
minetest.check_player_privs(playername, "protection_bypass") then if owner ~= playername then
return allow_take and allow_take(pos, listname, index, stack, player) or minetest.record_protection_violation(pos, playername)
stack:get_count() return nil
end end
minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4", local secret = meta:get_string("key_lock_secret")
playername, name, owner, minetest.pos_to_string(pos) if secret == "" then
)) secret = newsecret
return 0 meta:set_string("key_lock_secret", secret)
end
return secret, meta:get_string("description"), owner
end end
end end