chatcmds.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. --chatcmds.lua
  2. --Registers commands to modify the init and step code for LuaAutomation
  3. local function get_init_form(env, pname)
  4. local err = env.init_err or ""
  5. local code = env.init_code or ""
  6. local form = "size["..atlatc.CODE_FORM_SIZE.."]"
  7. .."style[code;font=mono]"
  8. .."button[0.0,0.2;2.5,1;run;Run Init Code]"
  9. .."button[2.5,0.2;2.5,1;cls;Clear S]"
  10. .."button[5.0,0.2;2.5,1;save;Save]"
  11. .."button[7.5,0.2;2.5,1;del;Delete Env.]"
  12. .."textarea[0.3,1.5;"..atlatc.CODE_FORM_SIZE..";code;Environment initialization code;"..minetest.formspec_escape(code).."]"
  13. .."label[0.0,9.7;"..err.."]"
  14. return form
  15. end
  16. core.register_chatcommand("env_setup", {
  17. params = "<environment name>",
  18. description = "Set up and modify AdvTrains LuaAutomation environment",
  19. privs = {atlatc=true},
  20. func = function(name, param)
  21. local env=atlatc.envs[param]
  22. if not env then return false,"Invalid environment name!" end
  23. minetest.show_formspec(name, "atlatc_envsetup_"..param, get_init_form(env, name))
  24. return true
  25. end,
  26. })
  27. core.register_chatcommand("env_create", {
  28. params = "<environment name>",
  29. description = "Create an AdvTrains LuaAutomation environment",
  30. privs = {atlatc=true},
  31. func = function(name, param)
  32. if not param or param=="" then return false, "Name required!" end
  33. if string.find(param, "[^a-zA-Z0-9-_]") then return false, "Invalid name (only common characters)" end
  34. if atlatc.envs[param] then return false, "Environment already exists!" end
  35. atlatc.envs[param] = atlatc.env_new(param)
  36. atlatc.envs[param].subscribers = {name}
  37. return true, "Created environment '"..param.."'. Use '/env_setup "..param.."' to define global initialization code, or start building LuaATC components!"
  38. end,
  39. })
  40. core.register_chatcommand("env_subscribe", {
  41. params = "<environment name>",
  42. description = "Subscribe to the log of an Advtrains LuaATC environment",
  43. privs = {atlatc=true},
  44. func = function(name, param)
  45. local env=atlatc.envs[param]
  46. if not env then return false,"Invalid environment name!" end
  47. for _,pname in ipairs(env.subscribers) do
  48. if pname==name then
  49. return false, "Already subscribed!"
  50. end
  51. end
  52. table.insert(env.subscribers, name)
  53. return true, "Subscribed to environment '"..param.."'."
  54. end,
  55. })
  56. core.register_chatcommand("env_unsubscribe", {
  57. params = "<environment name>",
  58. description = "Unubscribe to the log of an Advtrains LuaATC environment",
  59. privs = {atlatc=true},
  60. func = function(name, param)
  61. local env=atlatc.envs[param]
  62. if not env then return false,"Invalid environment name!" end
  63. for index,pname in ipairs(env.subscribers) do
  64. if pname==name then
  65. table.remove(env.subscribers, index)
  66. return true, "Successfully unsubscribed!"
  67. end
  68. end
  69. return false, "Not subscribed to environment '"..param.."'."
  70. end,
  71. })
  72. core.register_chatcommand("env_subscriptions", {
  73. params = "[environment name]",
  74. description = "List Advtrains LuaATC environments you are subscribed to (no parameters) or subscribers of an environment (giving an env name).",
  75. privs = {atlatc=true},
  76. func = function(name, param)
  77. if not param or param=="" then
  78. local none=true
  79. for envname, env in pairs(atlatc.envs) do
  80. for _,pname in ipairs(env.subscribers) do
  81. if pname==name then
  82. none=false
  83. minetest.chat_send_player(name, envname)
  84. end
  85. end
  86. end
  87. if none then
  88. return false, "Not subscribed to any!"
  89. end
  90. return true
  91. end
  92. local env=atlatc.envs[param]
  93. if not env then return false,"Invalid environment name!" end
  94. local none=true
  95. for index,pname in ipairs(env.subscribers) do
  96. none=false
  97. minetest.chat_send_player(name, pname)
  98. end
  99. if none then
  100. return false, "No subscribers!"
  101. end
  102. return true
  103. end,
  104. })
  105. minetest.register_on_player_receive_fields(function(player, formname, fields)
  106. local pname=player:get_player_name()
  107. if not minetest.check_player_privs(pname, {atlatc=true}) then return end
  108. local envname=string.match(formname, "^atlatc_delconfirm_(.+)$")
  109. if envname and fields.sure=="YES" then
  110. atlatc.envs[envname]=nil
  111. minetest.chat_send_player(pname, "Environment deleted!")
  112. return
  113. end
  114. envname=string.match(formname, "^atlatc_envsetup_(.+)$")
  115. if not envname then return end
  116. local env=atlatc.envs[envname]
  117. if not env then return end
  118. if fields.del then
  119. minetest.show_formspec(pname, "atlatc_delconfirm_"..envname, "field[sure;"..minetest.formspec_escape("SURE TO DELETE ENVIRONMENT "..envname.."? Type YES (all uppercase) to continue or just quit form to cancel.")..";]")
  120. return
  121. end
  122. env.init_err=nil
  123. if fields.code then
  124. env.init_code=fields.code
  125. end
  126. if fields.run then
  127. env:run_initcode()
  128. minetest.show_formspec(pname, formname, get_init_form(env, pname))
  129. end
  130. end)