From 1e4156d0a4e42b519ca2f64f4146c7a7faec49a3 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 17 Feb 2021 19:10:40 +0100 Subject: LuaATC: Improve error/print logging, log only to subscribed players --- advtrains_luaautomation/chatcmds.lua | 68 +++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'advtrains_luaautomation/chatcmds.lua') diff --git a/advtrains_luaautomation/chatcmds.lua b/advtrains_luaautomation/chatcmds.lua index 2d0c69d..468698b 100644 --- a/advtrains_luaautomation/chatcmds.lua +++ b/advtrains_luaautomation/chatcmds.lua @@ -43,12 +43,78 @@ core.register_chatcommand("env_create", { privs = {atlatc=true}, func = function(name, param) if not param or param=="" then return false, "Name required!" end + if string.find(param, "[^a-zA-Z0-9-_]") then return false, "Invalid name (only common characters)" end if atlatc.envs[param] then return false, "Environment already exists!" end atlatc.envs[param] = atlatc.env_new(param) + atlatc.envs[param].subscribers = {name} return true, "Created environment '"..param.."'. Use '/env_setup "..param.."' to define global initialization code, or start building LuaATC components!" end, }) - +core.register_chatcommand("env_subscribe", { + params = "", + description = "Subscribe to the log of an Advtrains LuaATC environment", + privs = {atlatc=true}, + func = function(name, param) + local env=atlatc.envs[param] + if not env then return false,"Invalid environment name!" end + for _,pname in ipairs(env.subscribers) do + if pname==name then + return false, "Already subscribed!" + end + end + table.insert(env.subscribers, name) + return true, "Subscribed to environment '"..param.."'." + end, +}) +core.register_chatcommand("env_unsubscribe", { + params = "", + description = "Unubscribe to the log of an Advtrains LuaATC environment", + privs = {atlatc=true}, + func = function(name, param) + local env=atlatc.envs[param] + if not env then return false,"Invalid environment name!" end + for index,pname in ipairs(env.subscribers) do + if pname==name then + table.remove(env.subscribers, index) + return true, "Successfully unsubscribed!" + end + end + return false, "Not subscribed to environment '"..param.."'." + end, +}) +core.register_chatcommand("env_subscriptions", { + params = "[environment name]", + description = "List Advtrains LuaATC environments you are subscribed to (no parameters) or subscribers of an environment (giving an env name).", + privs = {atlatc=true}, + func = function(name, param) + if not param or param=="" then + local none=true + for envname, env in pairs(atlatc.envs) do + for _,pname in ipairs(env.subscribers) do + if pname==name then + none=false + minetest.chat_send_player(name, envname) + end + end + end + if none then + return false, "Not subscribed to any!" + end + return true + end + local env=atlatc.envs[param] + if not env then return false,"Invalid environment name!" end + local none=true + for index,pname in ipairs(env.subscribers) do + none=false + minetest.chat_send_player(name, pname) + end + if none then + return false, "No subscribers!" + end + return true + end, +}) minetest.register_on_player_receive_fields(function(player, formname, fields) -- cgit v1.2.3