Merge pull request #689 from Xertis/main

Improving the cheat commands
This commit is contained in:
MihailRis 2025-11-21 21:51:01 +03:00 committed by GitHub
commit 3b22bfbe92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 88 additions and 24 deletions

View File

@ -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<String> -- Возвращает таблицу со списком команд
console.get_command_info(name: String) -> Table -- Возвращает информацию о команде
console.execute(command: str) -- Выполняет команду
```

View File

@ -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
```

View File

@ -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

View File

@ -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",
}

View File

@ -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", ...)

View File

@ -120,7 +120,7 @@ static int l_get_command_info(lua::State* L) {
}
const luaL_Reg consolelib[] = {
{"add_command", lua::wrap<l_add_command>},
{"__add_command", lua::wrap<l_add_command>},
{"execute", lua::wrap<l_execute>},
{"get", lua::wrap<l_get>},
{"set", lua::wrap<l_set>},