Add nil checks around get_player_by_name()

in case someone uses the commands from ncurses
This commit is contained in:
sfan5 2024-04-22 18:08:04 +02:00
parent 1d8d9a704f
commit 60b6b205ad
4 changed files with 18 additions and 10 deletions

@ -136,7 +136,8 @@ end
-- Return the marker that is closest to the player -- Return the marker that is closest to the player
worldedit.marker_get_closest_to_player = function(name) worldedit.marker_get_closest_to_player = function(name)
local playerpos = minetest.get_player_by_name(name):get_pos() local player = assert(minetest.get_player_by_name(name))
local playerpos = player:get_pos()
local dist1 = vector.distance(playerpos, worldedit.pos1[name]) local dist1 = vector.distance(playerpos, worldedit.pos1[name])
local dist2 = vector.distance(playerpos, worldedit.pos2[name]) local dist2 = vector.distance(playerpos, worldedit.pos2[name])

@ -84,7 +84,9 @@ worldedit.register_command("brush", {
return true, cmd, params return true, cmd, params
end, end,
func = function(name, cmd, params) func = function(name, cmd, params)
local itemstack = minetest.get_player_by_name(name):get_wielded_item() local player = minetest.get_player_by_name(name)
if not player then return end
local itemstack = player:get_wielded_item()
if itemstack == nil or itemstack:get_name() ~= "worldedit:brush" then if itemstack == nil or itemstack:get_name() ~= "worldedit:brush" then
return false, S("Not holding brush item.") return false, S("Not holding brush item.")
end end
@ -115,6 +117,6 @@ worldedit.register_command("brush", {
minetest.registered_tools["worldedit:brush"].description .. ": " .. fullcmd) minetest.registered_tools["worldedit:brush"].description .. ": " .. fullcmd)
worldedit.player_notify(name, S("Brush assigned to command: @1", fullcmd), "ok") worldedit.player_notify(name, S("Brush assigned to command: @1", fullcmd), "ok")
end end
minetest.get_player_by_name(name):set_wielded_item(itemstack) player:set_wielded_item(itemstack)
end, end,
}) })

@ -174,7 +174,12 @@ end
-- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1) -- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)
function worldedit.player_axis(name) function worldedit.player_axis(name)
local dir = minetest.get_player_by_name(name):get_look_dir() local player = minetest.get_player_by_name(name)
if not player then
-- we promised to return something valid...
return "y", -1
end
local dir = player:get_look_dir()
local x, y, z = math.abs(dir.x), math.abs(dir.y), math.abs(dir.z) local x, y, z = math.abs(dir.x), math.abs(dir.y), math.abs(dir.z)
if x > y then if x > y then
if x > z then if x > z then

@ -113,9 +113,9 @@ worldedit.register_command("pos1", {
category = S("Region operations"), category = S("Region operations"),
privs = {worldedit=true}, privs = {worldedit=true},
func = function(name) func = function(name)
local pos = minetest.get_player_by_name(name):get_pos() local player = minetest.get_player_by_name(name)
pos.x, pos.y, pos.z = math.floor(pos.x + 0.5), math.floor(pos.y + 0.5), math.floor(pos.z + 0.5) if not player then return end
set_pos1(name, pos) set_pos1(name, vector.round(player:get_pos()))
end, end,
}) })
@ -125,9 +125,9 @@ worldedit.register_command("pos2", {
category = S("Region operations"), category = S("Region operations"),
privs = {worldedit=true}, privs = {worldedit=true},
func = function(name) func = function(name)
local pos = minetest.get_player_by_name(name):get_pos() local player = minetest.get_player_by_name(name)
pos.x, pos.y, pos.z = math.floor(pos.x + 0.5), math.floor(pos.y + 0.5), math.floor(pos.z + 0.5) if not player then return end
set_pos2(name, pos) set_pos2(name, vector.round(player:get_pos()))
end, end,
}) })