From a2d7678ffd532dd82dcaa108eac3dace61ac94a2 Mon Sep 17 00:00:00 2001 From: DTA7 Date: Sun, 1 Oct 2017 15:41:58 +0200 Subject: [PATCH] Add nil checks for placer --- mods/beds/api.lua | 22 +++++++++++++--------- mods/boats/init.lua | 12 ++++++++---- mods/bucket/init.lua | 3 ++- mods/carts/cart_entity.lua | 3 ++- mods/default/functions.lua | 2 +- mods/default/nodes.lua | 4 +--- mods/default/tools.lua | 3 ++- mods/default/torch.lua | 3 ++- mods/default/trees.lua | 6 ++++-- mods/doors/init.lua | 7 ++++--- mods/farming/api.lua | 15 +++++++++------ mods/flowers/init.lua | 2 +- mods/screwdriver/init.lua | 9 +++++---- mods/stairs/init.lua | 34 ++++++++++++++++++---------------- 14 files changed, 72 insertions(+), 53 deletions(-) diff --git a/mods/beds/api.lua b/mods/beds/api.lua index 3632dc0c..9349545a 100644 --- a/mods/beds/api.lua +++ b/mods/beds/api.lua @@ -49,7 +49,8 @@ function beds.register_bed(name, def) local node = minetest.get_node(under) local udef = minetest.registered_nodes[node.name] if udef and udef.on_rightclick and - not (placer and placer:get_player_control().sneak) then + not (placer and placer:is_player() and + placer:get_player_control().sneak) then return udef.on_rightclick(under, node, placer, itemstack, pointed_thing) or itemstack end @@ -61,9 +62,11 @@ function beds.register_bed(name, def) pos = pointed_thing.above end - 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()) + local player_name = placer and placer:get_player_name() or "" + + if minetest.is_protected(pos, player_name) and + not minetest.check_player_privs(player_name, "protection_bypass") then + minetest.record_protection_violation(pos, player_name) return itemstack end @@ -72,12 +75,13 @@ function beds.register_bed(name, def) return itemstack end - local dir = minetest.dir_to_facedir(placer:get_look_dir()) + local dir = placer and placer:get_look_dir() and + minetest.dir_to_facedir(placer:get_look_dir()) or 0 local botpos = vector.add(pos, minetest.facedir_to_dir(dir)) - if minetest.is_protected(botpos, placer:get_player_name()) and - not minetest.check_player_privs(placer, "protection_bypass") then - minetest.record_protection_violation(botpos, placer:get_player_name()) + if minetest.is_protected(botpos, player_name) and + not minetest.check_player_privs(player_name, "protection_bypass") then + minetest.record_protection_violation(botpos, player_name) return itemstack end @@ -90,7 +94,7 @@ function beds.register_bed(name, def) minetest.set_node(botpos, {name = name .. "_top", param2 = dir}) if not (creative and creative.is_enabled_for - and creative.is_enabled_for(placer:get_player_name())) then + and creative.is_enabled_for(player_name)) then itemstack:take_item() end return itemstack diff --git a/mods/boats/init.lua b/mods/boats/init.lua index 38025d16..72ca478f 100644 --- a/mods/boats/init.lua +++ b/mods/boats/init.lua @@ -230,7 +230,8 @@ minetest.register_craftitem("boats:boat", { local node = minetest.get_node(under) local udef = minetest.registered_nodes[node.name] if udef and udef.on_rightclick and - not (placer and placer:get_player_control().sneak) then + not (placer and placer:is_player() and + placer:get_player_control().sneak) then return udef.on_rightclick(under, node, placer, itemstack, pointed_thing) or itemstack end @@ -244,9 +245,12 @@ minetest.register_craftitem("boats:boat", { pointed_thing.under.y = pointed_thing.under.y + 0.5 boat = minetest.add_entity(pointed_thing.under, "boats:boat") if boat then - boat:setyaw(placer:get_look_horizontal()) - if not (creative and creative.is_enabled_for - and creative.is_enabled_for(placer:get_player_name())) then + if placer then + boat:setyaw(placer:get_look_horizontal()) + end + local player_name = placer and placer:get_player_name() or "" + if not (creative and creative.is_enabled_for and + creative.is_enabled_for(player_name)) then itemstack:take_item() end end diff --git a/mods/bucket/init.lua b/mods/bucket/init.lua index 44565326..9c1481ef 100644 --- a/mods/bucket/init.lua +++ b/mods/bucket/init.lua @@ -69,7 +69,8 @@ function bucket.register_liquid(source, flowing, itemname, inventory_image, name -- Call on_rightclick if the pointed node defines it if ndef and ndef.on_rightclick and - user and not user:get_player_control().sneak then + not (user and user:is_player() and + user:get_player_control().sneak) then return ndef.on_rightclick( pointed_thing.under, node, user, diff --git a/mods/carts/cart_entity.lua b/mods/carts/cart_entity.lua index 88fd2be6..434ea943 100644 --- a/mods/carts/cart_entity.lua +++ b/mods/carts/cart_entity.lua @@ -363,7 +363,8 @@ minetest.register_craftitem("carts:cart", { local node = minetest.get_node(under) local udef = minetest.registered_nodes[node.name] if udef and udef.on_rightclick and - not (placer and placer:get_player_control().sneak) then + not (placer and placer:is_player() and + placer:get_player_control().sneak) then return udef.on_rightclick(under, node, placer, itemstack, pointed_thing) or itemstack end diff --git a/mods/default/functions.lua b/mods/default/functions.lua index 4eb1a1c9..20cf492a 100644 --- a/mods/default/functions.lua +++ b/mods/default/functions.lua @@ -334,7 +334,7 @@ end -- Prevent decay of placed leaves default.after_place_leaves = function(pos, placer, itemstack, pointed_thing) - if placer and not placer:get_player_control().sneak then + if placer and placer:is_player() and not placer:get_player_control().sneak then local node = minetest.get_node(pos) node.param2 = 1 minetest.set_node(pos, node) diff --git a/mods/default/nodes.lua b/mods/default/nodes.lua index 545d02c0..4aa35dc6 100644 --- a/mods/default/nodes.lua +++ b/mods/default/nodes.lua @@ -667,9 +667,7 @@ minetest.register_node("default:apple", { sounds = default.node_sound_leaves_defaults(), after_place_node = function(pos, placer, itemstack) - if placer:is_player() then - minetest.set_node(pos, {name = "default:apple", param2 = 1}) - end + minetest.set_node(pos, {name = "default:apple", param2 = 1}) end, }) diff --git a/mods/default/tools.lua b/mods/default/tools.lua index 1e719785..067613d3 100644 --- a/mods/default/tools.lua +++ b/mods/default/tools.lua @@ -389,7 +389,8 @@ minetest.register_tool("default:key", { local node = minetest.get_node(under) local def = minetest.registered_nodes[node.name] if def and def.on_rightclick and - not (placer and placer:get_player_control().sneak) then + not (placer and placer:is_player() and + placer:get_player_control().sneak) then return def.on_rightclick(under, node, placer, itemstack, pointed_thing) or itemstack end diff --git a/mods/default/torch.lua b/mods/default/torch.lua index 016ae685..a99a38db 100644 --- a/mods/default/torch.lua +++ b/mods/default/torch.lua @@ -78,7 +78,8 @@ minetest.register_node("default:torch", { local node = minetest.get_node(under) local def = minetest.registered_nodes[node.name] if def and def.on_rightclick and - ((not placer) or (placer and not placer:get_player_control().sneak)) then + not (placer and placer:is_player() and + placer:get_player_control().sneak) then return def.on_rightclick(under, node, placer, itemstack, pointed_thing) or itemstack end diff --git a/mods/default/trees.lua b/mods/default/trees.lua index d00a6b5e..44176d77 100644 --- a/mods/default/trees.lua +++ b/mods/default/trees.lua @@ -468,7 +468,9 @@ function default.sapling_on_place(itemstack, placer, pointed_thing, local node = minetest.get_node_or_nil(pos) local pdef = node and minetest.registered_nodes[node.name] - if pdef and pdef.on_rightclick and not placer:get_player_control().sneak then + if pdef and pdef.on_rightclick and + not (placer and placer:is_player() and + placer:get_player_control().sneak) then return pdef.on_rightclick(pos, node, placer, itemstack, pointed_thing) end @@ -481,7 +483,7 @@ function default.sapling_on_place(itemstack, placer, pointed_thing, end end - local player_name = placer:get_player_name() + local player_name = placer and placer:get_player_name() or "" -- Check sapling position for protection if minetest.is_protected(pos, player_name) then minetest.record_protection_violation(pos, player_name) diff --git a/mods/doors/init.lua b/mods/doors/init.lua index 2fcc23de..51879678 100644 --- a/mods/doors/init.lua +++ b/mods/doors/init.lua @@ -266,7 +266,8 @@ function doors.register(name, def) local node = minetest.get_node(pointed_thing.under) local pdef = minetest.registered_nodes[node.name] if pdef and pdef.on_rightclick and - not placer:get_player_control().sneak then + not (placer and placer:is_player() and + placer:get_player_control().sneak) then return pdef.on_rightclick(pointed_thing.under, node, placer, itemstack, pointed_thing) end @@ -290,12 +291,12 @@ function doors.register(name, def) return itemstack end - local pn = placer:get_player_name() + local pn = placer and placer:get_player_name() or "" if minetest.is_protected(pos, pn) or minetest.is_protected(above, pn) then return itemstack end - local dir = minetest.dir_to_facedir(placer:get_look_dir()) + local dir = placer and minetest.dir_to_facedir(placer:get_look_dir()) or 0 local ref = { {x = -1, y = 0, z = 0}, diff --git a/mods/farming/api.lua b/mods/farming/api.lua index 150301ad..1c63ee34 100644 --- a/mods/farming/api.lua +++ b/mods/farming/api.lua @@ -144,12 +144,14 @@ farming.place_seed = function(itemstack, placer, pointed_thing, plantname) local under = minetest.get_node(pt.under) local above = minetest.get_node(pt.above) - if minetest.is_protected(pt.under, placer:get_player_name()) then - minetest.record_protection_violation(pt.under, placer:get_player_name()) + local player_name = placer and placer:get_player_name() or "" + + if minetest.is_protected(pt.under, player_name) then + minetest.record_protection_violation(pt.under, player_name) return end - if minetest.is_protected(pt.above, placer:get_player_name()) then - minetest.record_protection_violation(pt.above, placer:get_player_name()) + if minetest.is_protected(pt.above, player_name) then + minetest.record_protection_violation(pt.above, player_name) return end @@ -180,7 +182,7 @@ farming.place_seed = function(itemstack, placer, pointed_thing, plantname) minetest.add_node(pt.above, {name = plantname, param2 = 1}) tick(pt.above) if not (creative and creative.is_enabled_for - and creative.is_enabled_for(placer:get_player_name())) then + and creative.is_enabled_for(player_name)) then itemstack:take_item() end return itemstack @@ -310,7 +312,8 @@ farming.register_plant = function(name, def) local node = minetest.get_node(under) local udef = minetest.registered_nodes[node.name] if udef and udef.on_rightclick and - not (placer and placer:get_player_control().sneak) then + not (placer and placer:is_player() and + placer:get_player_control().sneak) then return udef.on_rightclick(under, node, placer, itemstack, pointed_thing) or itemstack end diff --git a/mods/flowers/init.lua b/mods/flowers/init.lua index d28764f9..6836baa7 100644 --- a/mods/flowers/init.lua +++ b/mods/flowers/init.lua @@ -278,7 +278,7 @@ minetest.register_node("flowers:waterlily", { local pos = pointed_thing.above local node = minetest.get_node(pointed_thing.under) local def = minetest.registered_nodes[node.name] - local player_name = placer:get_player_name() + local player_name = placer and placer:get_player_name() or "" if def and def.on_rightclick then return def.on_rightclick(pointed_thing.under, node, placer, itemstack, diff --git a/mods/screwdriver/init.lua b/mods/screwdriver/init.lua index e76f0545..634edf85 100644 --- a/mods/screwdriver/init.lua +++ b/mods/screwdriver/init.lua @@ -85,9 +85,10 @@ screwdriver.handler = function(itemstack, user, pointed_thing, mode, uses) end local pos = pointed_thing.under + local player_name = user and user:get_player_name() or "" - if minetest.is_protected(pos, user:get_player_name()) then - minetest.record_protection_violation(pos, user:get_player_name()) + if minetest.is_protected(pos, player_name) then + minetest.record_protection_violation(pos, player_name) return end @@ -133,8 +134,8 @@ screwdriver.handler = function(itemstack, user, pointed_thing, mode, uses) minetest.check_for_falling(pos) end - if not (creative and creative.is_enabled_for - and creative.is_enabled_for(user:get_player_name())) then + if not (creative and creative.is_enabled_for and + creative.is_enabled_for(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 8b3ceed0..bb9f8ec8 100644 --- a/mods/stairs/init.lua +++ b/mods/stairs/init.lua @@ -22,21 +22,23 @@ local function rotate_and_place(itemstack, placer, pointed_thing) local p1 = pointed_thing.above local param2 = 0 - local placer_pos = placer:getpos() - if placer_pos then - param2 = minetest.dir_to_facedir(vector.subtract(p1, placer_pos)) - end + if placer then + local placer_pos = placer:getpos() + if placer_pos then + param2 = minetest.dir_to_facedir(vector.subtract(p1, placer_pos)) + end - local finepos = minetest.pointed_thing_to_face_pos(placer, pointed_thing) - local fpos = finepos.y % 1 + local finepos = minetest.pointed_thing_to_face_pos(placer, pointed_thing) + local fpos = finepos.y % 1 - if p0.y - 1 == p1.y or (fpos > 0 and fpos < 0.5) - or (fpos < -0.5 and fpos > -0.999999999) then - param2 = param2 + 20 - if param2 == 21 then - param2 = 23 - elseif param2 == 23 then - param2 = 21 + if p0.y - 1 == p1.y or (fpos > 0 and fpos < 0.5) + or (fpos < -0.5 and fpos > -0.999999999) then + param2 = param2 + 20 + if param2 == 21 then + param2 = 23 + elseif param2 == 23 then + param2 = 21 + end end end return minetest.item_place(itemstack, placer, pointed_thing, param2) @@ -175,8 +177,9 @@ 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 player_name = placer and placer:get_player_name() or "" local creative_enabled = (creative and creative.is_enabled_for - and creative.is_enabled_for(placer:get_player_name())) + and creative.is_enabled_for(player_name)) if under and under.name:find("stairs:slab_") then -- place slab using under node orientation @@ -192,9 +195,8 @@ function stairs.register_slab(subname, recipeitem, groups, images, description, if not recipeitem then return itemstack end - local player_name = placer:get_player_name() if minetest.is_protected(pointed_thing.under, player_name) and not - minetest.check_player_privs(placer, "protection_bypass") then + minetest.check_player_privs(player_name, "protection_bypass") then minetest.record_protection_violation(pointed_thing.under, player_name) return