Set placer to nil instead of a non-functional one in item_OnPlace (#6449)
authorDTA7 <dta7e@t-online.de>
Thu, 21 Sep 2017 19:52:52 +0000 (21:52 +0200)
committerSmallJoker <mk939@ymail.com>
Sun, 3 Jun 2018 15:31:59 +0000 (17:31 +0200)
* Set placer to nil instead of a non-functional one

This requires nil checks in core.rotate_node and core.rotate_and_place.

builtin/common/misc_helpers.lua
src/script/cpp_api/s_item.cpp
src/script/lua_api/l_env.cpp

index 1e282f88683678c07f795464ecf8e383ffba890d..51abed1bede80cea2b05c12d803f540d958c134a 100644 (file)
@@ -349,7 +349,7 @@ if INIT == "game" then
                                        itemstack, pointed_thing)
                        return
                end
-               local fdir = core.dir_to_facedir(placer:get_look_dir())
+               local fdir = placer and core.dir_to_facedir(placer:get_look_dir()) or 0
                local wield_name = itemstack:get_name()
 
                local above = pointed_thing.above
@@ -369,9 +369,9 @@ if INIT == "game" then
                        iswall = false
                end
 
-               if core.is_protected(pos, placer:get_player_name()) then
-                       core.record_protection_violation(pos,
-                                       placer:get_player_name())
+               local name = placer and placer:get_player_name() or ""
+               if core.is_protected(pos, name) then
+                       core.record_protection_violation(pos, name)
                        return
                end
 
@@ -432,9 +432,11 @@ if INIT == "game" then
        end
 
        core.rotate_node = function(itemstack, placer, pointed_thing)
+               local name = placer and placer:get_player_name() or ""
+               local invert_wall = placer and placer:get_player_control().sneak or false
                core.rotate_and_place(itemstack, placer, pointed_thing,
-                               is_creative(placer:get_player_name()),
-                               {invert_wall = placer:get_player_control().sneak})
+                               is_creative(name),
+                               {invert_wall = invert_wall})
                return itemstack
        end
 end
index 032018f2f7438c59bef671b7a5b3b85d3c290fc1..e13da1c8626842483c4ecf8312811b69d09fde95 100644 (file)
@@ -69,7 +69,12 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
 
        // Call function
        LuaItemStack::create(L, item);
-       objectrefGetOrCreate(L, placer);
+
+       if (!placer)
+               lua_pushnil(L);
+       else
+               objectrefGetOrCreate(L, placer);
+
        pushPointedThing(pointed);
        PCALL_RES(lua_pcall(L, 3, 1, error_handler));
        if (!lua_isnil(L, -1)) {
index 393c4ed5f5ebb07db74f489f49deab9474cc6174..46745facd26ac38540b11ca8163d763a796ee7f9 100644 (file)
@@ -291,9 +291,8 @@ int ModApiEnvMod::l_place_node(lua_State *L)
        pointed.type = POINTEDTHING_NODE;
        pointed.node_abovesurface = pos;
        pointed.node_undersurface = pos + v3s16(0,-1,0);
-       // Place it with a NULL placer (appears in Lua as a non-functional
-       // ObjectRef)
-       bool success = scriptIfaceItem->item_OnPlace(item, NULL, pointed);
+       // Place it with a NULL placer (appears in Lua as nil)
+       bool success = scriptIfaceItem->item_OnPlace(item, nullptr, pointed);
        lua_pushboolean(L, success);
        return 1;
 }