init.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. local S = minetest.get_translator("homedecor_books")
  2. local bookcolors = {
  3. { "red", 0xffd26466 },
  4. { "green", 0xff62aa66 },
  5. { "blue", 0xff8686d7 },
  6. { "violet", 0xff9c65a7 },
  7. { "grey", 0xff757579 },
  8. { "brown", 0xff896958 }
  9. }
  10. local color_locale = {
  11. red = S("red") ,
  12. green = S("green"),
  13. blue = S("blue"),
  14. violet = S("violet"),
  15. grey = S("grey"),
  16. brown = S("brown"),
  17. }
  18. local BOOK_FORMNAME = "homedecor:book_form"
  19. local player_current_book = { }
  20. for _, c in ipairs(bookcolors) do
  21. local color, hue = unpack(c)
  22. local function book_dig(pos, node, digger)
  23. if minetest.is_protected(pos, digger:get_player_name()) then return end
  24. local meta = minetest.get_meta(pos)
  25. local data = minetest.serialize({
  26. title = meta:get_string("title") or "",
  27. text = meta:get_string("text") or "",
  28. owner = meta:get_string("owner") or "",
  29. _recover = meta:get_string("_recover") or "",
  30. })
  31. local stack = ItemStack({
  32. name = "homedecor:book_"..color,
  33. metadata = data,
  34. })
  35. stack = digger:get_inventory():add_item("main", stack)
  36. if not stack:is_empty() then
  37. minetest.item_drop(stack, digger, pos)
  38. end
  39. minetest.remove_node(pos)
  40. end
  41. homedecor.register("book_"..color, {
  42. description = S("Writable Book (@1)", color_locale[color]),
  43. mesh = "homedecor_book.obj",
  44. tiles = {
  45. { name = "homedecor_book_cover.png", color = hue },
  46. { name = "homedecor_book_edges.png", color = "white" }
  47. },
  48. overlay_tiles = {
  49. { name = "homedecor_book_cover_trim.png", color = "white" },
  50. ""
  51. },
  52. groups = { snappy=3, oddly_breakable_by_hand=3, book=1 },
  53. walkable = false,
  54. stack_max = 1,
  55. on_punch = function(pos, node, puncher, pointed_thing)
  56. local fdir = node.param2
  57. minetest.swap_node(pos, { name = "homedecor:book_open_"..color, param2 = fdir })
  58. end,
  59. on_place = function(itemstack, placer, pointed_thing)
  60. local plname = placer:get_player_name()
  61. local pos = pointed_thing.under
  62. local node = minetest.get_node_or_nil(pos)
  63. local def = node and minetest.registered_nodes[node.name]
  64. if not def or not def.buildable_to then
  65. pos = pointed_thing.above
  66. node = minetest.get_node_or_nil(pos)
  67. def = node and minetest.registered_nodes[node.name]
  68. if not def or not def.buildable_to then return itemstack end
  69. end
  70. if minetest.is_protected(pos, plname) then return itemstack end
  71. local fdir = minetest.dir_to_facedir(placer:get_look_dir())
  72. minetest.set_node(pos, {
  73. name = "homedecor:book_"..color,
  74. param2 = fdir,
  75. })
  76. local text = itemstack:get_metadata() or ""
  77. local meta = minetest.get_meta(pos)
  78. local data = minetest.deserialize(text) or {}
  79. if type(data) ~= "table" then
  80. data = {}
  81. -- Store raw metadata in case some data is lost by the
  82. -- transition to the new meta format, so it is not lost
  83. -- and can be recovered if needed.
  84. meta:set_string("_recover", text)
  85. end
  86. meta:set_string("title", data.title or "")
  87. meta:set_string("text", data.text or "")
  88. meta:set_string("owner", data.owner or "")
  89. if data.title and data.title ~= "" then
  90. meta:set_string("infotext", data.title)
  91. end
  92. if not creative.is_enabled_for(plname) then
  93. itemstack:take_item()
  94. end
  95. return itemstack
  96. end,
  97. on_dig = book_dig,
  98. selection_box = {
  99. type = "fixed",
  100. fixed = {-0.2, -0.5, -0.25, 0.2, -0.35, 0.25}
  101. }
  102. })
  103. homedecor.register("book_open_"..color, {
  104. mesh = "homedecor_book_open.obj",
  105. tiles = {
  106. { name = "homedecor_book_cover.png", color = hue },
  107. { name = "homedecor_book_edges.png", color = "white" },
  108. { name = "homedecor_book_pages.png", color = "white" }
  109. },
  110. groups = { snappy=3, oddly_breakable_by_hand=3, not_in_creative_inventory=1 },
  111. drop = "homedecor:book_"..color,
  112. walkable = false,
  113. on_dig = book_dig,
  114. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  115. local meta = minetest.get_meta(pos)
  116. local player_name = clicker:get_player_name()
  117. local title = meta:get_string("title") or ""
  118. local text = meta:get_string("text") or ""
  119. local owner = meta:get_string("owner") or ""
  120. local formspec
  121. if owner == "" or owner == player_name then
  122. formspec = "size[8,8]"..default.gui_bg..default.gui_bg_img..
  123. "field[0.5,1;7.5,0;title;Book title :;"..
  124. minetest.formspec_escape(title).."]"..
  125. "textarea[0.5,1.5;7.5,7;text;Book content :;"..
  126. minetest.formspec_escape(text).."]"..
  127. "button_exit[2.5,7.5;3,1;save;Save]"
  128. else
  129. formspec = "size[8,8]"..default.gui_bg..
  130. "button_exit[7,0.25;1,0.5;close;X]"..
  131. default.gui_bg_img..
  132. "label[0.5,0.5;by "..owner.."]"..
  133. "label[0.5,0;"..minetest.formspec_escape(title).."]"..
  134. "textarea[0.5,1.5;7.5,7;;"..minetest.formspec_escape(text)..";]"
  135. end
  136. player_current_book[player_name] = pos
  137. minetest.show_formspec(player_name, BOOK_FORMNAME, formspec)
  138. return itemstack
  139. end,
  140. on_punch = function(pos, node, puncher, pointed_thing)
  141. local fdir = node.param2
  142. minetest.swap_node(pos, { name = "homedecor:book_"..color, param2 = fdir })
  143. minetest.sound_play("homedecor_book_close", {
  144. pos=pos,
  145. max_hear_distance = 3,
  146. gain = 2,
  147. })
  148. end,
  149. selection_box = {
  150. type = "fixed",
  151. fixed = {-0.35, -0.5, -0.25, 0.35, -0.4, 0.25}
  152. }
  153. })
  154. -- crafting
  155. minetest.register_craft({
  156. type = "shapeless",
  157. output = "homedecor:book_"..color,
  158. recipe = {
  159. "dye:"..color,
  160. "default:book"
  161. },
  162. })
  163. end
  164. minetest.register_on_player_receive_fields(function(player, form_name, fields)
  165. if form_name ~= BOOK_FORMNAME then
  166. return false
  167. end
  168. local player_name = player:get_player_name()
  169. local pos = player_current_book[player_name]
  170. if not pos then
  171. return true
  172. end
  173. local meta = minetest.get_meta(pos)
  174. local owner = meta:get_string("owner")
  175. if owner ~= "" and player_name ~= owner or not fields.save then
  176. player_current_book[player_name] = nil
  177. return true
  178. end
  179. meta:set_string("title", fields.title or "")
  180. meta:set_string("text", fields.text or "")
  181. meta:set_string("owner", player_name)
  182. if (fields.title or "") ~= "" then
  183. meta:set_string("infotext", fields.title)
  184. end
  185. minetest.log("action", S("@1 has written in a book (title: \"@2\"): \"@3\" at location @4",
  186. player:get_player_name(), fields.title, fields.text, minetest.pos_to_string(player:getpos())))
  187. player_current_book[player_name] = nil
  188. return true
  189. end)
  190. -- aliases
  191. minetest.register_alias("homedecor:book", "homedecor:book_grey")
  192. minetest.register_alias("homedecor:book_open", "homedecor:book_open_grey")