init.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. minetest.register_chatcommand("say", {
  2. params = "<text>",
  3. description = "Say <text> as the server",
  4. privs = {server=true},
  5. func = function(name, param)
  6. minetest.chat_send_all(name .. ": " .. param)
  7. end
  8. })
  9. minetest.register_chatcommand("tell", {
  10. params = "<name> <text>",
  11. description = "Say <text> to <name> privately",
  12. func = function(name, param)
  13. local found, _, target, message = param:find("^([^%s]+)%s+(.*)$")
  14. if found == nil then
  15. minetest.chat_send_player(name, "Invalid usage: " .. param)
  16. return
  17. end
  18. if not minetest.get_player_by_name(target) then
  19. minetest.chat_send_player(name, "Invalid target: " .. target)
  20. end
  21. minetest.chat_send_player(target, name .. " whispers: " .. message, false)
  22. end
  23. })
  24. minetest.register_chatcommand("hp", {
  25. params = "<name> <value>",
  26. description = "Set health of <name> to <value> hitpoints",
  27. privs = {ban=true},
  28. func = function(name, param)
  29. local found, _, target, value = param:find("^([^%s]+)%s+(%d+)$")
  30. if found == nil then
  31. minetest.chat_send_player(name, "Invalid usage: " .. param)
  32. return
  33. end
  34. local player = minetest.get_player_by_name(target)
  35. if player then
  36. player:set_hp(value)
  37. else
  38. minetest.chat_send_player(name, "Invalid target: " .. target)
  39. end
  40. end
  41. })
  42. local function initialize_data(meta)
  43. local commands = meta:get_string("commands")
  44. meta:set_string("formspec",
  45. "invsize[9,5;]" ..
  46. "textarea[0.5,0.5;8.5,4;commands;Commands;"..commands.."]" ..
  47. "label[1,3.8;@nearest, @farthest, and @random are replaced by the respective player names]" ..
  48. "button_exit[3.3,4.5;2,1;submit;Submit]")
  49. local owner = meta:get_string("owner")
  50. if owner == "" then
  51. owner = "not owned"
  52. else
  53. owner = "owned by " .. owner
  54. end
  55. meta:set_string("infotext", "Command Block\n" ..
  56. "(" .. owner .. ")\n" ..
  57. "Commands: "..commands)
  58. end
  59. local function construct(pos)
  60. local meta = minetest.get_meta(pos)
  61. meta:set_string("commands", "tell @nearest Commandblock unconfigured")
  62. meta:set_string("owner", "")
  63. initialize_data(meta)
  64. end
  65. local function after_place(pos, placer)
  66. if placer then
  67. local meta = minetest.get_meta(pos)
  68. meta:set_string("owner", placer:get_player_name())
  69. initialize_data(meta)
  70. end
  71. end
  72. local function receive_fields(pos, formname, fields, sender)
  73. if not fields.submit then
  74. return
  75. end
  76. local meta = minetest.get_meta(pos)
  77. local owner = meta:get_string("owner")
  78. if owner ~= "" and sender:get_player_name() ~= owner then
  79. return
  80. end
  81. meta:set_string("commands", fields.commands)
  82. initialize_data(meta)
  83. end
  84. local function resolve_commands(commands, pos)
  85. local nearest, farthest = nil, nil
  86. local min_distance, max_distance = math.huge, -1
  87. local players = minetest.get_connected_players()
  88. for index, player in pairs(players) do
  89. local distance = vector.distance(pos, player:getpos())
  90. if distance < min_distance then
  91. min_distance = distance
  92. nearest = player:get_player_name()
  93. end
  94. if distance > max_distance then
  95. max_distance = distance
  96. farthest = player:get_player_name()
  97. end
  98. end
  99. local random = players[math.random(#players)]:get_player_name()
  100. commands = commands:gsub("@nearest", nearest)
  101. commands = commands:gsub("@farthest", farthest)
  102. commands = commands:gsub("@random", random)
  103. return commands
  104. end
  105. local function commandblock_action_on(pos, node)
  106. if node.name ~= "mesecons_commandblock:commandblock_off" then
  107. return
  108. end
  109. minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_on"})
  110. local meta = minetest.get_meta(pos)
  111. local owner = meta:get_string("owner")
  112. if owner == "" then
  113. return
  114. end
  115. local commands = resolve_commands(meta:get_string("commands"), pos)
  116. for _, command in pairs(commands:split("\n")) do
  117. local pos = command:find(" ")
  118. local cmd, param = command, ""
  119. if pos then
  120. cmd = command:sub(1, pos - 1)
  121. param = command:sub(pos + 1)
  122. end
  123. local cmddef = minetest.chatcommands[cmd]
  124. if not cmddef then
  125. minetest.chat_send_player(owner, "The command "..cmd.." does not exist")
  126. return
  127. end
  128. local has_privs, missing_privs = minetest.check_player_privs(owner, cmddef.privs)
  129. if not has_privs then
  130. minetest.chat_send_player(owner, "You don't have permission "
  131. .."to run "..cmd
  132. .." (missing privileges: "
  133. ..table.concat(missing_privs, ", ")..")")
  134. return
  135. end
  136. cmddef.func(owner, param)
  137. end
  138. end
  139. local function commandblock_action_off(pos, node)
  140. if node.name == "mesecons_commandblock:commandblock_on" then
  141. minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"})
  142. end
  143. end
  144. local function can_dig(pos, player)
  145. local meta = minetest.get_meta(pos)
  146. local owner = meta:get_string("owner")
  147. return owner == "" or owner == player:get_player_name()
  148. end
  149. minetest.register_node("mesecons_commandblock:commandblock_off", {
  150. description = "Command Block",
  151. tiles = {"jeija_commandblock_off.png"},
  152. inventory_image = minetest.inventorycube("jeija_commandblock_off.png"),
  153. groups = {cracky=2, mesecon_effector_off=1},
  154. on_construct = construct,
  155. after_place_node = after_place,
  156. on_receive_fields = receive_fields,
  157. can_dig = can_dig,
  158. sounds = default.node_sound_stone_defaults(),
  159. mesecons = {effector = {
  160. action_on = commandblock_action_on
  161. }}
  162. })
  163. minetest.register_node("mesecons_commandblock:commandblock_on", {
  164. tiles = {"jeija_commandblock_on.png"},
  165. groups = {cracky=2, mesecon_effector_on=1, not_in_creative_inventory=1},
  166. light_source = 10,
  167. drop = "mesecons_commandblock:commandblock_off",
  168. on_construct = construct,
  169. after_place_node = after_place,
  170. on_receive_fields = receive_fields,
  171. can_dig = can_dig,
  172. sounds = default.node_sound_stone_defaults(),
  173. mesecons = {effector = {
  174. action_off = commandblock_action_off
  175. }}
  176. })