From 077316b4cd7558eda03e6d3df631ee3aa27bff11 Mon Sep 17 00:00:00 2001
From: zaoqi <zaoqiff@gmail.com>
Date: Wed, 29 Mar 2017 21:02:26 +0200
Subject: [PATCH] Use creative.is_enabled_for for creative mode if possible
 (#1558)

---
 mods/beds/api.lua          | 3 ++-
 mods/boats/init.lua        | 6 ++++--
 mods/bones/init.lua        | 3 ++-
 mods/carts/cart_entity.lua | 6 ++++--
 mods/creative/init.lua     | 2 +-
 mods/default/player.lua    | 3 ++-
 mods/default/trees.lua     | 3 ++-
 mods/doors/init.lua        | 4 ++--
 mods/farming/api.lua       | 6 ++++--
 mods/fire/init.lua         | 5 +++--
 mods/flowers/init.lua      | 3 ++-
 mods/screwdriver/init.lua  | 3 ++-
 mods/stairs/init.lua       | 6 ++++--
 13 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/mods/beds/api.lua b/mods/beds/api.lua
index daa6d8e2..97dde43d 100644
--- a/mods/beds/api.lua
+++ b/mods/beds/api.lua
@@ -89,7 +89,8 @@ function beds.register_bed(name, def)
 			minetest.set_node(pos, {name = name .. "_bottom", param2 = dir})
 			minetest.set_node(botpos, {name = name .. "_top", param2 = dir})
 
-			if not minetest.setting_getbool("creative_mode") then
+			if not (creative and creative.is_enabled_for
+					and creative.is_enabled_for(placer:get_player_name())) then
 				itemstack:take_item()
 			end
 			return itemstack
diff --git a/mods/boats/init.lua b/mods/boats/init.lua
index 6e1565d4..4d8f4673 100644
--- a/mods/boats/init.lua
+++ b/mods/boats/init.lua
@@ -110,7 +110,8 @@ function boat.on_punch(self, puncher)
 	if not self.driver then
 		self.removed = true
 		local inv = puncher:get_inventory()
-		if not minetest.setting_getbool("creative_mode")
+		if not (creative and creative.is_enabled_for
+				and creative.is_enabled_for(puncher:get_player_name()))
 				or not inv:contains_item("main", "boats:boat") then
 			local leftover = inv:add_item("main", "boats:boat")
 			-- if no room in inventory add a replacement boat to the world
@@ -244,7 +245,8 @@ minetest.register_craftitem("boats:boat", {
 		boat = minetest.add_entity(pointed_thing.under, "boats:boat")
 		if boat then
 			boat:setyaw(placer:get_look_horizontal())
-			if not minetest.setting_getbool("creative_mode") then
+			if not (creative and creative.is_enabled_for
+					and creative.is_enabled_for(placer:get_player_name())) then
 				itemstack:take_item()
 			end
 		end
diff --git a/mods/bones/init.lua b/mods/bones/init.lua
index 9542cab0..56f5fd04 100644
--- a/mods/bones/init.lua
+++ b/mods/bones/init.lua
@@ -167,7 +167,8 @@ minetest.register_on_dieplayer(function(player)
 	end
 
 	-- return if keep inventory set or in creative mode
-	if bones_mode == "keep" or minetest.setting_getbool("creative_mode") then
+	if bones_mode == "keep" or (creative and creative.is_enabled_for
+			and creative.is_enabled_for(player:get_player_name())) then
 		return
 	end
 
diff --git a/mods/carts/cart_entity.lua b/mods/carts/cart_entity.lua
index bf0a450d..4d44c873 100644
--- a/mods/carts/cart_entity.lua
+++ b/mods/carts/cart_entity.lua
@@ -92,7 +92,8 @@ function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities,
 		end
 		-- Pick up cart
 		local inv = puncher:get_inventory()
-		if not minetest.setting_getbool("creative_mode")
+		if not (creative and creative.is_enabled_for
++				and creative.is_enabled_for(puncher:get_player_name()))
 				or not inv:contains_item("main", "carts:cart") then
 			local leftover = inv:add_item("main", "carts:cart")
 			-- If no room in inventory add a replacement cart to the world
@@ -385,7 +386,8 @@ minetest.register_craftitem("carts:cart", {
 		minetest.sound_play({name = "default_place_node_metal", gain = 0.5},
 			{pos = pointed_thing.above})
 
-		if not minetest.setting_getbool("creative_mode") then
+		if not (creative and creative.is_enabled_for
+				and creative.is_enabled_for(placer:get_player_name())) then
 			itemstack:take_item()
 		end
 		return itemstack
diff --git a/mods/creative/init.lua b/mods/creative/init.lua
index 7471e603..f9047189 100644
--- a/mods/creative/init.lua
+++ b/mods/creative/init.lua
@@ -8,7 +8,7 @@ end
 
 dofile(minetest.get_modpath("creative") .. "/inventory.lua")
 
-if minetest.setting_getbool("creative_mode") then
+if creative_mode_cache then
 	-- Dig time is modified according to difference (leveldiff) between tool
 	-- 'maxlevel' and node 'level'. Digtime is divided by the larger of
 	-- leveldiff and 1.
diff --git a/mods/default/player.lua b/mods/default/player.lua
index fd7341f4..cd89a24c 100644
--- a/mods/default/player.lua
+++ b/mods/default/player.lua
@@ -96,7 +96,8 @@ minetest.register_on_joinplayer(function(player)
 	player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
 
 	-- set GUI
-	if not minetest.setting_getbool("creative_mode") then
+	if not (creative and creative.is_enabled_for
+			and creative.is_enabled_for(player:get_player_name())) then
 		player:set_inventory_formspec(default.gui_survival_form)
 	end
 	player:hud_set_hotbar_image("gui_hotbar.png")
diff --git a/mods/default/trees.lua b/mods/default/trees.lua
index 3e1bf102..0b829a75 100644
--- a/mods/default/trees.lua
+++ b/mods/default/trees.lua
@@ -471,7 +471,8 @@ function default.sapling_on_place(itemstack, placer, pointed_thing,
 	minetest.log("action", player_name .. " places node "
 			.. sapling_name .. " at " .. minetest.pos_to_string(pos))
 
-	local take_item = not minetest.setting_getbool("creative_mode")
+	local take_item = not (creative and creative.is_enabled_for
+		and creative.is_enabled_for(player_name))
 	local newnode = {name = sapling_name}
 	local ndef = minetest.registered_nodes[sapling_name]
 	minetest.set_node(pos, newnode)
diff --git a/mods/doors/init.lua b/mods/doors/init.lua
index 4ab40111..8ca26dac 100644
--- a/mods/doors/init.lua
+++ b/mods/doors/init.lua
@@ -328,7 +328,7 @@ function doors.register(name, def)
 				meta:set_string("infotext", "Owned by " .. pn)
 			end
 
-			if not minetest.setting_getbool("creative_mode") then
+			if not (creative and creative.is_enabled_for and creative.is_enabled_for(pn)) then
 				itemstack:take_item()
 			end
 
@@ -576,7 +576,7 @@ function doors.register_trapdoor(name, def)
 			meta:set_string("owner", pn)
 			meta:set_string("infotext", "Owned by "..pn)
 
-			return minetest.setting_getbool("creative_mode")
+			return (creative and creative.is_enabled_for and creative.is_enabled_for(pn))
 		end
 
 		def.on_blast = function() end
diff --git a/mods/farming/api.lua b/mods/farming/api.lua
index 0ba756cc..35a77e9f 100644
--- a/mods/farming/api.lua
+++ b/mods/farming/api.lua
@@ -57,7 +57,8 @@ farming.hoe_on_use = function(itemstack, user, pointed_thing, uses)
 		gain = 0.5,
 	})
 
-	if not minetest.setting_getbool("creative_mode") then
+	if not (creative and creative.is_enabled_for
+			and creative.is_enabled_for(user:get_player_name())) then
 		-- wear tool
 		local wdef = itemstack:get_definition()
 		itemstack:add_wear(65535/(uses-1))
@@ -187,7 +188,8 @@ farming.place_seed = function(itemstack, placer, pointed_thing, plantname)
 	-- add the node and remove 1 item from the itemstack
 	minetest.add_node(pt.above, {name = plantname, param2 = 1})
 	tick(pt.above)
-	if not minetest.setting_getbool("creative_mode") then
+	if not (creative and creative.is_enabled_for
+			and creative.is_enabled_for(placer:get_player_name())) then
 		itemstack:take_item()
 	end
 	return itemstack
diff --git a/mods/fire/init.lua b/mods/fire/init.lua
index a846b289..a9ceb9ea 100644
--- a/mods/fire/init.lua
+++ b/mods/fire/init.lua
@@ -85,13 +85,13 @@ minetest.register_tool("fire:flint_and_steel", {
 			"fire_flint_and_steel",
 			{pos = sound_pos, gain = 0.5, max_hear_distance = 8}
 		)
+		local player_name = user:get_player_name()
 		if pointed_thing.type == "node" then
 			local node_under = minetest.get_node(pointed_thing.under).name
 			local nodedef = minetest.registered_nodes[node_under]
 			if not nodedef then
 				return
 			end
-			local player_name = user:get_player_name()
 			if minetest.is_protected(pointed_thing.under, player_name) then
 				minetest.chat_send_player(player_name, "This area is protected")
 				return
@@ -103,7 +103,8 @@ minetest.register_tool("fire:flint_and_steel", {
 				minetest.set_node(pointed_thing.above, {name = "fire:basic_flame"})
 			end
 		end
-		if not minetest.setting_getbool("creative_mode") then
+		if not (creative and creative.is_enabled_for
+				and creative.is_enabled_for(player_name)) then
 			-- Wear tool
 			local wdef = itemstack:get_definition()
 			itemstack:add_wear(1000)
diff --git a/mods/flowers/init.lua b/mods/flowers/init.lua
index db7a4eb3..cb5b2193 100644
--- a/mods/flowers/init.lua
+++ b/mods/flowers/init.lua
@@ -289,7 +289,8 @@ minetest.register_node("flowers:waterlily", {
 			if not minetest.is_protected(pos, player_name) then
 				minetest.set_node(pos, {name = "flowers:waterlily",
 					param2 = math.random(0, 3)})
-				if not minetest.setting_getbool("creative_mode") then
+				if not (creative and creative.is_enabled_for
+						and creative.is_enabled_for(player_name)) then
 					itemstack:take_item()
 				end
 			else
diff --git a/mods/screwdriver/init.lua b/mods/screwdriver/init.lua
index 49544de3..2a53ae37 100644
--- a/mods/screwdriver/init.lua
+++ b/mods/screwdriver/init.lua
@@ -128,7 +128,8 @@ screwdriver.handler = function(itemstack, user, pointed_thing, mode, uses)
 		minetest.check_for_falling(pos)
 	end
 
-	if not minetest.setting_getbool("creative_mode") then
+	if not (creative and creative.is_enabled_for
+			and creative.is_enabled_for(user:get_player_name())) then
 		itemstack:add_wear(65535 / ((uses or 200) - 1))
 	end
 
diff --git a/mods/stairs/init.lua b/mods/stairs/init.lua
index 86dadf97..1379c8c0 100644
--- a/mods/stairs/init.lua
+++ b/mods/stairs/init.lua
@@ -150,6 +150,8 @@ function stairs.register_slab(subname, recipeitem, groups, images, description,
 		on_place = function(itemstack, placer, pointed_thing)
 			local under = minetest.get_node(pointed_thing.under)
 			local wield_item = itemstack:get_name()
+			local creative_enabled = (creative and creative.is_enabled_for
+					and creative.is_enabled_for(placer:get_player_name()))
 
 			if under and wield_item == under.name then
 				-- place slab using under node orientation
@@ -171,7 +173,7 @@ function stairs.register_slab(subname, recipeitem, groups, images, description,
 						return
 					end
 					minetest.set_node(pointed_thing.under, {name = recipeitem, param2 = p2})
-					if not minetest.setting_getbool("creative_mode") then
+					if not creative_enabled then
 						itemstack:take_item()
 					end
 					return itemstack
@@ -187,7 +189,7 @@ function stairs.register_slab(subname, recipeitem, groups, images, description,
 
 				-- else attempt to place node with proper param2
 				minetest.item_place_node(ItemStack(wield_item), placer, pointed_thing, p2)
-				if not minetest.setting_getbool("creative_mode") then
+				if not creative_enabled then
 					itemstack:take_item()
 				end
 				return itemstack
-- 
GitLab