Check for valid players in default triggers instead of crashing.

This commit is contained in:
Beha 2020-04-04 16:34:25 -04:00
parent 9d98f950b8
commit 7e3064ef93

View File

@ -14,6 +14,10 @@
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-- --
-- Check if a player object is valid for awards.
local function player_ok(player)
return player and player.is_player and player:is_player() and not player.is_fake_player
end
awards.register_trigger("chat", { awards.register_trigger("chat", {
type = "counted", type = "counted",
@ -22,7 +26,7 @@ awards.register_trigger("chat", {
}) })
minetest.register_on_chat_message(function(name, message) minetest.register_on_chat_message(function(name, message)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if not player or string.find(message, "/") then if not player_ok(player) or string.find(message, "/") then
return return
end end
@ -68,7 +72,7 @@ awards.register_trigger("dig", {
key_is_item = true, key_is_item = true,
}) })
minetest.register_on_dignode(function(pos, node, player) minetest.register_on_dignode(function(pos, node, player)
if not player or not pos or not node then if not player_ok(player) or not pos or not node then
return return
end end
@ -89,7 +93,7 @@ awards.register_trigger("place", {
key_is_item = true, key_is_item = true,
}) })
minetest.register_on_placenode(function(pos, node, player) minetest.register_on_placenode(function(pos, node, player)
if not player or not pos or not node then if not player_ok(player) or not pos or not node then
return return
end end
@ -110,7 +114,7 @@ awards.register_trigger("craft", {
key_is_item = true, key_is_item = true,
}) })
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if not player or itemstack:is_empty() then if not player_ok(player) or itemstack:is_empty() then
return return
end end
@ -131,7 +135,7 @@ awards.register_trigger("eat", {
key_is_item = true, key_is_item = true,
}) })
minetest.register_on_item_eat(function(_, _, itemstack, player, _) minetest.register_on_item_eat(function(_, _, itemstack, player, _)
if not player or itemstack:is_empty() then if not player_ok(player) or itemstack:is_empty() then
return return
end end