craftitems.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. -- mods/default/craftitems.lua
  2. minetest.register_craftitem("default:stick", {
  3. description = "Stick",
  4. inventory_image = "default_stick.png",
  5. groups = {stick = 1, flammable = 2},
  6. })
  7. minetest.register_craftitem("default:paper", {
  8. description = "Paper",
  9. inventory_image = "default_paper.png",
  10. groups = {flammable = 3},
  11. })
  12. local lpp = 14 -- Lines per book's page
  13. local function book_on_use(itemstack, user)
  14. local player_name = user:get_player_name()
  15. local meta = itemstack:get_meta()
  16. local title, text, owner = "", "", player_name
  17. local page, page_max, lines, string = 1, 1, {}, ""
  18. -- Backwards compatibility
  19. local old_data = minetest.deserialize(itemstack:get_metadata())
  20. if old_data then
  21. meta:from_table({ fields = old_data })
  22. end
  23. local data = meta:to_table().fields
  24. if data.owner then
  25. title = data.title
  26. text = data.text
  27. owner = data.owner
  28. for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
  29. lines[#lines+1] = str
  30. end
  31. if data.page then
  32. page = data.page
  33. page_max = data.page_max
  34. for i = ((lpp * page) - lpp) + 1, lpp * page do
  35. if not lines[i] then break end
  36. string = string .. lines[i] .. "\n"
  37. end
  38. end
  39. end
  40. local formspec
  41. if owner == player_name then
  42. formspec = "size[8,8]" .. default.gui_bg ..
  43. default.gui_bg_img ..
  44. "field[0.5,1;7.5,0;title;Title:;" ..
  45. minetest.formspec_escape(title) .. "]" ..
  46. "textarea[0.5,1.5;7.5,7;text;Contents:;" ..
  47. minetest.formspec_escape(text) .. "]" ..
  48. "button_exit[2.5,7.5;3,1;save;Save]"
  49. else
  50. formspec = "size[8,8]" .. default.gui_bg ..
  51. default.gui_bg_img ..
  52. "label[0.5,0.5;by " .. owner .. "]" ..
  53. "tablecolumns[color;text]" ..
  54. "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
  55. "table[0.4,0;7,0.5;title;#FFFF00," .. minetest.formspec_escape(title) .. "]" ..
  56. "textarea[0.5,1.5;7.5,7;;" ..
  57. minetest.formspec_escape(string ~= "" and string or text) .. ";]" ..
  58. "button[2.4,7.6;0.8,0.8;book_prev;<]" ..
  59. "label[3.2,7.7;Page " .. page .. " of " .. page_max .. "]" ..
  60. "button[4.9,7.6;0.8,0.8;book_next;>]"
  61. end
  62. minetest.show_formspec(player_name, "default:book", formspec)
  63. return itemstack
  64. end
  65. minetest.register_on_player_receive_fields(function(player, formname, fields)
  66. if formname ~= "default:book" then return end
  67. local inv = player:get_inventory()
  68. local stack = player:get_wielded_item()
  69. if fields.save and fields.title ~= "" and fields.text ~= "" then
  70. local new_stack, data
  71. if stack:get_name() ~= "default:book_written" then
  72. local count = stack:get_count()
  73. if count == 1 then
  74. stack:set_name("default:book_written")
  75. else
  76. stack:set_count(count - 1)
  77. new_stack = ItemStack("default:book_written")
  78. end
  79. else
  80. data = stack:get_meta():to_table().fields
  81. end
  82. if data and data.owner and data.owner ~= player:get_player_name() then
  83. return
  84. end
  85. if not data then data = {} end
  86. data.title = fields.title
  87. data.owner = player:get_player_name()
  88. data.description = "\""..fields.title.."\" by "..data.owner
  89. data.text = fields.text
  90. data.text_len = #data.text
  91. data.page = 1
  92. data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
  93. if new_stack then
  94. new_stack:get_meta():from_table({ fields = data })
  95. if inv:room_for_item("main", new_stack) then
  96. inv:add_item("main", new_stack)
  97. else
  98. minetest.add_item(player:getpos(), new_stack)
  99. end
  100. else
  101. stack:get_meta():from_table({ fields = data })
  102. end
  103. elseif fields.book_next or fields.book_prev then
  104. local data = stack:get_meta():to_table().fields
  105. if not data or not data.page then
  106. return
  107. end
  108. data.page = tonumber(data.page)
  109. data.page_max = tonumber(data.page_max)
  110. if fields.book_next then
  111. data.page = data.page + 1
  112. if data.page > data.page_max then
  113. data.page = 1
  114. end
  115. else
  116. data.page = data.page - 1
  117. if data.page == 0 then
  118. data.page = data.page_max
  119. end
  120. end
  121. stack:get_meta():from_table({fields = data})
  122. stack = book_on_use(stack, player)
  123. end
  124. -- Update stack
  125. player:set_wielded_item(stack)
  126. end)
  127. minetest.register_craftitem("default:book", {
  128. description = "Book",
  129. inventory_image = "default_book.png",
  130. groups = {book = 1, flammable = 3},
  131. on_use = book_on_use,
  132. })
  133. minetest.register_craftitem("default:book_written", {
  134. description = "Book With Text",
  135. inventory_image = "default_book_written.png",
  136. groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
  137. stack_max = 1,
  138. on_use = book_on_use,
  139. })
  140. minetest.register_craft({
  141. type = "shapeless",
  142. output = "default:book_written",
  143. recipe = {"default:book", "default:book_written"}
  144. })
  145. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  146. if itemstack:get_name() ~= "default:book_written" then
  147. return
  148. end
  149. local original
  150. local index
  151. for i = 1, player:get_inventory():get_size("craft") do
  152. if old_craft_grid[i]:get_name() == "default:book_written" then
  153. original = old_craft_grid[i]
  154. index = i
  155. end
  156. end
  157. if not original then
  158. return
  159. end
  160. local copymeta = original:get_meta():to_table()
  161. -- copy of the book held by player's mouse cursor
  162. itemstack:get_meta():from_table(copymeta)
  163. -- put the book with metadata back in the craft grid
  164. craft_inv:set_stack("craft", index, original)
  165. end)
  166. minetest.register_craftitem("default:skeleton_key", {
  167. description = "Skeleton Key",
  168. inventory_image = "default_key_skeleton.png",
  169. groups = {key = 1},
  170. on_use = function(itemstack, user, pointed_thing)
  171. if pointed_thing.type ~= "node" then
  172. return itemstack
  173. end
  174. local pos = pointed_thing.under
  175. local node = minetest.get_node(pos)
  176. if not node then
  177. return itemstack
  178. end
  179. local on_skeleton_key_use = minetest.registered_nodes[node.name].on_skeleton_key_use
  180. if not on_skeleton_key_use then
  181. return itemstack
  182. end
  183. -- make a new key secret in case the node callback needs it
  184. local random = math.random
  185. local newsecret = string.format(
  186. "%04x%04x%04x%04x",
  187. random(2^16) - 1, random(2^16) - 1,
  188. random(2^16) - 1, random(2^16) - 1)
  189. local secret, _, _ = on_skeleton_key_use(pos, user, newsecret)
  190. if secret then
  191. local inv = minetest.get_inventory({type="player", name=user:get_player_name()})
  192. -- update original itemstack
  193. itemstack:take_item()
  194. -- finish and return the new key
  195. local new_stack = ItemStack("default:key")
  196. local meta = new_stack:get_meta()
  197. meta:set_string("secret", secret)
  198. meta:set_string("description", "Key to "..user:get_player_name().."'s "
  199. ..minetest.registered_nodes[node.name].description)
  200. if itemstack:get_count() == 0 then
  201. itemstack = new_stack
  202. else
  203. if inv:add_item("main", new_stack):get_count() > 0 then
  204. minetest.add_item(user:getpos(), new_stack)
  205. end -- else: added to inventory successfully
  206. end
  207. return itemstack
  208. end
  209. end
  210. })
  211. minetest.register_craftitem("default:coal_lump", {
  212. description = "Coal Lump",
  213. inventory_image = "default_coal_lump.png",
  214. groups = {coal = 1, flammable = 1}
  215. })
  216. minetest.register_craftitem("default:iron_lump", {
  217. description = "Iron Lump",
  218. inventory_image = "default_iron_lump.png",
  219. })
  220. minetest.register_craftitem("default:copper_lump", {
  221. description = "Copper Lump",
  222. inventory_image = "default_copper_lump.png",
  223. })
  224. minetest.register_craftitem("default:tin_lump", {
  225. description = "Tin Lump",
  226. inventory_image = "default_tin_lump.png",
  227. })
  228. minetest.register_craftitem("default:mese_crystal", {
  229. description = "Mese Crystal",
  230. inventory_image = "default_mese_crystal.png",
  231. })
  232. minetest.register_craftitem("default:gold_lump", {
  233. description = "Gold Lump",
  234. inventory_image = "default_gold_lump.png",
  235. })
  236. minetest.register_craftitem("default:diamond", {
  237. description = "Diamond",
  238. inventory_image = "default_diamond.png",
  239. })
  240. minetest.register_craftitem("default:clay_lump", {
  241. description = "Clay Lump",
  242. inventory_image = "default_clay_lump.png",
  243. })
  244. minetest.register_craftitem("default:steel_ingot", {
  245. description = "Steel Ingot",
  246. inventory_image = "default_steel_ingot.png",
  247. })
  248. minetest.register_craftitem("default:copper_ingot", {
  249. description = "Copper Ingot",
  250. inventory_image = "default_copper_ingot.png",
  251. })
  252. minetest.register_craftitem("default:tin_ingot", {
  253. description = "Tin Ingot",
  254. inventory_image = "default_tin_ingot.png",
  255. })
  256. minetest.register_craftitem("default:bronze_ingot", {
  257. description = "Bronze Ingot",
  258. inventory_image = "default_bronze_ingot.png",
  259. })
  260. minetest.register_craftitem("default:gold_ingot", {
  261. description = "Gold Ingot",
  262. inventory_image = "default_gold_ingot.png"
  263. })
  264. minetest.register_craftitem("default:mese_crystal_fragment", {
  265. description = "Mese Crystal Fragment",
  266. inventory_image = "default_mese_crystal_fragment.png",
  267. })
  268. minetest.register_craftitem("default:clay_brick", {
  269. description = "Clay Brick",
  270. inventory_image = "default_clay_brick.png",
  271. })
  272. minetest.register_craftitem("default:obsidian_shard", {
  273. description = "Obsidian Shard",
  274. inventory_image = "default_obsidian_shard.png",
  275. })
  276. minetest.register_craftitem("default:flint", {
  277. description = "Flint",
  278. inventory_image = "default_flint.png"
  279. })