craftitems.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. -- mods/default/craftitems.lua
  2. minetest.register_craftitem("default:stick", {
  3. description = "Stick",
  4. inventory_image = "default_stick.png",
  5. groups = {stick=1},
  6. })
  7. minetest.register_craftitem("default:paper", {
  8. description = "Paper",
  9. inventory_image = "default_paper.png",
  10. })
  11. local lpp = 13
  12. local function book_on_use(itemstack, user)
  13. local player_name = user:get_player_name()
  14. local data = minetest.deserialize(itemstack:get_metadata())
  15. local title, text, owner = "", "", player_name
  16. local page, page_max, lines, string = 1, 1, {}, ""
  17. if data then
  18. title = data.title
  19. text = data.text
  20. owner = data.owner
  21. for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
  22. lines[#lines+1] = str
  23. end
  24. if data.page then
  25. page = data.page
  26. page_max = data.page_max
  27. for i = ((lpp * page) - lpp) + 1, lpp * page do
  28. if not lines[i] then break end
  29. string = string .. lines[i] .. "\n"
  30. end
  31. end
  32. end
  33. local formspec
  34. if owner == player_name then
  35. formspec = "size[8,8]" ..
  36. "background[8,8;1,1;gui_hobbitbg.png;true]"..
  37. "field[0.5,1;7.5,0;title;Title:;" ..
  38. minetest.formspec_escape(title) .. "]" ..
  39. "textarea[0.5,1.5;7.5,7;text;Contents:;" ..
  40. minetest.formspec_escape(text) .. "]" ..
  41. "button_exit[2.5,7.5;3,1;save;Save]"
  42. else
  43. formspec = "size[8,8]" ..
  44. "background[8,8;1,1;gui_hobbitbg.png;true]"..
  45. "label[0.5,0.5;by " .. owner .. "]" ..
  46. "label[0.4,0;" .. minetest.colorize("yellow", minetest.formspec_escape(title)) .. "]" ..
  47. "textarea[0.5,1.5;7.5,7;;" ..
  48. minetest.formspec_escape(string ~= "" and string or text) .. ";]" ..
  49. "button[2.4,7.6;0.8,0.8;book_prev;<]" ..
  50. "label[3.2,7.7;Page " .. page .. " of " .. page_max .. "]" ..
  51. "button[4.9,7.6;0.8,0.8;book_next;>]"
  52. end
  53. minetest.show_formspec(player_name, "default:book", formspec)
  54. end
  55. minetest.register_on_player_receive_fields(function(player, formname, fields)
  56. if formname ~= "default:book" then return end
  57. local inv = player:get_inventory()
  58. local stack = player:get_wielded_item()
  59. if fields.save and fields.title ~= "" and fields.text ~= "" then
  60. local new_stack, data
  61. if stack:get_name() ~= "default:book_written" then
  62. local count = stack:get_count()
  63. if count == 1 then
  64. stack:set_name("default:book_written")
  65. else
  66. stack:set_count(count - 1)
  67. new_stack = ItemStack("default:book_written")
  68. end
  69. else
  70. data = minetest.deserialize(stack:get_metadata())
  71. end
  72. if not data then data = {} end
  73. data.title = fields.title
  74. data.text = fields.text
  75. data.text_len = #data.text
  76. data.page = 1
  77. data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
  78. data.owner = player:get_player_name()
  79. local data_str = minetest.serialize(data)
  80. if new_stack then
  81. new_stack:set_metadata(data_str)
  82. if inv:room_for_item("main", new_stack) then
  83. inv:add_item("main", new_stack)
  84. else
  85. minetest.add_item(player:getpos(), new_stack)
  86. end
  87. else
  88. stack:set_metadata(data_str)
  89. end
  90. elseif fields.book_next or fields.book_prev then
  91. local data = minetest.deserialize(stack:get_metadata())
  92. if not data.page then return end
  93. if fields.book_next then
  94. data.page = data.page + 1
  95. if data.page > data.page_max then
  96. data.page = 1
  97. end
  98. else
  99. data.page = data.page - 1
  100. if data.page == 0 then
  101. data.page = data.page_max
  102. end
  103. end
  104. local data_str = minetest.serialize(data)
  105. stack:set_metadata(data_str)
  106. book_on_use(stack, player)
  107. end
  108. player:set_wielded_item(stack)
  109. end)
  110. minetest.register_craftitem("default:book", {
  111. description = "Book",
  112. inventory_image = "default_book.png",
  113. groups = {book = 1},
  114. on_use = function(itemstack, user)
  115. book_on_use(itemstack, user)
  116. end,
  117. })
  118. minetest.register_craftitem("default:book_written", {
  119. description = "Book With Text",
  120. inventory_image = "default_book_written.png",
  121. groups = {book = 1, not_in_creative_inventory = 1},
  122. stack_max = 1,
  123. on_use = function(itemstack, user)
  124. book_on_use(itemstack, user)
  125. end,
  126. })
  127. minetest.register_craft({
  128. type = "shapeless",
  129. output = "default:book_written",
  130. recipe = {"default:book", "default:book_written"}
  131. })
  132. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  133. if itemstack:get_name() ~= "default:book_written" then
  134. return
  135. end
  136. local original
  137. local index
  138. for i = 1, player:get_inventory():get_size("craft") do
  139. if old_craft_grid[i]:get_name() == "default:book_written" then
  140. original = old_craft_grid[i]
  141. index = i
  142. end
  143. end
  144. if not original then
  145. return
  146. end
  147. local copymeta = original:get_metadata()
  148. -- copy of the book held by player's mouse cursor
  149. itemstack:set_metadata(copymeta)
  150. -- put the book with metadata back in the craft grid
  151. craft_inv:set_stack("craft", index, original)
  152. end)
  153. minetest.register_craftitem("default:coal_lump", {
  154. description = "Coal Lump",
  155. inventory_image = "default_coal_lump.png",
  156. })
  157. minetest.register_craftitem("default:iron_lump", {
  158. description = "Iron Lump",
  159. inventory_image = "default_iron_lump.png",
  160. })
  161. minetest.register_craftitem("default:copper_lump", {
  162. description = "Copper Lump",
  163. inventory_image = "default_copper_lump.png",
  164. })
  165. minetest.register_craftitem("default:mese_crystal", {
  166. description = "Mese Crystal",
  167. inventory_image = "default_mese_crystal.png",
  168. })
  169. minetest.register_craftitem("default:gold_lump", {
  170. description = "Gold Lump",
  171. inventory_image = "default_gold_lump.png",
  172. })
  173. minetest.register_craftitem("default:clay_lump", {
  174. description = "Clay Lump",
  175. inventory_image = "default_clay_lump.png",
  176. })
  177. minetest.register_craftitem("default:steel_ingot", {
  178. description = "Steel Ingot",
  179. inventory_image = "default_steel_ingot.png",
  180. })
  181. minetest.register_craftitem("default:copper_ingot", {
  182. description = "Copper Ingot",
  183. inventory_image = "default_copper_ingot.png",
  184. })
  185. minetest.register_craftitem("default:bronze_ingot", {
  186. description = "Bronze Ingot",
  187. inventory_image = "default_bronze_ingot.png",
  188. })
  189. minetest.register_craftitem("default:gold_ingot", {
  190. description = "Gold Ingot",
  191. inventory_image = "default_gold_ingot.png"
  192. })
  193. minetest.register_craftitem("default:mese_crystal_fragment", {
  194. description = "Mese Crystal Fragment",
  195. inventory_image = "default_mese_crystal_fragment.png",
  196. })
  197. minetest.register_craftitem("default:clay_brick", {
  198. description = "Clay Brick",
  199. inventory_image = "default_clay_brick.png",
  200. })
  201. minetest.register_craftitem("default:scorched_stuff", {
  202. description = "Scorched Stuff",
  203. inventory_image = "default_scorched_stuff.png",
  204. })
  205. minetest.register_craftitem("default:obsidian_shard", {
  206. description = "Obsidian Shard",
  207. inventory_image = "default_obsidian_shard.png",
  208. })