button.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. --[[
  2. Tube Library
  3. ============
  4. Copyright (C) 2017-2020 Joachim Stolberg
  5. AGPL v3
  6. See LICENSE.txt for more information
  7. button.lua:
  8. Example of a simple communication node, only sending messages to other nodes.
  9. ]]--
  10. -- Load support for I18n
  11. local S = tubelib.S
  12. local function switch_on(pos, node)
  13. if tubelib.data_not_corrupted(pos, true) then
  14. node.name = "tubelib:button_active"
  15. minetest.swap_node(pos, node)
  16. minetest.sound_play("button", {
  17. pos = pos,
  18. gain = 0.5,
  19. max_hear_distance = 5,
  20. })
  21. local meta = minetest.get_meta(pos)
  22. local own_num = meta:get_string("own_num")
  23. local numbers = meta:get_string("numbers")
  24. local cycle_time = meta:get_int("cycle_time")
  25. if cycle_time > 0 then -- button mode?
  26. minetest.get_node_timer(pos):start(cycle_time)
  27. end
  28. local placer_name = meta:get_string("placer_name")
  29. local clicker_name = nil
  30. if meta:get_string("public") == "false" then
  31. clicker_name = meta:get_string("clicker_name")
  32. end
  33. tubelib.send_message(numbers, placer_name, clicker_name, "on", own_num) -- <<=== tubelib
  34. end
  35. end
  36. local function switch_off(pos)
  37. if tubelib.data_not_corrupted(pos, true) then
  38. local node = minetest.get_node(pos)
  39. node.name = "tubelib:button"
  40. minetest.swap_node(pos, node)
  41. minetest.get_node_timer(pos):stop()
  42. minetest.sound_play("button", {
  43. pos = pos,
  44. gain = 0.5,
  45. max_hear_distance = 5,
  46. })
  47. local meta = minetest.get_meta(pos)
  48. local own_num = meta:get_string("own_num")
  49. local numbers = meta:get_string("numbers")
  50. local placer_name = meta:get_string("placer_name")
  51. local clicker_name = nil
  52. if meta:get_string("public") == "false" then
  53. clicker_name = meta:get_string("clicker_name")
  54. end
  55. tubelib.send_message(numbers, placer_name, clicker_name, "off", own_num) -- <<=== tubelib
  56. end
  57. end
  58. minetest.register_node("tubelib:button", {
  59. description = S("Tubelib Button/Switch"),
  60. tiles = {
  61. -- up, down, right, left, back, front
  62. 'tubelib_front.png',
  63. 'tubelib_button.png',
  64. 'tubelib_button.png',
  65. 'tubelib_button.png',
  66. 'tubelib_button.png',
  67. "tubelib_button_off.png",
  68. },
  69. after_place_node = function(pos, placer)
  70. local meta = minetest.get_meta(pos)
  71. local own_num = tubelib.add_node(pos, "tubelib:button")
  72. meta:set_string("own_num", own_num)
  73. meta:set_string("formspec", "size[7.5,6]"..
  74. "dropdown[0.2,0;3;type;"..S("switch,button 2s,button 4s,button 8s,button 16s")..";1]"..
  75. "field[0.5,2;7,1;numbers;"..S("Insert destination node number(s)")..";]" ..
  76. "checkbox[1,3;public;"..S("public")..";false]"..
  77. "button_exit[2,4;3,1;exit;"..S("Save").."]")
  78. meta:set_string("placer_name", placer:get_player_name())
  79. meta:set_string("public", "false")
  80. meta:set_int("cycle_time", 0)
  81. meta:set_string("infotext", S("Tubelib Button").." "..own_num)
  82. end,
  83. on_receive_fields = function(pos, formname, fields, player)
  84. local meta = minetest.get_meta(pos)
  85. if tubelib.check_numbers(fields.numbers) then -- <<=== tubelib
  86. meta:set_string("numbers", fields.numbers)
  87. local own_num = meta:get_string("own_num")
  88. meta:set_string("infotext", S("Tubelib Button").." "..own_num..", "..S("connected with block").." "..fields.numbers)
  89. else
  90. return
  91. end
  92. if fields.public then
  93. meta:set_string("public", fields.public)
  94. end
  95. local cycle_time = nil
  96. if fields.type == "switch" then
  97. cycle_time = 0
  98. elseif fields.type == "button 2s" then
  99. cycle_time = 2
  100. elseif fields.type == "button 4s" then
  101. cycle_time = 4
  102. elseif fields.type == "button 8s" then
  103. cycle_time = 8
  104. elseif fields.type == "button 16s" then
  105. cycle_time = 16
  106. end
  107. if cycle_time ~= nil then
  108. meta:set_int("cycle_time", cycle_time)
  109. end
  110. if fields.exit then
  111. meta:set_string("formspec", nil)
  112. end
  113. end,
  114. on_rightclick = function(pos, node, clicker)
  115. local meta = minetest.get_meta(pos)
  116. if meta:get_string("numbers") ~= "" and meta:get_string("numbers") ~= nil then
  117. meta:set_string("clicker_name", clicker:get_player_name())
  118. switch_on(pos, node)
  119. end
  120. end,
  121. on_rotate = screwdriver.disallow,
  122. paramtype = "light",
  123. sunlight_propagates = true,
  124. paramtype2 = "facedir",
  125. groups = {choppy=2, cracky=2, crumbly=2},
  126. is_ground_content = false,
  127. sounds = default.node_sound_wood_defaults(),
  128. })
  129. minetest.register_node("tubelib:button_active", {
  130. description = S("Tubelib Button/Switch"),
  131. tiles = {
  132. -- up, down, right, left, back, front
  133. 'tubelib_front.png',
  134. 'tubelib_button.png',
  135. 'tubelib_button.png',
  136. 'tubelib_button.png',
  137. 'tubelib_button.png',
  138. "tubelib_button_on.png",
  139. },
  140. on_rightclick = function(pos, node, clicker)
  141. local meta = minetest.get_meta(pos)
  142. meta:set_string("clicker_name", clicker:get_player_name())
  143. if meta:get_int("cycle_time") == nil or meta:get_int("cycle_time") == 0 then
  144. switch_off(pos, node)
  145. end
  146. end,
  147. on_timer = switch_off,
  148. on_rotate = screwdriver.disallow,
  149. paramtype = "light",
  150. sunlight_propagates = true,
  151. paramtype2 = "facedir",
  152. groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
  153. is_ground_content = false,
  154. sounds = default.node_sound_wood_defaults(),
  155. drop = "tubelib:button",
  156. })
  157. minetest.register_craft({
  158. output = "tubelib:button",
  159. recipe = {
  160. {"", "group:wood", ""},
  161. {"default:glass", "tubelib:wlanchip", ""},
  162. {"", "group:wood", ""},
  163. },
  164. })