aboutsummaryrefslogtreecommitdiff
path: root/advtrains_luaautomation/chatcmds.lua
blob: 76910133eac8cf63351cfd0169e6b0342de7fbed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
generated by cgit v1.2.3 (git 2.39.1) at 2026-10-21 22:24:07 +0000
 


span class="hl opt">)
		if not param or param=="" then return false, S("Name required!") end
		if string.find(param, "[^a-zA-Z0-9-_]") then return false, S("Invalid name (only common characters)") end
		if atlatc.envs[param] then return false, S("Environment already exists!") end
		atlatc.envs[param] = atlatc.env_new(param)
		atlatc.envs[param].subscribers = {name}
		return true, S("Created environment '@1'. Use '/env_setup @2' to define global initialization code, or start building LuaATC components!", param, param)
	end,
})
core.register_chatcommand("env_subscribe", {
	params = "<environment name>",
	description = S("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,S("Invalid environment name!") end
		for _,pname in ipairs(env.subscribers) do
			if pname==name then
				return false, S("Already subscribed!")
			end
		end
		table.insert(env.subscribers, name)
		return true, S("Subscribed to environment '@1'.", param)
	end,
})
core.register_chatcommand("env_unsubscribe", {
	params = "<environment name>",
	description = S("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,S("Invalid environment name!") end
		for index,pname in ipairs(env.subscribers) do
			if pname==name then
				table.remove(env.subscribers, index)
				return true, S("Successfully unsubscribed!")
			end
		end
		return false, S("Not subscribed!")
	end,	
})
core.register_chatcommand("env_subscriptions", {
	params = "[environment name]",
	description = S("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, S("Not subscribed to any!")
			end
			return true
		end
		local env=atlatc.envs[param]
		if not env then return false,S("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, S("No subscribers!")
		end
		return true
	end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
	
	local pname=player:get_player_name()
	if not minetest.check_player_privs(pname, {atlatc=true}) then return end
	
	local envname=string.match(formname, "^atlatc_delconfirm_(.+)$")
	if envname and fields.sure=="YES" then
		atlatc.envs[envname]=nil
		minetest.chat_send_player(pname, S("Environment deleted!"))
		return
	end
	
	envname=string.match(formname, "^atlatc_envsetup_(.+)$")
	if not envname then return end
	
	local env=atlatc.envs[envname]
	if not env then return end
	
	if fields.del then
		minetest.show_formspec(pname, "atlatc_delconfirm_"..envname, "field[sure;"
		..minetest.formspec_escape(S("SURE TO DELETE ENVIRONMENT @1? Type YES (all uppercase) to continue or just quit form to cancel.", envname))..";]")
		return
	end
	
	env.init_err=nil
	if fields.code then
		env.init_code=fields.code
	end
	if fields.run then
		env:run_initcode()
		minetest.show_formspec(pname, formname, get_init_form(env, pname))
	end
end)