From 1d9e7a44ae9d149bc3dec2436105a5820c19fae6 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 25 Sep 2019 21:54:22 +0200 Subject: [PATCH] Add support for Minetest protection API --- mods/default/bucket.lua | 15 +++++++++++++-- mods/default/container.lua | 25 +++++++++++++++++++++++++ mods/default/fertilizer.lua | 7 +++++++ mods/default/furnace.lua | 25 +++++++++++++++++++++++++ mods/default/sign.lua | 5 +++++ mods/default/tools.lua | 6 ++++++ mods/jewels/init.lua | 34 ++++++++++++++++++++++++++++++++-- mods/locks/init.lua | 21 +++++++++++++++++++++ mods/mobs/api.lua | 9 +++++++-- mods/music/init.lua | 7 ++++++- mods/tnt/init.lua | 5 +++++ 11 files changed, 152 insertions(+), 7 deletions(-) diff --git a/mods/default/bucket.lua b/mods/default/bucket.lua index cb48a9c..0889a02 100644 --- a/mods/default/bucket.lua +++ b/mods/default/bucket.lua @@ -18,10 +18,14 @@ for b=1, #water_buckets do liquids_pointable = true, groups = { bucket = 2, bucket_water = 1 }, on_place = function(itemstack, user, pointed_thing) - if pointed_thing.type ~= "node" then return end + if pointed_thing.type ~= "node" then return itemstack end local pos_protected = minetest.get_pointed_thing_position(pointed_thing, true) - if minetest.is_protected(pos_protected, user) then return end + if minetest.is_protected(pos_protected, user:get_player_name()) and + not minetest.check_player_privs(user, "protection_bypass") then + minetest.record_protection_violation(pos_protected, user:get_player_name()) + return itemstack + end local inv=user:get_inventory() @@ -69,6 +73,13 @@ minetest.register_craftitem( on_use = function(itemstack, user, pointed_thing) if pointed_thing.type ~= "node" then return end + local pos_protected = minetest.get_pointed_thing_position(pointed_thing, true) + if minetest.is_protected(pos_protected, user:get_player_name()) and + not minetest.check_player_privs(user, "protection_bypass") then + minetest.record_protection_violation(pos_protected, user:get_player_name()) + return + end + local nodename=minetest.get_node(pointed_thing.under).name local replace_bucket = function(itemstack, new_bucket) diff --git a/mods/default/container.lua b/mods/default/container.lua index 5a98ebb..ea0be03 100644 --- a/mods/default/container.lua +++ b/mods/default/container.lua @@ -1,5 +1,24 @@ local S = minetest.get_translator("default") +local protection_check_move = function(pos, from_list, from_index, to_list, to_index, count, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + else + return count + end +end +local protection_check_put_take = function(pos, listname, index, stack, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + else + return stack:get_count() + end +end + -- Chest and bookshelf minetest.register_node( @@ -22,6 +41,9 @@ minetest.register_node( inv:set_size("main", 8 * 4) end, + -- Unlike other inventory nodes in this game, chests are NOT subject to protection. + -- This is done to allow something like "public chests" in protected areas. + -- To protect their belongings, players are supposed locked chests instead. can_dig = function(pos, player) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() @@ -66,6 +88,9 @@ minetest.register_node( local inv = meta:get_inventory() inv:set_size("main", 4*2) end, + allow_metadata_inventory_move = protection_check_move, + allow_metadata_inventory_put = protection_check_put_take, + allow_metadata_inventory_take = protection_check_put_take, can_dig = function(pos,player) local meta = minetest.get_meta(pos); local inv = meta:get_inventory() diff --git a/mods/default/fertilizer.lua b/mods/default/fertilizer.lua index a1d0d3c..3d03abb 100644 --- a/mods/default/fertilizer.lua +++ b/mods/default/fertilizer.lua @@ -96,6 +96,13 @@ minetest.register_craftitem( on_place = function(itemstack, user, pointed_thing) local pos = pointed_thing.above + local pos_protected = minetest.get_pointed_thing_position(pointed_thing, true) + if minetest.is_protected(pos_protected, user:get_player_name()) and + not minetest.check_player_privs(user, "protection_bypass") then + minetest.record_protection_violation(pos_protected, user:get_player_name()) + return itemstack + end + local undernode = minetest.get_node(pointed_thing.under) local diff = vector.subtract(pointed_thing.above, pointed_thing.under) diff --git a/mods/default/furnace.lua b/mods/default/furnace.lua index 7d129f6..08eae78 100644 --- a/mods/default/furnace.lua +++ b/mods/default/furnace.lua @@ -61,6 +61,25 @@ form_furnace = form_furnace .. "image[3.25,1.75;1,1;ui_arrow_bg.png^[transformR2 default.ui.register_page("default_furnace_inactive", form_furnace) +local protection_check_move = function(pos, from_list, from_index, to_list, to_index, count, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + else + return count + end +end +local protection_check_put_take = function(pos, listname, index, stack, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + else + return stack:get_count() + end +end + minetest.register_node( "default:furnace", { @@ -81,6 +100,9 @@ minetest.register_node( inv:set_size("src", 1) inv:set_size("dst", 4) end, + allow_metadata_inventory_move = protection_check_move, + allow_metadata_inventory_put = protection_check_put_take, + allow_metadata_inventory_take = protection_check_put_take, can_dig = function(pos,player) local meta = minetest.get_meta(pos); local inv = meta:get_inventory() @@ -117,6 +139,9 @@ minetest.register_node( inv:set_size("src", 1) inv:set_size("dst", 4) end, + allow_metadata_inventory_move = protection_check_move, + allow_metadata_inventory_put = protection_check_put_take, + allow_metadata_inventory_take = protection_check_put_take, can_dig = function(pos,player) local meta = minetest.get_meta(pos); local inv = meta:get_inventory() diff --git a/mods/default/sign.lua b/mods/default/sign.lua index 9cec79b..dce5e4f 100644 --- a/mods/default/sign.lua +++ b/mods/default/sign.lua @@ -33,6 +33,11 @@ minetest.register_node( end, on_receive_fields = function(pos, formname, fields, sender) if fields.text == nil then return end + if minetest.is_protected(pos, sender:get_player_name()) and + not minetest.check_player_privs(sender, "protection_bypass") then + minetest.record_protection_violation(pos, sender:get_player_name()) + return itemstack + end local meta = minetest.get_meta(pos) local text = fields.text if string.len(text) > SIGN_MAX_TEXT_LENGTH then diff --git a/mods/default/tools.lua b/mods/default/tools.lua index 0d83bff..383d4a8 100644 --- a/mods/default/tools.lua +++ b/mods/default/tools.lua @@ -709,6 +709,12 @@ minetest.register_tool( if pointed_thing.type ~= "node" then return end local pos = pointed_thing.under + if minetest.is_protected(pos, user:get_player_name()) and + not minetest.check_player_privs(user, "protection_bypass") then + minetest.record_protection_violation(pos, user:get_player_name()) + return itemstack + end + local node = minetest.get_node(pos) local nodename = node.name diff --git a/mods/jewels/init.lua b/mods/jewels/init.lua index b9f222f..1b2c86b 100644 --- a/mods/jewels/init.lua +++ b/mods/jewels/init.lua @@ -184,6 +184,25 @@ minetest.register_craftitem( -- Nodes +local protection_check_move = function(pos, from_list, from_index, to_list, to_index, count, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + else + return count + end +end +local protection_check_put_take = function(pos, listname, index, stack, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + else + return stack:get_count() + end +end + minetest.register_node( "jewels:bench", { @@ -209,10 +228,18 @@ minetest.register_node( return inv:is_empty("main") end, + allow_metadata_inventory_move = protection_check_move, + allow_metadata_inventory_put = protection_check_put_take, + allow_metadata_inventory_take = protection_check_put_take, on_punch = function(pos, node, player, pointed_thing) local itemstack = player:get_wielded_item() - + local itemstack_changed = false if itemstack:get_name() == "jewels:jewel" then + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return + end local meta = minetest.get_meta(pos) local inv = meta:get_inventory() @@ -223,6 +250,7 @@ minetest.register_node( if not minetest.settings:get_bool("creative_mode") then itemstack:take_item() + itemstack_changed = true end minetest.sound_play({name="jewels_jewelling_a_tool"}, {gain=0.8, pos=pos, max_hear_distance=8}) @@ -233,7 +261,9 @@ minetest.register_node( end end - player:set_wielded_item(itemstack) + if itemstack_changed then + player:set_wielded_item(itemstack) + end end, }) diff --git a/mods/locks/init.lua b/mods/locks/init.lua index 6452c75..78f5516 100644 --- a/mods/locks/init.lua +++ b/mods/locks/init.lua @@ -63,6 +63,12 @@ minetest.register_tool( return itemstack end local pos = pointed_thing.under + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return itemstack + end + local node = minetest.get_node(pos) if minetest.get_item_group(node.name, "locked") == 0 then return itemstack @@ -197,6 +203,11 @@ minetest.register_node( end end, allow_metadata_inventory_move = function(pos, from_l, from_i, to_l, to_i, cnt, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + end local meta = minetest.get_meta(pos) if locks.is_locked(meta, player) then return 0 @@ -204,6 +215,11 @@ minetest.register_node( return cnt end, allow_metadata_inventory_put = function(pos, listname, index, itemstack, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + end local meta = minetest.get_meta(pos) if locks.is_locked(meta, player) then return 0 @@ -211,6 +227,11 @@ minetest.register_node( return itemstack:get_count() end, allow_metadata_inventory_take = function(pos, listname, index, itemstack, player) + if minetest.is_protected(pos, player:get_player_name()) and + not minetest.check_player_privs(player, "protection_bypass") then + minetest.record_protection_violation(pos, player:get_player_name()) + return 0 + end local meta = minetest.get_meta(pos) if locks.is_locked(meta, player) then return 0 diff --git a/mods/mobs/api.lua b/mods/mobs/api.lua index 52da2ea..1cb1515 100644 --- a/mods/mobs/api.lua +++ b/mods/mobs/api.lua @@ -1600,8 +1600,13 @@ function mobs:register_egg(mob, desc, background) return itemstack end local pos = pointed_thing.above - if pointed_thing.above - and not minetest.is_protected(pos, placer:get_player_name()) then + if pointed_thing.above then + 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 itemstack + end + pos.y = pos.y + 0.5 local mob = minetest.add_entity(pos, mob) local ent = mob:get_luaentity() diff --git a/mods/music/init.lua b/mods/music/init.lua index f6e806d..0599528 100644 --- a/mods/music/init.lua +++ b/mods/music/init.lua @@ -135,7 +135,12 @@ if minetest.settings:get_bool("music_enable") then music.stop(pos) end, - on_rightclick = function(pos) + on_rightclick = function(pos, node, clicker) + if minetest.is_protected(pos, clicker:get_player_name()) and + not minetest.check_player_privs(clicker, "protection_bypass") then + minetest.record_protection_violation(pos, clicker:get_player_name()) + return + end music.toggle(pos) end, diff --git a/mods/tnt/init.lua b/mods/tnt/init.lua index ed0ba9f..5ad3a8d 100644 --- a/mods/tnt/init.lua +++ b/mods/tnt/init.lua @@ -265,6 +265,11 @@ minetest.register_node( local itemname = item:get_name() if itemname == "default:flint_and_steel" then + if minetest.is_protected(pos, puncher:get_player_name()) and + not minetest.check_player_privs(puncher, "protection_bypass") then + minetest.record_protection_violation(pos, puncher:get_player_name()) + return + end if not minetest.settings:get_bool("creative_mode") then item:add_wear(800) puncher:set_wielded_item(item)