aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author1F616EMO <root@1f616emo.xyz>2026-04-04 11:47:31 +0800
committerorwell <orwell@bleipb.de>2026-07-08 22:12:22 +0200
commitd251015ce1f19f5a276f0f298a4b6b7f632557f4 (patch)
treeac3267d48c59aa069b0bfdf0f4b771200463e9c1
parent38cc24e968d3671cba63918d69be59afc26b1052 (diff)
Introduce default LuaATC environment on place
Adds the command /env_default to set per-player default environment to be used on placing new LuaATC components. This makes component placement more predictable, and should any error be triggered, it won't go to the wrong player.
-rw-r--r--advtrains_luaautomation/active_common.lua38
-rw-r--r--advtrains_luaautomation/chatcmds.lua28
2 files changed, 60 insertions, 6 deletions
diff --git a/advtrains_luaautomation/active_common.lua b/advtrains_luaautomation/active_common.lua
index 63ccfcd..311ec04 100644
--- a/advtrains_luaautomation/active_common.lua
+++ b/advtrains_luaautomation/active_common.lua
@@ -12,16 +12,42 @@ function ac.save()
return {nodes = ac.nodes}
end
+function ac.get_player_default_environment(player)
+ local meta = player:get_meta()
+ local env = meta:get_string("atlatc_default_env")
+
+ if not atlatc.envs[env] then
+ -- Set the first key for the user as default environment
+ for envname, _ in pairs(atlatc.envs) do
+ meta:set_string("atlatc_default_env", envname)
+ return envname
+ end
+
+ -- We don't have any environments
+ return nil
+ end
+
+ return env
+end
+function ac.set_player_default_environment(player, env)
+ if atlatc.envs[env] then
+ local meta = player:get_meta()
+ meta:set_string("atlatc_default_env", env)
+ return true
+ end
+ return false
+end
+
function ac.after_place_node(pos, player)
+ local env = ac.get_player_default_environment(player)
+ if env then
+ local ph = minetest.pos_to_string(pos)
+ ac.nodes[ph] = { env = env }
+ end
+
local meta=minetest.get_meta(pos)
meta:set_string("formspec", ac.getform(pos, meta))
meta:set_string("infotext", S("Unconfigured LuaATC component"))
- local ph=minetest.pos_to_string(pos)
- --just get first available key!
- for en,_ in pairs(atlatc.envs) do
- ac.nodes[ph]={env=en}
- return
- end
end
function ac.getform(pos, meta_p)
local meta = meta_p or minetest.get_meta(pos)
diff --git a/advtrains_luaautomation/chatcmds.lua b/advtrains_luaautomation/chatcmds.lua
index 7691013..5f0da13 100644
--- a/advtrains_luaautomation/chatcmds.lua
+++ b/advtrains_luaautomation/chatcmds.lua
@@ -110,6 +110,34 @@ core.register_chatcommand("env_subscriptions", {
end,
})
+core.register_chatcommand("env_default", {
+ params = "[environment name]",
+ description = S("Set or get your default LuaATC environment."),
+ privs = {atlatc=true},
+ func = function(name, param)
+ local player = minetest.get_player_by_name(name)
+ if not player then
+ return false, S("You're not online!")
+ end
+
+ param = string.trim(param)
+ if param == "" then
+ local env = atlatc.active.get_player_default_environment(player)
+ if env then
+ return true, S("Your default environment is '@1'.", env)
+ else
+ return false, S("No avaliable environemnt! Create one with /env_create.")
+ end
+ end
+
+ if atlatc.active.set_player_default_environment(player, param) then
+ return true, S("Default environment set to '@1'.", param)
+ else
+ return false, S("Invalid environment name!")
+ end
+ end,
+})
+
minetest.register_on_player_receive_fields(function(player, formname, fields)
local pname=player:get_player_name()