formspec.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. -- Localize for performance.
  2. local vector_distance = vector.distance
  3. local vector_round = vector.round
  4. local vector_add = vector.add
  5. local vector_equals = vector.equals
  6. local math_random = math.random
  7. local CITYBLOCK_DELAY_TIME = city_block.CITYBLOCK_DELAY_TIME
  8. local time_active = city_block.time_active
  9. function city_block.create_formspec(pos, pname, blockdata)
  10. local meta = minetest.get_meta(pos)
  11. local inv = meta:get_inventory()
  12. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  13. -- Create inventory if needed.
  14. if inv:get_size("config") == 0 then
  15. inv:set_size("config", 1)
  16. end
  17. local pvp = "false"
  18. if blockdata.pvp_arena then
  19. pvp = "true"
  20. end
  21. local hud = "false"
  22. if blockdata.hud_beacon then
  23. hud = "true"
  24. end
  25. local formspec = "size[8.0,4.5]" ..
  26. default.gui_bg ..
  27. default.gui_bg_img ..
  28. default.gui_slots ..
  29. "real_coordinates[true]" ..
  30. "container[2.5,0]" ..
  31. "container[0,0]" ..
  32. "label[0.3,0.5;Enter city/area region name:]" ..
  33. "field[0.30,0.75;3.5,0.8;CITYNAME;;]" ..
  34. "button_exit[3.9,0.75;1.5,0.4;OK;Confirm]" ..
  35. "button_exit[3.9,1.16;1.5,0.4;CANCEL;Abort]" ..
  36. "field_close_on_enter[CITYNAME;true]" ..
  37. "container_end[]" ..
  38. "checkbox[0.3,3.1;pvp_arena;Mark Dueling Arena;" .. pvp .. "]" ..
  39. "checkbox[0.3,3.5;hud_beacon;Signal Nearby Keys;" .. hud .. "]" ..
  40. "container[0.4,0]" ..
  41. "list[nodemeta:" .. spos .. ";config;3.95,2.9;1,1;]" ..
  42. "image[3.95,2.9;1,1;configurator_inv.png]" ..
  43. "container_end[]" ..
  44. "container_end[]" ..
  45. "container[0.4,4.6]" ..
  46. "list[current_player;main;0,0;8,1;]" ..
  47. default.get_hotbar_bg(0, 0, true) ..
  48. "container_end[]"
  49. return formspec
  50. end
  51. local function check_cityname(cityname)
  52. return not string.match(cityname, "[^%a%s]")
  53. end
  54. function city_block.on_receive_fields(player, formname, fields)
  55. if formname ~= "city_block:main" then
  56. return
  57. end
  58. if not player or not player:is_player() then
  59. return
  60. end
  61. local pname = player:get_player_name()
  62. local pos = city_block.formspecs[pname]
  63. -- Context should have been created in 'on_rightclick'. CSM protection.
  64. if not pos then
  65. return true
  66. end
  67. local meta = minetest.get_meta(pos)
  68. local owner = meta:get_string("owner")
  69. -- Form sender must be owner.
  70. if pname ~= owner then
  71. return true
  72. end
  73. if fields.key_enter_field == "CITYNAME" or fields.OK then
  74. local area_name = (fields.CITYNAME or ""):trim()
  75. area_name = area_name:gsub("%s+", " ")
  76. -- Ensure city name is valid.
  77. local is_valid = true
  78. -- Empty area name means erase.
  79. --[[
  80. if #area_name == 0 then
  81. is_valid = false
  82. end
  83. --]]
  84. if #area_name > 20 then
  85. is_valid = false
  86. end
  87. if not check_cityname(area_name) then
  88. is_valid = false
  89. end
  90. if anticurse.check(pname, area_name, "foul") then
  91. is_valid = false
  92. elseif anticurse.check(pname, area_name, "curse") then
  93. is_valid = false
  94. end
  95. if not is_valid then
  96. minetest.chat_send_player(pname, "# Server: Region name not valid.")
  97. return
  98. end
  99. local block = city_block.get_block(pos)
  100. -- Ensure we got the city block data.
  101. if not block then
  102. return
  103. end
  104. -- Write out.
  105. meta:set_string("cityname", area_name)
  106. meta:set_string("infotext", city_block.get_infotext(pos))
  107. if #area_name > 0 then
  108. block.area_name = area_name
  109. else
  110. block.area_name = nil
  111. end
  112. city_block:save()
  113. ---[[
  114. elseif fields.pvp_arena == "true" then
  115. local block = city_block.get_block(pos)
  116. if block then
  117. minetest.chat_send_player(pname, "# Server: Enabled dueling arena.")
  118. block.pvp_arena = true
  119. meta:set_string("infotext", city_block.get_infotext(pos))
  120. city_block:save()
  121. end
  122. elseif fields.pvp_arena == "false" then
  123. local block = city_block.get_block(pos)
  124. if block then
  125. minetest.chat_send_player(pname, "# Server: Disabled dueling arena.")
  126. block.pvp_arena = nil
  127. meta:set_string("infotext", city_block.get_infotext(pos))
  128. city_block:save()
  129. end
  130. --]]
  131. ---[[
  132. elseif fields.hud_beacon == "true" then
  133. local block = city_block.get_block(pos)
  134. if block then
  135. minetest.chat_send_player(pname, "# Server: Activated KEY signal.")
  136. block.hud_beacon = true
  137. city_block:save()
  138. end
  139. elseif fields.hud_beacon == "false" then
  140. local block = city_block.get_block(pos)
  141. if block then
  142. minetest.chat_send_player(pname, "# Server: Disabled KEY signal.")
  143. block.hud_beacon = nil
  144. city_block:save()
  145. end
  146. --]]
  147. end
  148. return true
  149. end