init.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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(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. minetest.chat_send_player(target, name .. " whispers: " .. message)
  19. end
  20. })
  21. local initialize_data = function(meta, player, command, param)
  22. meta:set_string("formspec",
  23. "invsize[9,6;]" ..
  24. "field[1,1;7.5,1;player;Player;" .. player .. "]" ..
  25. "button[1.3,2;2,1;nearest;Nearest]" ..
  26. "button[3.3,2;2,1;farthest;Farthest]" ..
  27. "button[5.3,2;2,1;random;Random]" ..
  28. "field[1,4;2,1;command;Command;" .. command .. "]" ..
  29. "field[3,4;5.5,1;param;Parameter;" .. param .. "]" ..
  30. "button_exit[3.3,5;2,1;submit;Submit]")
  31. local owner = meta:get_string("owner")
  32. if owner == "" then
  33. owner = "not owned"
  34. else
  35. owner = "owned by " .. owner
  36. end
  37. meta:set_string("infotext", "Command Block\n" ..
  38. "(" .. owner .. ")\n" ..
  39. "Command: /" .. command .. " " .. param)
  40. end
  41. local construct = function(pos)
  42. local meta = minetest.env:get_meta(pos)
  43. meta:set_string("player", "@nearest")
  44. meta:set_string("command", "time")
  45. meta:set_string("param", "7000")
  46. meta:set_string("owner", "")
  47. initialize_data(meta, "@nearest", "time", "7000")
  48. end
  49. local after_place = function(pos, placer)
  50. if placer then
  51. local meta = minetest.env:get_meta(pos)
  52. meta:set_string("owner", placer:get_player_name())
  53. initialize_data(meta, "@nearest", "time", "7000")
  54. end
  55. end
  56. local receive_fields = function(pos, formname, fields, sender)
  57. local meta = minetest.env:get_meta(pos)
  58. if fields.nearest then
  59. initialize_data(meta, "@nearest", fields.command, fields.param)
  60. elseif fields.farthest then
  61. initialize_data(meta, "@farthest", fields.command, fields.param)
  62. elseif fields.random then
  63. initialize_data(meta, "@random", fields.command, fields.param)
  64. else --fields.submit or pressed enter
  65. meta:set_string("player", fields.player)
  66. meta:set_string("command", fields.command)
  67. meta:set_string("param", fields.param)
  68. initialize_data(meta, fields.player, fields.command, fields.param)
  69. end
  70. end
  71. local resolve_player = function(name, pos)
  72. local get_distance = function(pos1, pos2)
  73. return math.sqrt((pos1.x - pos2.x) ^ 2 + (pos1.y - pos2.y) ^ 2 + (pos1.z - pos2.z) ^ 2)
  74. end
  75. if name == "@nearest" then
  76. local min_distance = math.huge
  77. for index, player in ipairs(minetest.get_connected_players()) do
  78. local distance = get_distance(pos, player:getpos())
  79. if distance < min_distance then
  80. min_distance = distance
  81. name = player:get_player_name()
  82. end
  83. end
  84. elseif name == "@farthest" then
  85. local max_distance = -1
  86. for index, player in ipairs(minetest.get_connected_players()) do
  87. local distance = get_distance(pos, player:getpos())
  88. if distance > max_distance then
  89. max_distance = distance
  90. name = player:get_player_name()
  91. end
  92. end
  93. elseif name == "@random" then
  94. local players = minetest.get_connected_players()
  95. local player = players[math.random(#players)]
  96. name = player:get_player_name()
  97. end
  98. return name
  99. end
  100. local commandblock_action_on = function(pos, node)
  101. if node.name ~= "mesecons_commandblock:commandblock_off" then
  102. return
  103. end
  104. mesecon:swap_node(pos, "mesecons_commandblock:commandblock_on")
  105. local meta = minetest.env:get_meta(pos)
  106. local command = minetest.chatcommands[meta:get_string("command")]
  107. if command == nil then
  108. return
  109. end
  110. local owner = meta:get_string("owner")
  111. if owner == "" then
  112. return
  113. end
  114. local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs)
  115. if not has_privs then
  116. minetest.chat_send_player(owner, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")")
  117. return
  118. end
  119. local player = resolve_player(meta:get_string("player"), pos)
  120. command.func(player, meta:get_string("param"))
  121. end
  122. local commandblock_action_off = function(pos, node)
  123. if node.name == "mesecons_commandblock:commandblock_on" then
  124. mesecon:swap_node(pos, "mesecons_commandblock:commandblock_off")
  125. end
  126. end
  127. minetest.register_node("mesecons_commandblock:commandblock_off", {
  128. description = "Command Block",
  129. tiles = {"jeija_commandblock_off.png"},
  130. inventory_image = minetest.inventorycube("jeija_commandblock_off.png"),
  131. groups = {cracky=2, mesecon_effector_off=1},
  132. on_construct = construct,
  133. after_place_node = after_place,
  134. on_receive_fields = receive_fields,
  135. can_dig = function(pos,player)
  136. local owner = minetest.env:get_meta(pos):get_string("owner")
  137. return owner == "" or owner == player:get_player_name()
  138. end,
  139. mesecons = {effector = {
  140. action_on = commandblock_action_on
  141. }}
  142. })
  143. minetest.register_node("mesecons_commandblock:commandblock_on", {
  144. tiles = {"jeija_commandblock_on.png"},
  145. groups = {cracky=2, mesecon_effector_on=1, not_in_creative_inventory=1},
  146. light_source = 10,
  147. drop = "mesecons_commandblock:commandblock_off",
  148. on_construct = construct,
  149. after_place_node = after_place,
  150. on_receive_fields = receive_fields,
  151. can_dig = function(pos,player)
  152. local owner = minetest.env:get_meta(pos):get_string("owner")
  153. return owner == "" or owner == player:get_player_name()
  154. end,
  155. mesecons = {effector = {
  156. action_off = commandblock_action_off
  157. }}
  158. })