From 03b7a1c368e3258fc0430d6f1a7cc980704c1d0e Mon Sep 17 00:00:00 2001 From: GoldenTails Date: Sun, 26 Jul 2020 12:08:33 -0500 Subject: [PATCH] Make the player optional for COM_BufInsertText and COM_BufAddText. When no player is supplied, the command will run for all players. --- src/lua_consolelib.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/lua_consolelib.c b/src/lua_consolelib.c index 36b1c3669..fae4f2d14 100644 --- a/src/lua_consolelib.c +++ b/src/lua_consolelib.c @@ -236,15 +236,14 @@ static int lib_comAddCommand(lua_State *L) static int lib_comBufAddText(lua_State *L) { int n = lua_gettop(L); /* number of arguments */ - player_t *plr; + player_t *plr = NULL; if (n < 2) return luaL_error(L, "COM_BufAddText requires two arguments: player and text."); NOHUD lua_settop(L, 2); - plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); - if (!plr) - return LUA_ErrInvalid(L, "player_t"); - if (plr != &players[consoleplayer]) + if (!lua_isnoneornil(L, 1)) + plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); + if (plr && plr != &players[consoleplayer]) return 0; COM_BufAddTextEx(va("%s\n", luaL_checkstring(L, 2)), COM_SAFE); return 0; @@ -253,15 +252,14 @@ static int lib_comBufAddText(lua_State *L) static int lib_comBufInsertText(lua_State *L) { int n = lua_gettop(L); /* number of arguments */ - player_t *plr; + player_t *plr = NULL; if (n < 2) return luaL_error(L, "COM_BufInsertText requires two arguments: player and text."); NOHUD lua_settop(L, 2); - plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); - if (!plr) - return LUA_ErrInvalid(L, "player_t"); - if (plr != &players[consoleplayer]) + if (!lua_isnoneornil(L, 1)) + plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); + if (plr && plr != &players[consoleplayer]) return 0; COM_BufInsertTextEx(va("%s\n", luaL_checkstring(L, 2)), COM_SAFE); return 0;