craftitems.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. -- mods/default/craftitems.lua
  2. -- support for MT game translation.
  3. local S = default.get_translator
  4. local lpp = 14 -- Lines per book's page
  5. local function book_on_use(itemstack, user)
  6. local player_name = user:get_player_name()
  7. local meta = itemstack:get_meta()
  8. local title, text, owner = "", "", player_name
  9. local page, page_max, lines, string = 1, 1, {}, ""
  10. -- Backwards compatibility
  11. local old_data = minetest.deserialize(itemstack:get_metadata())
  12. if old_data then
  13. meta:from_table({ fields = old_data })
  14. end
  15. local data = meta:to_table().fields
  16. if data.owner then
  17. title = data.title
  18. text = data.text
  19. owner = data.owner
  20. for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
  21. lines[#lines+1] = str
  22. end
  23. if data.page then
  24. page = data.page
  25. page_max = data.page_max
  26. for i = ((lpp * page) - lpp) + 1, lpp * page do
  27. if not lines[i] then break end
  28. string = string .. lines[i] .. "\n"
  29. end
  30. end
  31. end
  32. local formspec
  33. local esc = minetest.formspec_escape
  34. if owner == player_name then
  35. formspec = "size[8,8]" ..
  36. "field[0.5,1;7.5,0;title;" .. esc(S("Title:")) .. ";" ..
  37. esc(title) .. "]" ..
  38. "textarea[0.5,1.5;7.5,7;text;" .. esc(S("Contents:")) .. ";" ..
  39. esc(text) .. "]" ..
  40. "button_exit[2.5,7.5;3,1;save;" .. esc(S("Save")) .. "]"
  41. else
  42. formspec = "size[8,8]" ..
  43. "label[0.5,0.5;" .. esc(S("by @1", owner)) .. "]" ..
  44. "tablecolumns[color;text]" ..
  45. "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
  46. "table[0.4,0;7,0.5;title;#FFFF00," .. esc(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;" .. esc(S("Page @1 of @2", page, 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. return itemstack
  55. end
  56. local max_text_size = 10000
  57. local max_title_size = 80
  58. local short_title_size = 35
  59. minetest.register_on_player_receive_fields(function(player, formname, fields)
  60. if formname ~= "default:book" then return end
  61. local inv = player:get_inventory()
  62. local stack = player:get_wielded_item()
  63. if fields.save and fields.title and fields.text
  64. and fields.title ~= "" and fields.text ~= "" then
  65. local new_stack, data
  66. if stack:get_name() ~= "default:book_written" then
  67. local count = stack:get_count()
  68. if count == 1 then
  69. stack:set_name("default:book_written")
  70. else
  71. stack:set_count(count - 1)
  72. new_stack = ItemStack("default:book_written")
  73. end
  74. else
  75. data = stack:get_meta():to_table().fields
  76. end
  77. if data and data.owner and data.owner ~= player:get_player_name() then
  78. return
  79. end
  80. if not data then data = {} end
  81. data.title = fields.title:sub(1, max_title_size)
  82. data.owner = player:get_player_name()
  83. local short_title = data.title
  84. -- Don't bother triming the title if the trailing dots would make it longer
  85. if #short_title > short_title_size + 3 then
  86. short_title = short_title:sub(1, short_title_size) .. "..."
  87. end
  88. data.description = S("\"@1\" by @2", short_title, data.owner)
  89. data.text = fields.text:sub(1, max_text_size)
  90. data.text = data.text:gsub("\r\n", "\n"):gsub("\r", "\n")
  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:get_pos(), 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:skeleton_key", {
  128. description = S("Skeleton Key"),
  129. inventory_image = "default_key_skeleton.png",
  130. on_use = function(itemstack, user, pointed_thing)
  131. if pointed_thing.type ~= "node" then
  132. return itemstack
  133. end
  134. local pos = pointed_thing.under
  135. local node = minetest.get_node(pos)
  136. if not node then
  137. return itemstack
  138. end
  139. local on_skeleton_key_use = minetest.registered_nodes[node.name].on_skeleton_key_use
  140. if not on_skeleton_key_use then
  141. return itemstack
  142. end
  143. -- make a new key secret in case the node callback needs it
  144. local random = math.random
  145. local newsecret = string.format(
  146. "%04x%04x%04x%04x",
  147. random(2^16) - 1, random(2^16) - 1,
  148. random(2^16) - 1, random(2^16) - 1)
  149. local secret, _, _ = on_skeleton_key_use(pos, user, newsecret)
  150. if secret then
  151. local inv = minetest.get_inventory({type="player", name=user:get_player_name()})
  152. -- update original itemstack
  153. itemstack:take_item()
  154. -- finish and return the new key
  155. local new_stack = ItemStack("default:key")
  156. local meta = new_stack:get_meta()
  157. meta:set_string("secret", secret)
  158. meta:set_string("description", S("Key to @1's @2", user:get_player_name(),
  159. minetest.registered_nodes[node.name].description))
  160. if itemstack:get_count() == 0 then
  161. itemstack = new_stack
  162. else
  163. if inv:add_item("main", new_stack):get_count() > 0 then
  164. minetest.add_item(user:get_pos(), new_stack)
  165. end -- else: added to inventory successfully
  166. end
  167. return itemstack
  168. end
  169. end
  170. })
  171. --
  172. -- Craftitem registry
  173. --
  174. minetest.register_craftitem("default:book", {
  175. description = S("Book"),
  176. inventory_image = "default_book.png",
  177. groups = {book = 1, flammable = 3},
  178. on_use = book_on_use,
  179. })
  180. minetest.register_craftitem("default:book_written", {
  181. description = S("Book with Text"),
  182. inventory_image = "default_book_written.png",
  183. groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
  184. stack_max = 1,
  185. on_use = book_on_use,
  186. })
  187. minetest.register_craftitem("default:bronze_ingot", {
  188. description = S("Bronze Ingot"),
  189. inventory_image = "default_bronze_ingot.png"
  190. })
  191. minetest.register_craftitem("default:clay_brick", {
  192. description = S("Clay Brick"),
  193. inventory_image = "default_clay_brick.png",
  194. })
  195. minetest.register_craftitem("default:clay_lump", {
  196. description = S("Clay Lump"),
  197. inventory_image = "default_clay_lump.png",
  198. })
  199. minetest.register_craftitem("default:coal_lump", {
  200. description = S("Coal Lump"),
  201. inventory_image = "default_coal_lump.png",
  202. groups = {coal = 1, flammable = 1}
  203. })
  204. minetest.register_craftitem("default:copper_ingot", {
  205. description = S("Copper Ingot"),
  206. inventory_image = "default_copper_ingot.png"
  207. })
  208. minetest.register_craftitem("default:copper_lump", {
  209. description = S("Copper Lump"),
  210. inventory_image = "default_copper_lump.png"
  211. })
  212. minetest.register_craftitem("default:diamond", {
  213. description = S("Diamond"),
  214. inventory_image = "default_diamond.png",
  215. })
  216. minetest.register_craftitem("default:flint", {
  217. description = S("Flint"),
  218. inventory_image = "default_flint.png"
  219. })
  220. minetest.register_craftitem("default:gold_ingot", {
  221. description = S("Gold Ingot"),
  222. inventory_image = "default_gold_ingot.png"
  223. })
  224. minetest.register_craftitem("default:gold_lump", {
  225. description = S("Gold Lump"),
  226. inventory_image = "default_gold_lump.png"
  227. })
  228. minetest.register_craftitem("default:iron_lump", {
  229. description = S("Iron Lump"),
  230. inventory_image = "default_iron_lump.png"
  231. })
  232. minetest.register_craftitem("default:mese_crystal", {
  233. description = S("Mese Crystal"),
  234. inventory_image = "default_mese_crystal.png",
  235. })
  236. minetest.register_craftitem("default:mese_crystal_fragment", {
  237. description = S("Mese Crystal Fragment"),
  238. inventory_image = "default_mese_crystal_fragment.png",
  239. })
  240. minetest.register_craftitem("default:obsidian_shard", {
  241. description = S("Obsidian Shard"),
  242. inventory_image = "default_obsidian_shard.png",
  243. })
  244. minetest.register_craftitem("default:paper", {
  245. description = S("Paper"),
  246. inventory_image = "default_paper.png",
  247. groups = {flammable = 3},
  248. })
  249. minetest.register_craftitem("default:steel_ingot", {
  250. description = S("Steel Ingot"),
  251. inventory_image = "default_steel_ingot.png"
  252. })
  253. minetest.register_craftitem("default:stick", {
  254. description = S("Stick"),
  255. inventory_image = "default_stick.png",
  256. groups = {stick = 1, flammable = 2},
  257. })
  258. minetest.register_craftitem("default:tin_ingot", {
  259. description = S("Tin Ingot"),
  260. inventory_image = "default_tin_ingot.png"
  261. })
  262. minetest.register_craftitem("default:tin_lump", {
  263. description = S("Tin Lump"),
  264. inventory_image = "default_tin_lump.png"
  265. })
  266. --
  267. -- Crafting recipes
  268. --
  269. minetest.register_craft({
  270. output = "default:book",
  271. recipe = {
  272. {"default:paper"},
  273. {"default:paper"},
  274. {"default:paper"},
  275. }
  276. })
  277. default.register_craft_metadata_copy("default:book", "default:book_written")
  278. minetest.register_craft({
  279. output = "default:bronze_ingot 9",
  280. recipe = {
  281. {"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
  282. {"default:copper_ingot", "default:tin_ingot", "default:copper_ingot"},
  283. {"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
  284. }
  285. })
  286. minetest.register_craft({
  287. output = "default:clay_brick 4",
  288. recipe = {
  289. {"default:brick"},
  290. }
  291. })
  292. minetest.register_craft({
  293. output = "default:clay_lump 4",
  294. recipe = {
  295. {"default:clay"},
  296. }
  297. })
  298. minetest.register_craft({
  299. output = "default:coal_lump 9",
  300. recipe = {
  301. {"default:coalblock"},
  302. }
  303. })
  304. minetest.register_craft({
  305. output = "default:copper_ingot 9",
  306. recipe = {
  307. {"default:copperblock"},
  308. }
  309. })
  310. minetest.register_craft({
  311. output = "default:diamond 9",
  312. recipe = {
  313. {"default:diamondblock"},
  314. }
  315. })
  316. minetest.register_craft({
  317. output = "default:gold_ingot 9",
  318. recipe = {
  319. {"default:goldblock"},
  320. }
  321. })
  322. minetest.register_craft({
  323. output = "default:mese_crystal",
  324. recipe = {
  325. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  326. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  327. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  328. }
  329. })
  330. minetest.register_craft({
  331. output = "default:mese_crystal 9",
  332. recipe = {
  333. {"default:mese"},
  334. }
  335. })
  336. minetest.register_craft({
  337. output = "default:mese_crystal_fragment 9",
  338. recipe = {
  339. {"default:mese_crystal"},
  340. }
  341. })
  342. minetest.register_craft({
  343. output = "default:obsidian_shard 9",
  344. recipe = {
  345. {"default:obsidian"}
  346. }
  347. })
  348. minetest.register_craft({
  349. output = "default:paper",
  350. recipe = {
  351. {"default:reed", "default:reed", "default:reed"},
  352. }
  353. })
  354. minetest.register_craft({
  355. output = "default:skeleton_key",
  356. recipe = {
  357. {"default:gold_ingot"},
  358. }
  359. })
  360. minetest.register_craft({
  361. output = "default:steel_ingot 9",
  362. recipe = {
  363. {"default:steelblock"},
  364. }
  365. })
  366. minetest.register_craft({
  367. output = "default:stick 4",
  368. recipe = {
  369. {"group:wood"},
  370. }
  371. })
  372. minetest.register_craft({
  373. output = "default:tin_ingot 9",
  374. recipe = {
  375. {"default:tinblock"},
  376. }
  377. })
  378. --
  379. -- Cooking recipes
  380. --
  381. minetest.register_craft({
  382. type = "cooking",
  383. output = "default:clay_brick",
  384. recipe = "default:clay_lump",
  385. })
  386. minetest.register_craft({
  387. type = "cooking",
  388. output = "default:copper_ingot",
  389. recipe = "default:copper_lump",
  390. })
  391. minetest.register_craft({
  392. type = "cooking",
  393. output = "default:gold_ingot",
  394. recipe = "default:gold_lump",
  395. })
  396. minetest.register_craft({
  397. type = "cooking",
  398. output = "default:gold_ingot",
  399. recipe = "default:key",
  400. cooktime = 5,
  401. })
  402. minetest.register_craft({
  403. type = "cooking",
  404. output = "default:gold_ingot",
  405. recipe = "default:skeleton_key",
  406. cooktime = 5,
  407. })
  408. minetest.register_craft({
  409. type = "cooking",
  410. output = "default:steel_ingot",
  411. recipe = "default:iron_lump",
  412. })
  413. minetest.register_craft({
  414. type = "cooking",
  415. output = "default:tin_ingot",
  416. recipe = "default:tin_lump",
  417. })
  418. --
  419. -- Fuels
  420. --
  421. minetest.register_craft({
  422. type = "fuel",
  423. recipe = "default:book",
  424. burntime = 3,
  425. })
  426. minetest.register_craft({
  427. type = "fuel",
  428. recipe = "default:book_written",
  429. burntime = 3,
  430. })
  431. minetest.register_craft({
  432. type = "fuel",
  433. recipe = "default:coal_lump",
  434. burntime = 40,
  435. })
  436. minetest.register_craft({
  437. type = "fuel",
  438. recipe = "default:paper",
  439. burntime = 1,
  440. })
  441. minetest.register_craft({
  442. type = "fuel",
  443. recipe = "group:stick",
  444. burntime = 1,
  445. })