buttons.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. local function button_formspec(xp, pos, desc)
  2. local formspec =
  3. 'formspec_version[3]'..
  4. 'size[8,6]'..
  5. 'textarea[.5,.5;7,2;;;You can change the POS manually if you want, if you provide bad data everybody that punches the button will see you are an idiot, so test your stuff. :)]'..
  6. 'field[1,4;2,.5;pos;Return pos (x,y,z);'..pos..']'..
  7. 'field[5,4;2,.5;xp;XP;'..xp..']'..
  8. 'field[1,2;6,.5;desc;description;'..desc..']'..
  9. 'button_exit[5,5;2,.5;save;Submit]'
  10. return formspec
  11. end
  12. local button_box = {
  13. type = 'fixed',
  14. fixed = {
  15. {-.4375, -.5, -.4375, .4375, .3125, .4375},},}
  16. minetest.register_node('platformer:button', {
  17. description = 'XP Button',
  18. drawtype = 'mesh',
  19. mesh = 'platformer_button.obj',
  20. tiles = {'platformer_button.png'},
  21. paramtype = 'light',
  22. light_source = 10,
  23. selection_box = button_box,
  24. collision_box = button_box,
  25. groups = {breakable = 1, not_in_creative_inventory=1},
  26. after_place_node = function(pos, placer)
  27. if minetest.check_player_privs(placer:get_player_name(), {server = true}) then
  28. local meta = minetest.get_meta(pos)
  29. local saved_data = platformer.player_config[placer:get_player_name()] or {x=0,y=0,z=0}
  30. local saved_pos = minetest.pos_to_string(saved_data, 1)
  31. meta:set_string('owner', placer:get_player_name())
  32. meta:set_int('xp', 1)
  33. meta:set_string('pos', saved_pos)
  34. else
  35. minetest.chat_send_player(placer:get_player_name(), 'How did you get this? No matter, you are not allowed to place it!')
  36. minetest.remove_node(pos)
  37. end
  38. end,
  39. on_rightclick = function(pos, node, clicker, itemstack)
  40. local meta = minetest.get_meta(pos)
  41. local owner = meta:get_string('owner')
  42. local item = itemstack:get_name()
  43. local name = clicker:get_player_name()
  44. if item == 'tasks:configurator' then
  45. if owner == name and minetest.check_player_privs(clicker, {server = true}) then
  46. tasks.player_config[name] = pos
  47. local xp = meta:get_int('xp')
  48. local pos = meta:get_string('pos')
  49. local desc = meta:get_string('infotext')
  50. minetest.show_formspec(name, 'platformer:button', button_formspec(xp, pos, desc))
  51. end
  52. end
  53. end,
  54. on_use = function(itemstack, user, pointed_thing)
  55. local pos = minetest.get_pointed_thing_position(pointed_thing)
  56. local name = user:get_player_name()
  57. local new_pos = {x=pos.x, y=pos.y+2, z=pos.z}
  58. platformer.player_config[name] = new_pos
  59. minetest.chat_send_player(name, 'Spawn point saved, use again to reset.')
  60. end,
  61. on_punch = function(pos, node, puncher)
  62. local wield = puncher:get_wielded_item()
  63. local wield_name = wield:get_name()
  64. local name = puncher:get_player_name()
  65. local meta = minetest.get_meta(pos)
  66. if wield_name == 'tasks:configurator' then
  67. local owner = meta:get_string('owner')
  68. if owner == name or minetest.check_player_privs(clicker, {server = true}) then
  69. tasks.player_config[name] = pos
  70. local xp = meta:get_int('xp')
  71. local pos = meta:get_string('pos')
  72. local desc = meta:get_string('infotext')
  73. minetest.show_formspec(name, 'platformer:button', button_formspec(xp, pos, desc))
  74. end
  75. elseif wield_name ~= 'creative:tool_breaking' then
  76. local owner = meta:get_string('owner')
  77. if owner ~= '' then
  78. local spawn = meta:get_string('pos')
  79. local spawn_pos = minetest.string_to_pos(spawn)
  80. if spawn_pos ~= nil then
  81. local xp = meta:get_int('xp')
  82. puncher:set_pos(spawn_pos)
  83. lobby.give_xp(puncher, xp)
  84. else
  85. minetest.chat_send_player(puncher:get_player_name(), 'Looks like '..owner..' misconfigured this button, sorry. Use /spawn to get back to the lobby.')
  86. end
  87. end
  88. end
  89. end,
  90. })
  91. minetest.register_on_player_receive_fields(function(player, formname, fields)
  92. local name = player:get_player_name()
  93. if formname == 'platformer:button' then
  94. if fields.save then
  95. local pos = tasks.player_config[name]
  96. local meta = minetest.get_meta(pos)
  97. meta:set_int('xp', fields.xp)
  98. meta:set_string('pos', fields.pos)
  99. meta:set_string('infotext', fields.desc)
  100. end
  101. end
  102. end)