bookmark.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. jumpdrive.write_to_book = function(pos, sender)
  2. local meta = minetest.get_meta(pos)
  3. local inv = meta:get_inventory()
  4. if inv:contains_item("main", {name="default:book", count=1}) then
  5. local stack = inv:remove_item("main", {name="default:book", count=1})
  6. local new_stack = ItemStack("default:book_written")
  7. local data = {}
  8. data.owner = sender:get_player_name()
  9. data.title = "Jumpdrive coordinates"
  10. data.description = "Jumpdrive coordiates"
  11. data.text = minetest.serialize(jumpdrive.get_meta_pos(pos))
  12. data.page = 1
  13. data.page_max = 1
  14. new_stack:get_meta():from_table({ fields = data })
  15. if inv:room_for_item("main", new_stack) then
  16. -- put written book back
  17. inv:add_item("main", new_stack)
  18. else
  19. -- put back old stack
  20. inv:add_item("main", stack)
  21. end
  22. end
  23. end
  24. jumpdrive.read_from_book = function(pos)
  25. local meta = minetest.get_meta(pos)
  26. local inv = meta:get_inventory()
  27. local player_name = meta:get_string("owner")
  28. if inv:contains_item("main", {name="default:book_written", count=1}) then
  29. local stack = inv:remove_item("main", {name="default:book_written", count=1})
  30. local stackMeta = stack:get_meta()
  31. local text = stackMeta:get_string("text")
  32. local data = minetest.deserialize(text)
  33. if data == nil then
  34. -- put book back, it may contain other information
  35. inv:add_item("main", stack)
  36. -- alert player
  37. if nil ~= player_name then
  38. minetest.chat_send_player(player_name, "Invalid data")
  39. end
  40. return
  41. end
  42. local x = tonumber(data.x)
  43. local y = tonumber(data.y)
  44. local z = tonumber(data.z)
  45. if x == nil or y == nil or z == nil then
  46. -- put book back, it may contain other information
  47. inv:add_item("main", stack)
  48. -- alert player
  49. if nil ~= player_name then
  50. minetest.chat_send_player(player_name, "Invalid coordinates")
  51. end
  52. return
  53. end
  54. meta:set_int("x", jumpdrive.sanitize_coord(x))
  55. meta:set_int("y", jumpdrive.sanitize_coord(y))
  56. meta:set_int("z", jumpdrive.sanitize_coord(z))
  57. -- put book back
  58. inv:add_item("main", stack)
  59. elseif inv:contains_item("main", {name="missions:wand_position", count=1}) then
  60. local stack = inv:remove_item("main", {name="missions:wand_position", count=1})
  61. local stackMeta = stack:get_meta()
  62. local text = stackMeta:get_string("pos")
  63. local target_pos = minetest.string_to_pos(text)
  64. if nil == target_pos then
  65. -- put wand back, I don't see a way to corrupt a wand atm
  66. inv:add_item("main", stack)
  67. return
  68. end
  69. local x = target_pos.x
  70. local y = target_pos.y
  71. local z = target_pos.z
  72. if x == nil or y == nil or z == nil then
  73. -- put wand back, I don't see a way to corrupt a wand atm
  74. inv:add_item("main", stack)
  75. return
  76. end
  77. meta:set_int("x", jumpdrive.sanitize_coord(x))
  78. meta:set_int("y", jumpdrive.sanitize_coord(y))
  79. meta:set_int("z", jumpdrive.sanitize_coord(z))
  80. -- put wand back
  81. inv:add_item("main", stack)
  82. end
  83. end