Creative: Add creative.is_enabled_for
authorrubenwardy <rubenwardy@gmail.com>
Fri, 30 Dec 2016 22:13:27 +0000 (22:13 +0000)
committerparamat <mat.gregory@virginmedia.com>
Thu, 26 Jan 2017 17:54:45 +0000 (17:54 +0000)
game_api.txt
mods/creative/init.lua
mods/creative/inventory.lua

index 38718c365eb2c76033065514c7b85ef6eb96de59..cef46f66b2a1ffd4257d45dec8558433ae4b3195 100644 (file)
@@ -78,6 +78,12 @@ For example,
 is used to show all tools. Name is used in the sfinv page name, title is the
 human readable title.
 
+`is_enabled_for` is used to check whether a player is in creative mode:
+
+    creative.is_enabled_for(name)
+
+Override this to allow per-player game modes.
+
 The contents of `creative.formspec_add` is appended to every creative inventory
 page. Mods can use it to add additional formspec elements onto the default
 creative inventory formspec to be drawn after each update.
index 868b802bbdc1b77bd4eaaf47f07a5ea20f48455f..24700eb9116b41c29cdfa25046d1c811b4ba9c92 100644 (file)
@@ -1,3 +1,9 @@
+creative = {}
+
+function creative.is_enabled_for(name)
+       return minetest.setting_getbool("creative_mode")
+end
+
 dofile(minetest.get_modpath("creative") .. "/inventory.lua")
 
 if minetest.setting_getbool("creative_mode") then
@@ -28,22 +34,25 @@ if minetest.setting_getbool("creative_mode") then
                        damage_groups = {fleshy = 10},
                }
        })
+end
 
-       minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
-               return true
-       end)
+-- Unlimited node placement
+minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
+       return creative.is_enabled_for(placer:get_player_name())
+end)
 
-       function minetest.handle_node_drops(pos, drops, digger)
-               if not digger or not digger:is_player() then
-                       return
-               end
-               local inv = digger:get_inventory()
-               if inv then
-                       for _, item in ipairs(drops) do
-                               item = ItemStack(item):get_name()
-                               if not inv:contains_item("main", item) then
-                                       inv:add_item("main", item)
-                               end
+-- Don't pick up if the item is already in the inventory
+function minetest.handle_node_drops(pos, drops, digger)
+       if not digger or not digger:is_player() or
+                       not creative.is_enabled_for(digger:get_player_name()) then
+               return
+       end
+       local inv = digger:get_inventory()
+       if inv then
+               for _, item in ipairs(drops) do
+                       item = ItemStack(item):get_name()
+                       if not inv:contains_item("main", item) then
+                               inv:add_item("main", item)
                        end
                end
        end
index be24b3a27e75ec91895962f32cf71fa865af3e5c..1363e294a0b1dddc285723d400c4ec975e96cc87 100644 (file)
@@ -1,4 +1,3 @@
-creative = {}
 local player_inventory = {}
 
 function creative.init_creative_inventory(player)
@@ -79,7 +78,7 @@ function creative.register_tab(name, title, items)
        sfinv.register_page("creative:" .. name, {
                title = title,
                is_in_nav = function(self, player, context)
-                       return minetest.setting_getbool("creative_mode")
+                       return creative.is_enabled_for(player:get_player_name())
                end,
                get = function(self, player, context)
                        local player_name = player:get_player_name()
@@ -172,7 +171,7 @@ creative.register_tab("craftitems", "Items", minetest.registered_craftitems)
 
 local old_homepage_name = sfinv.get_homepage_name
 function sfinv.get_homepage_name(player)
-       if minetest.setting_getbool("creative_mode") then
+       if creative.is_enabled_for(player:get_player_name()) then
                return "creative:all"
        else
                return old_homepage_name(player)