From a4511b1a23ca251488a03e87aa30743d30a3082b Mon Sep 17 00:00:00 2001 From: luk3yx Date: Sat, 21 May 2022 07:53:29 +1200 Subject: [PATCH] Disable damage for players with the creative priv (#40) Co-authored-by: Maksim --- files/creative/init.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/files/creative/init.lua b/files/creative/init.lua index 032a0eb..93b01b4 100644 --- a/files/creative/init.lua +++ b/files/creative/init.lua @@ -44,3 +44,36 @@ function minetest.handle_node_drops(pos, drops, digger) end end end + +-- Disable damage for players with creative mode +if minetest.settings:get_bool("enable_damage") then + local function set_player_immortal(player, immortal) + local groups = player:get_armor_groups() + groups.immortal = immortal and 1 or nil + player:set_armor_groups(groups) + end + + minetest.register_on_joinplayer(function(player) + if minetest.check_player_privs(player, "creative") then + set_player_immortal(player, true) + end + end) + + minetest.register_on_priv_grant(function(name, _, priv) + if priv ~= "creative" then return true end + local player = minetest.get_player_by_name(name) + if player then + set_player_immortal(player, true) + end + return true + end) + + minetest.register_on_priv_revoke(function(name, _, priv) + if priv ~= "creative" then return true end + local player = minetest.get_player_by_name(name) + if player then + set_player_immortal(player, false) + end + return true + end) +end