init.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. books = books or {}
  2. books.modpath = minetest.get_modpath("books")
  3. dofile(books.modpath .. "/book.lua")
  4. books.get_formspec = function()
  5. local formspec =
  6. "size[8,7;]" ..
  7. default.formspec.get_form_colors() ..
  8. default.formspec.get_form_image() ..
  9. default.formspec.get_slot_colors() ..
  10. "list[context;books;0,0.3;8,2;]" ..
  11. "list[current_player;main;0,2.85;8,1;]" ..
  12. "list[current_player;main;0,4.08;8,3;8]" ..
  13. "listring[context;books]" ..
  14. "listring[current_player;main]" ..
  15. default.get_hotbar_bg(0,2.85)
  16. -- Inventory slots overlay
  17. local bx, by = 0, 0.3
  18. for i = 1, 16 do
  19. if i == 9 then
  20. bx = 0
  21. by = by + 1
  22. end
  23. formspec = formspec .. "image["..bx..","..by..";1,1;books_slot.png]"
  24. bx = bx + 1
  25. end
  26. return formspec
  27. end
  28. books.on_construct = function(pos)
  29. local meta = minetest.get_meta(pos)
  30. meta:set_string("formspec", books.get_formspec())
  31. local inv = meta:get_inventory()
  32. inv:set_size("books", 8 * 2)
  33. end
  34. books.can_dig = function(pos,player)
  35. local inv = minetest.get_meta(pos):get_inventory()
  36. return inv:is_empty("books")
  37. end
  38. books.allow_metadata_inventory_put = function(pos, listname, index, stack)
  39. if minetest.get_item_group(stack:get_name(), "book") ~= 0 then
  40. return stack:get_count()
  41. end
  42. return 0
  43. end
  44. books.on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  45. minetest.log("action", player:get_player_name() .. " moves stuff in bookshelf at " .. minetest.pos_to_string(pos))
  46. end
  47. books.on_metadata_inventory_put = function(pos, listname, index, stack, player)
  48. minetest.log("action", player:get_player_name() .. " moves stuff to bookshelf at " .. minetest.pos_to_string(pos))
  49. end
  50. books.on_metadata_inventory_take = function(pos, listname, index, stack, player)
  51. minetest.log("action", player:get_player_name() .. " takes stuff from bookshelf at " .. minetest.pos_to_string(pos))
  52. end
  53. books.on_blast = function(pos)
  54. local drops = {}
  55. default.get_inventory_drops(pos, "books", drops)
  56. drops[#drops+1] = "books:bookshelf"
  57. minetest.remove_node(pos)
  58. return drops
  59. end
  60. if not books.run_once then
  61. local bookshelf_groups = utility.dig_groups("furniture", {flammable = 3})
  62. local bookshelf_sounds = default.node_sound_wood_defaults()
  63. minetest.register_node("books:bookshelf", {
  64. description = "Bookshelf",
  65. tiles = {
  66. "default_wood.png",
  67. "default_wood.png",
  68. "default_wood.png",
  69. "default_wood.png",
  70. "books_bookshelf.png",
  71. "books_bookshelf.png"
  72. },
  73. paramtype2 = "facedir",
  74. groups = bookshelf_groups,
  75. sounds = bookshelf_sounds,
  76. on_construct = function(...)
  77. return books.on_construct(...) end,
  78. can_dig = function(...)
  79. return books.can_dig(...) end,
  80. allow_metadata_inventory_put = function(...)
  81. return books.allow_metadata_inventory_put(...) end,
  82. on_metadata_inventory_move = function(...)
  83. return books.on_metadata_inventory_move(...) end,
  84. on_metadata_inventory_put = function(...)
  85. return books.on_metadata_inventory_put(...) end,
  86. on_metadata_inventory_take = function(...)
  87. return books.on_metadata_inventory_take(...) end,
  88. on_blast = function(...)
  89. return books.on_blast(...) end,
  90. })
  91. minetest.register_node("books:bookshelf_empty", {
  92. description = "Empty Bookshelf",
  93. tiles = {
  94. "default_wood.png",
  95. "default_wood.png",
  96. "default_wood.png",
  97. "default_wood.png",
  98. "books_bookshelf_empty.png",
  99. "books_bookshelf_empty.png"
  100. },
  101. paramtype2 = "facedir",
  102. groups = bookshelf_groups,
  103. sounds = bookshelf_sounds,
  104. })
  105. minetest.register_craft({
  106. type = "fuel",
  107. recipe = "books:bookshelf",
  108. burntime = 30,
  109. })
  110. minetest.register_craft({
  111. output = 'books:bookshelf_empty',
  112. recipe = {
  113. {'group:wood', 'group:wood', 'group:wood'},
  114. {'books:book_blank', 'books:book_blank', 'books:book_blank'},
  115. {'group:wood', 'group:wood', 'group:wood'},
  116. }
  117. })
  118. minetest.register_craft({
  119. type = "fuel",
  120. recipe = "books:bookshelf_empty",
  121. burntime = 26,
  122. })
  123. minetest.register_craft({
  124. output = "books:bookshelf",
  125. type = "shapeless",
  126. recipe = {"books:bookshelf_empty", "books:book_blank"},
  127. })
  128. minetest.register_alias("default:bookshelf", "books:bookshelf")
  129. minetest.register_alias("bookshelf:bookshelf", "books:bookshelf")
  130. minetest.register_alias("moreblocks:empty_bookshelf", "books:bookshelf_empty")
  131. minetest.register_alias("default:book", "books:book_blank")
  132. minetest.register_alias("default:book_written", "books:book_written")
  133. minetest.register_on_player_receive_fields(function(...) books.on_player_receive_fields(...) end)
  134. minetest.register_on_craft(function(...) books.on_craft(...) end)
  135. minetest.register_craftitem("books:book_blank", {
  136. description = "Book (Blank)",
  137. inventory_image = "default_book.png",
  138. groups = {book = 1, flammable = 3},
  139. on_use = function(...) return books.book_on_use(...) end,
  140. })
  141. minetest.register_craftitem("books:book_written", {
  142. description = "Book (Written)",
  143. inventory_image = "default_book_written.png",
  144. groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
  145. stack_max = 1,
  146. on_use = function(...) return books.book_on_use(...) end,
  147. })
  148. minetest.register_craft({
  149. type = "shapeless",
  150. output = "books:book_written",
  151. recipe = {"books:book_blank", "books:book_written"}
  152. })
  153. minetest.register_craft({
  154. output = 'books:book_blank',
  155. recipe = {
  156. {'default:paper'},
  157. {'default:paper'},
  158. {'default:paper'},
  159. }
  160. })
  161. -- Reloadable.
  162. local name = "books:core"
  163. local file = books.modpath .. "/init.lua"
  164. reload.register_file(name, file, false)
  165. books.run_once = true
  166. end