From 5e9b290aea0777786b4ee4aaefc3106de100ea67 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 6 Nov 2024 18:01:00 +0300 Subject: [PATCH] add 'cheat-commands' rule --- res/layouts/console.xml.lua | 16 ++++++++++++++++ res/scripts/stdcmd.lua | 16 +++++++++++++--- res/scripts/stdlib.lua | 10 ++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/res/layouts/console.xml.lua b/res/layouts/console.xml.lua index 791e1fb2..711f0435 100644 --- a/res/layouts/console.xml.lua +++ b/res/layouts/console.xml.lua @@ -52,6 +52,22 @@ function submit(text) add_to_history(text) setup_variables() + text = text:trim() + local name + for s in text:gmatch("%S+") do + name = s + break + end + if name == nil then + name = text + end + if not rules.get("cheat-commands") and table.has(console.cheats, name) then + console.log("cheat commands are disabled") + document.prompt.text = "" + document.prompt.focused = true + return + end + document.log.caret = -1 local status, result = pcall(console.execute, text) if result then diff --git a/res/scripts/stdcmd.lua b/res/scripts/stdcmd.lua index 930be637..1df86dd6 100644 --- a/res/scripts/stdcmd.lua +++ b/res/scripts/stdcmd.lua @@ -28,17 +28,17 @@ console.add_command( function(args, kwargs) local name = args[1] if #name == 0 then - local commands = console.get_commands_list() table.sort(commands) local str = "Available commands:" for i,k in ipairs(commands) do - str = str .. "\n " .. build_scheme(console.get_command_info(k)) + if rules.get("cheat-commands") or not table.has(console.cheats, k) then + str = str .. "\n " .. build_scheme(console.get_command_info(k)) + end end return str .. "\nuse 'help '" - end local command = console.get_command_info(name) @@ -255,3 +255,13 @@ console.add_command( return "registered rules:\n" .. names end ) + +console.cheats = { + "blocks.fill", + "tp", + "fragment.place", + "time.set", + "time.daycycle", + "entity.despawn", + "player.respawn" +} diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index be43f57d..22038fea 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -103,6 +103,9 @@ function time.post_runnable(runnable) table.insert(__post_runnables, runnable) end +--- Console library extension --- +console.cheats = {} + local log_element = Document.new("core:console").log function console.log(...) local args = {...} @@ -205,9 +208,10 @@ function _rules.listen(name, handler) end function _rules.create(name, value, handler) - print(name, value, handler) _rules.set(name, value) - return _rules.listen(name, handler) + if handler ~= nil then + return _rules.listen(name, handler) + end end function _rules.unlisten(name, id) @@ -218,6 +222,8 @@ end function _rules.clear() _rules.rules = {} _rules.nextid = 1 + + _rules.create("cheat-commands", true) end function __vc_create_hud_rules()