From 9c630645b496eb335fe3ef0b08c8804ddda8980c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 22 Jan 2025 10:15:31 +0300 Subject: [PATCH] add console.get(...) & add 'player' console variable --- doc/en/console.md | 2 +- doc/ru/console.md | 2 +- res/layouts/console.xml.lua | 1 + src/logic/scripting/lua/libs/libconsole.cpp | 6 ++++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/en/console.md b/doc/en/console.md index f4ff8aff..fff57c76 100644 --- a/doc/en/console.md +++ b/doc/en/console.md @@ -4,7 +4,7 @@ To work with the command interpreter, use the **console** library. When sending a command via the standard console (core:console layout): 1. the `allow-cheats` rule is checked -2. the `pos.x|y|z`, `entity.id`, `entity.selected` variables are automatically set. +2. the `player`, `pos.x|y|z`, `entity.id`, `entity.selected` variables are automatically set. 3. the command handler is called - console.submit or the default one. The default handler calls console.execute, passing the result to the console.log call. diff --git a/doc/ru/console.md b/doc/ru/console.md index b4099bc5..b5f931f6 100644 --- a/doc/ru/console.md +++ b/doc/ru/console.md @@ -4,7 +4,7 @@ При отправке команды через стандартную консоль (макет core:console): 1. проверяется правило `allow-cheats` -2. автоматически устанавливаются переменные `pos.x|y|z`, `entity.id`, `entity.selected`. +2. автоматически устанавливаются переменные `player`, `pos.x|y|z`, `entity.id`, `entity.selected`. 3. вызывается обработчик команд - console.submit или по-умолчанию. Обработчик по-умолчанию вызывает console.execute, передавая результат в вызов console.log. diff --git a/res/layouts/console.xml.lua b/res/layouts/console.xml.lua index b8f71b75..bb36863b 100644 --- a/res/layouts/console.xml.lua +++ b/res/layouts/console.xml.lua @@ -97,6 +97,7 @@ end) function setup_variables() local pid = hud.get_player() local x,y,z = player.get_pos(pid) + console.set("player", pid) console.set('pos.x', x) console.set('pos.y', y) console.set('pos.z', z) diff --git a/src/logic/scripting/lua/libs/libconsole.cpp b/src/logic/scripting/lua/libs/libconsole.cpp index 295d264b..2c21a0be 100644 --- a/src/logic/scripting/lua/libs/libconsole.cpp +++ b/src/logic/scripting/lua/libs/libconsole.cpp @@ -43,6 +43,11 @@ static int l_execute(lua::State* L) { } } +static int l_get(lua::State* L) { + auto name = lua::require_string(L, 1); + return lua::pushvalue(L, (*engine->getCommandsInterpreter())[name]); +} + static int l_set(lua::State* L) { auto name = lua::require_string(L, 1); auto value = lua::tovalue(L, 2); @@ -119,6 +124,7 @@ static int l_get_command_info(lua::State* L) { const luaL_Reg consolelib[] = { {"add_command", lua::wrap}, {"execute", lua::wrap}, + {"get", lua::wrap}, {"set", lua::wrap}, {"get_commands_list", lua::wrap}, {"get_command_info", lua::wrap},