From 84fbc38142105c0fc83473c3059f4ee610f732c7 Mon Sep 17 00:00:00 2001 From: k Date: Sun, 3 May 2020 15:30:25 +0200 Subject: [PATCH] Log Actions moved to separate module --- utils/actions.lua | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 utils/actions.lua diff --git a/utils/actions.lua b/utils/actions.lua new file mode 100644 index 0000000..c7bfb74 --- /dev/null +++ b/utils/actions.lua @@ -0,0 +1,90 @@ +local function get_inventory_auth_string(player, meta, pos, label) + return player:get_player_name() .. " tried to access a locked " .. label .. " belonging to " .. meta:get_string("owner") .. " at " .. minetest.pos_to_string(pos) +end + + +function has_locked_chest_privilege(meta, player) + if player:get_player_name() ~= meta:get_string("owner") then + return false + end + return true +end + + +function get_allow_metadata_inventory_move(t) + setmetatable(t, {__index={check_privs=has_locked_chest_privilege}}) + local label, check_privs = t[1], t.check_privs + return function(pos, from_list, from_index, to_list, to_index, count, player) + local meta = minetest.get_meta(pos) + if not check_privs(meta, player) then + minetest.log("action", get_inventory_auth_string(player, meta, pos, label)) + return 0 + end + return count + end +end + +function get_allow_metadata_inventory_put(t) + setmetatable(t, {__index={check_privs=has_locked_chest_privilege}}) + local label, check_privs = t[1], t.check_privs + return function(pos, listname, index, stack, player) + local meta = minetest.get_meta(pos) + if not check_privs(meta, player) then + minetest.log("action", get_inventory_auth_string(player, meta, pos, label)) + return 0 + end + return stack:get_count() + end +end + +function get_allow_metadata_inventory_take(t) + setmetatable(t, {__index={check_privs=has_locked_chest_privilege}}) + local label, check_privs = t[1], t.check_privs + return function(pos, listname, index, stack, player) + local meta = minetest.get_meta(pos) + if not check_privs(meta, player) then + minetest.log("action", get_inventory_auth_string(player, meta, pos, label)) + return 0 + end + return stack:get_count() + end +end + + + +local function get_inventory_action_string(player, pos, action, label) + return player:get_player_name() .. " moves stuff " .. action .. " locked " .. label .. " at " .. minetest.pos_to_string(pos) +end + + +function get_on_metadata_inventory_move(label) + return function(pos, from_list, from_index, to_list, to_index, count, player) + minetest.log("action", get_inventory_action_string(player, pos, "in", label) + ) + end +end + +function get_on_metadata_inventory_put(label) + return function(pos, listname, index, stack, player) + minetest.log("action", get_inventory_action_string(player, pos, "to", label)) + end +end + +function get_on_metadata_inventory_take(label) + return function(pos, listname, index, stack, player) + minetest.log("action", get_inventory_action_string(player, pos, "from", label)) + end +end + + + +actions = { + has_locked_chest_privilege = has_locked_chest_privilege, + get_allow_metadata_inventory_move = get_allow_metadata_inventory_move, + get_allow_metadata_inventory_put = get_allow_metadata_inventory_put, + get_allow_metadata_inventory_take = get_allow_metadata_inventory_take, + get_on_metadata_inventory_move = get_on_metadata_inventory_move, + get_on_metadata_inventory_put = get_on_metadata_inventory_put, + get_on_metadata_inventory_take = get_on_metadata_inventory_take, +} +return actions