diff --git a/doc/ru/console.md b/doc/ru/console.md index b5f931f6..f87dd89f 100644 --- a/doc/ru/console.md +++ b/doc/ru/console.md @@ -14,7 +14,7 @@ Для создания команды консоли используется следующая функция: ```python -console.add_command(схема: str, исполнитель: function) +console.add_command(схема: str, исполнитель: function, чит_команда: bool) ``` Схема имеет следующий синтаксис: @@ -106,7 +106,7 @@ console.add_command( "Teleport object", function (args, kwargs) player.set_pos(unpack(args)) - end + end, true ) ``` @@ -114,3 +114,23 @@ console.add_command( - В kwargs передается таблица значений именованных аргументов. Проверку и приведение типов интерпретатор команд производит автоматически. + +## Остальные методы + +```lua +console.set_cheat(name: String, cheat: Boolean) -> cheatIsChanged: Boolean +console.is_cheat(name: String) -> Boolean +``` +Сеттер и геттер статуса "чит" команды + + +```lua +console.log(...) -- Выводит информацию во внутриигровую консоль +console.chat(...) -- Выводит информацию во внутриигровой чат +console.get_commands_list() -> Table -- Возвращает таблицу со списком команд +console.get_command_info(name: String) -> Table -- Возвращает информацию о команде +console.execute(command: str) -- Выполняет команду +``` + + + diff --git a/doc/ru/scripting/extensions.md b/doc/ru/scripting/extensions.md index 7ef835f4..2d7f0a1d 100644 --- a/doc/ru/scripting/extensions.md +++ b/doc/ru/scripting/extensions.md @@ -94,6 +94,13 @@ table.sub(arr: table, start: number | nil, stop: number | nil) -> table Возвращает обрезанную версию таблицы с индекса **start** до индекса **stop** включительно, при этом пары ключ-значение не сохраняются в новой таблице. При значениях **nil** начинает с **1** и заканчивает **#arr** соответственно. +```lua +table.insert_unique(t: table, val: any) +table.insert_unique(t: table, pos: number, val: any) +``` + +Добавляет значение в таблицу, только если его там не было. + ```lua table.tostring(t: table) -> string ``` diff --git a/res/modules/internal/extensions/table.lua b/res/modules/internal/extensions/table.lua index 33047df3..a1856d38 100644 --- a/res/modules/internal/extensions/table.lua +++ b/res/modules/internal/extensions/table.lua @@ -167,6 +167,18 @@ function table.remove_value(t, x) end end +function table.insert_unique(t, pos_or_val, val) + if table.has(t, val or pos_or_val) then + return + end + + if val then + table.insert(t, pos_or_val, val) + else + table.insert(t, pos_or_val) + end +end + function table.tostring(t) local s = '[' for i,v in ipairs(t) do diff --git a/res/scripts/stdcmd.lua b/res/scripts/stdcmd.lua index 73f4b059..900371e4 100644 --- a/res/scripts/stdcmd.lua +++ b/res/scripts/stdcmd.lua @@ -93,7 +93,7 @@ console.add_command( if entity then entity.transform:set_pos({x, y, z}) end - end + end, true ) console.add_command( "echo value:str", @@ -108,7 +108,7 @@ console.add_command( function(args, kwargs) world.set_day_time(args[1]) return "Time set to " .. args[1] - end + end, true ) console.add_command( @@ -123,7 +123,7 @@ console.add_command( world.set_day_time_speed(1.0) return "Daily cycle has started" end - end + end, true ) console.add_command( @@ -144,7 +144,7 @@ console.add_command( local h = math.floor(math.abs(y2-y1+1) + 0.5) local d = math.floor(math.abs(z2-z1+1) + 0.5) return tostring(w * h * d) .. " blocks set" - end + end, true ) console.add_command( @@ -154,7 +154,7 @@ console.add_command( local eid = entities.spawn("base:player", {player.get_pos(args[1])}):get_uid() player.set_entity(args[1], eid) return "spawned new player entity #" .. tostring(eid) - end + end, true ) @@ -164,7 +164,7 @@ console.add_command( function(args, kwargs) local eid = entities.spawn(args[1], {args[2], args[3], args[4]}) return string.format("spawned %s at %s, %s, %s", unpack(args)) - end + end, true ) console.add_command( @@ -177,7 +177,7 @@ console.add_command( entity:despawn() return "despawned entity #" .. tostring(eid) end - end + end, true ) console.add_command( @@ -231,7 +231,7 @@ console.add_command( local rotation = args[5] local fragment = generation.load_fragment(filename) fragment:place({x, y, z}, rotation) - end + end, true ) console.add_command( @@ -242,7 +242,7 @@ console.add_command( local value = args[2] rules.set(name, value) return "rule '"..name.."' set to "..tostring(value) - end + end, true ) console.add_command( @@ -285,7 +285,7 @@ console.add_command( local preset = json.parse(file.read(filename)) gfx.weather.change(preset, args[2], args[1]) return "weather set to "..filename.." preset ("..tostring(args[2]).." s)" - end + end, true ) console.add_command( @@ -313,14 +313,3 @@ console.add_command( return "available presets:" .. presets end ) - -console.cheats = { - "blocks.fill", - "tp", - "fragment.place", - "time.set", - "time.daycycle", - "entity.despawn", - "player.respawn", - "weather.set", -} diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index f8cb1c07..b428785e 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -171,6 +171,42 @@ function console.log(...) log_element:paste(text) end +local console_add_command = console.__add_command +console.__add_command = nil + +function console.add_command(scheme, description, handler, is_cheat) + console_add_command(scheme, description, handler) + if not is_cheat then return end + + local name = string.match(scheme, "^(%S+)") + if not name then + error("Incorrect command syntax, command name not found") + end + + table.insert_unique(console.cheats, name) +end + +function console.is_cheat(name) + if not table.has(console.get_commands_list(), name) then + error(string.format("command \"%s\" not found", name)) + end + + return table.has(console.cheats, name) +end + +function console.set_cheat(name, status) + local is_cheat = console.is_cheat(name) + if status and not is_cheat then + table.insert(console.cheats, name) + return true + elseif not status and is_cheat then + table.remove_value(console.cheats, name) + return true + end + + return false +end + function console.chat(...) console.log(...) events.emit("core:chat", ...) diff --git a/src/logic/scripting/lua/libs/libconsole.cpp b/src/logic/scripting/lua/libs/libconsole.cpp index 28e71b4b..c5cc2fcb 100644 --- a/src/logic/scripting/lua/libs/libconsole.cpp +++ b/src/logic/scripting/lua/libs/libconsole.cpp @@ -120,7 +120,7 @@ static int l_get_command_info(lua::State* L) { } const luaL_Reg consolelib[] = { - {"add_command", lua::wrap}, + {"__add_command", lua::wrap}, {"execute", lua::wrap}, {"get", lua::wrap}, {"set", lua::wrap},